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.

glxdemo.c 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * A demonstration of using the GLX functions. This program is in the
  3. * public domain.
  4. *
  5. * Brian Paul
  6. */
  7. #include <GL/gl.h>
  8. #include <GL/glx.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. static void redraw( Display *dpy, Window w )
  12. {
  13. printf("Redraw event\n");
  14. glClear( GL_COLOR_BUFFER_BIT );
  15. glColor3f( 1.0, 1.0, 0.0 );
  16. glRectf( -0.8, -0.8, 0.8, 0.8 );
  17. glXSwapBuffers( dpy, w );
  18. }
  19. static void resize( unsigned int width, unsigned int height )
  20. {
  21. printf("Resize event\n");
  22. glViewport( 0, 0, width, height );
  23. glMatrixMode( GL_PROJECTION );
  24. glLoadIdentity();
  25. glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
  26. }
  27. static Window make_rgb_db_window( Display *dpy,
  28. unsigned int width, unsigned int height )
  29. {
  30. int attrib[] = { GLX_RGBA,
  31. GLX_RED_SIZE, 1,
  32. GLX_GREEN_SIZE, 1,
  33. GLX_BLUE_SIZE, 1,
  34. GLX_DOUBLEBUFFER,
  35. None };
  36. int scrnum;
  37. XSetWindowAttributes attr;
  38. unsigned long mask;
  39. Window root;
  40. Window win;
  41. GLXContext ctx;
  42. XVisualInfo *visinfo;
  43. scrnum = DefaultScreen( dpy );
  44. root = RootWindow( dpy, scrnum );
  45. visinfo = glXChooseVisual( dpy, scrnum, attrib );
  46. if (!visinfo) {
  47. printf("Error: couldn't get an RGB, Double-buffered visual\n");
  48. exit(1);
  49. }
  50. /* window attributes */
  51. attr.background_pixel = 0;
  52. attr.border_pixel = 0;
  53. attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
  54. attr.event_mask = StructureNotifyMask | ExposureMask;
  55. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  56. win = XCreateWindow( dpy, root, 0, 0, width, height,
  57. 0, visinfo->depth, InputOutput,
  58. visinfo->visual, mask, &attr );
  59. ctx = glXCreateContext( dpy, visinfo, NULL, True );
  60. if (!ctx) {
  61. printf("Error: glXCreateContext failed\n");
  62. exit(1);
  63. }
  64. glXMakeCurrent( dpy, win, ctx );
  65. return win;
  66. }
  67. static void event_loop( Display *dpy )
  68. {
  69. XEvent event;
  70. while (1) {
  71. XNextEvent( dpy, &event );
  72. switch (event.type) {
  73. case Expose:
  74. redraw( dpy, event.xany.window );
  75. break;
  76. case ConfigureNotify:
  77. resize( event.xconfigure.width, event.xconfigure.height );
  78. break;
  79. }
  80. }
  81. }
  82. int main( int argc, char *argv[] )
  83. {
  84. Display *dpy;
  85. Window win;
  86. dpy = XOpenDisplay(NULL);
  87. win = make_rgb_db_window( dpy, 300, 300 );
  88. glShadeModel( GL_FLAT );
  89. glClearColor( 0.5, 0.5, 0.5, 1.0 );
  90. XMapWindow( dpy, win );
  91. event_loop( dpy );
  92. return 0;
  93. }