Add more structure constructor tests.
[piglit/hramrach.git] / tests / general / texgen.c
blobcbe9907bfd35e2ffac61eb831269e1037ed2af34
1 /*
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
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 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.
24 /**
25 * @file
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;
36 /**
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
39 * in in @ref Init.
41 static GLfloat TextureData[4][4][3];
43 /**
44 * Implement the inner part of the above texture in a fragment program.
46 static const char TextureFP[] =
47 "!!ARBfp1.0\n"
48 "TEMP r0;\n"
49 "MUL r0, fragment.texcoord, 4;\n"
50 "FLR r0, r0;\n"
51 "MUL result.color, r0, 0.25;\n"
52 "END\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);
58 if (Automatic)
59 piglit_report_result(PIGLIT_FAILURE);
63 /**
64 * Sanity test whether the texture is rendered correctly at all.
66 static void test_sanity(void)
68 int x, y;
70 glClearColor(0.5, 0.5, 0.5, 1.0);
71 glClear(GL_COLOR_BUFFER_BIT);
73 glBegin(GL_QUADS);
74 glTexCoord2f(0, 0);
75 glVertex2f(0, 0);
76 glTexCoord2f(1, 0);
77 glVertex2f(1, 0);
78 glTexCoord2f(1, 1);
79 glVertex2f(1, 1);
80 glTexCoord2f(0, 1);
81 glVertex2f(0, 1);
82 glEnd();
84 glutSwapBuffers();
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 };
98 int x, y;
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
109 glBegin(GL_QUADS);
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);
114 glEnd();
116 // Draw lower right quad
117 glTexGenfv(GL_S, GL_EYE_PLANE, sPlane2);
118 glPushMatrix();
119 glTranslatef(0.5, -0.5, 0.0);
120 glScalef(2.0, 1.0, 1.0);
121 glBegin(GL_QUADS);
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);
126 glEnd();
127 glPopMatrix();
129 // Draw upper left quad
130 glPushMatrix();
131 glTranslatef(1.0, 0.5, 0.0);
132 glScalef(-1.0, 1.0, 1.0);
133 glTexGenfv(GL_S, GL_EYE_PLANE, sPlane3);
134 glBegin(GL_QUADS);
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);
139 glEnd();
140 glPopMatrix();
142 glDisable(GL_TEXTURE_GEN_S);
144 glutSwapBuffers();
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);
167 static struct {
168 const char* name;
169 void (*function)(void);
170 } Tests[] = {
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)
179 if (Automatic) {
180 int i;
181 for(i = 0; i < NrTests; ++i)
182 Tests[i].function();
184 piglit_report_result(PIGLIT_SUCCESS);
185 } else {
186 Tests[CurrentTest].function();
191 static void Reshape(int width, int height)
193 Width = width;
194 Height = height;
195 glViewport(0, 0, width, height);
196 glMatrixMode(GL_PROJECTION);
197 glLoadIdentity();
198 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
199 glMatrixMode(GL_MODELVIEW);
200 glLoadIdentity();
204 static void Init(void)
206 int x, y;
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)
234 (void) x;
235 (void) y;
236 switch (key) {
237 case 't':
238 CurrentTest++;
239 if (CurrentTest >= NrTests)
240 CurrentTest = 0;
241 printf("Test: %s\n", Tests[CurrentTest].name);
242 break;
243 case 27:
244 exit(0);
245 break;
247 glutPostRedisplay();
250 int main(int argc, char *argv[])
252 glutInit(&argc, argv);
253 if (argc == 2 && !strcmp(argv[1], "-auto"))
254 Automatic = 1;
255 glutInitWindowPosition(0, 0);
256 glutInitWindowSize(Width, Height);
257 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
258 glutCreateWindow(argv[0]);
259 glutReshapeFunc(Reshape);
260 glutDisplayFunc(Redisplay);
261 if (!Automatic) {
262 printf("Press 't' to switch tests; Escape to quit\n");
263 glutKeyboardFunc(Key);
265 Init();
266 glutMainLoop();
267 return 0;