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 8.0KB

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