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.

glxpixmap.c 4.6KB

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