Brian Paul | 2d47c07 | 2005-07-19 15:27:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * GLX overlay test/demo. |
| 3 | * |
| 4 | * Brian Paul |
| 5 | * 18 July 2005 |
| 6 | */ |
| 7 | |
| 8 | #include <GL/gl.h> |
| 9 | #include <GL/glx.h> |
| 10 | #include <X11/keysym.h> |
| 11 | #include <assert.h> |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | |
| 15 | static int WinWidth = 300, WinHeight = 300; |
| 16 | static Window NormalWindow = 0; |
| 17 | static Window OverlayWindow = 0; |
| 18 | static GLXContext NormalContext = 0; |
| 19 | static GLXContext OverlayContext = 0; |
| 20 | static GLboolean RGBOverlay = GL_FALSE; |
| 21 | static GLfloat Angle = 0.0; |
| 22 | |
| 23 | |
| 24 | static void |
| 25 | RedrawNormal(Display *dpy) |
| 26 | { |
| 27 | glXMakeCurrent(dpy, NormalWindow, NormalContext); |
| 28 | glViewport(0, 0, WinWidth, WinHeight); |
| 29 | glMatrixMode(GL_PROJECTION); |
| 30 | glLoadIdentity(); |
| 31 | glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); |
| 32 | glMatrixMode(GL_MODELVIEW); |
| 33 | glClearColor(0.5, 0.5, 0.5, 1.0); |
| 34 | glClear(GL_COLOR_BUFFER_BIT); |
| 35 | glColor3f(1.0, 1.0, 0.0); |
| 36 | glPushMatrix(); |
| 37 | glRotatef(Angle, 0, 0, 1); |
| 38 | glRectf(-0.8, -0.8, 0.8, 0.8); |
| 39 | glPopMatrix(); |
| 40 | glXSwapBuffers(dpy, NormalWindow); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | static void |
| 45 | RedrawOverlay(Display *dpy) |
| 46 | { |
| 47 | glXMakeCurrent(dpy, OverlayWindow, OverlayContext); |
| 48 | glViewport(0, 0, WinWidth, WinHeight); |
| 49 | glMatrixMode(GL_PROJECTION); |
| 50 | glLoadIdentity(); |
| 51 | glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); |
| 52 | glMatrixMode(GL_MODELVIEW); |
| 53 | glClear(GL_COLOR_BUFFER_BIT); |
| 54 | if (RGBOverlay) { |
| 55 | glColor3f(0.0, 1.0, 1.0); |
| 56 | } |
| 57 | else { |
| 58 | glIndexi(2); |
| 59 | } |
| 60 | glBegin(GL_LINES); |
| 61 | glVertex2f(-1, -1); |
| 62 | glVertex2f(1, 1); |
| 63 | glVertex2f(1, -1); |
| 64 | glVertex2f(-1, 1); |
| 65 | glEnd(); |
| 66 | glXSwapBuffers(dpy, OverlayWindow); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | static Window |
| 71 | MakeWindow(Display *dpy, XVisualInfo *visinfo, Window parent, |
| 72 | unsigned int width, unsigned int height) |
| 73 | { |
| 74 | int scrnum; |
| 75 | XSetWindowAttributes attr; |
| 76 | unsigned long mask; |
| 77 | Window root; |
| 78 | Window win; |
| 79 | |
| 80 | scrnum = DefaultScreen(dpy); |
| 81 | root = RootWindow(dpy, scrnum); |
| 82 | |
| 83 | /* window attributes */ |
| 84 | attr.background_pixel = 0; |
| 85 | attr.border_pixel = 0; |
| 86 | attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone); |
| 87 | attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask; |
| 88 | mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; |
| 89 | |
| 90 | win = XCreateWindow(dpy, parent, 0, 0, width, height, |
| 91 | 0, visinfo->depth, InputOutput, |
| 92 | visinfo->visual, mask, &attr); |
| 93 | return win; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | static void |
| 98 | MakeNormalWindow(Display *dpy) |
| 99 | { |
| 100 | int attrib[] = { GLX_RGBA, |
| 101 | GLX_RED_SIZE, 1, |
| 102 | GLX_GREEN_SIZE, 1, |
| 103 | GLX_BLUE_SIZE, 1, |
| 104 | GLX_DOUBLEBUFFER, |
| 105 | None }; |
| 106 | int scrnum; |
| 107 | Window root; |
| 108 | XVisualInfo *visinfo; |
| 109 | |
| 110 | scrnum = DefaultScreen(dpy); |
| 111 | root = RootWindow(dpy, scrnum); |
| 112 | |
| 113 | visinfo = glXChooseVisual(dpy, scrnum, attrib); |
| 114 | if (!visinfo) { |
| 115 | printf("Error: couldn't get an RGB, Double-buffered visual\n"); |
| 116 | exit(1); |
| 117 | } |
| 118 | |
| 119 | NormalWindow = MakeWindow(dpy, visinfo, root, WinWidth, WinHeight); |
| 120 | assert(NormalWindow); |
| 121 | |
| 122 | NormalContext = glXCreateContext(dpy, visinfo, NULL, True); |
| 123 | assert(NormalContext); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | static void |
| 128 | MakeOverlayWindow(Display *dpy) |
| 129 | { |
| 130 | int rgbAttribs[] = { |
| 131 | GLX_RGBA, |
| 132 | GLX_RED_SIZE, 1, |
| 133 | GLX_GREEN_SIZE, 1, |
| 134 | GLX_BLUE_SIZE, 1, |
| 135 | GLX_DOUBLEBUFFER, |
| 136 | GLX_LEVEL, 1, |
| 137 | None |
| 138 | }; |
| 139 | int indexAttribs[] = { |
| 140 | /*GLX_RGBA, leave this out */ |
| 141 | GLX_RED_SIZE, 1, |
| 142 | GLX_GREEN_SIZE, 1, |
| 143 | GLX_BLUE_SIZE, 1, |
| 144 | GLX_DOUBLEBUFFER, |
| 145 | GLX_LEVEL, 1, |
| 146 | None |
| 147 | }; |
| 148 | int scrnum; |
| 149 | Window root; |
| 150 | XVisualInfo *visinfo; |
| 151 | |
| 152 | scrnum = DefaultScreen(dpy); |
| 153 | root = RootWindow(dpy, scrnum); |
| 154 | |
| 155 | visinfo = glXChooseVisual(dpy, scrnum, rgbAttribs); |
| 156 | if (visinfo) { |
| 157 | printf("Found RGB overlay visual 0x%x\n", (int) visinfo->visualid); |
| 158 | RGBOverlay = GL_TRUE; |
| 159 | } |
| 160 | else { |
| 161 | visinfo = glXChooseVisual(dpy, scrnum, indexAttribs); |
| 162 | if (visinfo) { |
| 163 | printf("Found Color Index overlay visual 0x%x\n", |
| 164 | (int) visinfo->visualid); |
| 165 | /* XXX setup the colormap entries! */ |
| 166 | } |
| 167 | else { |
Brian Paul | 5f75013 | 2009-02-11 07:53:36 -0700 | [diff] [blame] | 168 | printf("Couldn't get an overlay visual.\n"); |
| 169 | printf("Your hardware probably doesn't support framebuffer overlay planes.\n"); |
Brian Paul | 2d47c07 | 2005-07-19 15:27:03 +0000 | [diff] [blame] | 170 | exit(1); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | OverlayWindow = MakeWindow(dpy, visinfo, NormalWindow, WinWidth, WinHeight); |
| 175 | assert(OverlayWindow); |
| 176 | |
| 177 | OverlayContext = glXCreateContext(dpy, visinfo, NULL, True); |
| 178 | assert(OverlayContext); |
| 179 | } |
| 180 | |
| 181 | |
| 182 | static void |
| 183 | EventLoop(Display *dpy) |
| 184 | { |
| 185 | XEvent event; |
| 186 | |
| 187 | while (1) { |
| 188 | XNextEvent(dpy, &event); |
| 189 | |
| 190 | switch (event.type) { |
| 191 | case Expose: |
| 192 | RedrawNormal(dpy); |
| 193 | RedrawOverlay(dpy); |
| 194 | break; |
| 195 | case ConfigureNotify: |
| 196 | WinWidth = event.xconfigure.width; |
| 197 | WinHeight = event.xconfigure.height; |
| 198 | if (event.xconfigure.window == NormalWindow) |
| 199 | XResizeWindow(dpy, OverlayWindow, WinWidth, WinHeight); |
| 200 | break; |
| 201 | case KeyPress: |
| 202 | { |
| 203 | char buffer[10]; |
| 204 | int r, code; |
| 205 | code = XLookupKeysym(&event.xkey, 0); |
| 206 | r = XLookupString(&event.xkey, buffer, sizeof(buffer), |
| 207 | NULL, NULL); |
| 208 | if (buffer[0] == 27) { |
| 209 | /* escape */ |
| 210 | return; |
| 211 | } |
| 212 | else if (buffer[0] == ' ') { |
| 213 | Angle += 5.0; |
| 214 | RedrawNormal(dpy); |
| 215 | } |
| 216 | } |
| 217 | break; |
| 218 | default: |
| 219 | ; /* nothing */ |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
| 225 | int |
| 226 | main(int argc, char *argv[]) |
| 227 | { |
| 228 | Display *dpy = XOpenDisplay(NULL); |
| 229 | |
| 230 | assert(dpy); |
| 231 | |
| 232 | MakeNormalWindow(dpy); |
| 233 | MakeOverlayWindow(dpy); |
| 234 | |
| 235 | XMapWindow(dpy, NormalWindow); |
| 236 | XMapWindow(dpy, OverlayWindow); |
| 237 | |
| 238 | EventLoop(dpy); |
| 239 | |
| 240 | glXDestroyContext(dpy, OverlayContext); |
| 241 | glXDestroyContext(dpy, NormalContext); |
| 242 | XDestroyWindow(dpy, OverlayWindow); |
| 243 | XDestroyWindow(dpy, NormalWindow); |
| 244 | |
| 245 | return 0; |
| 246 | } |