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

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