2 * Test the mini GLX interface.
11 #define USE_MINI_GLX 1
13 #include <GL/miniglx.h>
18 static GLXContext ctx
;
20 static GLuint NumFrames
= 100;
21 static GLuint NumDisplays
= 1;
22 static GLboolean Texture
= GL_FALSE
;
23 static GLboolean SingleBuffer
= GL_FALSE
;
24 static GLboolean Sleeps
= GL_TRUE
;
28 rect(GLfloat x1
, GLfloat y1
, GLfloat x2
, GLfloat y2
)
31 glTexCoord2f(0, 0); glColor3f(0, 0, 1); glVertex2f(x1
, y1
);
32 glTexCoord2f(1, 0); glColor3f(1, 0, 0); glVertex2f(x2
, y1
);
33 glTexCoord2f(1, 1); glColor3f(0, 1, 0); glVertex2f(x2
, y2
);
34 glTexCoord2f(0, 1); glColor3f(0, 0, 0); glVertex2f(x1
, y2
);
40 redraw(Display
*dpy
, Window w
, int rot
)
44 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
47 glRotatef(rot
, 0, 0, 1);
49 for (a
= 0.0; a
< 360.0; a
+= 30.0) {
51 glRotatef(a
, 0, 0, 1);
52 glRotatef(40, 1, 0, 0);
53 glColor3f(a
/ 360.0, 1-a
/360.0, 0);
54 rect(0.3, -0.25, 1.5, 0.25);
62 glXSwapBuffers(dpy
, w
);
67 make_window(Display
*dpy
, unsigned int width
, unsigned int height
)
69 int attrib_single
[] = { GLX_RGBA
,
75 int attrib_double
[] = { GLX_RGBA
,
82 int *attrib
= SingleBuffer
? attrib_single
: attrib_double
;
84 XSetWindowAttributes attr
;
90 root
= RootWindow(dpy
, scrnum
);
92 if (!(visinfo
= glXChooseVisual(dpy
, scrnum
, attrib
))) {
93 printf("Error: couldn't get an RGB, Double-buffered visual\n");
97 if (!(ctx
= glXCreateContext(dpy
, visinfo
, NULL
, True
))) {
98 printf("Error: glXCreateContext failed\n");
102 /* window attributes */
103 attr
.background_pixel
= 0;
104 attr
.border_pixel
= 0;
105 attr
.colormap
= XCreateColormap(dpy
, root
, visinfo
->visual
, AllocNone
);
106 attr
.event_mask
= StructureNotifyMask
| ExposureMask
;
107 mask
= CWBackPixel
| CWBorderPixel
| CWColormap
| CWEventMask
;
109 win
= XCreateWindow(dpy
, root
, 0, 0, width
, height
,
110 0, visinfo
->depth
, InputOutput
,
111 visinfo
->visual
, mask
, &attr
);
113 printf("Error: XCreateWindow failed\n");
117 glXMakeCurrent(dpy
, win
, ctx
);
119 glViewport(0, 0, width
, height
);
126 event_loop(Display
*dpy
, Window win
)
130 printf("Drawing %d frames\n", NumFrames
);
132 for (i
= 0; i
< NumFrames
; i
++) {
133 redraw(dpy
, win
, -i
*2);
147 dpy
= XOpenDisplay(NULL
);
149 printf("Error: XOpenDisplay failed\n");
153 win
= make_window(dpy
, 800, 600);
158 glClearColor(0.5, 0.5, 0.5, 1.0);
159 glEnable(GL_DEPTH_TEST
);
161 GLubyte image
[16][16][4];
163 for (i
= 0; i
< 16; i
++) {
164 for (j
= 0; j
< 16; j
++) {
165 if (((i
/ 2) ^ (j
/ 2)) & 1) {
166 image
[i
][j
][0] = 255;
167 image
[i
][j
][1] = 255;
168 image
[i
][j
][2] = 255;
169 image
[i
][j
][3] = 255;
172 image
[i
][j
][0] = 128;
173 image
[i
][j
][1] = 128;
174 image
[i
][j
][2] = 128;
175 image
[i
][j
][3] = 128;
179 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, 16, 16, 0,
180 GL_RGBA
, GL_UNSIGNED_BYTE
, image
);
181 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
182 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
183 glEnable(GL_TEXTURE_2D
);
186 glDrawBuffer(GL_FRONT
);
187 glReadBuffer(GL_FRONT
);
190 glDrawBuffer(GL_BACK
);
193 XMapWindow(dpy
, win
);
195 /* wait for window to get mapped */
200 if (e
.type
== MapNotify
&& e
.xmap
.window
== win
) {
206 event_loop(dpy
, win
);
208 glXDestroyContext(dpy
, ctx
);
209 XDestroyWindow(dpy
, win
);
221 printf(" -f N render N frames (default %d)\n", NumFrames
);
222 printf(" -d N do N display cycles\n");
223 printf(" -t texturing\n");
224 printf(" -s single buffering\n");
225 printf(" -n no usleep() delay\n");
230 parse_args(int argc
, char *argv
[])
233 for (i
= 1; i
< argc
; i
++) {
234 if (strcmp(argv
[i
], "-f") == 0) {
235 NumFrames
= atoi(argv
[i
+ 1]);
238 else if (strcmp(argv
[i
], "-d") == 0) {
239 NumDisplays
= atoi(argv
[i
+ 1]);
242 else if (strcmp(argv
[i
], "-n") == 0) {
245 else if (strcmp(argv
[i
], "-s") == 0) {
246 SingleBuffer
= GL_TRUE
;
248 else if (strcmp(argv
[i
], "-t") == 0) {
260 main(int argc
, char *argv
[])
264 parse_args(argc
, argv
);
266 for (i
= 0; i
< NumDisplays
; i
++) {