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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* $Id: glxpixmap.c,v 1.2 2000/07/11 16:05:29 brianp Exp $ */
  2. /*
  3. * A demonstration of using the GLXPixmap functions. This program is in
  4. * the public domain.
  5. *
  6. * Brian Paul
  7. */
  8. #include <GL/gl.h>
  9. #include <GL/glx.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. static GLXContext ctx;
  14. static XVisualInfo *visinfo;
  15. static GC gc;
  16. static Window make_rgb_window( Display *dpy,
  17. unsigned int width, unsigned int height )
  18. {
  19. const int sbAttrib[] = { GLX_RGBA,
  20. GLX_RED_SIZE, 1,
  21. GLX_GREEN_SIZE, 1,
  22. GLX_BLUE_SIZE, 1,
  23. None };
  24. const int dbAttrib[] = { GLX_RGBA,
  25. GLX_RED_SIZE, 1,
  26. GLX_GREEN_SIZE, 1,
  27. GLX_BLUE_SIZE, 1,
  28. GLX_DOUBLEBUFFER,
  29. None };
  30. int scrnum;
  31. XSetWindowAttributes attr;
  32. unsigned long mask;
  33. Window root;
  34. Window win;
  35. scrnum = DefaultScreen( dpy );
  36. root = RootWindow( dpy, scrnum );
  37. visinfo = glXChooseVisual( dpy, scrnum, (int *) sbAttrib );
  38. if (!visinfo) {
  39. visinfo = glXChooseVisual( dpy, scrnum, (int *) dbAttrib );
  40. if (!visinfo) {
  41. printf("Error: couldn't get an RGB visual\n");
  42. exit(1);
  43. }
  44. }
  45. /* window attributes */
  46. attr.background_pixel = 0;
  47. attr.border_pixel = 0;
  48. /* TODO: share root colormap if possible */
  49. attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
  50. attr.event_mask = StructureNotifyMask | ExposureMask;
  51. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  52. win = XCreateWindow( dpy, root, 0, 0, width, height,
  53. 0, visinfo->depth, InputOutput,
  54. visinfo->visual, mask, &attr );
  55. /* make an X GC so we can do XCopyArea later */
  56. gc = XCreateGC( dpy, win, 0, NULL );
  57. /* need indirect context */
  58. ctx = glXCreateContext( dpy, visinfo, NULL, False );
  59. if (!ctx) {
  60. printf("Error: glXCreateContext failed\n");
  61. exit(-1);
  62. }
  63. printf("Direct rendering: %s\n", glXIsDirect(dpy, ctx) ? "Yes" : "No");
  64. return win;
  65. }
  66. static GLXPixmap make_pixmap( Display *dpy, Window win,
  67. unsigned int width, unsigned int height,
  68. Pixmap *pixmap)
  69. {
  70. Pixmap pm;
  71. GLXPixmap glxpm;
  72. XWindowAttributes attr;
  73. pm = XCreatePixmap( dpy, win, width, height, visinfo->depth );
  74. if (!pm) {
  75. printf("Error: XCreatePixmap failed\n");
  76. exit(-1);
  77. }
  78. XGetWindowAttributes( dpy, win, &attr );
  79. /*
  80. * IMPORTANT:
  81. * Use the glXCreateGLXPixmapMESA funtion when using Mesa because
  82. * Mesa needs to know the colormap associated with a pixmap in order
  83. * to render correctly. This is because Mesa allows RGB rendering
  84. * into any kind of visual, not just TrueColor or DirectColor.
  85. */
  86. #ifdef GLX_MESA_pixmap_colormap
  87. if (strstr(glXQueryExtensionsString(dpy, 0), "GLX_MESA_pixmap_colormap")) {
  88. /* stand-alone Mesa, specify the colormap */
  89. glxpm = glXCreateGLXPixmapMESA( dpy, visinfo, pm, attr.colormap );
  90. }
  91. else {
  92. glxpm = glXCreateGLXPixmap( dpy, visinfo, pm );
  93. }
  94. #else
  95. /* This will work with Mesa too if the visual is TrueColor or DirectColor */
  96. glxpm = glXCreateGLXPixmap( dpy, visinfo, pm );
  97. #endif
  98. if (!glxpm) {
  99. printf("Error: GLXCreateGLXPixmap failed\n");
  100. exit(-1);
  101. }
  102. *pixmap = pm;
  103. return glxpm;
  104. }
  105. static void event_loop( Display *dpy, GLXPixmap pm )
  106. {
  107. XEvent event;
  108. while (1) {
  109. XNextEvent( dpy, &event );
  110. switch (event.type) {
  111. case Expose:
  112. printf("Redraw\n");
  113. /* copy the image from GLXPixmap to window */
  114. XCopyArea( dpy, pm, event.xany.window, /* src, dest */
  115. gc, 0, 0, 300, 300, /* gc, src pos, size */
  116. 0, 0 ); /* dest pos */
  117. break;
  118. case ConfigureNotify:
  119. /* nothing */
  120. break;
  121. }
  122. }
  123. }
  124. int main( int argc, char *argv[] )
  125. {
  126. Display *dpy;
  127. Window win;
  128. Pixmap pm;
  129. GLXPixmap glxpm;
  130. dpy = XOpenDisplay(NULL);
  131. win = make_rgb_window( dpy, 300, 300 );
  132. glxpm = make_pixmap( dpy, win, 300, 300, &pm );
  133. glXMakeCurrent( dpy, glxpm, ctx );
  134. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  135. /* Render an image into the pixmap */
  136. glShadeModel( GL_FLAT );
  137. glClearColor( 0.5, 0.5, 0.5, 1.0 );
  138. glClear( GL_COLOR_BUFFER_BIT );
  139. glViewport( 0, 0, 300, 300 );
  140. glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
  141. glColor3f( 0.0, 1.0, 1.0 );
  142. glRectf( -0.75, -0.75, 0.75, 0.75 );
  143. glFlush();
  144. XMapWindow( dpy, win );
  145. event_loop( dpy, pm );
  146. return 0;
  147. }