4 * Written by Nigel Stewart November 2003
6 * This program is test harness for the sphere, cone
7 * and torus shapes in FreeGLUT.
9 * Spinning wireframe and smooth shaded shapes are
10 * displayed until the ESC or q key is pressed. The
11 * number of geometry stacks and slices can be adjusted
12 * using the + and - keys.
15 #include <GL/freeglut.h>
19 static int slices = 16;
20 static int stacks = 16;
22 /* GLUT callback Handlers */
25 resize(int width, int height)
27 const float ar = (float) width / (float) height;
29 glViewport(0, 0, width, height);
30 glMatrixMode(GL_PROJECTION);
32 glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
34 glMatrixMode(GL_MODELVIEW);
41 const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
42 const double a = t*90.0;
44 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
48 glTranslated(-2.4,1.2,-6);
51 glutSolidSphere(1,slices,stacks);
55 glTranslated(0,1.2,-6);
58 glutSolidCone(1,1,slices,stacks);
62 glTranslated(2.4,1.2,-6);
65 glutSolidTorus(0.2,0.8,slices,stacks);
69 glTranslated(-2.4,-1.2,-6);
72 glutWireSphere(1,slices,stacks);
76 glTranslated(0,-1.2,-6);
79 glutWireCone(1,1,slices,stacks);
83 glTranslated(2.4,-1.2,-6);
86 glutWireTorus(0.2,0.8,slices,stacks);
94 key(unsigned char key, int x, int y)
109 if (slices>3 && stacks>3)
126 const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
127 const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
128 const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
129 const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
131 const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
132 const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
133 const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
134 const GLfloat high_shininess[] = { 100.0f };
136 /* Program entry point */
139 main(int argc, char *argv[])
141 glutInit(&argc, argv);
142 glutInitWindowSize(640,480);
143 glutInitWindowPosition (40,40);
144 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
146 glutCreateWindow("FreeGLUT Shapes");
148 glutReshapeFunc(resize);
149 glutDisplayFunc(display);
150 glutKeyboardFunc(key);
153 glClearColor(1,1,1,1);
154 glEnable(GL_CULL_FACE);
157 glEnable(GL_DEPTH_TEST);
158 glDepthFunc(GL_LESS);
161 glEnable(GL_NORMALIZE);
162 glEnable(GL_COLOR_MATERIAL);
163 glEnable(GL_LIGHTING);
165 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
166 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
167 glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
168 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
170 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
171 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
172 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
173 glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);