2 * Test glRead/DrawPixels for GL_STENCIL_INDEX, with pixelzoom.
15 static GLint WinWidth
= 500, WinHeight
= 500;
16 static GLboolean TestPacking
= GL_FALSE
;
17 static GLboolean TestList
= GL_FALSE
;
20 static GLuint StencilValue
= 192;
22 static void Display(void)
24 GLubyte stencil
[100 * 100 * 2];
25 GLubyte stencil2
[400 * 400]; /* *2 to test pixelstore stuff */
28 glClearColor(0.5, 0.5, 0.5, 1.0);
29 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
);
31 glEnable(GL_DEPTH_TEST
);
33 glStencilOp(GL_INVERT
, GL_KEEP
, GL_REPLACE
);
34 glStencilFunc(GL_ALWAYS
, StencilValue
, ~0);
35 glEnable(GL_STENCIL_TEST
);
38 glViewport(0, 0, 100, 100);
39 glMatrixMode(GL_PROJECTION
);
41 glOrtho(-1, 1, -1, 1, -1, 0); /* clip away back half of sphere */
42 glMatrixMode(GL_MODELVIEW
);
44 glutSolidSphere(1.0, 20, 10);
46 glDisable(GL_STENCIL_TEST
);
49 glPixelStorei(GL_PACK_ROW_LENGTH
, 120);
50 glPixelStorei(GL_PACK_SKIP_PIXELS
, 5);
53 /* read the stencil image */
54 glReadPixels(0, 0, 100, 100, GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
, stencil
);
58 min
= max
= stencil
[0];
59 for (i
= 1; i
< 100 * 100; i
++) {
65 printf("Stencil value range: [%u, %u]\n", min
, max
);
66 if (max
!= StencilValue
) {
67 printf("Error: expected max stencil val of %u, found %u\n",
72 /* Draw the Z image as luminance above original rendering */
73 glWindowPos2i(0, 100);
74 glDrawPixels(100, 100, GL_LUMINANCE
, GL_UNSIGNED_BYTE
, stencil
);
78 glPixelStorei(GL_PACK_ROW_LENGTH
, 0);
79 glPixelStorei(GL_PACK_SKIP_PIXELS
, 0);
80 glPixelStorei(GL_UNPACK_ROW_LENGTH
, 120);
81 glPixelStorei(GL_UNPACK_SKIP_PIXELS
, 5);
84 /* draw stencil image with scaling (into stencil buffer) */
85 glPixelZoom(4.0, 4.0);
86 glColor4f(1, 0, 0, 0);
87 glWindowPos2i(100, 0);
91 glNewList(list
, GL_COMPILE
);
92 glDrawPixels(100, 100, GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
, stencil
);
95 glDeleteLists(list
, 1);
98 glDrawPixels(100, 100, GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
, stencil
);
102 glPixelStorei(GL_UNPACK_ROW_LENGTH
, 0);
103 glPixelStorei(GL_UNPACK_SKIP_PIXELS
, 0);
106 /* read back scaled stencil image */
107 glReadPixels(100, 0, 400, 400, GL_STENCIL_INDEX
,
108 GL_UNSIGNED_BYTE
, stencil2
);
109 /* draw as luminance */
110 glPixelZoom(1.0, 1.0);
111 glWindowPos2i(100, 0);
112 glDrawPixels(400, 400, GL_LUMINANCE
, GL_UNSIGNED_BYTE
, stencil2
);
119 static void Reshape(int width
, int height
)
123 glViewport(0, 0, width
, height
);
127 static void Key(unsigned char key
, int x
, int y
)
133 TestPacking
= !TestPacking
;
134 printf("Test pixel pack/unpack: %d\n", TestPacking
);
137 TestList
= !TestList
;
138 printf("Test dlist: %d\n", TestList
);
148 static void Init(void)
150 const GLfloat blue
[4] = {.1, .1, 1.0, 1.0};
151 const GLfloat gray
[4] = {0.2, 0.2, 0.2, 1.0};
152 const GLfloat white
[4] = {1.0, 1.0, 1.0, 1.0};
153 const GLfloat pos
[4] = {0, 0, 10, 0};
156 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
157 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
159 glGetIntegerv(GL_STENCIL_BITS
, &bits
);
160 printf("Stencil bits: %d\n", bits
);
164 glMaterialfv(GL_FRONT_AND_BACK
, GL_DIFFUSE
, blue
);
165 glLightfv(GL_LIGHT0
, GL_AMBIENT
, gray
);
166 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, white
);
167 glLightfv(GL_LIGHT0
, GL_POSITION
, pos
);
168 glEnable(GL_LIGHTING
);
173 int main(int argc
, char *argv
[])
175 glutInit(&argc
, argv
);
176 glutInitWindowPosition(0, 0);
177 glutInitWindowSize(WinWidth
, WinHeight
);
178 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_STENCIL
| GLUT_DOUBLE
);
179 glutCreateWindow(argv
[0]);
181 glutReshapeFunc(Reshape
);
182 glutKeyboardFunc(Key
);
183 glutDisplayFunc(Display
);