132001f49Smrg
232001f49Smrg
332001f49Smrg/*
432001f49Smrg * A demonstration of using the GLX functions.  This program is in the
532001f49Smrg * public domain.
632001f49Smrg *
732001f49Smrg * Brian Paul
832001f49Smrg */
932001f49Smrg
1032001f49Smrg#include <GL/gl.h>
1132001f49Smrg#include <GL/glx.h>
1232001f49Smrg#include <stdio.h>
1332001f49Smrg#include <stdlib.h>
1432001f49Smrg
1532001f49Smrg
1632001f49Smrg
1732001f49Smrgstatic void redraw( Display *dpy, Window w )
1832001f49Smrg{
1932001f49Smrg   printf("Redraw event\n");
2032001f49Smrg
2132001f49Smrg   glClear( GL_COLOR_BUFFER_BIT );
2232001f49Smrg
2332001f49Smrg   glColor3f( 1.0, 1.0, 0.0 );
2432001f49Smrg   glRectf( -0.8, -0.8, 0.8, 0.8 );
2532001f49Smrg
2632001f49Smrg   glXSwapBuffers( dpy, w );
2732001f49Smrg}
2832001f49Smrg
2932001f49Smrg
3032001f49Smrg
3132001f49Smrgstatic void resize( unsigned int width, unsigned int height )
3232001f49Smrg{
3332001f49Smrg   printf("Resize event\n");
3432001f49Smrg   glViewport( 0, 0, width, height );
3532001f49Smrg   glMatrixMode( GL_PROJECTION );
3632001f49Smrg   glLoadIdentity();
3732001f49Smrg   glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
3832001f49Smrg}
3932001f49Smrg
4032001f49Smrg
4132001f49Smrg
4232001f49Smrgstatic Window make_rgb_db_window( Display *dpy,
4332001f49Smrg				  unsigned int width, unsigned int height )
4432001f49Smrg{
4532001f49Smrg   int attrib[] = { GLX_RGBA,
4632001f49Smrg		    GLX_RED_SIZE, 1,
4732001f49Smrg		    GLX_GREEN_SIZE, 1,
4832001f49Smrg		    GLX_BLUE_SIZE, 1,
4932001f49Smrg		    GLX_DOUBLEBUFFER,
5032001f49Smrg		    None };
5132001f49Smrg   int scrnum;
5232001f49Smrg   XSetWindowAttributes attr;
5332001f49Smrg   unsigned long mask;
5432001f49Smrg   Window root;
5532001f49Smrg   Window win;
5632001f49Smrg   GLXContext ctx;
5732001f49Smrg   XVisualInfo *visinfo;
5832001f49Smrg
5932001f49Smrg   scrnum = DefaultScreen( dpy );
6032001f49Smrg   root = RootWindow( dpy, scrnum );
6132001f49Smrg
6232001f49Smrg   visinfo = glXChooseVisual( dpy, scrnum, attrib );
6332001f49Smrg   if (!visinfo) {
6432001f49Smrg      printf("Error: couldn't get an RGB, Double-buffered visual\n");
6532001f49Smrg      exit(1);
6632001f49Smrg   }
6732001f49Smrg
6832001f49Smrg   /* window attributes */
6932001f49Smrg   attr.background_pixel = 0;
7032001f49Smrg   attr.border_pixel = 0;
7132001f49Smrg   attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
7232001f49Smrg   attr.event_mask = StructureNotifyMask | ExposureMask;
7332001f49Smrg   mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
7432001f49Smrg
7532001f49Smrg   win = XCreateWindow( dpy, root, 0, 0, width, height,
7632001f49Smrg		        0, visinfo->depth, InputOutput,
7732001f49Smrg		        visinfo->visual, mask, &attr );
7832001f49Smrg
7932001f49Smrg   ctx = glXCreateContext( dpy, visinfo, NULL, True );
8032001f49Smrg   if (!ctx) {
8132001f49Smrg      printf("Error: glXCreateContext failed\n");
8232001f49Smrg      exit(1);
8332001f49Smrg   }
8432001f49Smrg
8532001f49Smrg   glXMakeCurrent( dpy, win, ctx );
8632001f49Smrg
8732001f49Smrg   return win;
8832001f49Smrg}
8932001f49Smrg
9032001f49Smrg
9132001f49Smrgstatic void event_loop( Display *dpy )
9232001f49Smrg{
9332001f49Smrg   XEvent event;
9432001f49Smrg
9532001f49Smrg   while (1) {
9632001f49Smrg      XNextEvent( dpy, &event );
9732001f49Smrg
9832001f49Smrg      switch (event.type) {
9932001f49Smrg	 case Expose:
10032001f49Smrg	    redraw( dpy, event.xany.window );
10132001f49Smrg	    break;
10232001f49Smrg	 case ConfigureNotify:
10332001f49Smrg	    resize( event.xconfigure.width, event.xconfigure.height );
10432001f49Smrg	    break;
10532001f49Smrg      }
10632001f49Smrg   }
10732001f49Smrg}
10832001f49Smrg
10932001f49Smrg
11032001f49Smrg
11132001f49Smrgint main( int argc, char *argv[] )
11232001f49Smrg{
11332001f49Smrg   Display *dpy;
11432001f49Smrg   Window win;
11532001f49Smrg
11632001f49Smrg   dpy = XOpenDisplay(NULL);
11732001f49Smrg
11832001f49Smrg   win = make_rgb_db_window( dpy, 300, 300 );
11932001f49Smrg
12032001f49Smrg   glShadeModel( GL_FLAT );
12132001f49Smrg   glClearColor( 0.5, 0.5, 0.5, 1.0 );
12232001f49Smrg
12332001f49Smrg   XMapWindow( dpy, win );
12432001f49Smrg
12532001f49Smrg   event_loop( dpy );
12632001f49Smrg   return 0;
12732001f49Smrg}
128