Merge remote branch 'origin/7.8'
[mesa.git] / progs / tests / stencilreaddraw.c
1 /*
2 * Test glRead/DrawPixels for GL_STENCIL_INDEX, with pixelzoom.
3 *
4 * Brian Paul
5 * 8 April 2010
6 */
7
8 #include <assert.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <math.h>
12 #include <GL/glew.h>
13 #include <GL/glut.h>
14
15 static GLint WinWidth = 500, WinHeight = 500;
16 static GLboolean TestPacking = GL_FALSE;
17 static GLboolean TestList = GL_FALSE;
18
19
20 static GLuint StencilValue = 192;
21
22 static void Display(void)
23 {
24 GLubyte stencil[100 * 100 * 2];
25 GLubyte stencil2[400 * 400]; /* *2 to test pixelstore stuff */
26 GLuint list;
27
28 glClearColor(0.5, 0.5, 0.5, 1.0);
29 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
30
31 glEnable(GL_DEPTH_TEST);
32
33 glStencilOp(GL_INVERT, GL_KEEP, GL_REPLACE);
34 glStencilFunc(GL_ALWAYS, StencilValue, ~0);
35 glEnable(GL_STENCIL_TEST);
36
37 /* draw a sphere */
38 glViewport(0, 0, 100, 100);
39 glMatrixMode(GL_PROJECTION);
40 glLoadIdentity();
41 glOrtho(-1, 1, -1, 1, -1, 0); /* clip away back half of sphere */
42 glMatrixMode(GL_MODELVIEW);
43 glLoadIdentity();
44 glutSolidSphere(1.0, 20, 10);
45
46 glDisable(GL_STENCIL_TEST);
47
48 if (TestPacking) {
49 glPixelStorei(GL_PACK_ROW_LENGTH, 120);
50 glPixelStorei(GL_PACK_SKIP_PIXELS, 5);
51 }
52
53 /* read the stencil image */
54 glReadPixels(0, 0, 100, 100, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, stencil);
55 {
56 GLubyte min, max;
57 int i;
58 min = max = stencil[0];
59 for (i = 1; i < 100 * 100; i++) {
60 if (stencil[i] < min)
61 min = stencil[i];
62 if (stencil[i] > max)
63 max = stencil[i];
64 }
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",
68 StencilValue, max);
69 }
70 }
71
72 /* Draw the Z image as luminance above original rendering */
73 glWindowPos2i(0, 100);
74 glDrawPixels(100, 100, GL_LUMINANCE, GL_UNSIGNED_BYTE, stencil);
75
76 if (1) {
77 if (TestPacking) {
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);
82 }
83
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);
88
89 if (TestList) {
90 list = glGenLists(1);
91 glNewList(list, GL_COMPILE);
92 glDrawPixels(100, 100, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, stencil);
93 glEndList();
94 glCallList(list);
95 glDeleteLists(list, 1);
96 }
97 else {
98 glDrawPixels(100, 100, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, stencil);
99 }
100
101 if (TestPacking) {
102 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
103 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
104 }
105
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);
113 }
114
115 glutSwapBuffers();
116 }
117
118
119 static void Reshape(int width, int height)
120 {
121 WinWidth = width;
122 WinHeight = height;
123 glViewport(0, 0, width, height);
124 }
125
126
127 static void Key(unsigned char key, int x, int y)
128 {
129 (void) x;
130 (void) y;
131 switch (key) {
132 case 'p':
133 TestPacking = !TestPacking;
134 printf("Test pixel pack/unpack: %d\n", TestPacking);
135 break;
136 case 'l':
137 TestList = !TestList;
138 printf("Test dlist: %d\n", TestList);
139 break;
140 case 27:
141 exit(0);
142 break;
143 }
144 glutPostRedisplay();
145 }
146
147
148 static void Init(void)
149 {
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};
154 GLint bits;
155
156 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
157 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
158
159 glGetIntegerv(GL_STENCIL_BITS, &bits);
160 printf("Stencil bits: %d\n", bits);
161
162 assert(bits >= 8);
163
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);
169 glEnable(GL_LIGHT0);
170 }
171
172
173 int main(int argc, char *argv[])
174 {
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]);
180 glewInit();
181 glutReshapeFunc(Reshape);
182 glutKeyboardFunc(Key);
183 glutDisplayFunc(Display);
184 Init();
185 glutMainLoop();
186 return 0;
187 }