home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #include <stdio.h>
- #include <X11/Intrinsic.h>
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include "interfere.h"
- #include "ogl_shapes.h"
-
- #define STEN_MASK ((1 << sten_size) - 1)
-
- #define PLASTIC_SPECULAR 0.8
- #define PLASTIC_SHININESS 10.
- #define PLANE_LENGTH 2.
- #define NUM_CLIP_PLANES 1
- #define MAX_CLIP_PLANE 6
- #define ANGLE_DELTA 1
- #define VIEWPOINT_Z 8.
- #define NUMBER(array) ((unsigned long) (sizeof(array) / sizeof(array[0])))
-
- enum
- {
- VIEWING_TRANSFORMATION = 0,
- CLIPPING_TRANSFORMATION,
- MODEL_TRANSFORMATION = CLIPPING_TRANSFORMATION + MAX_CLIP_PLANE
- };
-
- typedef GLvoid (*Method) (GLvoid);
-
- typedef struct tagMaterial
- {
- GLfloat color[4];
- GLfloat specular[4];
- GLfloat shininess;
- } Material, * MaterialPtr;
-
- typedef struct tagTransformation
- {
- GLfloat depth;
- GLfloat uniform_scale;
- GLuint x_rot, y_rot, z_rot;
- } Transformation, * TransformationPtr;
-
- typedef struct tagShape
- {
- Method constructor;
- GLuint object, material_object;
- Material material;
- Transformation transform;
- } Shape, * ShapePtr;
-
- static Shape shapes[] = {
- {doSolidTorus, 0, 0,
- {{0.1, 0.8, 0.1, 1.},
- {PLASTIC_SPECULAR, PLASTIC_SPECULAR, PLASTIC_SPECULAR, 1.},
- PLASTIC_SHININESS},
- {0., 1., 45., 0., 0.}},
- {doSolidTorus, 0, 0,
- {{0.1, 0.1, 0.8, 1.},
- {PLASTIC_SPECULAR, PLASTIC_SPECULAR, PLASTIC_SPECULAR, 1.},
- PLASTIC_SHININESS},
- {0., 1., 90., 0., 0.}},
- };
-
- static Transformation transformations[] =
- {
- {VIEWPOINT_Z, 1., 20, 340, 0},
- {0., 1., 0, 0, 0},
- {0., 1., 0, 90, 0},
- {0., 1., 0, 0, 90},
- {0., 1., 0, 0, 0},
- {0., 1., 0, 0, 0},
- {0., 1., 0, 0, 0},
- {0., 1., 0, 0, 0}
- };
-
- static GLvoid
- setupProjection (GLvoid)
- {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60., 1., 0.25, 10000.);
- glMatrixMode(GL_MODELVIEW);
- }
-
- static GLvoid
- defineLighting (GLvoid)
- {
- GLfloat light_model_ambient[] = {0.2, 0.2, 0.2, 1.};
- GLfloat light0_position[] = {1., 1., 1, 0.};
-
- glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
- glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);
- glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_model_ambient);
-
- glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
-
- glEnable(GL_LIGHT0);
- glShadeModel(GL_SMOOTH);
- }
-
- static GLvoid
- whiteAxes (GLvoid)
- {
- GLfloat origin[] = {0., 0., 0.};
- GLfloat x_unit[] = {1., 0., 0.};
- GLfloat y_unit[] = {0., 1., 0.};
- GLfloat z_unit[] = {0., 0., 1.};
- GLfloat axes_color[] = {1., 1., 1.};
-
- glPushMatrix();
- glColor3fv(axes_color);
- glScalef(2., 2., 2.);
- glBegin(GL_LINES);
- glVertex3fv(origin);
- glVertex3fv(x_unit);
- glEnd();
- glBegin(GL_LINES);
- glVertex3fv(origin);
- glVertex3fv(y_unit);
- glEnd();
- glBegin(GL_LINES);
- glVertex3fv(origin);
- glVertex3fv(z_unit);
- glEnd();
- glPopMatrix();
- }
-
- static GLvoid
- initShapes (GLvoid)
- {
- GLuint i = 0;
- GLuint base = glGenLists(NUMBER(shapes));
- GLuint mat_base = glGenLists(NUMBER(shapes));
- GLfloat mv_matrix[16];
-
- initStockShapes();
- for (i = 0; i < NUMBER(shapes); i++) {
- shapes[i].object = base + i;
- shapes[i].material_object = mat_base + i;
- glNewList(shapes[i].material_object, GL_COMPILE);
- glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE,
- shapes[i].material.color);
- glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,
- shapes[i].material.specular);
- glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS,
- shapes[i].material.shininess);
- glEndList();
-
- glPushMatrix();
- glScalef(shapes[i].transform.uniform_scale,
- shapes[i].transform.uniform_scale,
- shapes[i].transform.uniform_scale);
- glTranslatef(0., 0., shapes[i].transform.depth);
- glRotatef(shapes[i].transform.x_rot, 1., 0., 0.);
- glRotatef(shapes[i].transform.y_rot, 0., 1., 0.);
- glRotatef(shapes[i].transform.z_rot, 0., 0., 1.);
- glGetFloatv(GL_MODELVIEW_MATRIX, mv_matrix);
- glPopMatrix();
-
- glPushMatrix();
- glNewList(shapes[i].object, GL_COMPILE);
- glMultMatrixf(mv_matrix);
- glCallList(shapes[i].material_object);
- (*(shapes[i].constructor))();
- glEndList();
- glPopMatrix();
- }
- }
-
- void
- initGraphics (void)
- {
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_AUTO_NORMAL);
- glCullFace(GL_BACK);
- glClear(GL_STENCIL_BUFFER_BIT);
- glDepthRange(0, 1);
- setupProjection();
- defineLighting();
- initShapes();
- }
-
- static GLvoid
- doViewingTransformation (void)
- {
- glTranslatef(0., 0., -transformations[VIEWING_TRANSFORMATION].depth);
- glRotatef(transformations[VIEWING_TRANSFORMATION].x_rot, 1., 0., 0.);
- glRotatef(transformations[VIEWING_TRANSFORMATION].y_rot, 0., 1., 0.);
- glRotatef(transformations[VIEWING_TRANSFORMATION].z_rot, 0., 0., 1.);
- }
-
- static GLvoid
- doTransformation (unsigned int index)
- {
- doViewingTransformation();
- glScalef(transformations[index].uniform_scale,
- transformations[index].uniform_scale,
- transformations[index].uniform_scale);
- glTranslatef(0., 0., transformations[index].depth);
- glRotatef(transformations[index].x_rot, 1., 0., 0.);
- glRotatef(transformations[index].y_rot, 0., 1., 0.);
- glRotatef(transformations[index].z_rot, 0., 0., 1.);
- }
-
- static GLvoid
- doClippingTransformation (GLuint clipping_plane)
- {
- doTransformation(CLIPPING_TRANSFORMATION + clipping_plane);
- }
-
- static GLvoid
- drawClipPlanes (GLvoid)
- {
- GLfloat clipplane_vertices[4][3] = {
- {PLANE_LENGTH, -PLANE_LENGTH, 0.},
- {-PLANE_LENGTH, -PLANE_LENGTH, 0.},
- {-PLANE_LENGTH, PLANE_LENGTH, 0.},
- {PLANE_LENGTH, PLANE_LENGTH, 0.},
- };
- register unsigned int i = 0;
-
- glColor3ub(0xff, 0xff, 0x0);
-
- for (i = 0 ; i < NUM_CLIP_PLANES; i++) {
- glPushMatrix();
- glRotatef(transformations[CLIPPING_TRANSFORMATION + i].x_rot,
- 1., 0., 0.);
- glRotatef(transformations[CLIPPING_TRANSFORMATION + i].y_rot,
- 0., 1., 0.);
- glRotatef(transformations[CLIPPING_TRANSFORMATION + i].z_rot,
- 0., 0., 1.);
- glBegin(GL_LINE_LOOP);
- glVertex3fv(clipplane_vertices[0]);
- glVertex3fv(clipplane_vertices[1]);
- glVertex3fv(clipplane_vertices[2]);
- glVertex3fv(clipplane_vertices[3]);
- glEnd();
- glPopMatrix();
- }
- }
-
- static GLvoid
- doModelingTransformation (void)
- {
- doTransformation(MODEL_TRANSFORMATION);
- }
-
- static GLvoid
- drawObject (GLuint index)
- {
- glPushMatrix();
- glCallList(shapes[index].object);
- glPopMatrix();
- }
-
- static GLvoid
- drawCap (GLuint index)
- {
- GLfloat cap_vertices[4][3] =
- {
- {-1., -1., 0.},
- { 1., -1., 0.},
- {-1., 1., 0.},
- { 1., 1., 0.},
- };
- register unsigned int i = 0;
-
- glDepthFunc(GL_ALWAYS);
- glStencilFunc(GL_EQUAL, 0x1, 0x1);
- glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
- glEnable(GL_STENCIL_TEST);
-
- glEnable(GL_LIGHTING);
- for (i = 0; i < NUM_CLIP_PLANES; i++) {
- glPushMatrix();
- doClippingTransformation(i);
- glCallList(shapes[index].material_object);
- glScalef(100., 100., 100.);
- glBegin(GL_POLYGON);
- glVertex3fv(cap_vertices[0]);
- glVertex3fv(cap_vertices[2]);
- glVertex3fv(cap_vertices[3]);
- glVertex3fv(cap_vertices[1]);
- glEnd();
- glPopMatrix();
- }
- glDisable(GL_LIGHTING);
-
- glDepthFunc(GL_LESS);
- glDisable(GL_STENCIL_TEST);
- }
-
- static GLvoid
- drawCappedObject (GLuint index)
- {
- register unsigned int i = 0;
-
- glPushMatrix();
-
- doModelingTransformation();
-
- for (i = 0; i < NUM_CLIP_PLANES; i++) {
- glEnable(GL_CLIP_PLANE0 + i);
- }
-
- if (do_cap) {
- glStencilMask(0x01);
- glEnable(GL_STENCIL_TEST);
- glStencilFunc(GL_ALWAYS, 0, 0x1);
- glStencilOp(GL_INVERT, GL_INVERT, GL_INVERT);
- }
-
- glEnable(GL_LIGHTING);
- drawObject(index);
- glDisable(GL_LIGHTING);
-
- if (do_cap) {
- glStencilMask(STEN_MASK);
- }
-
- glPopMatrix();
-
- for (i = 0; i < NUM_CLIP_PLANES; i++) {
- glDisable(GL_CLIP_PLANE0 + i);
- }
-
- if (do_cap) {
- drawCap(index);
- }
- }
-
- static GLvoid
- drawInterferenceRegion (GLvoid)
- {
- glEnable(GL_STENCIL_TEST);
- glStencilFunc(GL_LESS, 0x2, STEN_MASK);
- glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO);
-
- glDepthFunc(GL_ALWAYS);
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, 1, 0, 1, -1, 1);
- glMatrixMode(GL_MODELVIEW);
-
- glPushMatrix();
- glLoadIdentity();
- glColor3f(1., 0., 0.);
- glRectf(0., 0., 1., 1.);
- glPopMatrix();
-
- setupProjection();
-
- glDisable(GL_STENCIL_TEST);
- glDepthFunc(GL_LESS);
- }
-
- static GLvoid
- updateAngles (GLvoid)
- {
- transformations[MODEL_TRANSFORMATION].x_rot =
- (transformations[MODEL_TRANSFORMATION].x_rot + ANGLE_DELTA) % 360;
- transformations[MODEL_TRANSFORMATION].z_rot =
- (transformations[MODEL_TRANSFORMATION].z_rot + ANGLE_DELTA) % 360;
- }
-
- void
- drawScene (void)
- {
- GLdouble cp[4] = {0, 0, -1, 0};
- GLuint i = 0;
- GLint error = 0;
-
- while (error = glGetError()) {
- fprintf(stderr, "ERROR : 0x%x\n", error);
- }
-
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
- for (i = 0; i < NUM_CLIP_PLANES; i++) {
- glPushMatrix();
- doClippingTransformation(i);
- glClipPlane(GL_CLIP_PLANE0 + i, cp);
- glPopMatrix();
- }
-
- glPushMatrix();
- doViewingTransformation();
- whiteAxes();
- drawClipPlanes ();
- glPopMatrix();
-
- for (i = 0; i < NUMBER(shapes); i++) {
- drawCappedObject(i);
- }
- if (do_cap) {
- drawInterferenceRegion();
- }
-
- showCurrent();
- updateAngles();
- }
-
-