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
* Version of glxgears that creates/destroys the rendering context for each
24
* frame. Also periodically destroy/recreate the window.
25
* Good for finding memory leaks, etc.
27
* Command line options:
28
* -info print GL implementation information
39
#include <X11/keysym.h>
48
/* XXX this probably isn't very portable */
53
/* return current time (in seconds) */
59
(void) gettimeofday(&tv, NULL );
62
(void) gettimeofday(&tv, &tz);
64
return (double) tv.tv_sec + tv.tv_usec / 1000000.0;
73
/* update this function for other platforms! */
74
static double t = 0.0;
77
fprintf(stderr, "Warning: current_time() not implemented!!\n");
88
#define M_PI 3.14159265
92
static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
93
static GLint gear1, gear2, gear3;
94
static GLfloat angle = 0.0;
96
static XVisualInfo *visinfo = NULL;
97
static int WinWidth = 300, WinHeight = 300;
102
* Draw a gear wheel. You'll probably want to call this function when
103
* building a display list since we do a lot of trig here.
105
* Input: inner_radius - radius of hole at center
106
* outer_radius - radius at center of teeth
107
* width - width of gear
108
* teeth - number of teeth
109
* tooth_depth - depth of tooth
112
gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
113
GLint teeth, GLfloat tooth_depth)
121
r1 = outer_radius - tooth_depth / 2.0;
122
r2 = outer_radius + tooth_depth / 2.0;
124
da = 2.0 * M_PI / teeth / 4.0;
126
glShadeModel(GL_FLAT);
128
glNormal3f(0.0, 0.0, 1.0);
130
/* draw front face */
131
glBegin(GL_QUAD_STRIP);
132
for (i = 0; i <= teeth; i++) {
133
angle = i * 2.0 * M_PI / teeth;
134
glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
135
glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
137
glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
138
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
144
/* draw front sides of teeth */
146
da = 2.0 * M_PI / teeth / 4.0;
147
for (i = 0; i < teeth; i++) {
148
angle = i * 2.0 * M_PI / teeth;
150
glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
151
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
152
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
154
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
159
glNormal3f(0.0, 0.0, -1.0);
162
glBegin(GL_QUAD_STRIP);
163
for (i = 0; i <= teeth; i++) {
164
angle = i * 2.0 * M_PI / teeth;
165
glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
166
glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
168
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
170
glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
175
/* draw back sides of teeth */
177
da = 2.0 * M_PI / teeth / 4.0;
178
for (i = 0; i < teeth; i++) {
179
angle = i * 2.0 * M_PI / teeth;
181
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
183
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
185
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
186
glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
190
/* draw outward faces of teeth */
191
glBegin(GL_QUAD_STRIP);
192
for (i = 0; i < teeth; i++) {
193
angle = i * 2.0 * M_PI / teeth;
195
glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
196
glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
197
u = r2 * cos(angle + da) - r1 * cos(angle);
198
v = r2 * sin(angle + da) - r1 * sin(angle);
199
len = sqrt(u * u + v * v);
202
glNormal3f(v, -u, 0.0);
203
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
204
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
205
glNormal3f(cos(angle), sin(angle), 0.0);
206
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
208
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
210
u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
211
v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
212
glNormal3f(v, -u, 0.0);
213
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
215
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
217
glNormal3f(cos(angle), sin(angle), 0.0);
220
glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
221
glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
225
glShadeModel(GL_SMOOTH);
227
/* draw inside radius cylinder */
228
glBegin(GL_QUAD_STRIP);
229
for (i = 0; i <= teeth; i++) {
230
angle = i * 2.0 * M_PI / teeth;
231
glNormal3f(-cos(angle), -sin(angle), 0.0);
232
glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
233
glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
242
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
245
glRotatef(view_rotx, 1.0, 0.0, 0.0);
246
glRotatef(view_roty, 0.0, 1.0, 0.0);
247
glRotatef(view_rotz, 0.0, 0.0, 1.0);
250
glTranslatef(-3.0, -2.0, 0.0);
251
glRotatef(angle, 0.0, 0.0, 1.0);
256
glTranslatef(3.1, -2.0, 0.0);
257
glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
262
glTranslatef(-3.1, 4.2, 0.0);
263
glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
271
/* new window size or exposure */
273
reshape(int width, int height)
275
glViewport(0, 0, (GLint) width, (GLint) height);
278
GLfloat h = (GLfloat) height / (GLfloat) width;
280
glMatrixMode(GL_PROJECTION);
282
glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
285
glMatrixMode(GL_MODELVIEW);
287
glTranslatef(0.0, 0.0, -40.0);
294
static GLfloat pos[4] = { 5.0, 5.0, 10.0, 0.0 };
295
static GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 };
296
static GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 };
297
static GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 };
299
glLightfv(GL_LIGHT0, GL_POSITION, pos);
300
glEnable(GL_CULL_FACE);
301
glEnable(GL_LIGHTING);
303
glEnable(GL_DEPTH_TEST);
306
gear1 = glGenLists(1);
307
glNewList(gear1, GL_COMPILE);
308
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
309
gear(1.0, 4.0, 1.0, 20, 0.7);
312
gear2 = glGenLists(1);
313
glNewList(gear2, GL_COMPILE);
314
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
315
gear(0.5, 2.0, 2.0, 10, 0.7);
318
gear3 = glGenLists(1);
319
glNewList(gear3, GL_COMPILE);
320
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
321
gear(1.3, 2.0, 0.5, 10, 0.7);
324
glEnable(GL_NORMALIZE);
329
draw( Display *dpy, Window win )
333
ctx = glXCreateContext( dpy, visinfo, NULL, True );
335
printf("Error: glXCreateContext failed\n");
339
glXMakeCurrent(dpy, win, ctx);
343
reshape(WinWidth, WinHeight);
347
glDeleteLists(gear1, 1);
348
glDeleteLists(gear2, 1);
349
glDeleteLists(gear3, 1);
351
glXSwapBuffers(dpy, win);
352
glXDestroyContext(dpy, ctx);
357
* Create an RGB, double-buffered window.
358
* Return the window and context handles.
361
make_window( Display *dpy, const char *name,
362
int x, int y, int width, int height,
365
int attribs[] = { GLX_RGBA,
373
XSetWindowAttributes attr;
378
scrnum = DefaultScreen( dpy );
379
root = RootWindow( dpy, scrnum );
384
visinfo = glXChooseVisual( dpy, scrnum, attribs );
386
printf("Error: couldn't get an RGB, Double-buffered visual\n");
390
/* window attributes */
391
attr.background_pixel = 0;
392
attr.border_pixel = 0;
393
attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
394
attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
395
attr.override_redirect = 0;
396
mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect;
398
win = XCreateWindow( dpy, root, x, y, width, height,
399
0, visinfo->depth, InputOutput,
400
visinfo->visual, mask, &attr );
402
/* set hints and properties */
404
XSizeHints sizehints;
407
sizehints.width = width;
408
sizehints.height = height;
409
sizehints.flags = USSize | USPosition;
410
XSetNormalHints(dpy, win, &sizehints);
411
XSetStandardProperties(dpy, win, name, name,
412
None, (char **)NULL, 0, &sizehints);
420
event_loop(Display *dpy)
423
make_window(dpy, "glxgears", 0, 0, WinWidth, WinHeight, &win);
424
XMapWindow(dpy, win);
427
while (XPending(dpy) > 0) {
429
XNextEvent(dpy, &event);
430
switch (event.type) {
432
/* we'll redraw below */
434
case ConfigureNotify:
435
WinWidth = event.xconfigure.width;
436
WinHeight = event.xconfigure.height;
442
code = XLookupKeysym(&event.xkey, 0);
443
if (code == XK_Left) {
446
else if (code == XK_Right) {
449
else if (code == XK_Up) {
452
else if (code == XK_Down) {
456
r = XLookupString(&event.xkey, buffer, sizeof(buffer),
458
if (buffer[0] == 27) {
468
static int frames = 0;
469
static double tRot0 = -1.0, tRate0 = -1.0;
470
double dt, t = current_time();
476
/* advance rotation for next frame */
477
angle += 70.0 * dt; /* 70 degrees per second */
488
if (t - tRate0 >= 1.0) {
489
GLfloat seconds = t - tRate0;
490
GLfloat fps = frames / seconds;
491
printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames, seconds,
495
/* Destroy window and create new one */
496
XDestroyWindow(dpy, win);
497
make_window(dpy, "glxgears",
498
(int)(fps * 100) % 100, (int)(fps * 100) % 100, /* x,y */
499
WinWidth, WinHeight, &win);
500
XMapWindow(dpy, win);
510
main(int argc, char *argv[])
513
char *dpyName = NULL;
514
GLboolean printInfo = GL_FALSE;
517
for (i = 1; i < argc; i++) {
518
if (strcmp(argv[i], "-display") == 0) {
522
else if (strcmp(argv[i], "-info") == 0) {
526
printf("Warrning: unknown parameter: %s\n", argv[i]);
529
dpy = XOpenDisplay(dpyName);
531
fprintf(stderr, "Error: couldn't open display %s\n",
532
XDisplayName(dpyName));
537
printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
538
printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
539
printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
540
printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));