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
38 #define MAX_CONFIGS 10
45 /* XXX this probably isn't very portable */
50 /* return current time (in seconds) */
56 (void) gettimeofday(&tv
, NULL
);
59 (void) gettimeofday(&tv
, &tz
);
61 return (double) tv
.tv_sec
+ tv
.tv_usec
/ 1000000.0;
70 /* update this function for other platforms! */
71 static double t
= 0.0;
74 fprintf(stderr
, "Warning: current_time() not implemented!!\n");
84 #define M_PI 3.14159265
88 static GLfloat view_rotx
= 20.0, view_roty
= 30.0, view_rotz
= 0.0;
89 static GLint gear1
, gear2
, gear3
;
90 static GLfloat angle
= 0.0;
93 static GLfloat eyesep
= 5.0; /* Eye separation. */
94 static GLfloat fix_point
= 40.0; /* Fixation point distance. */
95 static GLfloat left
, right
, asp
; /* Stereo frustum params. */
101 * Draw a gear wheel. You'll probably want to call this function when
102 * building a display list since we do a lot of trig here.
104 * Input: inner_radius - radius of hole at center
105 * outer_radius - radius at center of teeth
106 * width - width of gear
107 * teeth - number of teeth
108 * tooth_depth - depth of tooth
111 gear(GLfloat inner_radius
, GLfloat outer_radius
, GLfloat width
,
112 GLint teeth
, GLfloat tooth_depth
)
120 r1
= outer_radius
- tooth_depth
/ 2.0;
121 r2
= outer_radius
+ tooth_depth
/ 2.0;
123 da
= 2.0 * M_PI
/ teeth
/ 4.0;
125 glShadeModel(GL_FLAT
);
127 glNormal3f(0.0, 0.0, 1.0);
129 /* draw front face */
130 glBegin(GL_QUAD_STRIP
);
131 for (i
= 0; i
<= teeth
; i
++) {
132 angle
= i
* 2.0 * M_PI
/ teeth
;
133 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
134 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
136 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
137 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
143 /* draw front sides of teeth */
145 da
= 2.0 * M_PI
/ teeth
/ 4.0;
146 for (i
= 0; i
< teeth
; i
++) {
147 angle
= i
* 2.0 * M_PI
/ teeth
;
149 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
150 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), width
* 0.5);
151 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
),
153 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
158 glNormal3f(0.0, 0.0, -1.0);
161 glBegin(GL_QUAD_STRIP
);
162 for (i
= 0; i
<= teeth
; i
++) {
163 angle
= i
* 2.0 * M_PI
/ teeth
;
164 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
165 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
167 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
169 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
174 /* draw back sides of teeth */
176 da
= 2.0 * M_PI
/ teeth
/ 4.0;
177 for (i
= 0; i
< teeth
; i
++) {
178 angle
= i
* 2.0 * M_PI
/ teeth
;
180 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
182 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
),
184 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), -width
* 0.5);
185 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
189 /* draw outward faces of teeth */
190 glBegin(GL_QUAD_STRIP
);
191 for (i
= 0; i
< teeth
; i
++) {
192 angle
= i
* 2.0 * M_PI
/ teeth
;
194 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
195 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
196 u
= r2
* cos(angle
+ da
) - r1
* cos(angle
);
197 v
= r2
* sin(angle
+ da
) - r1
* sin(angle
);
198 len
= sqrt(u
* u
+ v
* v
);
201 glNormal3f(v
, -u
, 0.0);
202 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), width
* 0.5);
203 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), -width
* 0.5);
204 glNormal3f(cos(angle
), sin(angle
), 0.0);
205 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
),
207 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
),
209 u
= r1
* cos(angle
+ 3 * da
) - r2
* cos(angle
+ 2 * da
);
210 v
= r1
* sin(angle
+ 3 * da
) - r2
* sin(angle
+ 2 * da
);
211 glNormal3f(v
, -u
, 0.0);
212 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
214 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
216 glNormal3f(cos(angle
), sin(angle
), 0.0);
219 glVertex3f(r1
* cos(0), r1
* sin(0), width
* 0.5);
220 glVertex3f(r1
* cos(0), r1
* sin(0), -width
* 0.5);
224 glShadeModel(GL_SMOOTH
);
226 /* draw inside radius cylinder */
227 glBegin(GL_QUAD_STRIP
);
228 for (i
= 0; i
<= teeth
; i
++) {
229 angle
= i
* 2.0 * M_PI
/ teeth
;
230 glNormal3f(-cos(angle
), -sin(angle
), 0.0);
231 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
232 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
241 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
244 glRotatef(view_rotx
, 1.0, 0.0, 0.0);
245 glRotatef(view_roty
, 0.0, 1.0, 0.0);
246 glRotatef(view_rotz
, 0.0, 0.0, 1.0);
249 glTranslatef(-3.0, -2.0, 0.0);
250 glRotatef(angle
, 0.0, 0.0, 1.0);
255 glTranslatef(3.1, -2.0, 0.0);
256 glRotatef(-2.0 * angle
- 9.0, 0.0, 0.0, 1.0);
261 glTranslatef(-3.1, 4.2, 0.0);
262 glRotatef(-2.0 * angle
- 25.0, 0.0, 0.0, 1.0);
270 /* new window size or exposure */
272 reshape(int width
, int height
)
274 glViewport(0, 0, (GLint
) width
, (GLint
) height
);
276 GLfloat h
= (GLfloat
) height
/ (GLfloat
) width
;
278 glMatrixMode(GL_PROJECTION
);
280 glFrustum(-1.0, 1.0, -h
, h
, 5.0, 60.0);
282 glMatrixMode(GL_MODELVIEW
);
284 glTranslatef(0.0, 0.0, -40.0);
292 static GLfloat pos
[4] = { 5.0, 5.0, 10.0, 0.0 };
293 static GLfloat red
[4] = { 0.8, 0.1, 0.0, 1.0 };
294 static GLfloat green
[4] = { 0.0, 0.8, 0.2, 1.0 };
295 static GLfloat blue
[4] = { 0.2, 0.2, 1.0, 1.0 };
297 glLightfv(GL_LIGHT0
, GL_POSITION
, pos
);
298 glEnable(GL_CULL_FACE
);
299 glEnable(GL_LIGHTING
);
301 glEnable(GL_DEPTH_TEST
);
304 gear1
= glGenLists(1);
305 glNewList(gear1
, GL_COMPILE
);
306 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, red
);
307 gear(1.0, 4.0, 1.0, 20, 0.7);
310 gear2
= glGenLists(1);
311 glNewList(gear2
, GL_COMPILE
);
312 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, green
);
313 gear(0.5, 2.0, 2.0, 10, 0.7);
316 gear3
= glGenLists(1);
317 glNewList(gear3
, GL_COMPILE
);
318 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, blue
);
319 gear(1.3, 2.0, 0.5, 10, 0.7);
322 glEnable(GL_NORMALIZE
);
328 static void run_gears(EGLDisplay dpy
, EGLSurface surf
, int ttr
)
330 double st
= current_time();
333 while (ct
- st
< ttr
)
335 double tt
= current_time();
339 /* advance rotation for next frame */
340 angle
+= 70.0 * dt
; /* 70 degrees per second */
346 eglSwapBuffers(dpy
, surf
);
352 GLfloat seconds
= ct
- st
;
353 GLfloat fps
= frames
/ seconds
;
354 printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames
, seconds
, fps
);
360 main(int argc
, char *argv
[])
364 EGLSurface screen_surf
;
365 EGLConfig configs
[MAX_CONFIGS
];
366 EGLint numConfigs
, i
;
369 EGLint screenAttribs
[10];
370 EGLModeMESA mode
[MAX_MODES
];
371 EGLScreenMESA screen
;
372 EGLint count
, chosenMode
;
373 GLboolean printInfo
= GL_FALSE
;
374 EGLint width
= 0, height
= 0;
376 /* parse cmd line args */
377 for (i
= 1; i
< argc
; i
++)
379 if (strcmp(argv
[i
], "-info") == 0)
384 printf("Warning: unknown parameter: %s\n", argv
[i
]);
387 /* DBR : Create EGL context/surface etc */
388 d
= eglGetDisplay(":0");
391 if (!eglInitialize(d
, &maj
, &min
)) {
392 printf("eglgears: eglInitialize failed\n");
396 printf("eglgears: EGL version = %d.%d\n", maj
, min
);
397 printf("eglgears: EGL_VENDOR = %s\n", eglQueryString(d
, EGL_VENDOR
));
399 /* XXX use ChooseConfig */
400 eglGetConfigs(d
, configs
, MAX_CONFIGS
, &numConfigs
);
401 eglGetScreensMESA(d
, &screen
, 1, &count
);
403 if (!eglGetModesMESA(d
, screen
, mode
, MAX_MODES
, &count
) || count
== 0) {
404 printf("eglgears: eglGetModesMESA failed!\n");
408 /* Print list of modes, and find the one to use */
409 printf("eglgears: Found %d modes:\n", count
);
410 for (i
= 0; i
< count
; i
++) {
412 eglGetModeAttribMESA(d
, mode
[i
], EGL_WIDTH
, &w
);
413 eglGetModeAttribMESA(d
, mode
[i
], EGL_HEIGHT
, &h
);
414 printf("%3d: %d x %d\n", i
, w
, h
);
415 if (w
> width
&& h
> height
&& w
<= 1280 && h
<= 1024) {
421 printf("eglgears: Using screen mode/size %d: %d x %d\n", chosenMode
, width
, height
);
423 ctx
= eglCreateContext(d
, configs
[0], EGL_NO_CONTEXT
, NULL
);
424 if (ctx
== EGL_NO_CONTEXT
) {
425 printf("eglgears: failed to create context\n");
429 /* build up screenAttribs array */
431 screenAttribs
[i
++] = EGL_WIDTH
;
432 screenAttribs
[i
++] = width
;
433 screenAttribs
[i
++] = EGL_HEIGHT
;
434 screenAttribs
[i
++] = height
;
435 screenAttribs
[i
++] = EGL_NONE
;
437 screen_surf
= eglCreateScreenSurfaceMESA(d
, configs
[0], screenAttribs
);
438 if (screen_surf
== EGL_NO_SURFACE
) {
439 printf("eglgears: failed to create screen surface\n");
443 b
= eglShowScreenSurfaceMESA(d
, screen
, screen_surf
, mode
[chosenMode
]);
445 printf("eglgears: show surface failed\n");
449 b
= eglMakeCurrent(d
, screen_surf
, screen_surf
, ctx
);
451 printf("eglgears: make current failed\n");
457 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
458 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
459 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR
));
460 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS
));
464 reshape(width
, height
);
466 glDrawBuffer( GL_BACK
);
468 run_gears(d
, screen_surf
, 5.0);
470 eglDestroySurface(d
, screen_surf
);
471 eglDestroyContext(d
, ctx
);