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