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...
23 #include "glut_wrap.h"
26 static int Width
= 100, Height
= 100;
28 static float Step
= 0.125;
30 /* This tiny offset fixes errors in Mesa/Xlib */
31 static float Xtrans
= 0.5 * 0.125;
32 static float Ytrans
= 0.5 * 0.125;
34 static float Xtrans
= 0.0;
35 static float Ytrans
= 0.0;
40 PointCalibrate(int xpos
, int ypos
)
44 float xmin
, ymin
, xmax
, ymax
;
47 xmax
= ymax
= -1000.0;
49 for (y
= -1.0; y
<= 1.0; y
+= Step
) {
50 for (x
= -1.0; x
<= 1.0; x
+= Step
) {
51 glClear(GL_COLOR_BUFFER_BIT
);
53 glVertex2f(xpos
+ x
, ypos
+ y
);
55 glReadPixels(xpos
, ypos
, 1, 1, GL_RGBA
, GL_FLOAT
, rgba
);
56 if (rgba
[0] == 1.0 && rgba
[1] == 1.0 && rgba
[2] == 1.0) {
70 printf("Point at (%2d, %2d) drawn for x in [%6.3f, %6.3f] and y in [%6.3f, %6.3f]\n",
72 xpos
+ xmin
, xpos
+ xmax
,
73 ypos
+ ymin
, ypos
+ ymax
);
75 if (xmax
- xmin
!= 1.0 - Step
) {
76 printf(" => Inconsistant X-axis rasterization!\n");
78 if (ymax
- ymin
!= 1.0 - Step
) {
79 printf(" => Inconsistant Y-axis rasterization!\n");
82 printf(" => Points should be X biased by about %f\n", xmin
);
85 printf(" => Points should be Y biased by about %f\n", ymin
);
88 printf(" => Points should be X biased by about %f\n", 1.0 - xmax
);
91 printf(" => Points should be Y biased by about %f\n", 1.0 - ymax
);
98 * XXX Implement VLineCalibrate() someday
101 HLineCalibrate(int xpos
, int ypos
, int len
)
106 float xmin_left
, xmax_left
, xmin_right
, xmax_right
;
108 xmin_left
= xmin_right
= 1000.0;
109 xmax_left
= xmax_right
= -1000.0;
114 * First, check vertical positioning of the horizontal line
116 for (y
= -1.0; y
<= 1.0; y
+= Step
) {
117 glClear(GL_COLOR_BUFFER_BIT
);
119 glVertex2f(xpos
, ypos
+ y
);
120 glVertex2f(xpos
+ len
, ypos
+ y
);
123 glReadPixels(xpos
+ len
/ 2, ypos
, 1, 1, GL_RGBA
, GL_FLOAT
, rgba
);
124 if (rgba
[0][0] == 1.0) {
133 printf("H-line at Y=%2d drawn for y in [%6.3f, %6.3f]\n",
135 ypos
+ ymin
, ypos
+ ymax
);
137 if (ymax
- ymin
!= 1.0 - Step
) {
138 printf(" => Inconsistant Y-axis rasterization!\n");
142 printf(" => Lines should be Y biased by about %f\n", ymin
- 0.5);
146 printf(" => Lines should be Y biased by about %f\n", 0.5 - ymax
);
150 * Second, check endpoints (for Y at 1/2 pixel)
152 for (x
= -1.0; x
<= 1.0; x
+= Step
) {
153 glClear(GL_COLOR_BUFFER_BIT
);
155 glVertex2f(xpos
+ x
, ypos
+ 0.5f
);
156 glVertex2f(xpos
+ x
+ len
, ypos
+ 0.5f
);
160 glReadPixels(xpos
- 1, ypos
, 2, 1, GL_RGBA
, GL_FLOAT
, rgba
);
161 if (rgba
[0][0] == 0.0 && rgba
[1][0] == 1.0) {
170 glReadPixels(xpos
+ len
- 1, ypos
, 2, 1, GL_RGBA
, GL_FLOAT
, rgba
);
171 if (rgba
[0][0] == 1.0 && rgba
[1][0] == 0.0) {
180 printf("H-line [%d..%d) hit left end for x in [%6.3f, %6.3f] "
181 "hit right end for x in [%6.3f, %6.3f]\n",
183 xpos
+ xmin_left
, xpos
+ xmax_left
,
184 xpos
+ len
+ xmin_right
, xpos
+ len
+ xmax_right
);
186 if (xmax_left
- xmin_left
> 1.0 - Step
) {
187 printf(" => Inconsistant left-end rasterization!\n");
189 if (xmax_right
- xmin_right
> 1.0 - Step
) {
190 printf(" => Inconsistant right-end rasterization!\n");
193 if (xmin_left
!= xmin_right
||
194 xmax_left
!= xmax_right
) {
195 printf(" => Inconsistant length!\n");
198 if (xmin_left
< 0.0) {
199 printf(" => Coords should be X biased by about %f\n", xmin_left
);
201 if (xmin_right
< 0.0) {
202 printf(" => Coords should be X biased by about %f\n", xmin_right
);
204 if (xmax_left
>= 1.0) {
205 printf(" => Coords should be X biased by about %f\n", -xmax_right
+ 1.0);
207 if (xmax_right
>= 1.0) {
208 printf(" => Coords should be X biased by about %f\n", -xmax_right
+ 1.0);
215 QuadCalibrate(int xpos
, int ypos
, int width
, int height
)
220 float xmin
, ymin
, xmax
, ymax
;
222 xmin
= ymin
= 1000.0;
223 xmax
= ymax
= -1000.0;
225 for (y
= -1.0; y
<= 1.0; y
+= Step
) {
226 for (x
= -1.0; x
<= 1.0; x
+= Step
) {
227 glClear(GL_COLOR_BUFFER_BIT
);
229 glVertex2f(xpos
+ x
, ypos
+ y
);
230 glVertex2f(xpos
+ x
+ width
, ypos
+ y
);
231 glVertex2f(xpos
+ x
+ width
, ypos
+ y
+ height
);
232 glVertex2f(xpos
+ x
, ypos
+ y
+ height
);
235 /* horizontal measurement */
236 glReadPixels(xpos
- 1, ypos
+ 2, 2, 1, GL_RGBA
, GL_FLOAT
, rgba1
);
237 glReadPixels(xpos
+ width
- 1, ypos
+ 2, 2, 1, GL_RGBA
, GL_FLOAT
, rgba2
);
238 if (rgba1
[0][0] == 0.0 && rgba1
[1][0] == 1.0 &&
239 rgba2
[0][0] == 1.0 && rgba2
[1][0] == 0.0) {
246 /* vertical measurement */
247 glReadPixels(xpos
+ 2, ypos
- 1, 1, 2, GL_RGBA
, GL_FLOAT
, rgba1
);
248 glReadPixels(xpos
+ 2, ypos
+ height
- 1, 1, 2, GL_RGBA
, GL_FLOAT
, rgba2
);
249 if (rgba1
[0][0] == 0.0 && rgba1
[1][0] == 1.0 &&
250 rgba2
[0][0] == 1.0 && rgba2
[1][0] == 0.0) {
259 printf("Quad at (%2d, %2d)..(%2d, %2d) drawn"
260 " for x in [%6.3f, %6.3f] and y in [%6.3f, %6.3f]\n",
262 xpos
+ width
, ypos
+ height
,
263 xpos
+ xmin
, xpos
+ xmax
,
264 ypos
+ ymin
, ypos
+ ymax
);
266 if (xmax
- xmin
!= 1.0 - Step
) {
267 printf(" => Inconsistant X-axis rasterization/size!\n");
269 if (ymax
- ymin
!= 1.0 - Step
) {
270 printf(" => Inconsistant Y-axis rasterization/size!\n");
274 printf(" => Coords should be X biased by about %f\n", 0.5 + xmin
);
277 printf(" => Coords should be Y biased by about %f\n", 0.5 + ymin
);
280 printf(" => Coords should be X biased by about %f\n", -xmax
+ 0.5);
283 printf(" => Coords should be Y biased by about %f\n", -ymax
+ 0.5);
289 * Misc/disabled code for debugging.
294 glClear(GL_COLOR_BUFFER_BIT
);
296 glBlendFunc(GL_ONE
, GL_ONE
);
298 glColor3f(.5, .5, .5);
301 glVertex2f(30, 35.5);
302 glVertex2f(54, 35.5);
303 glVertex2f(54, 35.5);
304 glVertex2f(66, 35.5);
315 glClear(GL_COLOR_BUFFER_BIT
);
318 glTranslatef(Xtrans
, Ytrans
, 0);
320 PointCalibrate(1, 1);
321 PointCalibrate(50, 50);
322 PointCalibrate(28, 17);
323 PointCalibrate(17, 18);
326 HLineCalibrate(5, 10, 10);
327 HLineCalibrate(25, 22, 12);
328 HLineCalibrate(54, 33, 12);
329 HLineCalibrate(54+12, 33, 12);
332 QuadCalibrate(2, 2, 10, 10);
333 QuadCalibrate(50, 50, 10, 10);
334 QuadCalibrate(28, 17, 12, 12);
335 QuadCalibrate(17, 28, 12, 12);
346 Reshape(int width
, int height
)
350 glViewport(0, 0, width
, height
);
351 glMatrixMode(GL_PROJECTION
);
353 glOrtho(0, width
, 0, height
, -1, 1);
354 glMatrixMode(GL_MODELVIEW
);
360 Key(unsigned char key
, int x
, int y
)
366 glutDestroyWindow(Win
);
377 printf("Measurement/callibration for basic prim rasterization...\n");
378 printf("GL_RENDERER: %s\n", (char*) glGetString(GL_RENDERER
));
383 main(int argc
, char *argv
[])
385 glutInit(&argc
, argv
);
386 glutInitWindowPosition(0, 0);
387 glutInitWindowSize(Width
, Height
);
388 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
);
389 Win
= glutCreateWindow(argv
[0]);
391 glutReshapeFunc(Reshape
);
392 glutKeyboardFunc(Key
);
393 glutDisplayFunc(Draw
);