2 * Copyright (c) The Piglit project 2008
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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
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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * Test a number of basic TexGen functions.
29 #include "piglit-util.h"
31 static int Width
= 128, Height
= 128;
32 static int Automatic
= 0;
33 static int CurrentTest
= 0;
34 static int UseFragmentProgram
= 0;
37 * The test uses a 4x4 clamped, nearest-filtered texture with the following
38 * RGB colors. The pattern matches what @ref TextureFP produces and is filled
41 static GLfloat TextureData
[4][4][3];
44 * Implement the inner part of the above texture in a fragment program.
46 static const char TextureFP
[] =
49 "MUL r0, fragment.texcoord, 4;\n"
51 "MUL result.color, r0, 0.25;\n"
54 static void probe_cell(const char* testname
, int x
, int y
, const float* expected
)
56 if (!piglit_probe_pixel_rgb((2*x
+1)*Width
/8, (2*y
+1)*Height
/8, expected
)) {
57 fprintf(stderr
, "%s: %i,%i failed\n", testname
, x
, y
);
59 piglit_report_result(PIGLIT_FAILURE
);
64 * Sanity test whether the texture is rendered correctly at all.
66 static void test_sanity(void)
70 glClearColor(0.5, 0.5, 0.5, 1.0);
71 glClear(GL_COLOR_BUFFER_BIT
);
85 glReadBuffer(GL_FRONT
);
87 for(y
= 0; y
< 4; ++y
) {
88 for(x
= 0; x
< 4; ++x
)
89 probe_cell("test_sanity", x
, y
, TextureData
[y
][x
]);
93 static void do_test_texgen_eye(const char* testname
)
95 static GLfloat sPlane1
[4] = { 1.0, 0.0, 0.0, 0.25 };
96 static GLfloat sPlane2
[4] = { 1.0, 0.0, 0.0, -0.25 };
97 static GLfloat sPlane3
[4] = { -1.0, 0.0, 0.0, 1.25 };
100 glClearColor(0.5, 0.5, 0.5, 1.0);
101 glClear(GL_COLOR_BUFFER_BIT
);
103 // Note: Modelview matrix is identity
104 glTexGeni(GL_S
, GL_TEXTURE_GEN_MODE
, GL_EYE_LINEAR
);
105 glTexGenfv(GL_S
, GL_EYE_PLANE
, sPlane1
);
106 glEnable(GL_TEXTURE_GEN_S
);
108 // Draw lower left quad
110 glTexCoord2f(0, 0.25); glVertex2f(0.0, 0.0);
111 glTexCoord2f(0, 0.25); glVertex2f(0.5, 0.0);
112 glTexCoord2f(0, 0.75); glVertex2f(0.5, 0.5);
113 glTexCoord2f(0, 0.75); glVertex2f(0.0, 0.5);
116 // Draw lower right quad
117 glTexGenfv(GL_S
, GL_EYE_PLANE
, sPlane2
);
119 glTranslatef(0.5, -0.5, 0.0);
120 glScalef(2.0, 1.0, 1.0);
122 glTexCoord2f(0, 0.25); glVertex2f(0.0, 0.5);
123 glTexCoord2f(0, 0.25); glVertex2f(0.25, 0.5);
124 glTexCoord2f(0, 0.75); glVertex2f(0.25, 1.0);
125 glTexCoord2f(0, 0.75); glVertex2f(0.0, 1.0);
129 // Draw upper left quad
131 glTranslatef(1.0, 0.5, 0.0);
132 glScalef(-1.0, 1.0, 1.0);
133 glTexGenfv(GL_S
, GL_EYE_PLANE
, sPlane3
);
135 glTexCoord2f(0, 0.25); glVertex2f(1.0, 0.0);
136 glTexCoord2f(0, 0.25); glVertex2f(0.5, 0.0);
137 glTexCoord2f(0, 0.75); glVertex2f(0.5, 0.5);
138 glTexCoord2f(0, 0.75); glVertex2f(1.0, 0.5);
142 glDisable(GL_TEXTURE_GEN_S
);
145 glReadBuffer(GL_FRONT
);
147 for(y
= 0; y
< 2; ++y
) {
148 for(x
= 0; x
< 2; ++x
)
149 probe_cell(testname
, x
, y
, TextureData
[y
+1][x
+1]);
153 static void test_texgen_eye(void)
155 do_test_texgen_eye("test_texgen_eye");
158 static void test_texgen_eye_fp(void)
160 if (UseFragmentProgram
) {
161 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
162 do_test_texgen_eye("test_texgen_eye_fp");
163 glDisable(GL_FRAGMENT_PROGRAM_ARB
);
169 void (*function
)(void);
171 { "sanity", &test_sanity
},
172 { "texgen_eye", &test_texgen_eye
},
173 { "texgen_eye_fp", &test_texgen_eye_fp
}
175 #define NrTests (ARRAY_SIZE(Tests))
177 static void Redisplay(void)
181 for(i
= 0; i
< NrTests
; ++i
)
184 piglit_report_result(PIGLIT_SUCCESS
);
186 Tests
[CurrentTest
].function();
191 static void Reshape(int width
, int height
)
195 glViewport(0, 0, width
, height
);
196 glMatrixMode(GL_PROJECTION
);
198 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
199 glMatrixMode(GL_MODELVIEW
);
204 static void Init(void)
208 if (piglit_use_fragment_program()) {
209 UseFragmentProgram
= 1;
210 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
,
211 piglit_compile_program(GL_FRAGMENT_PROGRAM_ARB
, TextureFP
));
214 for(y
= 0; y
< 4; ++y
) {
215 for(x
= 0; x
< 4; ++x
) {
216 TextureData
[y
][x
][0] = x
* 0.25;
217 TextureData
[y
][x
][1] = y
* 0.25;
218 TextureData
[y
][x
][2] = 0.0;
222 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
223 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
224 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP
);
225 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP
);
226 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, 4, 4, 0, GL_RGB
, GL_FLOAT
, TextureData
);
227 glEnable(GL_TEXTURE_2D
);
229 Reshape(Width
,Height
);
232 static void Key(unsigned char key
, int x
, int y
)
239 if (CurrentTest
>= NrTests
)
241 printf("Test: %s\n", Tests
[CurrentTest
].name
);
250 int main(int argc
, char *argv
[])
252 glutInit(&argc
, argv
);
253 if (argc
== 2 && !strcmp(argv
[1], "-auto"))
255 glutInitWindowPosition(0, 0);
256 glutInitWindowSize(Width
, Height
);
257 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
);
258 glutCreateWindow(argv
[0]);
259 glutReshapeFunc(Reshape
);
260 glutDisplayFunc(Redisplay
);
262 printf("Press 't' to switch tests; Escape to quit\n");
263 glutKeyboardFunc(Key
);