Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xdemo.c 7.7KB

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