2 * Use glCopyTexSubImage2D to draw animated gears on the sides of a box.
12 #include "glut_wrap.h"
15 #define M_PI 3.14159265
18 static GLint WinWidth
= 800, WinHeight
= 500;
19 static GLint TexWidth
, TexHeight
;
20 static GLuint TexObj
= 1;
21 static GLenum IntFormat
= GL_RGB
;
23 static GLboolean WireFrame
= GL_FALSE
;
26 static GLint Frames
= 0;
29 static GLfloat ViewRotX
= 20.0, ViewRotY
= 30.0, ViewRotZ
= 0.0;
30 static GLint Gear1
, Gear2
, Gear3
;
31 static GLfloat GearRot
= 0.0;
32 static GLfloat CubeRot
= 0.0;
36 Draw a gear wheel. You'll probably want to call this function when
37 building a display list since we do a lot of trig here.
39 Input: inner_radius - radius of hole at center
40 outer_radius - radius at center of teeth
42 teeth - number of teeth
43 tooth_depth - depth of tooth
46 gear(GLfloat inner_radius
, GLfloat outer_radius
, GLfloat width
,
47 GLint teeth
, GLfloat tooth_depth
)
55 r1
= outer_radius
- tooth_depth
/ 2.0;
56 r2
= outer_radius
+ tooth_depth
/ 2.0;
58 da
= 2.0 * M_PI
/ teeth
/ 4.0;
60 glShadeModel(GL_FLAT
);
62 glNormal3f(0.0, 0.0, 1.0);
65 glBegin(GL_QUAD_STRIP
);
66 for (i
= 0; i
<= teeth
; i
++) {
67 angle
= i
* 2.0 * M_PI
/ teeth
;
68 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
69 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
71 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
72 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
), width
* 0.5);
77 /* draw front sides of teeth */
79 da
= 2.0 * M_PI
/ teeth
/ 4.0;
80 for (i
= 0; i
< teeth
; i
++) {
81 angle
= i
* 2.0 * M_PI
/ teeth
;
83 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
84 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), width
* 0.5);
85 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
), width
* 0.5);
86 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
), width
* 0.5);
90 glNormal3f(0.0, 0.0, -1.0);
93 glBegin(GL_QUAD_STRIP
);
94 for (i
= 0; i
<= teeth
; i
++) {
95 angle
= i
* 2.0 * M_PI
/ teeth
;
96 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
97 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
99 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
), -width
* 0.5);
100 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
105 /* draw back sides of teeth */
107 da
= 2.0 * M_PI
/ teeth
/ 4.0;
108 for (i
= 0; i
< teeth
; i
++) {
109 angle
= i
* 2.0 * M_PI
/ teeth
;
111 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
), -width
* 0.5);
112 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
), -width
* 0.5);
113 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), -width
* 0.5);
114 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
118 /* draw outward faces of teeth */
119 glBegin(GL_QUAD_STRIP
);
120 for (i
= 0; i
< teeth
; i
++) {
121 angle
= i
* 2.0 * M_PI
/ teeth
;
123 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
124 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
125 u
= r2
* cos(angle
+ da
) - r1
* cos(angle
);
126 v
= r2
* sin(angle
+ da
) - r1
* sin(angle
);
127 len
= sqrt(u
* u
+ v
* v
);
130 glNormal3f(v
, -u
, 0.0);
131 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), width
* 0.5);
132 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), -width
* 0.5);
133 glNormal3f(cos(angle
), sin(angle
), 0.0);
134 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
), width
* 0.5);
135 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
), -width
* 0.5);
136 u
= r1
* cos(angle
+ 3 * da
) - r2
* cos(angle
+ 2 * da
);
137 v
= r1
* sin(angle
+ 3 * da
) - r2
* sin(angle
+ 2 * da
);
138 glNormal3f(v
, -u
, 0.0);
139 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
), width
* 0.5);
140 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
), -width
* 0.5);
141 glNormal3f(cos(angle
), sin(angle
), 0.0);
144 glVertex3f(r1
* cos(0), r1
* sin(0), width
* 0.5);
145 glVertex3f(r1
* cos(0), r1
* sin(0), -width
* 0.5);
149 glShadeModel(GL_SMOOTH
);
151 /* draw inside radius cylinder */
152 glBegin(GL_QUAD_STRIP
);
153 for (i
= 0; i
<= teeth
; i
++) {
154 angle
= i
* 2.0 * M_PI
/ teeth
;
155 glNormal3f(-cos(angle
), -sin(angle
), 0.0);
156 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
157 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
166 glDeleteTextures(1, &TexObj
);
167 glDeleteLists(Gear1
, 1);
168 glDeleteLists(Gear2
, 1);
169 glDeleteLists(Gear3
, 1);
170 glutDestroyWindow(Win
);
178 glPolygonMode(GL_FRONT_AND_BACK
, GL_LINE
);
182 glRotatef(20/*ViewRotX*/, 1.0, 0.0, 0.0);
183 glRotatef(ViewRotY
, 0.0, 1.0, 0.0);
184 glRotatef(ViewRotZ
, 0.0, 0.0, 1.0);
187 glTranslatef(-3.0, -2.0, 0.0);
188 glRotatef(GearRot
, 0.0, 0.0, 1.0);
193 glTranslatef(3.1, -2.0, 0.0);
194 glRotatef(-2.0 * GearRot
- 9.0, 0.0, 0.0, 1.0);
199 glTranslatef(-3.1, 4.2, 0.0);
200 glRotatef(-2.0 * GearRot
- 25.0, 0.0, 0.0, 1.0);
206 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
213 static const GLfloat texcoords
[4][2] = {
214 { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 }
216 static const GLfloat vertices
[4][2] = {
217 { -1, -1 }, { 1, -1 }, { 1, 1 }, { -1, 1 }
219 static const GLfloat xforms
[6][4] = {
227 static const GLfloat mat
[4] = { 1.0, 1.0, 0.5, 1.0 };
230 glMaterialfv(GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
, mat
);
231 glEnable(GL_TEXTURE_2D
);
234 glRotatef(ViewRotX
, 1.0, 0.0, 0.0);
235 glRotatef(15, 1, 0, 0);
236 glRotatef(CubeRot
, 0, 1, 0);
239 for (i
= 0; i
< 6; i
++) {
241 glRotatef(xforms
[i
][0], xforms
[i
][1], xforms
[i
][2], xforms
[i
][3]);
242 glTranslatef(0, 0, 1.1);
245 for (j
= 0; j
< 4; j
++) {
246 glTexCoord2fv(texcoords
[j
]);
247 glVertex2fv(vertices
[j
]);
254 glDisable(GL_TEXTURE_2D
);
263 glMatrixMode(GL_MODELVIEW
);
265 glTranslatef(0.0, 0.0, -40.0);
267 /* clear whole depth buffer */
268 glDisable(GL_SCISSOR_TEST
);
269 glClear(GL_DEPTH_BUFFER_BIT
);
270 glEnable(GL_SCISSOR_TEST
);
272 /* clear upper-left corner of color buffer (unused space) */
273 glScissor(0, TexHeight
, TexWidth
, WinHeight
- TexHeight
);
274 glClearColor(0.0, 0.0, 0.0, 0.0);
275 glClear(GL_COLOR_BUFFER_BIT
);
277 /* clear lower-left corner of color buffer */
278 glViewport(0, 0, TexWidth
, TexHeight
);
279 glScissor(0, 0, TexWidth
, TexHeight
);
280 glClearColor(1, 1, 1, 0);
281 glClear(GL_COLOR_BUFFER_BIT
);
283 /* draw gears in lower-left corner */
284 glMatrixMode(GL_PROJECTION
);
286 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 60.0);
287 glMatrixMode(GL_MODELVIEW
);
290 /* copy color buffer to texture */
291 glCopyTexSubImage2D(GL_TEXTURE_2D
, 0, 0, 0, 0, 0, TexWidth
, TexHeight
);
293 /* clear right half of color buffer */
294 glViewport(TexWidth
, 0, WinWidth
- TexWidth
, WinHeight
);
295 glScissor(TexWidth
, 0, WinWidth
- TexWidth
, WinHeight
);
296 glClearColor(0.5, 0.5, 0.8, 0.0);
297 glClear(GL_COLOR_BUFFER_BIT
);
299 /* draw textured cube in right half of window */
300 ar
= (float) (WinWidth
- TexWidth
) / WinHeight
;
301 glMatrixMode(GL_PROJECTION
);
303 glFrustum(-ar
, ar
, -1.0, 1.0, 5.0, 60.0);
304 glMatrixMode(GL_MODELVIEW
);
312 GLint t
= glutGet(GLUT_ELAPSED_TIME
);
313 if (t
- T0
>= 5000) {
314 GLfloat seconds
= (t
- T0
) / 1000.0;
315 GLfloat fps
= Frames
/ seconds
;
316 printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames
, seconds
, fps
);
328 static double t0
= -1.;
329 double dt
, t
= glutGet(GLUT_ELAPSED_TIME
) / 1000.0;
335 /* fmod to prevent overflow */
336 GearRot
= fmod(GearRot
+ 70.0 * dt
, 360.0); /* 70 deg/sec */
337 CubeRot
= fmod(CubeRot
+ 15.0 * dt
, 360.0); /* 15 deg/sec */
343 /* change view angle, exit upon ESC */
345 key(unsigned char k
, int x
, int y
)
351 WireFrame
= !WireFrame
;
359 case 27: /* Escape */
369 /* change view angle */
371 special(int k
, int x
, int y
)
395 /* new window size or exposure */
397 reshape(int width
, int height
)
405 init(int argc
, char *argv
[])
407 static GLfloat pos
[4] = {5.0, 5.0, 10.0, 0.0};
408 static GLfloat red
[4] = {0.8, 0.1, 0.0, 1.0};
409 static GLfloat green
[4] = {0.0, 0.8, 0.2, 1.0};
410 static GLfloat blue
[4] = {0.2, 0.2, 1.0, 1.0};
413 glLightfv(GL_LIGHT0
, GL_POSITION
, pos
);
415 glEnable(GL_CULL_FACE
);
417 glEnable(GL_LIGHTING
);
419 glEnable(GL_DEPTH_TEST
);
422 Gear1
= glGenLists(1);
423 glNewList(Gear1
, GL_COMPILE
);
424 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, red
);
425 gear(1.0, 4.0, 1.0, 20, 0.7);
428 Gear2
= glGenLists(1);
429 glNewList(Gear2
, GL_COMPILE
);
430 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, green
);
431 gear(0.5, 2.0, 2.0, 10, 0.7);
434 Gear3
= glGenLists(1);
435 glNewList(Gear3
, GL_COMPILE
);
436 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, blue
);
437 gear(1.3, 2.0, 0.5, 10, 0.7);
440 glEnable(GL_NORMALIZE
);
442 /* xxx make size dynamic */
446 glBindTexture(GL_TEXTURE_2D
, TexObj
);
447 glTexImage2D(GL_TEXTURE_2D
, 0, IntFormat
, TexWidth
, TexHeight
, 0,
448 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
449 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
450 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
451 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
453 for ( i
=1; i
<argc
; i
++ ) {
454 if (strcmp(argv
[i
], "-info")==0) {
455 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
456 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
457 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR
));
458 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS
));
467 if (vis
== GLUT_VISIBLE
)
475 main(int argc
, char *argv
[])
477 glutInitWindowSize(WinWidth
, WinHeight
);
478 glutInit(&argc
, argv
);
479 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
481 Win
= glutCreateWindow("gearbox");
484 glutDisplayFunc(draw
);
485 glutReshapeFunc(reshape
);
486 glutKeyboardFunc(key
);
487 glutSpecialFunc(special
);
488 glutVisibilityFunc(visible
);
491 return 0; /* ANSI C requires main to return int. */