1/** 2 * Test flat shading and clipping. 3 * 4 * Brian Paul 5 * 30 August 2007 6 */ 7 8 9#include <stdio.h> 10#include <stdlib.h> 11#include <math.h> 12#include "glut_wrap.h" 13 14static int Win; 15static GLfloat Scale = 2.0, Zrot = 50; 16static GLenum Mode = GL_LINE_LOOP; 17static GLboolean Smooth = 0; 18static GLenum PolygonMode = GL_FILL; 19 20 21static void 22Draw(void) 23{ 24 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 25 26 if (Smooth) 27 glShadeModel(GL_SMOOTH); 28 else 29 glShadeModel(GL_FLAT); 30 31 glPushMatrix(); 32 glScalef(Scale, Scale, 1); 33 glRotatef(Zrot, 0, 0, 1); 34 35 glPolygonMode(GL_FRONT_AND_BACK, PolygonMode); 36 37 glBegin(Mode); 38 glColor3f(1, 0, 0); 39 glVertex2f(-1, -1); 40 glColor3f(0, 1, 0); 41 glVertex2f( 2, -1); 42 glColor3f(0, 0, 1); 43 glVertex2f( 0, 1); 44 glEnd(); 45 46 glPushMatrix(); 47 glScalef(0.9, 0.9, 1); 48 glBegin(Mode); 49 glColor3f(1, 0, 0); 50 glVertex2f( 0, 1); 51 52 glColor3f(0, 0, 1); 53 glVertex2f( 2, -1); 54 55 glColor3f(0, 1, 0); 56 glVertex2f(-1, -1); 57 58 glEnd(); 59 glPopMatrix(); 60 61 glPopMatrix(); 62 63 glutSwapBuffers(); 64} 65 66 67static void 68Reshape(int width, int height) 69{ 70 glViewport(0, 0, width, height); 71 glMatrixMode(GL_PROJECTION); 72 glLoadIdentity(); 73 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0); 74 glMatrixMode(GL_MODELVIEW); 75 glLoadIdentity(); 76 glTranslatef(0.0, 0.0, -15.0); 77} 78 79 80static void 81Key(unsigned char key, int x, int y) 82{ 83 (void) x; 84 (void) y; 85 switch (key) { 86 case 'p': 87 if (Mode == GL_TRIANGLES) 88 Mode = GL_LINE_LOOP; 89 else 90 Mode = GL_TRIANGLES; 91 break; 92 case 'f': 93 if (PolygonMode == GL_POINT) 94 PolygonMode = GL_LINE; 95 else if (PolygonMode == GL_LINE) 96 PolygonMode = GL_FILL; 97 else 98 PolygonMode = GL_POINT; 99 printf("PolygonMode = 0x%x\n", PolygonMode); 100 break; 101 case 'r': 102 Zrot -= 5.0; 103 break; 104 case 'R': 105 Zrot += 5.0; 106 break; 107 case 'z': 108 Scale *= 1.1; 109 break; 110 case 'Z': 111 Scale /= 1.1; 112 break; 113 case 's': 114 Smooth = !Smooth; 115 break; 116 case 27: 117 glutDestroyWindow(Win); 118 exit(0); 119 break; 120 } 121 glutPostRedisplay(); 122} 123 124 125static void 126Init(void) 127{ 128 printf("Usage:\n"); 129 printf(" z/Z: change triangle size\n"); 130 printf(" r/R: rotate\n"); 131 printf(" p: toggle line/fill mode\n"); 132 printf(" s: toggle smooth/flat shading\n"); 133 printf(" f: switch polygon fill mode\n"); 134} 135 136 137int 138main(int argc, char *argv[]) 139{ 140 glutInit(&argc, argv); 141 glutInitWindowPosition(0, 0); 142 glutInitWindowSize(400, 400); 143 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); 144 Win = glutCreateWindow(argv[0]); 145 glutReshapeFunc(Reshape); 146 glutKeyboardFunc(Key); 147 glutDisplayFunc(Draw); 148 Init(); 149 glutMainLoop(); 150 return 0; 151} 152