Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

offset.c 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /****************************************************************************
  2. Copyright 1995 by Silicon Graphics Incorporated, Mountain View, California.
  3. All Rights Reserved
  4. Permission to use, copy, modify, and distribute this software and its
  5. documentation for any purpose and without fee is hereby granted,
  6. provided that the above copyright notice appear in all copies and that
  7. both that copyright notice and this permission notice appear in
  8. supporting documentation, and that the name of Silicon Graphics not be
  9. used in advertising or publicity pertaining to distribution of the
  10. software without specific, written prior permission.
  11. SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  12. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  13. EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  14. CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  16. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  17. PERFORMANCE OF THIS SOFTWARE.
  18. ****************************************************************************/
  19. /*
  20. * Derived from code written by Kurt Akeley, November 1992
  21. *
  22. * Uses PolygonOffset to draw hidden-line images. PolygonOffset
  23. * shifts the z values of polygons an amount that is
  24. * proportional to their slope in screen z. This keeps
  25. * the lines, which are drawn without displacement, from
  26. * interacting with their respective polygons, and
  27. * thus eliminates line dropouts.
  28. *
  29. * The left image shows an ordinary antialiased wireframe image.
  30. * The center image shows an antialiased hidden-line image without
  31. * PolygonOffset.
  32. * The right image shows an antialiased hidden-line image using
  33. * PolygonOffset to reduce artifacts.
  34. *
  35. * Drag with a mouse button pressed to rotate the models.
  36. * Press the escape key to exit.
  37. */
  38. /*
  39. * Modified for OpenGL 1.1 glPolygonOffset() conventions
  40. */
  41. #include <GL/glx.h>
  42. #include <GL/glu.h>
  43. #include <X11/keysym.h>
  44. #include <stdlib.h>
  45. #include <stdio.h>
  46. #include <string.h>
  47. #undef GL_EXT_polygon_offset /* use GL 1.1 version instead of extension */
  48. #ifndef EXIT_FAILURE
  49. # define EXIT_FAILURE 1
  50. #endif
  51. #ifndef EXIT_SUCCESS
  52. # define EXIT_SUCCESS 0
  53. #endif
  54. #define MAXQUAD 6
  55. typedef float Vertex[3];
  56. typedef Vertex Quad[4];
  57. /* data to define the six faces of a unit cube */
  58. Quad quads[MAXQUAD] = {
  59. { {0,0,0}, {1,0,0}, {1,1,0}, {0,1,0} },
  60. { {0,0,1}, {1,0,1}, {1,1,1}, {0,1,1} },
  61. { {0,0,0}, {1,0,0}, {1,0,1}, {0,0,1} },
  62. { {0,1,0}, {1,1,0}, {1,1,1}, {0,1,1} },
  63. { {0,0,0}, {0,0,1}, {0,1,1}, {0,1,0} },
  64. { {1,0,0}, {1,0,1}, {1,1,1}, {1,1,0} }
  65. };
  66. #define WIREFRAME 0
  67. #define HIDDEN_LINE 1
  68. static void error(const char* prog, const char* msg);
  69. static void cubes(int mx, int my, int mode);
  70. static void fill(Quad quad);
  71. static void outline(Quad quad);
  72. static void draw_hidden(Quad quad, int mode);
  73. static void process_input(Display *dpy, Window win);
  74. static int query_extension(char* extName);
  75. static int attributeList[] = { GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1,
  76. GLX_BLUE_SIZE, 1, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE, 1, None };
  77. static int dimension = 3;
  78. int main(int argc, char** argv) {
  79. Display *dpy;
  80. XVisualInfo *vi;
  81. XSetWindowAttributes swa;
  82. Window win;
  83. GLXContext cx;
  84. dpy = XOpenDisplay(0);
  85. if (!dpy) error(argv[0], "can't open display");
  86. vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList);
  87. if (!vi) error(argv[0], "no suitable visual");
  88. cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  89. swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
  90. vi->visual, AllocNone);
  91. swa.border_pixel = 0;
  92. swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask |
  93. ButtonPressMask | ButtonMotionMask;
  94. win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 900, 300,
  95. 0, vi->depth, InputOutput, vi->visual,
  96. CWBorderPixel|CWColormap|CWEventMask, &swa);
  97. XStoreName(dpy, win, "hiddenline");
  98. XMapWindow(dpy, win);
  99. glXMakeCurrent(dpy, win, cx);
  100. /* check for the polygon offset extension */
  101. #ifndef GL_VERSION_1_1
  102. if (!query_extension("GL_EXT_polygon_offset"))
  103. error(argv[0], "polygon_offset extension is not available");
  104. #else
  105. (void) query_extension;
  106. #endif
  107. /* set up viewing parameters */
  108. glMatrixMode(GL_PROJECTION);
  109. gluPerspective(20, 1, 0.1, 20);
  110. glMatrixMode(GL_MODELVIEW);
  111. glTranslatef(0, 0, -15);
  112. /* set other relevant state information */
  113. glEnable(GL_DEPTH_TEST);
  114. #ifdef GL_EXT_polygon_offset
  115. printf("using 1.0 offset extension\n");
  116. glPolygonOffsetEXT( 1.0, 0.00001 );
  117. #else
  118. printf("using 1.1 offset\n");
  119. glPolygonOffset( 1.0, 0.5 );
  120. #endif
  121. glShadeModel( GL_FLAT );
  122. glDisable( GL_DITHER );
  123. /* process events until the user presses ESC */
  124. while (1) process_input(dpy, win);
  125. return 0;
  126. }
  127. static void
  128. draw_scene(int mx, int my) {
  129. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  130. glPushMatrix();
  131. glTranslatef(-1.7, 0.0, 0.0);
  132. cubes(mx, my, WIREFRAME);
  133. glPopMatrix();
  134. glPushMatrix();
  135. cubes(mx, my, HIDDEN_LINE);
  136. glPopMatrix();
  137. glPushMatrix();
  138. glTranslatef(1.7, 0.0, 0.0);
  139. #ifdef GL_EXT_polygon_offset
  140. glEnable(GL_POLYGON_OFFSET_EXT);
  141. #else
  142. glEnable(GL_POLYGON_OFFSET_FILL);
  143. #endif
  144. cubes(mx, my, HIDDEN_LINE);
  145. #ifdef GL_EXT_polygon_offset
  146. glDisable(GL_POLYGON_OFFSET_EXT);
  147. #else
  148. glDisable(GL_POLYGON_OFFSET_FILL);
  149. #endif
  150. glPopMatrix();
  151. }
  152. static void
  153. cubes(int mx, int my, int mode) {
  154. int x, y, z, i;
  155. /* track the mouse */
  156. glRotatef(mx / 2.0, 0, 1, 0);
  157. glRotatef(my / 2.0, 1, 0, 0);
  158. /* draw the lines as hidden polygons */
  159. glTranslatef(-0.5, -0.5, -0.5);
  160. glScalef(1.0/dimension, 1.0/dimension, 1.0/dimension);
  161. for (z = 0; z < dimension; z++) {
  162. for (y = 0; y < dimension; y++) {
  163. for (x = 0; x < dimension; x++) {
  164. glPushMatrix();
  165. glTranslatef(x, y, z);
  166. glScalef(0.8, 0.8, 0.8);
  167. for (i = 0; i < MAXQUAD; i++)
  168. draw_hidden(quads[i], mode);
  169. glPopMatrix();
  170. }
  171. }
  172. }
  173. }
  174. static void
  175. fill(Quad quad) {
  176. /* draw a filled polygon */
  177. glBegin(GL_QUADS);
  178. glVertex3fv(quad[0]);
  179. glVertex3fv(quad[1]);
  180. glVertex3fv(quad[2]);
  181. glVertex3fv(quad[3]);
  182. glEnd();
  183. }
  184. static void
  185. outline(Quad quad) {
  186. /* draw an outlined polygon */
  187. glBegin(GL_LINE_LOOP);
  188. glVertex3fv(quad[0]);
  189. glVertex3fv(quad[1]);
  190. glVertex3fv(quad[2]);
  191. glVertex3fv(quad[3]);
  192. glEnd();
  193. }
  194. static void
  195. draw_hidden(Quad quad, int mode) {
  196. if (mode == HIDDEN_LINE) {
  197. glColor3f(0, 0, 0);
  198. fill(quad);
  199. }
  200. /* draw the outline using white, optionally fill the interior with black */
  201. glColor3f(1, 1, 1);
  202. outline(quad);
  203. }
  204. static void
  205. process_input(Display *dpy, Window win) {
  206. XEvent event;
  207. static int prevx, prevy;
  208. static int deltax = 90, deltay = 40;
  209. do {
  210. char buf[31];
  211. KeySym keysym;
  212. XNextEvent(dpy, &event);
  213. switch(event.type) {
  214. case Expose:
  215. break;
  216. case ConfigureNotify: {
  217. /* this approach preserves a 1:1 viewport aspect ratio */
  218. int vX, vY, vW, vH;
  219. int eW = event.xconfigure.width, eH = event.xconfigure.height;
  220. if (eW >= eH) {
  221. vX = 0;
  222. vY = (eH - eW) >> 1;
  223. vW = vH = eW;
  224. } else {
  225. vX = (eW - eH) >> 1;
  226. vY = 0;
  227. vW = vH = eH;
  228. }
  229. glViewport(vX, vY, vW, vH);
  230. }
  231. break;
  232. case KeyPress:
  233. (void) XLookupString(&event.xkey, buf, sizeof(buf), &keysym, NULL);
  234. switch (keysym) {
  235. case XK_Escape:
  236. exit(EXIT_SUCCESS);
  237. default:
  238. break;
  239. }
  240. case ButtonPress:
  241. prevx = event.xbutton.x;
  242. prevy = event.xbutton.y;
  243. break;
  244. case MotionNotify:
  245. deltax += (event.xbutton.x - prevx); prevx = event.xbutton.x;
  246. deltay += (event.xbutton.y - prevy); prevy = event.xbutton.y;
  247. break;
  248. default:
  249. break;
  250. }
  251. } while (XPending(dpy));
  252. draw_scene(deltax, deltay);
  253. glXSwapBuffers(dpy, win);
  254. }
  255. static void
  256. error(const char *prog, const char *msg) {
  257. fprintf(stderr, "%s: %s\n", prog, msg);
  258. exit(EXIT_FAILURE);
  259. }
  260. static int
  261. query_extension(char* extName) {
  262. char *p = (char *) glGetString(GL_EXTENSIONS);
  263. char *end = p + strlen(p);
  264. while (p < end) {
  265. int n = strcspn(p, " ");
  266. if ((strlen(extName) == n) && (strncmp(extName, p, n) == 0))
  267. return GL_TRUE;
  268. p += (n + 1);
  269. }
  270. return GL_FALSE;
  271. }