blob: c49cd8514010ba7cd9ba72de5a1b2e911655584a [file] [log] [blame]
jtgafb833d1999-08-19 00:55:39 +00001/* $Id: glxdemo.c,v 1.1 1999/08/19 00:55:43 jtg Exp $ */
2
3
4/*
5 * A demonstration of using the GLX functions. This program is in the
6 * public domain.
7 *
8 * Brian Paul
9 */
10
11
12/*
13 * $Log: glxdemo.c,v $
14 * Revision 1.1 1999/08/19 00:55:43 jtg
15 * Initial revision
16 *
17 * Revision 3.0 1998/02/21 02:16:54 brianp
18 * initial rev
19 *
20 */
21
22
23#include <GL/gl.h>
24#include <GL/glx.h>
25#include <stdio.h>
26#include <stdlib.h>
27
28
29
30static void redraw( Display *dpy, Window w )
31{
32 printf("Redraw event\n");
33
34 glClear( GL_COLOR_BUFFER_BIT );
35
36 glColor3f( 1.0, 1.0, 0.0 );
37 glRectf( -0.8, -0.8, 0.8, 0.8 );
38
39 glXSwapBuffers( dpy, w );
40}
41
42
43
44static void resize( unsigned int width, unsigned int height )
45{
46 printf("Resize event\n");
47 glViewport( 0, 0, width, height );
48 glMatrixMode( GL_PROJECTION );
49 glLoadIdentity();
50 glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
51}
52
53
54
55static Window make_rgb_db_window( Display *dpy,
56 unsigned int width, unsigned int height )
57{
58 int attrib[] = { GLX_RGBA,
59 GLX_RED_SIZE, 1,
60 GLX_GREEN_SIZE, 1,
61 GLX_BLUE_SIZE, 1,
62 GLX_DOUBLEBUFFER,
63 None };
64 int scrnum;
65 XSetWindowAttributes attr;
66 unsigned long mask;
67 Window root;
68 Window win;
69 GLXContext ctx;
70 XVisualInfo *visinfo;
71
72 scrnum = DefaultScreen( dpy );
73 root = RootWindow( dpy, scrnum );
74
75 visinfo = glXChooseVisual( dpy, scrnum, attrib );
76 if (!visinfo) {
77 printf("Error: couldn't get an RGB, Double-buffered visual\n");
78 exit(1);
79 }
80
81 /* window attributes */
82 attr.background_pixel = 0;
83 attr.border_pixel = 0;
84 attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
85 attr.event_mask = StructureNotifyMask | ExposureMask;
86 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
87
88 win = XCreateWindow( dpy, root, 0, 0, width, height,
89 0, visinfo->depth, InputOutput,
90 visinfo->visual, mask, &attr );
91
92 ctx = glXCreateContext( dpy, visinfo, NULL, True );
93
94 glXMakeCurrent( dpy, win, ctx );
95
96 return win;
97}
98
99
100static void event_loop( Display *dpy )
101{
102 XEvent event;
103
104 while (1) {
105 XNextEvent( dpy, &event );
106
107 switch (event.type) {
108 case Expose:
109 redraw( dpy, event.xany.window );
110 break;
111 case ConfigureNotify:
112 resize( event.xconfigure.width, event.xconfigure.height );
113 break;
114 }
115 }
116}
117
118
119
120int main( int argc, char *argv[] )
121{
122 Display *dpy;
123 Window win;
124
125 dpy = XOpenDisplay(NULL);
126
127 win = make_rgb_db_window( dpy, 300, 300 );
128
129 glShadeModel( GL_FLAT );
130 glClearColor( 0.5, 0.5, 0.5, 1.0 );
131
132 XMapWindow( dpy, win );
133
134 event_loop( dpy );
135 return 0;
136}