blob: 28f945b27d8f53228297423b9ed3588cf047ac2a [file] [log] [blame]
Brian Paulc8735e82000-04-05 22:09:58 +00001/* $Id: xdemo.c,v 1.2 2000/04/05 22:09:58 brianp Exp $ */
jtgafb833d1999-08-19 00:55:39 +00002
3
4/*
5 * Very simple demo of how to use the Mesa/X11 interface instead of the
6 * glx, tk or aux toolkits. I highly recommend using the GLX interface
7 * instead of the X/Mesa interface, however.
8 *
9 * This program is in the public domain.
10 *
11 * Brian Paul
12 */
13
14
15/*
16 * $Log: xdemo.c,v $
Brian Paulc8735e82000-04-05 22:09:58 +000017 * Revision 1.2 2000/04/05 22:09:58 brianp
18 * new arguments to XMesaCreateVisual()
19 *
20 * Revision 1.1.1.1 1999/08/19 00:55:43 jtg
21 * Imported sources
jtgafb833d1999-08-19 00:55:39 +000022 *
23 * Revision 3.0 1998/02/21 02:16:54 brianp
24 * initial rev
25 *
26 */
27
28
29
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <X11/Xlib.h>
34#include <X11/Xutil.h>
35#include "GL/xmesa.h"
36#include "GL/gl.h"
37
38
39
40static GLint Black, Red, Green, Blue;
41
42
43
44static void make_window( char *title, int color_flag )
45{
46 int x = 10, y = 10, width = 400, height = 300;
47 Display *dpy;
48 int scr;
49 Window root, win;
50 Colormap cmap;
51 XColor xcolor;
52 int attr_flags;
53 XVisualInfo *visinfo;
54 XSetWindowAttributes attr;
55 XTextProperty tp;
56 XSizeHints sh;
57 XEvent e;
58 XMesaContext context;
59 XMesaVisual visual;
60 XMesaBuffer buffer;
61
62
63 /*
64 * Do the usual X things to make a window.
65 */
66
67 dpy = XOpenDisplay(NULL);
68 if (!dpy) {
69 printf("Couldn't open default display!\n");
70 exit(1);
71 }
72
73 scr = DefaultScreen(dpy);
74 root = RootWindow(dpy, scr);
75
76 /* alloc visinfo struct */
77 visinfo = (XVisualInfo *) malloc( sizeof(XVisualInfo) );
78
79 /* Get a visual and colormap */
80 if (color_flag) {
81 /* Open TrueColor window */
82
83/*
84 if (!XMatchVisualInfo( dpy, scr, 24, TrueColor, visinfo )) {
85 printf("Couldn't get 24-bit TrueColor visual!\n");
86 exit(1);
87 }
88*/
89 if (!XMatchVisualInfo( dpy, scr, 8, PseudoColor, visinfo )) {
90 printf("Couldn't get 8-bit PseudoColor visual!\n");
91 exit(1);
92 }
93
94 cmap = XCreateColormap( dpy, root, visinfo->visual, AllocNone );
95 Black = Red = Green = Blue = 0;
96 }
97 else {
98 /* Open color index window */
99
100 if (!XMatchVisualInfo( dpy, scr, 8, PseudoColor, visinfo )) {
101 printf("Couldn't get 8-bit PseudoColor visual\n");
102 exit(1);
103 }
104
105 cmap = XCreateColormap( dpy, root, visinfo->visual, AllocNone );
106
107 /* Allocate colors */
108 xcolor.red = 0x0;
109 xcolor.green = 0x0;
110 xcolor.blue = 0x0;
111 xcolor.flags = DoRed | DoGreen | DoBlue;
112 if (!XAllocColor( dpy, cmap, &xcolor )) {
113 printf("Couldn't allocate black!\n");
114 exit(1);
115 }
116 Black = xcolor.pixel;
117
118 xcolor.red = 0xffff;
119 xcolor.green = 0x0;
120 xcolor.blue = 0x0;
121 xcolor.flags = DoRed | DoGreen | DoBlue;
122 if (!XAllocColor( dpy, cmap, &xcolor )) {
123 printf("Couldn't allocate red!\n");
124 exit(1);
125 }
126 Red = xcolor.pixel;
127
128 xcolor.red = 0x0;
129 xcolor.green = 0xffff;
130 xcolor.blue = 0x0;
131 xcolor.flags = DoRed | DoGreen | DoBlue;
132 if (!XAllocColor( dpy, cmap, &xcolor )) {
133 printf("Couldn't allocate green!\n");
134 exit(1);
135 }
136 Green = xcolor.pixel;
137
138 xcolor.red = 0x0;
139 xcolor.green = 0x0;
140 xcolor.blue = 0xffff;
141 xcolor.flags = DoRed | DoGreen | DoBlue;
142 if (!XAllocColor( dpy, cmap, &xcolor )) {
143 printf("Couldn't allocate blue!\n");
144 exit(1);
145 }
146 Blue = xcolor.pixel;
147 }
148
149 /* set window attributes */
150 attr.colormap = cmap;
151 attr.event_mask = ExposureMask | StructureNotifyMask;
152 attr.border_pixel = BlackPixel( dpy, scr );
153 attr.background_pixel = BlackPixel( dpy, scr );
154 attr_flags = CWColormap | CWEventMask | CWBorderPixel | CWBackPixel;
155
156 /* Create the window */
157 win = XCreateWindow( dpy, root, x,y, width, height, 0,
158 visinfo->depth, InputOutput,
159 visinfo->visual,
160 attr_flags, &attr);
161 if (!win) {
162 printf("Couldn't open window!\n");
163 exit(1);
164 }
165
166 XStringListToTextProperty(&title, 1, &tp);
167 sh.flags = USPosition | USSize;
168 XSetWMProperties(dpy, win, &tp, &tp, 0, 0, &sh, 0, 0);
169 XMapWindow(dpy, win);
170 while (1) {
171 XNextEvent( dpy, &e );
172 if (e.type == MapNotify && e.xmap.window == win) {
173 break;
174 }
175 }
176
177
178 /*
179 * Now do the special Mesa/Xlib stuff!
180 */
181
182 visual = XMesaCreateVisual( dpy, visinfo,
183 (GLboolean) color_flag,
184 GL_FALSE, /* alpha_flag */
185 GL_FALSE, /* db_flag */
186 GL_FALSE, /* stereo flag */
187 GL_FALSE, /* ximage_flag */
188 0, /* depth size */
189 0, /* stencil size */
Brian Paulc8735e82000-04-05 22:09:58 +0000190 0,0,0,0, /* accum_size */
191 0, /* num samples */
192 0, /* level */
193 0 /* caveat */
jtgafb833d1999-08-19 00:55:39 +0000194 );
195 if (!visual) {
196 printf("Couldn't create Mesa/X visual!\n");
197 exit(1);
198 }
199
200 /* Create a Mesa rendering context */
201 context = XMesaCreateContext( visual,
202 NULL /* share_list */
203 );
204 if (!context) {
205 printf("Couldn't create Mesa/X context!\n");
206 exit(1);
207 }
208
209 buffer = XMesaCreateWindowBuffer( visual, win );
210 if (!buffer) {
211 printf("Couldn't create Mesa/X buffer!\n");
212 exit(1);
213 }
214
215
216 XMesaMakeCurrent( context, buffer );
217
218 /* Ready to render! */
219}
220
221
222
223static void draw_cube( void )
224{
225 /* X faces */
226 glIndexi( Red );
227 glColor3f( 1.0, 0.0, 0.0 );
228 glBegin( GL_POLYGON );
229 glVertex3f( 1.0, 1.0, 1.0 );
230 glVertex3f( 1.0, -1.0, 1.0 );
231 glVertex3f( 1.0, -1.0, -1.0 );
232 glVertex3f( 1.0, 1.0, -1.0 );
233 glEnd();
234
235 glBegin( GL_POLYGON );
236 glVertex3f( -1.0, 1.0, 1.0 );
237 glVertex3f( -1.0, 1.0, -1.0 );
238 glVertex3f( -1.0, -1.0, -1.0 );
239 glVertex3f( -1.0, -1.0, 1.0 );
240 glEnd();
241
242 /* Y faces */
243 glIndexi( Green );
244 glColor3f( 0.0, 1.0, 0.0 );
245 glBegin( GL_POLYGON );
246 glVertex3f( 1.0, 1.0, 1.0 );
247 glVertex3f( 1.0, 1.0, -1.0 );
248 glVertex3f( -1.0, 1.0, -1.0 );
249 glVertex3f( -1.0, 1.0, 1.0 );
250 glEnd();
251
252 glBegin( GL_POLYGON );
253 glVertex3f( 1.0, -1.0, 1.0 );
254 glVertex3f( -1.0, -1.0, 1.0 );
255 glVertex3f( -1.0, -1.0, -1.0 );
256 glVertex3f( 1.0, -1.0, -1.0 );
257 glEnd();
258
259 /* Z faces */
260 glIndexi( Blue );
261 glColor3f( 0.0, 0.0, 1.0 );
262 glBegin( GL_POLYGON );
263 glVertex3f( 1.0, 1.0, 1.0 );
264 glVertex3f( -1.0, 1.0, 1.0 );
265 glVertex3f( -1.0, -1.0, 1.0 );
266 glVertex3f( 1.0, -1.0, 1.0 );
267 glEnd();
268
269 glBegin( GL_POLYGON );
270 glVertex3f( 1.0, 1.0, -1.0 );
271 glVertex3f( 1.0,-1.0, -1.0 );
272 glVertex3f( -1.0,-1.0, -1.0 );
273 glVertex3f( -1.0, 1.0, -1.0 );
274 glEnd();
275}
276
277
278
279
280static void display_loop( void )
281{
282 GLfloat xrot, yrot, zrot;
283
284 xrot = yrot = zrot = 0.0;
285
286 glClearColor( 0.0, 0.0, 0.0, 0.0 );
287 glClearIndex( Black );
288
289 glMatrixMode( GL_PROJECTION );
290 glLoadIdentity();
291 glFrustum( -1.0, 1.0, -1.0, 1.0, 1.0, 10.0 );
292 glTranslatef( 0.0, 0.0, -5.0 );
293
294 glMatrixMode( GL_MODELVIEW );
295 glLoadIdentity();
296
297 glCullFace( GL_BACK );
298 glEnable( GL_CULL_FACE );
299
300 glShadeModel( GL_FLAT );
301
302 while (1) {
303 glClear( GL_COLOR_BUFFER_BIT );
304 glPushMatrix();
305 glRotatef( xrot, 1.0, 0.0, 0.0 );
306 glRotatef( yrot, 0.0, 1.0, 0.0 );
307 glRotatef( zrot, 0.0, 0.0, 1.0 );
308
309 draw_cube();
310
311 glPopMatrix();
312 glFinish();
313
314 xrot += 10.0;
315 yrot += 7.0;
316 zrot -= 3.0;
317 }
318
319}
320
321
322
323
324int main( int argc, char *argv[] )
325{
326 int mode = 0;
327
328 if (argc >= 2)
329 {
330 if (strcmp(argv[1],"-ci")==0)
331 mode = 0;
332 else if (strcmp(argv[1],"-rgb")==0)
333 mode = 1;
334 else
335 {
336 printf("Bad flag: %s\n", argv[1]);
337 printf("Specify -ci for 8-bit color index or -rgb for RGB mode\n");
338 exit(1);
339 }
340 }
341 else
342 {
343 printf("Specify -ci for 8-bit color index or -rgb for RGB mode\n");
344 printf("Defaulting to 8-bit color index\n");
345 }
346
347 make_window( argv[0], mode );
348
349 display_loop();
350 return 0;
351}
352