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 "gears" demo to straight GLX (i.e. no GLUT)
24 * Port by Brian Paul 23 March 2001
26 * Command line options:
27 * -info print GL implementation information
28 * -stereo use stereo enabled GLX visual
38 #include <X11/keysym.h>
47 /* XXX this probably isn't very portable */
52 /* return current time (in seconds) */
58 (void) gettimeofday(&tv
, NULL
);
61 (void) gettimeofday(&tv
, &tz
);
63 return (double) tv
.tv_sec
+ tv
.tv_usec
/ 1000000.0;
72 /* update this function for other platforms! */
73 static double t
= 0.0;
76 fprintf(stderr
, "Warning: current_time() not implemented!!\n");
87 #define M_PI 3.14159265
91 /** Event handler results: */
96 static GLfloat view_rotx
= 20.0, view_roty
= 30.0, view_rotz
= 0.0;
97 static GLint gear1
, gear2
, gear3
;
98 static GLfloat angle
= 0.0;
100 static GLboolean fullscreen
= GL_FALSE
; /* Create a single fullscreen window */
101 static GLboolean stereo
= GL_FALSE
; /* Enable stereo. */
102 static GLboolean animate
= GL_TRUE
; /* Animation */
103 static GLfloat eyesep
= 5.0; /* Eye separation. */
104 static GLfloat fix_point
= 40.0; /* Fixation point distance. */
105 static GLfloat left
, right
, asp
; /* Stereo frustum params. */
110 * Draw a gear wheel. You'll probably want to call this function when
111 * building a display list since we do a lot of trig here.
113 * Input: inner_radius - radius of hole at center
114 * outer_radius - radius at center of teeth
115 * width - width of gear
116 * teeth - number of teeth
117 * tooth_depth - depth of tooth
120 gear(GLfloat inner_radius
, GLfloat outer_radius
, GLfloat width
,
121 GLint teeth
, GLfloat tooth_depth
)
129 r1
= outer_radius
- tooth_depth
/ 2.0;
130 r2
= outer_radius
+ tooth_depth
/ 2.0;
132 da
= 2.0 * M_PI
/ teeth
/ 4.0;
134 glShadeModel(GL_FLAT
);
136 glNormal3f(0.0, 0.0, 1.0);
138 /* draw front face */
139 glBegin(GL_QUAD_STRIP
);
140 for (i
= 0; i
<= teeth
; i
++) {
141 angle
= i
* 2.0 * M_PI
/ teeth
;
142 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
143 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
145 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
146 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
152 /* draw front sides of teeth */
154 da
= 2.0 * M_PI
/ teeth
/ 4.0;
155 for (i
= 0; i
< teeth
; i
++) {
156 angle
= i
* 2.0 * M_PI
/ teeth
;
158 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
159 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), width
* 0.5);
160 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
),
162 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
167 glNormal3f(0.0, 0.0, -1.0);
170 glBegin(GL_QUAD_STRIP
);
171 for (i
= 0; i
<= teeth
; i
++) {
172 angle
= i
* 2.0 * M_PI
/ teeth
;
173 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
174 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
176 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
178 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
183 /* draw back sides of teeth */
185 da
= 2.0 * M_PI
/ teeth
/ 4.0;
186 for (i
= 0; i
< teeth
; i
++) {
187 angle
= i
* 2.0 * M_PI
/ teeth
;
189 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
191 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
),
193 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), -width
* 0.5);
194 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
198 /* draw outward faces of teeth */
199 glBegin(GL_QUAD_STRIP
);
200 for (i
= 0; i
< teeth
; i
++) {
201 angle
= i
* 2.0 * M_PI
/ teeth
;
203 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
204 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
205 u
= r2
* cos(angle
+ da
) - r1
* cos(angle
);
206 v
= r2
* sin(angle
+ da
) - r1
* sin(angle
);
207 len
= sqrt(u
* u
+ v
* v
);
210 glNormal3f(v
, -u
, 0.0);
211 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), width
* 0.5);
212 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), -width
* 0.5);
213 glNormal3f(cos(angle
), sin(angle
), 0.0);
214 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
),
216 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
),
218 u
= r1
* cos(angle
+ 3 * da
) - r2
* cos(angle
+ 2 * da
);
219 v
= r1
* sin(angle
+ 3 * da
) - r2
* sin(angle
+ 2 * da
);
220 glNormal3f(v
, -u
, 0.0);
221 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
223 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
),
225 glNormal3f(cos(angle
), sin(angle
), 0.0);
228 glVertex3f(r1
* cos(0), r1
* sin(0), width
* 0.5);
229 glVertex3f(r1
* cos(0), r1
* sin(0), -width
* 0.5);
233 glShadeModel(GL_SMOOTH
);
235 /* draw inside radius cylinder */
236 glBegin(GL_QUAD_STRIP
);
237 for (i
= 0; i
<= teeth
; i
++) {
238 angle
= i
* 2.0 * M_PI
/ teeth
;
239 glNormal3f(-cos(angle
), -sin(angle
), 0.0);
240 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
241 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
250 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
253 glRotatef(view_rotx
, 1.0, 0.0, 0.0);
254 glRotatef(view_roty
, 0.0, 1.0, 0.0);
255 glRotatef(view_rotz
, 0.0, 0.0, 1.0);
258 glTranslatef(-3.0, -2.0, 0.0);
259 glRotatef(angle
, 0.0, 0.0, 1.0);
264 glTranslatef(3.1, -2.0, 0.0);
265 glRotatef(-2.0 * angle
- 9.0, 0.0, 0.0, 1.0);
270 glTranslatef(-3.1, 4.2, 0.0);
271 glRotatef(-2.0 * angle
- 25.0, 0.0, 0.0, 1.0);
283 /* First left eye. */
284 glDrawBuffer(GL_BACK_LEFT
);
286 glMatrixMode(GL_PROJECTION
);
288 glFrustum(left
, right
, -asp
, asp
, 5.0, 60.0);
290 glMatrixMode(GL_MODELVIEW
);
293 glTranslated(+0.5 * eyesep
, 0.0, 0.0);
297 /* Then right eye. */
298 glDrawBuffer(GL_BACK_RIGHT
);
300 glMatrixMode(GL_PROJECTION
);
302 glFrustum(-right
, -left
, -asp
, asp
, 5.0, 60.0);
304 glMatrixMode(GL_MODELVIEW
);
307 glTranslated(-0.5 * eyesep
, 0.0, 0.0);
317 /** Draw single frame, do SwapBuffers, compute FPS */
319 draw_frame(Display
*dpy
, Window win
)
321 static int frames
= 0;
322 static double tRot0
= -1.0, tRate0
= -1.0;
323 double dt
, t
= current_time();
331 /* advance rotation for next frame */
332 angle
+= 70.0 * dt
; /* 70 degrees per second */
338 glXSwapBuffers(dpy
, win
);
344 if (t
- tRate0
>= 5.0) {
345 GLfloat seconds
= t
- tRate0
;
346 GLfloat fps
= frames
/ seconds
;
347 printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames
, seconds
,
355 /* new window size or exposure */
357 reshape(int width
, int height
)
359 glViewport(0, 0, (GLint
) width
, (GLint
) height
);
364 asp
= (GLfloat
) height
/ (GLfloat
) width
;
365 w
= fix_point
* (1.0 / 5.0);
367 left
= -5.0 * ((w
- 0.5 * eyesep
) / fix_point
);
368 right
= 5.0 * ((w
+ 0.5 * eyesep
) / fix_point
);
371 GLfloat h
= (GLfloat
) height
/ (GLfloat
) width
;
373 glMatrixMode(GL_PROJECTION
);
375 glFrustum(-1.0, 1.0, -h
, h
, 5.0, 60.0);
378 glMatrixMode(GL_MODELVIEW
);
380 glTranslatef(0.0, 0.0, -40.0);
388 static GLfloat pos
[4] = { 5.0, 5.0, 10.0, 0.0 };
389 static GLfloat red
[4] = { 0.8, 0.1, 0.0, 1.0 };
390 static GLfloat green
[4] = { 0.0, 0.8, 0.2, 1.0 };
391 static GLfloat blue
[4] = { 0.2, 0.2, 1.0, 1.0 };
393 glLightfv(GL_LIGHT0
, GL_POSITION
, pos
);
394 glEnable(GL_CULL_FACE
);
395 glEnable(GL_LIGHTING
);
397 glEnable(GL_DEPTH_TEST
);
400 gear1
= glGenLists(1);
401 glNewList(gear1
, GL_COMPILE
);
402 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, red
);
403 gear(1.0, 4.0, 1.0, 20, 0.7);
406 gear2
= glGenLists(1);
407 glNewList(gear2
, GL_COMPILE
);
408 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, green
);
409 gear(0.5, 2.0, 2.0, 10, 0.7);
412 gear3
= glGenLists(1);
413 glNewList(gear3
, GL_COMPILE
);
414 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, blue
);
415 gear(1.3, 2.0, 0.5, 10, 0.7);
418 glEnable(GL_NORMALIZE
);
423 * Remove window border/decorations.
426 no_border( Display
*dpy
, Window w
)
428 static const unsigned MWM_HINTS_DECORATIONS
= (1 << 1);
429 static const int PROP_MOTIF_WM_HINTS_ELEMENTS
= 5;
434 unsigned long functions
;
435 unsigned long decorations
;
437 unsigned long status
;
440 PropMotifWmHints motif_hints
;
442 unsigned long flags
= 0;
444 /* setup the property */
445 motif_hints
.flags
= MWM_HINTS_DECORATIONS
;
446 motif_hints
.decorations
= flags
;
448 /* get the atom for the property */
449 prop
= XInternAtom( dpy
, "_MOTIF_WM_HINTS", True
);
451 /* something went wrong! */
455 /* not sure this is correct, seems to work, XA_WM_HINTS didn't work */
458 XChangeProperty( dpy
, w
, /* display, window */
459 prop
, proptype
, /* property, type */
460 32, /* format: 32-bit datums */
461 PropModeReplace
, /* mode */
462 (unsigned char *) &motif_hints
, /* data */
463 PROP_MOTIF_WM_HINTS_ELEMENTS
/* nelements */
469 * Create an RGB, double-buffered window.
470 * Return the window and context handles.
473 make_window( Display
*dpy
, const char *name
,
474 int x
, int y
, int width
, int height
,
475 Window
*winRet
, GLXContext
*ctxRet
)
477 int attribs
[] = { GLX_RGBA
,
484 int stereoAttribs
[] = { GLX_RGBA
,
493 XSetWindowAttributes attr
;
498 XVisualInfo
*visinfo
;
500 scrnum
= DefaultScreen( dpy
);
501 root
= RootWindow( dpy
, scrnum
);
505 width
= DisplayWidth( dpy
, scrnum
);
506 height
= DisplayHeight( dpy
, scrnum
);
510 visinfo
= glXChooseVisual( dpy
, scrnum
, stereoAttribs
);
512 visinfo
= glXChooseVisual( dpy
, scrnum
, attribs
);
515 printf("Error: couldn't get an RGB, "
516 "Double-buffered, Stereo visual\n");
518 printf("Error: couldn't get an RGB, Double-buffered visual\n");
522 /* window attributes */
523 attr
.background_pixel
= 0;
524 attr
.border_pixel
= 0;
525 attr
.colormap
= XCreateColormap( dpy
, root
, visinfo
->visual
, AllocNone
);
526 attr
.event_mask
= StructureNotifyMask
| ExposureMask
| KeyPressMask
;
527 /* XXX this is a bad way to get a borderless window! */
528 mask
= CWBackPixel
| CWBorderPixel
| CWColormap
| CWEventMask
;
530 win
= XCreateWindow( dpy
, root
, x
, y
, width
, height
,
531 0, visinfo
->depth
, InputOutput
,
532 visinfo
->visual
, mask
, &attr
);
537 /* set hints and properties */
539 XSizeHints sizehints
;
542 sizehints
.width
= width
;
543 sizehints
.height
= height
;
544 sizehints
.flags
= USSize
| USPosition
;
545 XSetNormalHints(dpy
, win
, &sizehints
);
546 XSetStandardProperties(dpy
, win
, name
, name
,
547 None
, (char **)NULL
, 0, &sizehints
);
550 ctx
= glXCreateContext( dpy
, visinfo
, NULL
, True
);
552 printf("Error: glXCreateContext failed\n");
564 * Handle one X event.
565 * \return NOP, EXIT or DRAW
568 handle_event(Display
*dpy
, Window win
, XEvent
*event
)
570 switch (event
->type
) {
573 case ConfigureNotify
:
574 reshape(event
->xconfigure
.width
, event
->xconfigure
.height
);
580 code
= XLookupKeysym(&event
->xkey
, 0);
581 if (code
== XK_Left
) {
584 else if (code
== XK_Right
) {
587 else if (code
== XK_Up
) {
590 else if (code
== XK_Down
) {
594 r
= XLookupString(&event
->xkey
, buffer
, sizeof(buffer
),
596 if (buffer
[0] == 27) {
600 else if (buffer
[0] == 'a' || buffer
[0] == 'A') {
612 event_loop(Display
*dpy
, Window win
)
616 while (!animate
|| XPending(dpy
) > 0) {
618 XNextEvent(dpy
, &event
);
619 op
= handle_event(dpy
, win
, &event
);
626 draw_frame(dpy
, win
);
635 printf(" -display <displayname> set the display to run on\n");
636 printf(" -stereo run in stereo mode\n");
637 printf(" -fullscreen run in fullscreen mode\n");
638 printf(" -info display OpenGL renderer info\n");
639 printf(" -geometry WxH+X+Y window geometry\n");
644 main(int argc
, char *argv
[])
646 unsigned int winWidth
= 300, winHeight
= 300;
651 char *dpyName
= NULL
;
652 GLboolean printInfo
= GL_FALSE
;
655 for (i
= 1; i
< argc
; i
++) {
656 if (strcmp(argv
[i
], "-display") == 0) {
660 else if (strcmp(argv
[i
], "-info") == 0) {
663 else if (strcmp(argv
[i
], "-stereo") == 0) {
666 else if (strcmp(argv
[i
], "-fullscreen") == 0) {
667 fullscreen
= GL_TRUE
;
669 else if (i
< argc
-1 && strcmp(argv
[i
], "-geometry") == 0) {
670 XParseGeometry(argv
[i
+1], &x
, &y
, &winWidth
, &winHeight
);
679 dpy
= XOpenDisplay(dpyName
);
681 printf("Error: couldn't open display %s\n",
682 dpyName
? dpyName
: getenv("DISPLAY"));
686 make_window(dpy
, "glxgears", x
, y
, winWidth
, winHeight
, &win
, &ctx
);
687 XMapWindow(dpy
, win
);
688 glXMakeCurrent(dpy
, win
, ctx
);
691 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
692 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
693 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR
));
694 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS
));
699 /* Set initial projection/viewing transformation.
700 * We can't be sure we'll get a ConfigureNotify event when the window
703 reshape(winWidth
, winHeight
);
705 event_loop(dpy
, win
);
707 glDeleteLists(gear1
, 1);
708 glDeleteLists(gear2
, 1);
709 glDeleteLists(gear3
, 1);
710 glXDestroyContext(dpy
, ctx
);
711 XDestroyWindow(dpy
, win
);