/* * Logo.c * * draws a letter 'L' */ /* #include */ #include #include #include #include #include #include int windType; GLint windW, windH; /* camera parameters */ #define EYEX_INIT 2.0 #define EYEY_INIT 3.0 #define EYEZ_INIT 10.0 GLdouble eyeX = EYEX_INIT; GLdouble eyeY = EYEY_INIT; GLdouble eyeZ = EYEZ_INIT; GLdouble centerX = 1.0; GLdouble centerY = 1.5; GLdouble centerZ = 0.5; GLuint letterL; // display list speeds up rendering /* draw the letter L in a display list called L */ void drawL (void) { #define NPOINTS 12 #define NPOLYGONS 10 /* define all the points on the letter */ GLdouble point[NPOINTS][3] = { {0.0, 0.0, 1.0}, {2.0, 0.0, 1.0}, {2.0, 1.0, 1.0}, {1.0, 1.0, 1.0}, {1.0, 3.0, 1.0}, {0.0, 3.0, 1.0}, {0.0, 0.0, 0.0}, {2.0, 0.0, 0.0}, {2.0, 1.0, 0.0}, {1.0, 1.0, 0.0}, {1.0, 3.0, 0.0}, {0.0, 3.0, 0.0}}; /* each polygonal face references 4 points */ int polygon[NPOLYGONS][4] = { {0, 1, 2, 3}, /* front faces */ {0, 3, 4, 5}, {9, 8, 7, 6}, /* back faces */ {11, 10, 9, 6}, {0, 5, 11, 6}, /* side faces */ {1, 0, 6, 7}, {2, 1, 7, 8}, {3, 2, 8, 9}, {4, 3, 9, 10}, {5, 4, 10, 11}}; /* color array defines a different color for each face */ GLdouble color[NPOLYGONS][3] = { {1.0, 1.0, 1.0}, /* front faces */ {1.0, 1.0, 1.0}, {0.2, 0.2, 0.2}, /* back faces */ {0.2, 0.2, 0.2}, {0.4, 0.4, 0.4}, /* side faces */ {0.5, 0.5, 0.5}, {0.6, 0.6, 0.6}, {0.7, 0.7, 0.7}, {0.8, 0.8, 0.8}, {0.9, 0.9, 0.9}}; int face, pt; /* Setup display list */ letterL = glGenLists(1); glNewList (letterL, GL_COMPILE); /* Draw the letter */ for (face = 0; face < NPOLYGONS; face++) { glColor3dv(color[face]); /* set the color of the face */ glBegin(GL_POLYGON); for (pt = 0; pt < 4; pt++) /* draw all points on the face */ glVertex3dv (point[polygon[face][pt]]); glEnd(); } /* Finish the display list */ glEndList(); } /* Setup OpenGL state machine */ static void Init(void) { /* Set background color */ glClearColor(0.0, 0.0, 0.0, 0.0); /* Enable depth buffer */ glEnable(GL_DEPTH_TEST); /* Enable back-face culling */ glCullFace(GL_BACK); glEnable(GL_CULL_FACE); } /* Window size has changed: adjust viewport and perspective transformations */ static void Reshape(int width, int height) { float aspect; windW = (GLint)width; windH = (GLint)height; aspect = (float)windW / (float)windH; aspect *= 2.0; /* widen the lense */ /* Viewport fills the entire window */ glViewport(0, 0, width, height); /* Use orthographic projection to set visibility boundaries relative to viewing coordinates */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho (-aspect, aspect, -2.0, 2.0, 0.0, 1000.0); /* left, right, bottom, top, near, far */ /* Return to modelview mode */ glMatrixMode(GL_MODELVIEW); } /* Handle key presses */ static void Key(unsigned char key, int x, int y) { switch (key) { case 27: /* ESC to quit */ exit(1); break; default: return; } glutPostRedisplay(); /* force update of the display */ } /* Set modeling and viewing transformations, then draw the letter polygons */ static void Draw(void) { // int face, pt; /* clear the screen AND the depth buffer */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* Set the viewing transformation(s) */ glLoadIdentity(); gluLookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, 0.0, 1.0, 0.0); /* Draw the 1st letter */ glCallList(letterL); glFlush(); /* force all changes to be applied to buffer */ glutSwapBuffers(); /* display what you just drew */ } void main(int argc, char **argv) { /* Setup the window that logo will appear in */ windW = 300; windH = 300; glutInitWindowPosition(0, 0); glutInitWindowSize( windW, windH); /* Window uses depth buffer, full color, and double buffering */ windType = GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE; glutInitDisplayMode((GLenum)windType); if (glutCreateWindow("Logo") == GL_FALSE) { exit(1); } Init(); drawL(); /* Create display list letterL */ glutReshapeFunc(Reshape); glutKeyboardFunc(Key); glutDisplayFunc(Draw); glutMainLoop(); }