home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / demos / offset.c < prev    next >
C/C++ Source or Header  |  1996-05-27  |  8KB  |  292 lines

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