132001f49Smrg/****************************************************************************
232001f49SmrgCopyright 1995 by Silicon Graphics Incorporated, Mountain View, California.
332001f49Smrg
432001f49Smrg                        All Rights Reserved
532001f49Smrg
632001f49SmrgPermission to use, copy, modify, and distribute this software and its
732001f49Smrgdocumentation for any purpose and without fee is hereby granted,
832001f49Smrgprovided that the above copyright notice appear in all copies and that
932001f49Smrgboth that copyright notice and this permission notice appear in
1032001f49Smrgsupporting documentation, and that the name of Silicon Graphics not be
1132001f49Smrgused in advertising or publicity pertaining to distribution of the
1232001f49Smrgsoftware without specific, written prior permission.
1332001f49Smrg
1432001f49SmrgSILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
1532001f49SmrgINCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
1632001f49SmrgEVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
1732001f49SmrgCONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
1832001f49SmrgUSE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
1932001f49SmrgOTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
2032001f49SmrgPERFORMANCE OF THIS SOFTWARE.
2132001f49Smrg
2232001f49Smrg****************************************************************************/
2332001f49Smrg
2432001f49Smrg/*
2532001f49Smrg * Derived from code written by Kurt Akeley, November 1992
2632001f49Smrg *
2732001f49Smrg *	Uses PolygonOffset to draw hidden-line images.  PolygonOffset
2832001f49Smrg *	    shifts the z values of polygons an amount that is
2932001f49Smrg *	    proportional to their slope in screen z.  This keeps
3032001f49Smrg *	    the lines, which are drawn without displacement, from
3132001f49Smrg *	    interacting with their respective polygons, and
3232001f49Smrg *	    thus eliminates line dropouts.
3332001f49Smrg *
3432001f49Smrg *	The left image shows an ordinary antialiased wireframe image.
3532001f49Smrg *	The center image shows an antialiased hidden-line image without
3632001f49Smrg *	    PolygonOffset.
3732001f49Smrg *	The right image shows an antialiased hidden-line image using
3832001f49Smrg *	    PolygonOffset to reduce artifacts.
3932001f49Smrg *
4032001f49Smrg *	Drag with a mouse button pressed to rotate the models.
4132001f49Smrg *	Press the escape key to exit.
4232001f49Smrg */
4332001f49Smrg
4432001f49Smrg/*
4532001f49Smrg * Modified for OpenGL 1.1 glPolygonOffset() conventions
4632001f49Smrg */
4732001f49Smrg
4832001f49Smrg
4932001f49Smrg#include <GL/glx.h>
5032001f49Smrg#include <X11/keysym.h>
5132001f49Smrg#include <stdlib.h>
5232001f49Smrg#include <stdio.h>
5332001f49Smrg#include <string.h>
5432001f49Smrg
5532001f49Smrg#undef GL_EXT_polygon_offset  /* use GL 1.1 version instead of extension */
5632001f49Smrg
5732001f49Smrg
5832001f49Smrg#ifndef EXIT_FAILURE
5932001f49Smrg#  define EXIT_FAILURE    1
6032001f49Smrg#endif
6132001f49Smrg#ifndef EXIT_SUCCESS
6232001f49Smrg#  define EXIT_SUCCESS    0
6332001f49Smrg#endif
6432001f49Smrg
6532001f49Smrg#define MAXQUAD 6
6632001f49Smrg
6732001f49Smrgtypedef float Vertex[3];
6832001f49Smrg
6932001f49Smrgtypedef Vertex Quad[4];
7032001f49Smrg
7132001f49Smrg/* data to define the six faces of a unit cube */
7232001f49SmrgQuad quads[MAXQUAD] = {
7332001f49Smrg   { {0,0,0}, {0,0,1}, {0,1,1}, {0,1,0} }, /* x = 0 */
7432001f49Smrg   { {0,0,0}, {1,0,0}, {1,0,1}, {0,0,1} }, /* y = 0 */
7532001f49Smrg   { {0,0,0}, {1,0,0}, {1,1,0}, {0,1,0} }, /* z = 0 */
7632001f49Smrg   { {1,0,0}, {1,0,1}, {1,1,1}, {1,1,0} }, /* x = 1 */
7732001f49Smrg   { {0,1,0}, {1,1,0}, {1,1,1}, {0,1,1} }, /* y = 1 */
7832001f49Smrg   { {0,0,1}, {1,0,1}, {1,1,1}, {0,1,1} }  /* z = 1 */
7932001f49Smrg};
8032001f49Smrg
8132001f49Smrg#define WIREFRAME	0
8232001f49Smrg#define HIDDEN_LINE	1
8332001f49Smrg
8432001f49Smrgstatic void error(const char* prog, const char* msg);
8532001f49Smrgstatic void cubes(int mx, int my, int mode);
8632001f49Smrgstatic void fill(Quad quad);
8732001f49Smrgstatic void outline(Quad quad);
8832001f49Smrgstatic void draw_hidden(Quad quad, int mode, int face);
8932001f49Smrgstatic void process_input(Display *dpy, Window win);
9032001f49Smrgstatic int query_extension(char* extName);
9132001f49Smrg
9232001f49Smrgstatic int attributeList[] = { GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1,
9332001f49Smrg    GLX_BLUE_SIZE, 1, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE, 1, None };
9432001f49Smrg
9532001f49Smrgstatic int dimension = 3;
9632001f49Smrg
9732001f49Smrgstatic float Scale = 1.0;
9832001f49Smrg
9932001f49Smrg
10032001f49Smrgint main(int argc, char** argv) {
10132001f49Smrg    Display *dpy;
10232001f49Smrg    XVisualInfo *vi;
10332001f49Smrg    XSetWindowAttributes swa;
10432001f49Smrg    Window win;
10532001f49Smrg    GLXContext cx;
10632001f49Smrg    GLint z;
10732001f49Smrg
10832001f49Smrg    dpy = XOpenDisplay(0);
10932001f49Smrg    if (!dpy) error(argv[0], "can't open display");
11032001f49Smrg
11132001f49Smrg    vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList);
11232001f49Smrg    if (!vi) error(argv[0], "no suitable visual");
11332001f49Smrg
11432001f49Smrg    cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
11532001f49Smrg
11632001f49Smrg    swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
11732001f49Smrg                                   vi->visual, AllocNone);
11832001f49Smrg
11932001f49Smrg    swa.border_pixel = 0;
12032001f49Smrg    swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask |
12132001f49Smrg	ButtonPressMask | ButtonMotionMask;
12232001f49Smrg    win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 900, 300,
12332001f49Smrg			0, vi->depth, InputOutput, vi->visual,
12432001f49Smrg			CWBorderPixel|CWColormap|CWEventMask, &swa);
12532001f49Smrg    XStoreName(dpy, win, "hiddenline");
12632001f49Smrg    XMapWindow(dpy, win);
12732001f49Smrg
12832001f49Smrg    glXMakeCurrent(dpy, win, cx);
12932001f49Smrg
13032001f49Smrg    /* check for the polygon offset extension */
13132001f49Smrg#ifndef GL_VERSION_1_1
13232001f49Smrg    if (!query_extension("GL_EXT_polygon_offset"))
13332001f49Smrg        error(argv[0], "polygon_offset extension is not available");
13432001f49Smrg#else
13532001f49Smrg   (void) query_extension;
13632001f49Smrg#endif
13732001f49Smrg
13832001f49Smrg    /* set up viewing parameters */
13932001f49Smrg    glMatrixMode(GL_PROJECTION);
14032001f49Smrg    glFrustum(-1, 1, -1, 1, 6, 20);
14132001f49Smrg    glMatrixMode(GL_MODELVIEW);
14232001f49Smrg    glTranslatef(0, 0, -15);
14332001f49Smrg
14432001f49Smrg    /* set other relevant state information */
14532001f49Smrg    glEnable(GL_DEPTH_TEST);
14632001f49Smrg
14732001f49Smrg    glGetIntegerv(GL_DEPTH_BITS, &z);
14832001f49Smrg    printf("GL_DEPTH_BITS = %d\n", z);
14932001f49Smrg
15032001f49Smrg#ifdef GL_EXT_polygon_offset
15132001f49Smrg    printf("using 1.0 offset extension\n");
15232001f49Smrg    glPolygonOffsetEXT( 1.0, 0.00001 );
15332001f49Smrg#else
15432001f49Smrg    printf("using 1.1 offset\n");
15532001f49Smrg    glPolygonOffset( 1.0, 0.5 );
15632001f49Smrg#endif
15732001f49Smrg
15832001f49Smrg    glShadeModel( GL_FLAT );
15932001f49Smrg    glDisable( GL_DITHER );
16032001f49Smrg
16132001f49Smrg    /* process events until the user presses ESC */
16232001f49Smrg    while (1) process_input(dpy, win);
16332001f49Smrg
16432001f49Smrg    return 0;
16532001f49Smrg}
16632001f49Smrg
16732001f49Smrgstatic void
16832001f49Smrgdraw_scene(int mx, int my) {
16932001f49Smrg   glClearColor(0.25, 0.25, 0.25, 0);
17032001f49Smrg    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
17132001f49Smrg
17232001f49Smrg    glPushMatrix();
17332001f49Smrg    glTranslatef(-1.7, 0.0, 0.0);
17432001f49Smrg    cubes(mx, my, WIREFRAME);
17532001f49Smrg    glPopMatrix();
17632001f49Smrg
17732001f49Smrg    glPushMatrix();
17832001f49Smrg    cubes(mx, my, HIDDEN_LINE);
17932001f49Smrg    glPopMatrix();
18032001f49Smrg
18132001f49Smrg    glPushMatrix();
18232001f49Smrg    glTranslatef(1.7, 0.0, 0.0);
18332001f49Smrg#ifdef GL_EXT_polygon_offset
18432001f49Smrg    glEnable(GL_POLYGON_OFFSET_EXT);
18532001f49Smrg#else
18632001f49Smrg    glEnable(GL_POLYGON_OFFSET_FILL);
18732001f49Smrg#endif
18832001f49Smrg    glScalef(Scale, Scale, Scale);
18932001f49Smrg    cubes(mx, my, HIDDEN_LINE);
19032001f49Smrg#ifdef GL_EXT_polygon_offset
19132001f49Smrg    glDisable(GL_POLYGON_OFFSET_EXT);
19232001f49Smrg#else
19332001f49Smrg    glDisable(GL_POLYGON_OFFSET_FILL);
19432001f49Smrg#endif
19532001f49Smrg    glPopMatrix();
19632001f49Smrg}
19732001f49Smrg
19832001f49Smrg
19932001f49Smrgstatic void
20032001f49Smrgcubes(int mx, int my, int mode) {
20132001f49Smrg    int x, y, z, i;
20232001f49Smrg
20332001f49Smrg    /* track the mouse */
20432001f49Smrg    glRotatef(mx / 2.0, 0, 1, 0);
20532001f49Smrg    glRotatef(my / 2.0, 1, 0, 0);
20632001f49Smrg
20732001f49Smrg    /* draw the lines as hidden polygons */
20832001f49Smrg    glTranslatef(-0.5, -0.5, -0.5);
20932001f49Smrg    glScalef(1.0/dimension, 1.0/dimension, 1.0/dimension);
21032001f49Smrg    for (z = 0; z < dimension; z++) {
21132001f49Smrg	for (y = 0; y < dimension; y++) {
21232001f49Smrg	    for (x = 0; x < dimension; x++) {
21332001f49Smrg		glPushMatrix();
21432001f49Smrg		glTranslatef(x, y, z);
21532001f49Smrg		glScalef(0.8, 0.8, 0.8);
21632001f49Smrg		for (i = 0; i < MAXQUAD; i++)
21732001f49Smrg                    draw_hidden(quads[i], mode, i);
21832001f49Smrg		glPopMatrix();
21932001f49Smrg	    }
22032001f49Smrg	}
22132001f49Smrg    }
22232001f49Smrg}
22332001f49Smrg
22432001f49Smrgstatic void
22532001f49Smrgfill(Quad quad) {
22632001f49Smrg    /* draw a filled polygon */
22732001f49Smrg    glBegin(GL_QUADS);
22832001f49Smrg    glVertex3fv(quad[0]);
22932001f49Smrg    glVertex3fv(quad[1]);
23032001f49Smrg    glVertex3fv(quad[2]);
23132001f49Smrg    glVertex3fv(quad[3]);
23232001f49Smrg    glEnd();
23332001f49Smrg}
23432001f49Smrg
23532001f49Smrgstatic void
23632001f49Smrgoutline(Quad quad) {
23732001f49Smrg    /* draw an outlined polygon */
23832001f49Smrg    glBegin(GL_LINE_LOOP);
23932001f49Smrg    glVertex3fv(quad[0]);
24032001f49Smrg    glVertex3fv(quad[1]);
24132001f49Smrg    glVertex3fv(quad[2]);
24232001f49Smrg    glVertex3fv(quad[3]);
24332001f49Smrg    glEnd();
24432001f49Smrg}
24532001f49Smrg
24632001f49Smrgstatic void
24732001f49Smrgdraw_hidden(Quad quad, int mode, int face) {
24832001f49Smrg    static const GLfloat colors[3][3] = {
24932001f49Smrg        {0.5, 0.5, 0.0},
25032001f49Smrg        {0.8, 0.5, 0.0},
25132001f49Smrg        {0.0, 0.5, 0.8}
25232001f49Smrg    };
25332001f49Smrg    if (mode == HIDDEN_LINE) {
25432001f49Smrg        glColor3fv(colors[face % 3]);
25532001f49Smrg	fill(quad);
25632001f49Smrg    }
25732001f49Smrg
25832001f49Smrg    /* draw the outline using white */
25932001f49Smrg    glColor3f(1, 1, 1);
26032001f49Smrg    outline(quad);
26132001f49Smrg}
26232001f49Smrg
26332001f49Smrgstatic void
26432001f49Smrgprocess_input(Display *dpy, Window win) {
26532001f49Smrg    XEvent event;
26632001f49Smrg    static int prevx, prevy;
26732001f49Smrg    static int deltax = 90, deltay = 40;
26832001f49Smrg
26932001f49Smrg    do {
27032001f49Smrg	char buf[31];
27132001f49Smrg	KeySym keysym;
27232001f49Smrg
27332001f49Smrg	XNextEvent(dpy, &event);
27432001f49Smrg	switch(event.type) {
27532001f49Smrg	case Expose:
27632001f49Smrg	    break;
27732001f49Smrg	case ConfigureNotify: {
27832001f49Smrg	    /* this approach preserves a 1:1 viewport aspect ratio */
27932001f49Smrg	    int vX, vY, vW, vH;
28032001f49Smrg	    int eW = event.xconfigure.width, eH = event.xconfigure.height;
28132001f49Smrg	    if (eW >= eH) {
28232001f49Smrg		vX = 0;
28332001f49Smrg		vY = (eH - eW) >> 1;
28432001f49Smrg		vW = vH = eW;
28532001f49Smrg	    } else {
28632001f49Smrg		vX = (eW - eH) >> 1;
28732001f49Smrg		vY = 0;
28832001f49Smrg		vW = vH = eH;
28932001f49Smrg	    }
29032001f49Smrg	    glViewport(vX, vY, vW, vH);
29132001f49Smrg	    }
29232001f49Smrg	    break;
29332001f49Smrg	case KeyPress:
29432001f49Smrg	    (void) XLookupString(&event.xkey, buf, sizeof(buf), &keysym, NULL);
29532001f49Smrg	    switch (keysym) {
29632001f49Smrg            case 'Z':
29732001f49Smrg               Scale *= 1.1;
29832001f49Smrg               break;
29932001f49Smrg            case 'z':
30032001f49Smrg               Scale *= 0.9;
30132001f49Smrg               break;
30232001f49Smrg	    case XK_Escape:
30332001f49Smrg		exit(EXIT_SUCCESS);
30432001f49Smrg	    default:
30532001f49Smrg		break;
30632001f49Smrg	    }
30732001f49Smrg	    break;
30832001f49Smrg	case ButtonPress:
30932001f49Smrg	    prevx = event.xbutton.x;
31032001f49Smrg	    prevy = event.xbutton.y;
31132001f49Smrg	    break;
31232001f49Smrg	case MotionNotify:
31332001f49Smrg	    deltax += (event.xbutton.x - prevx); prevx = event.xbutton.x;
31432001f49Smrg	    deltay += (event.xbutton.y - prevy); prevy = event.xbutton.y;
31532001f49Smrg	    break;
31632001f49Smrg	default:
31732001f49Smrg	    break;
31832001f49Smrg	}
31932001f49Smrg    } while (XPending(dpy));
32032001f49Smrg
32132001f49Smrg    draw_scene(deltax, deltay);
32232001f49Smrg    glXSwapBuffers(dpy, win);
32332001f49Smrg}
32432001f49Smrg
32532001f49Smrgstatic void
32632001f49Smrgerror(const char *prog, const char *msg) {
32732001f49Smrg    fprintf(stderr, "%s: %s\n", prog, msg);
32832001f49Smrg    exit(EXIT_FAILURE);
32932001f49Smrg}
33032001f49Smrg
33132001f49Smrgstatic int
33232001f49Smrgquery_extension(char* extName) {
33332001f49Smrg    char *p = (char *) glGetString(GL_EXTENSIONS);
33432001f49Smrg    char *end = p + strlen(p);
33532001f49Smrg    while (p < end) {
33632001f49Smrg        int n = strcspn(p, " ");
33732001f49Smrg        if ((strlen(extName) == n) && (strncmp(extName, p, n) == 0))
33832001f49Smrg            return GL_TRUE;
33932001f49Smrg        p += (n + 1);
34032001f49Smrg    }
34132001f49Smrg    return GL_FALSE;
34232001f49Smrg}
34332001f49Smrg
344