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

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