3 * Very simple demo of how to use the Mesa/X11 interface instead of the
4 * glx, tk or aux toolkits. I highly recommend using the GLX interface
5 * instead of the X/Mesa interface, however.
7 * This program is in the public domain.
16 #include <X11/Xutil.h>
22 static GLint Black
, Red
, Green
, Blue
;
26 static void make_window( char *title
, int color_flag
)
28 int x
= 10, y
= 10, width
= 400, height
= 300;
36 XSetWindowAttributes attr
;
46 * Do the usual X things to make a window.
49 dpy
= XOpenDisplay(NULL
);
51 printf("Couldn't open default display!\n");
55 scr
= DefaultScreen(dpy
);
56 root
= RootWindow(dpy
, scr
);
58 /* alloc visinfo struct */
59 visinfo
= (XVisualInfo
*) malloc( sizeof(XVisualInfo
) );
61 /* Get a visual and colormap */
63 /* Open TrueColor window */
66 if (!XMatchVisualInfo( dpy, scr, 24, TrueColor, visinfo )) {
67 printf("Couldn't get 24-bit TrueColor visual!\n");
71 if (!XMatchVisualInfo( dpy
, scr
, 8, PseudoColor
, visinfo
)) {
72 printf("Couldn't get 8-bit PseudoColor visual!\n");
76 cmap
= XCreateColormap( dpy
, root
, visinfo
->visual
, AllocNone
);
77 Black
= Red
= Green
= Blue
= 0;
80 /* Open color index window */
82 if (!XMatchVisualInfo( dpy
, scr
, 8, PseudoColor
, visinfo
)) {
83 printf("Couldn't get 8-bit PseudoColor visual\n");
87 cmap
= XCreateColormap( dpy
, root
, visinfo
->visual
, AllocNone
);
93 xcolor
.flags
= DoRed
| DoGreen
| DoBlue
;
94 if (!XAllocColor( dpy
, cmap
, &xcolor
)) {
95 printf("Couldn't allocate black!\n");
103 xcolor
.flags
= DoRed
| DoGreen
| DoBlue
;
104 if (!XAllocColor( dpy
, cmap
, &xcolor
)) {
105 printf("Couldn't allocate red!\n");
111 xcolor
.green
= 0xffff;
113 xcolor
.flags
= DoRed
| DoGreen
| DoBlue
;
114 if (!XAllocColor( dpy
, cmap
, &xcolor
)) {
115 printf("Couldn't allocate green!\n");
118 Green
= xcolor
.pixel
;
122 xcolor
.blue
= 0xffff;
123 xcolor
.flags
= DoRed
| DoGreen
| DoBlue
;
124 if (!XAllocColor( dpy
, cmap
, &xcolor
)) {
125 printf("Couldn't allocate blue!\n");
131 /* set window attributes */
132 attr
.colormap
= cmap
;
133 attr
.event_mask
= ExposureMask
| StructureNotifyMask
;
134 attr
.border_pixel
= BlackPixel( dpy
, scr
);
135 attr
.background_pixel
= BlackPixel( dpy
, scr
);
136 attr_flags
= CWColormap
| CWEventMask
| CWBorderPixel
| CWBackPixel
;
138 /* Create the window */
139 win
= XCreateWindow( dpy
, root
, x
,y
, width
, height
, 0,
140 visinfo
->depth
, InputOutput
,
144 printf("Couldn't open window!\n");
148 XStringListToTextProperty(&title
, 1, &tp
);
149 sh
.flags
= USPosition
| USSize
;
150 XSetWMProperties(dpy
, win
, &tp
, &tp
, 0, 0, &sh
, 0, 0);
151 XMapWindow(dpy
, win
);
153 XNextEvent( dpy
, &e
);
154 if (e
.type
== MapNotify
&& e
.xmap
.window
== win
) {
161 * Now do the special Mesa/Xlib stuff!
164 visual
= XMesaCreateVisual( dpy
, visinfo
,
165 (GLboolean
) color_flag
,
166 GL_FALSE
, /* alpha_flag */
167 GL_FALSE
, /* db_flag */
168 GL_FALSE
, /* stereo flag */
169 GL_FALSE
, /* ximage_flag */
171 0, /* stencil size */
172 0,0,0,0, /* accum_size */
178 printf("Couldn't create Mesa/X visual!\n");
182 /* Create a Mesa rendering context */
183 context
= XMesaCreateContext( visual
,
184 NULL
/* share_list */
187 printf("Couldn't create Mesa/X context!\n");
191 buffer
= XMesaCreateWindowBuffer( visual
, win
);
193 printf("Couldn't create Mesa/X buffer!\n");
198 XMesaMakeCurrent( context
, buffer
);
200 /* Ready to render! */
205 static void draw_cube( void )
209 glColor3f( 1.0, 0.0, 0.0 );
210 glBegin( GL_POLYGON
);
211 glVertex3f( 1.0, 1.0, 1.0 );
212 glVertex3f( 1.0, -1.0, 1.0 );
213 glVertex3f( 1.0, -1.0, -1.0 );
214 glVertex3f( 1.0, 1.0, -1.0 );
217 glBegin( GL_POLYGON
);
218 glVertex3f( -1.0, 1.0, 1.0 );
219 glVertex3f( -1.0, 1.0, -1.0 );
220 glVertex3f( -1.0, -1.0, -1.0 );
221 glVertex3f( -1.0, -1.0, 1.0 );
226 glColor3f( 0.0, 1.0, 0.0 );
227 glBegin( GL_POLYGON
);
228 glVertex3f( 1.0, 1.0, 1.0 );
229 glVertex3f( 1.0, 1.0, -1.0 );
230 glVertex3f( -1.0, 1.0, -1.0 );
231 glVertex3f( -1.0, 1.0, 1.0 );
234 glBegin( GL_POLYGON
);
235 glVertex3f( 1.0, -1.0, 1.0 );
236 glVertex3f( -1.0, -1.0, 1.0 );
237 glVertex3f( -1.0, -1.0, -1.0 );
238 glVertex3f( 1.0, -1.0, -1.0 );
243 glColor3f( 0.0, 0.0, 1.0 );
244 glBegin( GL_POLYGON
);
245 glVertex3f( 1.0, 1.0, 1.0 );
246 glVertex3f( -1.0, 1.0, 1.0 );
247 glVertex3f( -1.0, -1.0, 1.0 );
248 glVertex3f( 1.0, -1.0, 1.0 );
251 glBegin( GL_POLYGON
);
252 glVertex3f( 1.0, 1.0, -1.0 );
253 glVertex3f( 1.0,-1.0, -1.0 );
254 glVertex3f( -1.0,-1.0, -1.0 );
255 glVertex3f( -1.0, 1.0, -1.0 );
262 static void display_loop( void )
264 GLfloat xrot
, yrot
, zrot
;
266 xrot
= yrot
= zrot
= 0.0;
268 glClearColor( 0.0, 0.0, 0.0, 0.0 );
269 glClearIndex( Black
);
271 glMatrixMode( GL_PROJECTION
);
273 glFrustum( -1.0, 1.0, -1.0, 1.0, 1.0, 10.0 );
274 glTranslatef( 0.0, 0.0, -5.0 );
276 glMatrixMode( GL_MODELVIEW
);
279 glCullFace( GL_BACK
);
280 glEnable( GL_CULL_FACE
);
282 glShadeModel( GL_FLAT
);
285 glClear( GL_COLOR_BUFFER_BIT
);
287 glRotatef( xrot
, 1.0, 0.0, 0.0 );
288 glRotatef( yrot
, 0.0, 1.0, 0.0 );
289 glRotatef( zrot
, 0.0, 0.0, 1.0 );
306 int main( int argc
, char *argv
[] )
312 if (strcmp(argv
[1],"-ci")==0)
314 else if (strcmp(argv
[1],"-rgb")==0)
318 printf("Bad flag: %s\n", argv
[1]);
319 printf("Specify -ci for 8-bit color index or -rgb for RGB mode\n");
325 printf("Specify -ci for 8-bit color index or -rgb for RGB mode\n");
326 printf("Defaulting to 8-bit color index\n");
329 make_window( argv
[0], mode
);