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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 <X11/keysym.h>
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45. #include <string.h>
  46. #undef GL_EXT_polygon_offset /* use GL 1.1 version instead of extension */
  47. #ifndef EXIT_FAILURE
  48. # define EXIT_FAILURE 1
  49. #endif
  50. #ifndef EXIT_SUCCESS
  51. # define EXIT_SUCCESS 0
  52. #endif
  53. #define MAXQUAD 6
  54. typedef float Vertex[3];
  55. typedef Vertex Quad[4];
  56. /* data to define the six faces of a unit cube */
  57. Quad quads[MAXQUAD] = {
  58. { {0,0,0}, {0,0,1}, {0,1,1}, {0,1,0} }, /* x = 0 */
  59. { {0,0,0}, {1,0,0}, {1,0,1}, {0,0,1} }, /* y = 0 */
  60. { {0,0,0}, {1,0,0}, {1,1,0}, {0,1,0} }, /* z = 0 */
  61. { {1,0,0}, {1,0,1}, {1,1,1}, {1,1,0} }, /* x = 1 */
  62. { {0,1,0}, {1,1,0}, {1,1,1}, {0,1,1} }, /* y = 1 */
  63. { {0,0,1}, {1,0,1}, {1,1,1}, {0,1,1} } /* z = 1 */
  64. };
  65. #define WIREFRAME 0
  66. #define HIDDEN_LINE 1
  67. static void error(const char* prog, const char* msg);
  68. static void cubes(int mx, int my, int mode);
  69. static void fill(Quad quad);
  70. static void outline(Quad quad);
  71. static void draw_hidden(Quad quad, int mode, int face);
  72. static void process_input(Display *dpy, Window win);
  73. static int query_extension(char* extName);
  74. static int attributeList[] = { GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1,
  75. GLX_BLUE_SIZE, 1, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE, 1, None };
  76. static int dimension = 3;
  77. int main(int argc, char** argv) {
  78. Display *dpy;
  79. XVisualInfo *vi;
  80. XSetWindowAttributes swa;
  81. Window win;
  82. GLXContext cx;
  83. GLint z;
  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. glFrustum(-1, 1, -1, 1, 6, 20);
  110. glMatrixMode(GL_MODELVIEW);
  111. glTranslatef(0, 0, -15);
  112. /* set other relevant state information */
  113. glEnable(GL_DEPTH_TEST);
  114. glGetIntegerv(GL_DEPTH_BITS, &z);
  115. printf("GL_DEPTH_BITS = %d\n", z);
  116. #ifdef GL_EXT_polygon_offset
  117. printf("using 1.0 offset extension\n");
  118. glPolygonOffsetEXT( 1.0, 0.00001 );
  119. #else
  120. printf("using 1.1 offset\n");
  121. glPolygonOffset( 1.0, 0.5 );
  122. #endif
  123. glShadeModel( GL_FLAT );
  124. glDisable( GL_DITHER );
  125. /* process events until the user presses ESC */
  126. while (1) process_input(dpy, win);
  127. return 0;
  128. }
  129. static void
  130. draw_scene(int mx, int my) {
  131. glClearColor(0.25, 0.25, 0.25, 0);
  132. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  133. glPushMatrix();
  134. glTranslatef(-1.7, 0.0, 0.0);
  135. cubes(mx, my, WIREFRAME);
  136. glPopMatrix();
  137. glPushMatrix();
  138. cubes(mx, my, HIDDEN_LINE);
  139. glPopMatrix();
  140. glPushMatrix();
  141. glTranslatef(1.7, 0.0, 0.0);
  142. #ifdef GL_EXT_polygon_offset
  143. glEnable(GL_POLYGON_OFFSET_EXT);
  144. #else
  145. glEnable(GL_POLYGON_OFFSET_FILL);
  146. #endif
  147. cubes(mx, my, HIDDEN_LINE);
  148. #ifdef GL_EXT_polygon_offset
  149. glDisable(GL_POLYGON_OFFSET_EXT);
  150. #else
  151. glDisable(GL_POLYGON_OFFSET_FILL);
  152. #endif
  153. glPopMatrix();
  154. }
  155. static void
  156. cubes(int mx, int my, int mode) {
  157. int x, y, z, i;
  158. /* track the mouse */
  159. glRotatef(mx / 2.0, 0, 1, 0);
  160. glRotatef(my / 2.0, 1, 0, 0);
  161. /* draw the lines as hidden polygons */
  162. glTranslatef(-0.5, -0.5, -0.5);
  163. glScalef(1.0/dimension, 1.0/dimension, 1.0/dimension);
  164. for (z = 0; z < dimension; z++) {
  165. for (y = 0; y < dimension; y++) {
  166. for (x = 0; x < dimension; x++) {
  167. glPushMatrix();
  168. glTranslatef(x, y, z);
  169. glScalef(0.8, 0.8, 0.8);
  170. for (i = 0; i < MAXQUAD; i++)
  171. draw_hidden(quads[i], mode, i);
  172. glPopMatrix();
  173. }
  174. }
  175. }
  176. }
  177. static void
  178. fill(Quad quad) {
  179. /* draw a filled polygon */
  180. glBegin(GL_QUADS);
  181. glVertex3fv(quad[0]);
  182. glVertex3fv(quad[1]);
  183. glVertex3fv(quad[2]);
  184. glVertex3fv(quad[3]);
  185. glEnd();
  186. }
  187. static void
  188. outline(Quad quad) {
  189. /* draw an outlined polygon */
  190. glBegin(GL_LINE_LOOP);
  191. glVertex3fv(quad[0]);
  192. glVertex3fv(quad[1]);
  193. glVertex3fv(quad[2]);
  194. glVertex3fv(quad[3]);
  195. glEnd();
  196. }
  197. static void
  198. draw_hidden(Quad quad, int mode, int face) {
  199. static const GLfloat colors[3][3] = {
  200. {0.5, 0.5, 0.0},
  201. {0.8, 0.5, 0.0},
  202. {0.0, 0.5, 0.8}
  203. };
  204. if (mode == HIDDEN_LINE) {
  205. glColor3fv(colors[face % 3]);
  206. fill(quad);
  207. }
  208. /* draw the outline using white */
  209. glColor3f(1, 1, 1);
  210. outline(quad);
  211. }
  212. static void
  213. process_input(Display *dpy, Window win) {
  214. XEvent event;
  215. static int prevx, prevy;
  216. static int deltax = 90, deltay = 40;
  217. do {
  218. char buf[31];
  219. KeySym keysym;
  220. XNextEvent(dpy, &event);
  221. switch(event.type) {
  222. case Expose:
  223. break;
  224. case ConfigureNotify: {
  225. /* this approach preserves a 1:1 viewport aspect ratio */
  226. int vX, vY, vW, vH;
  227. int eW = event.xconfigure.width, eH = event.xconfigure.height;
  228. if (eW >= eH) {
  229. vX = 0;
  230. vY = (eH - eW) >> 1;
  231. vW = vH = eW;
  232. } else {
  233. vX = (eW - eH) >> 1;
  234. vY = 0;
  235. vW = vH = eH;
  236. }
  237. glViewport(vX, vY, vW, vH);
  238. }
  239. break;
  240. case KeyPress:
  241. (void) XLookupString(&event.xkey, buf, sizeof(buf), &keysym, NULL);
  242. switch (keysym) {
  243. case XK_Escape:
  244. exit(EXIT_SUCCESS);
  245. default:
  246. break;
  247. }
  248. case ButtonPress:
  249. prevx = event.xbutton.x;
  250. prevy = event.xbutton.y;
  251. break;
  252. case MotionNotify:
  253. deltax += (event.xbutton.x - prevx); prevx = event.xbutton.x;
  254. deltay += (event.xbutton.y - prevy); prevy = event.xbutton.y;
  255. break;
  256. default:
  257. break;
  258. }
  259. } while (XPending(dpy));
  260. draw_scene(deltax, deltay);
  261. glXSwapBuffers(dpy, win);
  262. }
  263. static void
  264. error(const char *prog, const char *msg) {
  265. fprintf(stderr, "%s: %s\n", prog, msg);
  266. exit(EXIT_FAILURE);
  267. }
  268. static int
  269. query_extension(char* extName) {
  270. char *p = (char *) glGetString(GL_EXTENSIONS);
  271. char *end = p + strlen(p);
  272. while (p < end) {
  273. int n = strcspn(p, " ");
  274. if ((strlen(extName) == n) && (strncmp(extName, p, n) == 0))
  275. return GL_TRUE;
  276. p += (n + 1);
  277. }
  278. return GL_FALSE;
  279. }