2 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * This is a port of the infamous "glxgears" demo to straight EGL
24 * Port by Dane Rushton 10 July 2005
26 * No command line options.
27 * Program runs for 5 seconds then exits, outputing framerate to console
36 static GLfloat view_rotx
= 20.0, view_roty
= 30.0, view_rotz
= 0.0;
37 static GLint gear1
, gear2
, gear3
;
38 static GLfloat angle
= 0.0;
42 * Draw a gear wheel. You'll probably want to call this function when
43 * building a display list since we do a lot of trig here.
45 * Input: inner_radius - radius of hole at center
46 * outer_radius - radius at center of teeth
47 * width - width of gear
48 * teeth - number of teeth
49 * tooth_depth - depth of tooth
52 gear(GLfloat inner_radius
, GLfloat outer_radius
, GLfloat width
,
53 GLint teeth
, GLfloat tooth_depth
)
61 r1
= outer_radius
- tooth_depth
/ 2.0;
62 r2
= outer_radius
+ tooth_depth
/ 2.0;
64 da
= 2.0 * M_PI
/ teeth
/ 4.0;
66 glShadeModel(GL_FLAT
);
68 glNormal3f(0.0, 0.0, 1.0);
71 glBegin(GL_QUAD_STRIP
);
72 for (i
= 0; i
<= teeth
; i
++) {
73 angle
= i
* 2.0 * M_PI
/ teeth
;
74 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
75 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
77 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
78 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
84 /* draw front sides of teeth */
86 da
= 2.0 * M_PI
/ teeth
/ 4.0;
87 for (i
= 0; i
< teeth
; i
++) {
88 angle
= i
* 2.0 * M_PI
/ teeth
;
90 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
91 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), width
* 0.5);
92 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
),
94 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
99 glNormal3f(0.0, 0.0, -1.0);
102 glBegin(GL_QUAD_STRIP
);
103 for (i
= 0; i
<= teeth
; i
++) {
104 angle
= i
* 2.0 * M_PI
/ teeth
;
105 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
106 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
108 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
110 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
115 /* draw back sides of teeth */
117 da
= 2.0 * M_PI
/ teeth
/ 4.0;
118 for (i
= 0; i
< teeth
; i
++) {
119 angle
= i
* 2.0 * M_PI
/ teeth
;
121 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
123 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
),
125 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), -width
* 0.5);
126 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
130 /* draw outward faces of teeth */
131 glBegin(GL_QUAD_STRIP
);
132 for (i
= 0; i
< teeth
; i
++) {
133 angle
= i
* 2.0 * M_PI
/ teeth
;
135 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
136 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
137 u
= r2
* cos(angle
+ da
) - r1
* cos(angle
);
138 v
= r2
* sin(angle
+ da
) - r1
* sin(angle
);
139 len
= sqrt(u
* u
+ v
* v
);
142 glNormal3f(v
, -u
, 0.0);
143 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), width
* 0.5);
144 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), -width
* 0.5);
145 glNormal3f(cos(angle
), sin(angle
), 0.0);
146 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
),
148 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
),
150 u
= r1
* cos(angle
+ 3 * da
) - r2
* cos(angle
+ 2 * da
);
151 v
= r1
* sin(angle
+ 3 * da
) - r2
* sin(angle
+ 2 * da
);
152 glNormal3f(v
, -u
, 0.0);
153 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
155 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
157 glNormal3f(cos(angle
), sin(angle
), 0.0);
160 glVertex3f(r1
* cos(0), r1
* sin(0), width
* 0.5);
161 glVertex3f(r1
* cos(0), r1
* sin(0), -width
* 0.5);
165 glShadeModel(GL_SMOOTH
);
167 /* draw inside radius cylinder */
168 glBegin(GL_QUAD_STRIP
);
169 for (i
= 0; i
<= teeth
; i
++) {
170 angle
= i
* 2.0 * M_PI
/ teeth
;
171 glNormal3f(-cos(angle
), -sin(angle
), 0.0);
172 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
173 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
182 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
185 glRotatef(view_rotx
, 1.0, 0.0, 0.0);
186 glRotatef(view_roty
, 0.0, 1.0, 0.0);
187 glRotatef(view_rotz
, 0.0, 0.0, 1.0);
190 glTranslatef(-3.0, -2.0, 0.0);
191 glRotatef(angle
, 0.0, 0.0, 1.0);
196 glTranslatef(3.1, -2.0, 0.0);
197 glRotatef(-2.0 * angle
- 9.0, 0.0, 0.0, 1.0);
202 glTranslatef(-3.1, 4.2, 0.0);
203 glRotatef(-2.0 * angle
- 25.0, 0.0, 0.0, 1.0);
214 static double t0
= -1.;
215 double dt
, t
= eglutGet(EGLUT_ELAPSED_TIME
) / 1000.0;
221 angle
+= 70.0 * dt
; /* 70 degrees per second */
222 angle
= fmod(angle
, 360.0); /* prevents eventual overflow */
224 eglutPostRedisplay();
227 /* new window size or exposure */
229 reshape(int width
, int height
)
231 GLfloat h
= (GLfloat
) height
/ (GLfloat
) width
;
233 glViewport(0, 0, (GLint
) width
, (GLint
) height
);
235 glMatrixMode(GL_PROJECTION
);
237 glFrustum(-1.0, 1.0, -h
, h
, 5.0, 60.0);
239 glMatrixMode(GL_MODELVIEW
);
241 glTranslatef(0.0, 0.0, -40.0);
249 static GLfloat pos
[4] = { 5.0, 5.0, 10.0, 0.0 };
250 static GLfloat red
[4] = { 0.8, 0.1, 0.0, 1.0 };
251 static GLfloat green
[4] = { 0.0, 0.8, 0.2, 1.0 };
252 static GLfloat blue
[4] = { 0.2, 0.2, 1.0, 1.0 };
254 glLightfv(GL_LIGHT0
, GL_POSITION
, pos
);
255 glEnable(GL_CULL_FACE
);
256 glEnable(GL_LIGHTING
);
258 glEnable(GL_DEPTH_TEST
);
261 gear1
= glGenLists(1);
262 glNewList(gear1
, GL_COMPILE
);
263 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, red
);
264 gear(1.0, 4.0, 1.0, 20, 0.7);
267 gear2
= glGenLists(1);
268 glNewList(gear2
, GL_COMPILE
);
269 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, green
);
270 gear(0.5, 2.0, 2.0, 10, 0.7);
273 gear3
= glGenLists(1);
274 glNewList(gear3
, GL_COMPILE
);
275 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, blue
);
276 gear(1.3, 2.0, 0.5, 10, 0.7);
279 glEnable(GL_NORMALIZE
);
283 main(int argc
, char *argv
[])
285 eglutInitWindowSize(300, 300);
286 eglutInitAPIMask(EGLUT_OPENGL_BIT
);
287 eglutInit(argc
, argv
);
289 eglutCreateWindow("eglgears");
292 eglutReshapeFunc(reshape
);
293 eglutDisplayFunc(draw
);
296 glDrawBuffer(GL_BACK
);