Brian Paul | 6d0f9d5 | 2008-04-11 09:21:37 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Test Z compositing with glDrawPixels(GL_DEPTH_COMPONENT) and stencil test. |
| 3 | */ |
| 4 | |
Brian Paul | 6d0f9d5 | 2008-04-11 09:21:37 -0600 | [diff] [blame] | 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <math.h> |
Keith Whitwell | a58065d | 2009-03-10 13:11:23 +0000 | [diff] [blame] | 8 | #include <GL/glew.h> |
Brian Paul | 6d0f9d5 | 2008-04-11 09:21:37 -0600 | [diff] [blame] | 9 | #include <GL/glut.h> |
| 10 | #include "../util/showbuffer.c" |
| 11 | |
| 12 | |
| 13 | static int Win; |
| 14 | static GLfloat Xrot = 0, Yrot = 0, Zpos = 6; |
| 15 | static GLboolean Anim = GL_FALSE; |
| 16 | |
| 17 | static int Width = 400, Height = 200; |
| 18 | static GLfloat *Zimg; |
| 19 | static GLubyte *Cimg; |
| 20 | static GLboolean showZ = 0; |
| 21 | |
| 22 | |
| 23 | static void |
| 24 | Idle(void) |
| 25 | { |
| 26 | Xrot += 3.0; |
| 27 | Yrot += 4.0; |
| 28 | glutPostRedisplay(); |
| 29 | } |
| 30 | |
| 31 | |
| 32 | /** |
| 33 | * Draw first object, save color+Z images |
| 34 | */ |
| 35 | static void |
| 36 | DrawFirst(void) |
| 37 | { |
| 38 | static const GLfloat red[4] = { 1.0, 0.0, 0.0, 0.0 }; |
| 39 | |
| 40 | glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red); |
| 41 | |
| 42 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 43 | |
| 44 | glPushMatrix(); |
| 45 | glTranslatef(-1, 0, 0); |
| 46 | glRotatef(45 + Xrot, 1, 0, 0); |
| 47 | |
| 48 | glutSolidTorus(0.75, 2.0, 10, 20); |
| 49 | |
| 50 | glPopMatrix(); |
| 51 | |
| 52 | glReadPixels(0, 0, Width, Height, GL_DEPTH_COMPONENT, GL_FLOAT, Zimg); |
| 53 | glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, Cimg); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | /** |
| 58 | * Draw second object. |
| 59 | */ |
| 60 | static void |
| 61 | DrawSecond(void) |
| 62 | { |
| 63 | static const GLfloat blue[4] = { 0.0, 0.0, 1.0, 0.0 }; |
| 64 | |
| 65 | glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue); |
| 66 | |
| 67 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
| 68 | |
| 69 | glPushMatrix(); |
| 70 | glTranslatef(+1, 0, 0); |
| 71 | glRotatef(-45 + Xrot, 1, 0, 0); |
| 72 | |
| 73 | glutSolidTorus(0.75, 2.0, 10, 20); |
| 74 | |
| 75 | glPopMatrix(); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | /** |
| 80 | * Composite first/saved image over second rendering. |
| 81 | */ |
| 82 | static void |
| 83 | Composite(void) |
| 84 | { |
| 85 | glWindowPos2i(0, 0); |
| 86 | |
| 87 | /* Draw Z values, set stencil where Z test passes */ |
| 88 | glEnable(GL_STENCIL_TEST); |
| 89 | glStencilFunc(GL_ALWAYS, 1, ~0); |
| 90 | glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); |
| 91 | glColorMask(0,0,0,0); |
| 92 | glDrawPixels(Width, Height, GL_DEPTH_COMPONENT, GL_FLOAT, Zimg); |
| 93 | glColorMask(1,1,1,1); |
| 94 | |
| 95 | /* Draw color where stencil==1 */ |
| 96 | glStencilFunc(GL_EQUAL, 1, ~0); |
| 97 | glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); |
| 98 | glDisable(GL_DEPTH_TEST); |
| 99 | glDrawPixels(Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, Cimg); |
| 100 | glEnable(GL_DEPTH_TEST); |
| 101 | |
| 102 | glDisable(GL_STENCIL_TEST); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | static void |
| 107 | Draw(void) |
| 108 | { |
| 109 | DrawFirst(); |
| 110 | DrawSecond(); |
| 111 | Composite(); |
| 112 | glutSwapBuffers(); |
| 113 | } |
| 114 | |
| 115 | |
| 116 | static void |
| 117 | Reshape(int width, int height) |
| 118 | { |
| 119 | GLfloat ar = (float) width / height; |
| 120 | glViewport(0, 0, width, height); |
| 121 | glMatrixMode(GL_PROJECTION); |
| 122 | glLoadIdentity(); |
| 123 | glFrustum(-ar, ar, -1.0, 1.0, 5.0, 30.0); |
| 124 | glMatrixMode(GL_MODELVIEW); |
| 125 | glLoadIdentity(); |
| 126 | glTranslatef(0.0, 0.0, -15.0); |
| 127 | |
| 128 | Width = width; |
| 129 | Height = height; |
| 130 | |
| 131 | if (Zimg) |
| 132 | free(Zimg); |
| 133 | if (Cimg) |
| 134 | free(Cimg); |
| 135 | Zimg = (float *) malloc(width * height * 4); |
| 136 | Cimg = (GLubyte *) malloc(width * height * 4); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | static void |
| 141 | Key(unsigned char key, int x, int y) |
| 142 | { |
| 143 | const GLfloat step = 1.0; |
| 144 | (void) x; |
| 145 | (void) y; |
| 146 | switch (key) { |
| 147 | case 'a': |
| 148 | Anim = !Anim; |
| 149 | if (Anim) |
| 150 | glutIdleFunc(Idle); |
| 151 | else |
| 152 | glutIdleFunc(NULL); |
| 153 | break; |
| 154 | case 'd': |
| 155 | showZ = !showZ; |
| 156 | break; |
| 157 | case 'z': |
| 158 | Zpos -= step; |
| 159 | break; |
| 160 | case 'Z': |
| 161 | Zpos += step; |
| 162 | break; |
| 163 | case 27: |
| 164 | glutDestroyWindow(Win); |
| 165 | exit(0); |
| 166 | break; |
| 167 | } |
| 168 | glutPostRedisplay(); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | static void |
| 173 | SpecialKey(int key, int x, int y) |
| 174 | { |
| 175 | const GLfloat step = 3.0; |
| 176 | (void) x; |
| 177 | (void) y; |
| 178 | switch (key) { |
| 179 | case GLUT_KEY_UP: |
| 180 | Xrot -= step; |
| 181 | break; |
| 182 | case GLUT_KEY_DOWN: |
| 183 | Xrot += step; |
| 184 | break; |
| 185 | case GLUT_KEY_LEFT: |
| 186 | Yrot -= step; |
| 187 | break; |
| 188 | case GLUT_KEY_RIGHT: |
| 189 | Yrot += step; |
| 190 | break; |
| 191 | } |
| 192 | glutPostRedisplay(); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | static void |
| 197 | Init(void) |
| 198 | { |
| 199 | /* setup lighting, etc */ |
| 200 | glEnable(GL_DEPTH_TEST); |
| 201 | glEnable(GL_LIGHTING); |
| 202 | glEnable(GL_LIGHT0); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | int |
| 207 | main(int argc, char *argv[]) |
| 208 | { |
| 209 | glutInit(&argc, argv); |
| 210 | glutInitWindowPosition(0, 0); |
| 211 | glutInitWindowSize(Width, Height); |
| 212 | glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL); |
| 213 | Win = glutCreateWindow(argv[0]); |
Keith Whitwell | a58065d | 2009-03-10 13:11:23 +0000 | [diff] [blame] | 214 | glewInit(); |
Brian Paul | 6d0f9d5 | 2008-04-11 09:21:37 -0600 | [diff] [blame] | 215 | glutReshapeFunc(Reshape); |
| 216 | glutKeyboardFunc(Key); |
| 217 | glutSpecialFunc(SpecialKey); |
| 218 | glutDisplayFunc(Draw); |
| 219 | if (Anim) |
| 220 | glutIdleFunc(Idle); |
| 221 | Init(); |
| 222 | glutMainLoop(); |
| 223 | return 0; |
| 224 | } |