2 * Automatic primitive rasterization precision test.
4 * Draw prims at various sub-pixel offsets and examine where the quad is
6 * Check if the range of offsets which paint the right pixels falls within
7 * OpenGL's specification.
8 * In case of failures, report the coordinate bias needed to fix the problem.
10 * Note that even Mesa/swrast fails some line tests. This is because some
11 * window coordinates wind up as 53.9999 instead of 54, for example. Enabling
12 * the small translation factor below fixes that. Revisit someday...
25 static int Width
= 100, Height
= 100;
27 static float Step
= 0.125;
29 /* This tiny offset fixes errors in Mesa/Xlib */
30 static float Xtrans
= 0.5 * 0.125;
31 static float Ytrans
= 0.5 * 0.125;
33 static float Xtrans
= 0.0;
34 static float Ytrans
= 0.0;
39 PointCalibrate(int xpos
, int ypos
)
43 float xmin
, ymin
, xmax
, ymax
;
46 xmax
= ymax
= -1000.0;
48 for (y
= -1.0; y
<= 1.0; y
+= Step
) {
49 for (x
= -1.0; x
<= 1.0; x
+= Step
) {
50 glClear(GL_COLOR_BUFFER_BIT
);
52 glVertex2f(xpos
+ x
, ypos
+ y
);
54 glReadPixels(xpos
, ypos
, 1, 1, GL_RGBA
, GL_FLOAT
, rgba
);
55 if (rgba
[0] == 1.0 && rgba
[1] == 1.0 && rgba
[2] == 1.0) {
69 printf("Point at (%2d, %2d) drawn for x in [%6.3f, %6.3f] and y in [%6.3f, %6.3f]\n",
71 xpos
+ xmin
, xpos
+ xmax
,
72 ypos
+ ymin
, ypos
+ ymax
);
74 if (xmax
- xmin
!= 1.0 - Step
) {
75 printf(" => Inconsistant X-axis rasterization!\n");
77 if (ymax
- ymin
!= 1.0 - Step
) {
78 printf(" => Inconsistant Y-axis rasterization!\n");
81 printf(" => Points should be X biased by about %f\n", xmin
);
84 printf(" => Points should be Y biased by about %f\n", ymin
);
87 printf(" => Points should be X biased by about %f\n", 1.0 - xmax
);
90 printf(" => Points should be Y biased by about %f\n", 1.0 - ymax
);
97 * XXX Implement VLineCalibrate() someday
100 HLineCalibrate(int xpos
, int ypos
, int len
)
105 float xmin_left
, xmax_left
, xmin_right
, xmax_right
;
107 xmin_left
= xmin_right
= 1000.0;
108 xmax_left
= xmax_right
= -1000.0;
113 * First, check vertical positioning of the horizontal line
115 for (y
= -1.0; y
<= 1.0; y
+= Step
) {
116 glClear(GL_COLOR_BUFFER_BIT
);
118 glVertex2f(xpos
, ypos
+ y
);
119 glVertex2f(xpos
+ len
, ypos
+ y
);
122 glReadPixels(xpos
+ len
/ 2, ypos
, 1, 1, GL_RGBA
, GL_FLOAT
, rgba
);
123 if (rgba
[0][0] == 1.0) {
132 printf("H-line at Y=%2d drawn for y in [%6.3f, %6.3f]\n",
134 ypos
+ ymin
, ypos
+ ymax
);
136 if (ymax
- ymin
!= 1.0 - Step
) {
137 printf(" => Inconsistant Y-axis rasterization!\n");
141 printf(" => Lines should be Y biased by about %f\n", ymin
- 0.5);
145 printf(" => Lines should be Y biased by about %f\n", 0.5 - ymax
);
149 * Second, check endpoints (for Y at 1/2 pixel)
151 for (x
= -1.0; x
<= 1.0; x
+= Step
) {
152 glClear(GL_COLOR_BUFFER_BIT
);
154 glVertex2f(xpos
+ x
, ypos
+ 0.5f
);
155 glVertex2f(xpos
+ x
+ len
, ypos
+ 0.5f
);
159 glReadPixels(xpos
- 1, ypos
, 2, 1, GL_RGBA
, GL_FLOAT
, rgba
);
160 if (rgba
[0][0] == 0.0 && rgba
[1][0] == 1.0) {
169 glReadPixels(xpos
+ len
- 1, ypos
, 2, 1, GL_RGBA
, GL_FLOAT
, rgba
);
170 if (rgba
[0][0] == 1.0 && rgba
[1][0] == 0.0) {
179 printf("H-line [%d..%d) hit left end for x in [%6.3f, %6.3f] "
180 "hit right end for x in [%6.3f, %6.3f]\n",
182 xpos
+ xmin_left
, xpos
+ xmax_left
,
183 xpos
+ len
+ xmin_right
, xpos
+ len
+ xmax_right
);
185 if (xmax_left
- xmin_left
> 1.0 - Step
) {
186 printf(" => Inconsistant left-end rasterization!\n");
188 if (xmax_right
- xmin_right
> 1.0 - Step
) {
189 printf(" => Inconsistant right-end rasterization!\n");
192 if (xmin_left
!= xmin_right
||
193 xmax_left
!= xmax_right
) {
194 printf(" => Inconsistant length!\n");
197 if (xmin_left
< 0.0) {
198 printf(" => Coords should be X biased by about %f\n", xmin_left
);
200 if (xmin_right
< 0.0) {
201 printf(" => Coords should be X biased by about %f\n", xmin_right
);
203 if (xmax_left
>= 1.0) {
204 printf(" => Coords should be X biased by about %f\n", -xmax_right
+ 1.0);
206 if (xmax_right
>= 1.0) {
207 printf(" => Coords should be X biased by about %f\n", -xmax_right
+ 1.0);
214 QuadCalibrate(int xpos
, int ypos
, int width
, int height
)
219 float xmin
, ymin
, xmax
, ymax
;
221 xmin
= ymin
= 1000.0;
222 xmax
= ymax
= -1000.0;
224 for (y
= -1.0; y
<= 1.0; y
+= Step
) {
225 for (x
= -1.0; x
<= 1.0; x
+= Step
) {
226 glClear(GL_COLOR_BUFFER_BIT
);
228 glVertex2f(xpos
+ x
, ypos
+ y
);
229 glVertex2f(xpos
+ x
+ width
, ypos
+ y
);
230 glVertex2f(xpos
+ x
+ width
, ypos
+ y
+ height
);
231 glVertex2f(xpos
+ x
, ypos
+ y
+ height
);
234 /* horizontal measurement */
235 glReadPixels(xpos
- 1, ypos
+ 2, 2, 1, GL_RGBA
, GL_FLOAT
, rgba1
);
236 glReadPixels(xpos
+ width
- 1, ypos
+ 2, 2, 1, GL_RGBA
, GL_FLOAT
, rgba2
);
237 if (rgba1
[0][0] == 0.0 && rgba1
[1][0] == 1.0 &&
238 rgba2
[0][0] == 1.0 && rgba2
[1][0] == 0.0) {
245 /* vertical measurement */
246 glReadPixels(xpos
+ 2, ypos
- 1, 1, 2, GL_RGBA
, GL_FLOAT
, rgba1
);
247 glReadPixels(xpos
+ 2, ypos
+ height
- 1, 1, 2, GL_RGBA
, GL_FLOAT
, rgba2
);
248 if (rgba1
[0][0] == 0.0 && rgba1
[1][0] == 1.0 &&
249 rgba2
[0][0] == 1.0 && rgba2
[1][0] == 0.0) {
258 printf("Quad at (%2d, %2d)..(%2d, %2d) drawn"
259 " for x in [%6.3f, %6.3f] and y in [%6.3f, %6.3f]\n",
261 xpos
+ width
, ypos
+ height
,
262 xpos
+ xmin
, xpos
+ xmax
,
263 ypos
+ ymin
, ypos
+ ymax
);
265 if (xmax
- xmin
!= 1.0 - Step
) {
266 printf(" => Inconsistant X-axis rasterization/size!\n");
268 if (ymax
- ymin
!= 1.0 - Step
) {
269 printf(" => Inconsistant Y-axis rasterization/size!\n");
273 printf(" => Coords should be X biased by about %f\n", 0.5 + xmin
);
276 printf(" => Coords should be Y biased by about %f\n", 0.5 + ymin
);
279 printf(" => Coords should be X biased by about %f\n", -xmax
+ 0.5);
282 printf(" => Coords should be Y biased by about %f\n", -ymax
+ 0.5);
288 * Misc/disabled code for debugging.
293 glClear(GL_COLOR_BUFFER_BIT
);
295 glBlendFunc(GL_ONE
, GL_ONE
);
297 glColor3f(.5, .5, .5);
300 glVertex2f(30, 35.5);
301 glVertex2f(54, 35.5);
302 glVertex2f(54, 35.5);
303 glVertex2f(66, 35.5);
314 glClear(GL_COLOR_BUFFER_BIT
);
317 glTranslatef(Xtrans
, Ytrans
, 0);
319 PointCalibrate(1, 1);
320 PointCalibrate(50, 50);
321 PointCalibrate(28, 17);
322 PointCalibrate(17, 18);
325 HLineCalibrate(5, 10, 10);
326 HLineCalibrate(25, 22, 12);
327 HLineCalibrate(54, 33, 12);
328 HLineCalibrate(54+12, 33, 12);
331 QuadCalibrate(2, 2, 10, 10);
332 QuadCalibrate(50, 50, 10, 10);
333 QuadCalibrate(28, 17, 12, 12);
334 QuadCalibrate(17, 28, 12, 12);
345 Reshape(int width
, int height
)
349 glViewport(0, 0, width
, height
);
350 glMatrixMode(GL_PROJECTION
);
352 glOrtho(0, width
, 0, height
, -1, 1);
353 glMatrixMode(GL_MODELVIEW
);
359 Key(unsigned char key
, int x
, int y
)
365 glutDestroyWindow(Win
);
376 printf("Measurement/callibration for basic prim rasterization...\n");
377 printf("GL_RENDERER: %s\n", (char*) glGetString(GL_RENDERER
));
382 main(int argc
, char *argv
[])
384 glutInit(&argc
, argv
);
385 glutInitWindowPosition(0, 0);
386 glutInitWindowSize(Width
, Height
);
387 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
);
388 Win
= glutCreateWindow(argv
[0]);
389 glutReshapeFunc(Reshape
);
390 glutKeyboardFunc(Key
);
391 glutDisplayFunc(Draw
);