Add more structure constructor tests.
[piglit/hramrach.git] / tests / general / scissor-bitmap.c
blob3469393ffad58b0a74ca24d59916ac013aa0df08
1 /*
2 * Copyright © 2008 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
28 /** @file scissor-bitmap.c
30 * Tests that clipping of glBitmap to the backbuffer works correctly, whether
31 * it's to window boundaries or glScissor.
34 #include "piglit-util.h"
35 #include "piglit-framework.h"
37 int piglit_width = 400;
38 int piglit_height = 400;
39 int piglit_window_mode = GLUT_DOUBLE | GLUT_RGB;
41 struct probes {
42 struct test_position {
43 const GLfloat *color;
44 const char *name;
45 int x;
46 int y;
47 int bitmap_x_off;
48 int bitmap_y_off;
49 int width;
50 int height;
51 } probes[30];
52 int n_probes;
55 static void
56 add_probe(struct probes *probes,
57 const char *name, const GLfloat *color,
58 int x, int y, int width, int height,
59 int bitmap_x_off, int bitmap_y_off)
61 probes->probes[probes->n_probes].color = color;
62 probes->probes[probes->n_probes].name = name;
63 probes->probes[probes->n_probes].x = x;
64 probes->probes[probes->n_probes].y = y;
65 probes->probes[probes->n_probes].bitmap_x_off = bitmap_x_off;
66 probes->probes[probes->n_probes].bitmap_y_off = bitmap_y_off;
67 probes->probes[probes->n_probes].width = width;
68 probes->probes[probes->n_probes].height = height;
69 probes->n_probes++;
72 static GLboolean
73 get_bitmap_bit(int x, int y)
75 const uint8_t *row = &fdo_bitmap[y * fdo_bitmap_width / 8];
77 return (row[x / 8] >> (7 - x % 8)) & 1;
80 static GLboolean
81 verify_bitmap_pixel(struct probes *probes, int i, int x, int y)
83 const GLfloat black[4] = {0.0, 0.0, 0.0, 0.0};
84 int x1 = probes->probes[i].x;
85 int y1 = probes->probes[i].y;
86 int bitmap_x1 = probes->probes[i].bitmap_x_off;
87 int bitmap_y1 = probes->probes[i].bitmap_y_off;
88 const GLfloat *expected;
89 GLboolean pass;
91 if (probes->probes[i].color == NULL) {
92 expected = black;
93 } else {
94 GLboolean on;
96 on = get_bitmap_bit(x - x1 + bitmap_x1,
97 y - y1 + bitmap_y1);
98 /* Verify that the region is black if unset, or the foreground
99 * color otherwise.
101 if (on)
102 expected = probes->probes[i].color;
103 else
104 expected = black;
107 /* Make sure the region is black */
108 pass = piglit_probe_pixel_rgb(x, y, expected);
109 if (!pass)
110 printf("glBitmap error in %s (test offset %d,%d)\n",
111 probes->probes[i].name,
112 x - probes->probes[i].x,
113 y - probes->probes[i].y);
114 return pass;
117 static GLboolean
118 verify_bitmap_contents(struct probes *probes, int i)
120 int x, y;
121 int x1 = probes->probes[i].x;
122 int y1 = probes->probes[i].y;
123 int x2 = probes->probes[i].x + probes->probes[i].width;
124 int y2 = probes->probes[i].y + probes->probes[i].height;
125 GLboolean pass = GL_TRUE;
127 for (y = y1; y < y2; y++) {
128 if (y < 0 || y >= piglit_height)
129 continue;
130 for (x = x1; x < x2; x++) {
131 if (x < 0 || x >= piglit_width)
132 continue;
134 pass &= verify_bitmap_pixel(probes, i, x, y);
135 if (!pass)
136 return pass;
140 return pass;
143 enum piglit_result
144 piglit_display()
146 const GLfloat red[4] = {1.0, 0.0, 0.0, 0.0};
147 const GLfloat green[4] = {0.0, 1.0, 0.0, 0.0};
148 const GLfloat blue[4] = {0.0, 0.0, 1.0, 0.0};
149 int i;
150 int center_x_start = (piglit_width - fdo_bitmap_width) / 2;
151 int center_y_start = (piglit_height - fdo_bitmap_height) / 2;
152 int start_x, start_y;
153 struct probes probes;
154 GLboolean pass = GL_TRUE;
155 piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
157 memset(&probes, 0, sizeof(probes));
159 glClearColor(0, 0, 0, 0);
160 glClear(GL_COLOR_BUFFER_BIT);
162 glColor4fv(red);
163 /* Center: full image */
164 glRasterPos2f(center_x_start, center_y_start);
165 glBitmap(fdo_bitmap_width, fdo_bitmap_height, 0, 0, 0, 0, fdo_bitmap);
166 add_probe(&probes, "center full", red,
167 center_x_start, center_y_start,
168 fdo_bitmap_width, fdo_bitmap_height,
169 0, 0);
171 glEnable(GL_SCISSOR_TEST);
172 glColor4fv(green);
173 /* Left clipped */
174 start_x = center_x_start - fdo_bitmap_width - 10;
175 start_y = center_y_start;
176 glScissor(start_x + fdo_bitmap_width / 4,
177 start_y,
178 fdo_bitmap_width,
179 fdo_bitmap_height);
180 glRasterPos2f(start_x, start_y);
181 glBitmap(fdo_bitmap_width, fdo_bitmap_height, 0, 0, 0, 0, fdo_bitmap);
182 add_probe(&probes, "left glscissor clipped area", NULL,
183 start_x, start_y,
184 fdo_bitmap_width / 4, fdo_bitmap_height,
185 0, 0);
186 add_probe(&probes, "left glscissor unclipped area", green,
187 start_x + fdo_bitmap_width / 4, start_y,
188 fdo_bitmap_width * 3 / 4, fdo_bitmap_height,
189 fdo_bitmap_width / 4, 0);
191 /* Right clipped */
192 start_x = center_x_start + fdo_bitmap_width + 10;
193 start_y = center_y_start;
194 glScissor(start_x,
195 start_y,
196 fdo_bitmap_width * 3 / 4,
197 fdo_bitmap_height);
198 glRasterPos2f(start_x, start_y);
199 glBitmap(fdo_bitmap_width, fdo_bitmap_height, 0, 0, 0, 0, fdo_bitmap);
200 add_probe(&probes, "right glscissor clipped area", NULL,
201 start_x + fdo_bitmap_width * 3 /4,
202 start_y,
203 fdo_bitmap_width / 4,
204 fdo_bitmap_height,
205 0, 0);
206 add_probe(&probes, "right glscissor unclipped area", green,
207 start_x, start_y,
208 fdo_bitmap_width * 3 / 4, fdo_bitmap_height,
209 0, 0);
211 /* Top clipped */
212 start_x = center_x_start;
213 start_y = center_y_start + fdo_bitmap_height + 10;
214 glScissor(start_x,
215 start_y,
216 fdo_bitmap_width,
217 fdo_bitmap_height * 3 / 4);
218 glRasterPos2f(start_x, start_y);
219 glBitmap(fdo_bitmap_width, fdo_bitmap_height, 0, 0, 0, 0, fdo_bitmap);
220 add_probe(&probes, "top glscissor clipped area", NULL,
221 start_x,
222 start_y + fdo_bitmap_height * 3 /4,
223 fdo_bitmap_width,
224 fdo_bitmap_height / 4,
225 0, 0);
226 add_probe(&probes, "top glscissor unclipped area", green,
227 start_x, start_y,
228 fdo_bitmap_width, fdo_bitmap_height * 3 / 4,
229 0, 0);
231 /* Bottom clipped */
232 start_x = center_x_start;
233 start_y = center_y_start - fdo_bitmap_height - 10;
234 glScissor(start_x,
235 start_y + fdo_bitmap_height / 4,
236 fdo_bitmap_width,
237 fdo_bitmap_height);
238 glRasterPos2f(start_x, start_y);
239 glBitmap(fdo_bitmap_width, fdo_bitmap_height, 0, 0, 0, 0, fdo_bitmap);
240 add_probe(&probes, "bottom glscissor clipped area", NULL,
241 start_x, start_y,
242 fdo_bitmap_width, fdo_bitmap_height / 4,
243 0, 0);
244 add_probe(&probes, "bottom glscissor unclipped area", green,
245 start_x, start_y + fdo_bitmap_height / 4,
246 fdo_bitmap_width, fdo_bitmap_height * 3 / 4,
247 0, fdo_bitmap_height / 4);
249 glDisable(GL_SCISSOR_TEST);
250 glColor4fv(blue);
251 /* Left side of window (not drawn due to invalid pos) */
252 start_x = -fdo_bitmap_width / 4;
253 start_y = center_y_start;
254 glRasterPos2f(start_x, start_y);
255 glBitmap(fdo_bitmap_width, fdo_bitmap_height, 0, 0, 0, 0, fdo_bitmap);
256 add_probe(&probes, "left window clipped area", NULL,
257 start_x + fdo_bitmap_width / 4, start_y,
258 fdo_bitmap_width * 3 / 4, fdo_bitmap_height,
259 fdo_bitmap_width / 4, 0);
260 /* Right side of window */
261 start_x = piglit_width - fdo_bitmap_width * 3 / 4;
262 start_y = center_y_start;
263 glRasterPos2f(start_x, start_y);
264 glBitmap(fdo_bitmap_width, fdo_bitmap_height, 0, 0, 0, 0, fdo_bitmap);
265 add_probe(&probes, "right window unclipped area", blue,
266 start_x, start_y,
267 fdo_bitmap_width * 3 / 4, fdo_bitmap_height,
268 0, 0);
270 /* Top of window */
271 start_x = center_x_start;
272 start_y = piglit_height - fdo_bitmap_height * 3 / 4;
273 glRasterPos2f(start_x, start_y);
274 glBitmap(fdo_bitmap_width, fdo_bitmap_height, 0, 0, 0, 0, fdo_bitmap);
275 add_probe(&probes, "top window unclipped area", blue,
276 start_x, start_y,
277 fdo_bitmap_width, fdo_bitmap_height * 3 / 4,
278 0, 0);
279 /* Bottom of window (not drawn due to invalid pos) */
280 start_x = center_x_start;
281 start_y = -fdo_bitmap_height / 4;
282 glRasterPos2f(start_x, start_y);
283 glBitmap(fdo_bitmap_width, fdo_bitmap_height, 0, 0, 0, 0, fdo_bitmap);
284 add_probe(&probes, "bottom window clipped area", NULL,
285 start_x, start_y,
286 fdo_bitmap_width, fdo_bitmap_height * 3 / 4,
287 0, 0);
289 glutSwapBuffers();
290 glFlush();
292 for (i = 0; i < probes.n_probes; i++) {
293 pass = pass && verify_bitmap_contents(&probes, i);
296 return pass ? PIGLIT_SUCCESS : PIGLIT_FAILURE;
300 void
301 piglit_init(int argc, char **argv)
303 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);