Add more structure constructor tests.
[piglit/hramrach.git] / tests / glean / tpointatten.cpp
blob1fce09cbb642f053f77f9954289cff6684d80dcb
1 // BEGIN_COPYRIGHT -*- glean -*-
2 //
3 // Copyright (C) 1999 Allen Akin All Rights Reserved.
4 //
5 // Permission is hereby granted, free of charge, to any person
6 // obtaining a copy of this software and associated documentation
7 // files (the "Software"), to deal in the Software without
8 // restriction, including without limitation the rights to use,
9 // copy, modify, merge, publish, distribute, sublicense, and/or
10 // sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following
12 // conditions:
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the
16 // Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
19 // KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20 // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
21 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ALLEN AKIN BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 // AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 // OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 // DEALINGS IN THE SOFTWARE.
27 // END_COPYRIGHT
29 // tpointatten.h: Test GL_ARB_point_parameters extension.
30 // Brian Paul 6 October 2005
33 #include "tpointatten.h"
34 #include <cassert>
35 #include <cmath>
38 namespace GLEAN {
40 // Max tested point size
41 #define MAX_SIZE 25.0
44 /* Clamp X to [MIN,MAX] */
45 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
48 static PFNGLPOINTPARAMETERFVARBPROC PointParameterfvARB = NULL;
49 static PFNGLPOINTPARAMETERFARBPROC PointParameterfARB = NULL;
52 void
53 PointAttenuationTest::setup(void)
55 PointParameterfvARB = (PFNGLPOINTPARAMETERFVARBPROC)
56 GLUtils::getProcAddress("glPointParameterfvARB");
57 assert(PointParameterfvARB);
58 PointParameterfARB = (PFNGLPOINTPARAMETERFARBPROC)
59 GLUtils::getProcAddress("glPointParameterfARB");
60 assert(PointParameterfARB);
62 glGetFloatv(GL_ALIASED_POINT_SIZE_RANGE, aliasedLimits);
63 glGetFloatv(GL_SMOOTH_POINT_SIZE_RANGE, smoothLimits);
64 glMatrixMode(GL_PROJECTION);
65 glLoadIdentity();
66 glOrtho(-10.0, 10.0, -10.0, 10.0, -10.0, 10.0);
67 glMatrixMode(GL_MODELVIEW);
68 glLoadIdentity();
72 void
73 PointAttenuationTest::reportFailure(GLfloat initSize,
74 const GLfloat attenuation[3],
75 GLfloat min, GLfloat max,
76 GLfloat eyeZ, GLboolean smooth,
77 GLfloat expected, GLfloat actual) const
79 env->log << "\tFAILURE:\n";
80 env->log << "\tExpected size: " << expected << " Actual size: " << actual << "\n";
81 env->log << "\tSize: " << initSize << "\n";
82 env->log << "\tMin: " << min << " Max: " << max << "\n";
83 env->log << "\tAttenuation: " << attenuation[0] << " " << attenuation[1] << " " << attenuation[2] << "\n";
84 env->log << "\tEye Z: " << eyeZ << "\n";
85 if (smooth)
86 env->log << "\tSmooth/antialiased\n";
87 else
88 env->log << "\tAliased\n";
92 void
93 PointAttenuationTest::reportSuccess(int count, GLboolean smooth) const
95 env->log << "PASS: " << count;
96 if (smooth)
97 env->log << " aliased combinations tested.\n";
98 else
99 env->log << " antialiased combinations tested.\n";
103 // Compute the expected point size given various point state
104 GLfloat
105 PointAttenuationTest::expectedSize(GLfloat initSize,
106 const GLfloat attenuation[3],
107 GLfloat min, GLfloat max,
108 GLfloat eyeZ, GLboolean smooth) const
110 const GLfloat dist = fabs(eyeZ);
111 const GLfloat atten = sqrt(1.0 / (attenuation[0] +
112 attenuation[1] * dist +
113 attenuation[2] * dist * dist));
115 float size = initSize * atten;
117 size = CLAMP(size, min, max);
119 if (smooth)
120 size = CLAMP(size, smoothLimits[0], smoothLimits[1]);
121 else
122 size = CLAMP(size, aliasedLimits[0], aliasedLimits[1]);
123 return size;
127 // measure size of rendered point at yPos (in model coords)
128 GLfloat
129 PointAttenuationTest::measureSize(GLfloat yPos) const
131 assert(yPos >= -10.0);
132 assert(yPos <= 10.0);
133 float yNdc = (yPos + 10.0) / 20.0; // See glOrtho above
134 int x = 0;
135 int y = (int) (yNdc * windowHeight);
136 int w = windowWidth;
137 int h = 1;
138 GLfloat image[windowWidth * 3];
139 // Read row of pixels and add up colors, which should be white
140 // or shades of gray if smoothing is enabled.
141 glReadPixels(x, y, w, h, GL_RGB, GL_FLOAT, image);
142 float sum = 0.0;
143 for (int i = 0; i < w; i++) {
144 sum += (image[i*3+0] + image[i*3+1] + image[i*3+2]) / 3.0;
146 return sum;
150 bool
151 PointAttenuationTest::testPointRendering(GLboolean smooth)
153 // epsilon is the allowed size difference in pixels between the
154 // expected and actual rendering.
155 const GLfloat epsilon = smooth ? 1.5 : 1.0;
156 GLfloat atten[3];
157 int count = 0;
159 // Enable front buffer if you want to see the rendering
160 //glDrawBuffer(GL_FRONT);
161 //glReadBuffer(GL_FRONT);
163 if (smooth) {
164 glEnable(GL_POINT_SMOOTH);
165 glEnable(GL_BLEND);
166 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
168 else {
169 glDisable(GL_POINT_SMOOTH);
170 glDisable(GL_BLEND);
173 for (int a = 0; a < 3; a++) {
174 atten[0] = pow(10.0, -a);
175 for (int b = -2; b < 3; b++) {
176 atten[1] = (b == -1) ? 0.0 : pow(10.0, -b);
177 for (int c = -2; c < 3; c++) {
178 atten[2] = (c == -1) ? 0.0 : pow(10.0, -c);
179 PointParameterfvARB(GL_POINT_DISTANCE_ATTENUATION_ARB, atten);
180 for (float min = 1.0; min < MAX_SIZE; min += 5) {
181 PointParameterfARB(GL_POINT_SIZE_MIN_ARB, min);
182 for (float max = min; max < MAX_SIZE; max += 5) {
183 PointParameterfARB(GL_POINT_SIZE_MAX_ARB, max);
184 for (float size = 1.0; size < MAX_SIZE; size += 4) {
185 glPointSize(size);
187 // draw column of points
188 glClear(GL_COLOR_BUFFER_BIT);
189 glBegin(GL_POINTS);
190 for (float z = -6.0; z <= 6.0; z += 1.0) {
191 glVertex3f(0, z, z);
193 glEnd();
195 // test the column of points
196 for (float z = -6.0; z <= 6.0; z += 1.0) {
197 count++;
198 float expected
199 = expectedSize(size, atten, min, max,
200 z, smooth);
201 float actual = measureSize(z);
202 if (fabs(expected - actual) > epsilon) {
203 reportFailure(size, atten, min, max,
204 z, smooth,
205 expected, actual);
206 return false;
215 reportSuccess(count, smooth);
216 return true;
219 void
220 PointAttenuationTest::runOne(BasicResult &r, Window &w)
222 (void) w; // silence warning
223 r.pass = true;
224 errorCode = 0;
225 errorPos = NULL;
227 setup();
229 if (r.pass)
230 r.pass = testPointRendering(GL_FALSE);
231 if (r.pass)
232 r.pass = testPointRendering(GL_TRUE);
236 void
237 PointAttenuationTest::logOne(BasicResult &r)
239 if (r.pass) {
240 logPassFail(r);
241 logConcise(r);
246 // constructor
247 PointAttenuationTest::PointAttenuationTest(const char *testName,
248 const char *filter,
249 const char *extensions,
250 const char *description)
251 : BasicTest(testName, filter, extensions, description)
253 fWidth = windowWidth;
254 fHeight = windowHeight;
259 // The test object itself:
260 PointAttenuationTest pointAttenuationTest("pointAtten", "window, rgb",
261 "GL_ARB_point_parameters",
262 "Test point size attenuation with the GL_ARB_point_parameters extension.\n");
266 } // namespace GLEAN