texgen.c 6.44 KB
Newer Older
Nicolai Hähnle's avatar
Nicolai Hähnle committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * Copyright (c) The Piglit project 2008
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
 * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * @file
 * Test a number of basic TexGen functions.
 */

29
#include "piglit-util-gl-common.h"
Nicolai Hähnle's avatar
Nicolai Hähnle committed
30

31 32 33 34
PIGLIT_GL_TEST_MAIN(
    128 /*window_width*/,
    128 /*window_height*/,
    GLUT_RGB | GLUT_DOUBLE)
35

Nicolai Hähnle's avatar
Nicolai Hähnle committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
static int CurrentTest = 0;
static int UseFragmentProgram = 0;

/**
 * The test uses a 4x4 clamped, nearest-filtered texture with the following
 * RGB colors. The pattern matches what @ref TextureFP produces and is filled
 * in in @ref Init.
 */
static GLfloat TextureData[4][4][3];

/**
 * Implement the inner part of the above texture in a fragment program.
 */
static const char TextureFP[] =
"!!ARBfp1.0\n"
"TEMP r0;\n"
"MUL r0, fragment.texcoord, 4;\n"
"FLR r0, r0;\n"
"MUL result.color, r0, 0.25;\n"
"END\n";

static void probe_cell(const char* testname, int x, int y, const float* expected)
{
59
	if (!piglit_probe_pixel_rgb((2*x+1)*piglit_width/8, (2*y+1)*piglit_height/8, expected)) {
Nicolai Hähnle's avatar
Nicolai Hähnle committed
60
		fprintf(stderr, "%s: %i,%i failed\n", testname, x, y);
61
		if (piglit_automatic)
62
			piglit_report_result(PIGLIT_FAIL);
Nicolai Hähnle's avatar
Nicolai Hähnle committed
63 64 65 66 67 68
	}
}

/**
 * Sanity test whether the texture is rendered correctly at all.
 */
Vinson Lee's avatar
Vinson Lee committed
69
static void test_sanity(void)
Nicolai Hähnle's avatar
Nicolai Hähnle committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
{
	int x, y;

	glClearColor(0.5, 0.5, 0.5, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);

	glBegin(GL_QUADS);
		glTexCoord2f(0, 0);
		glVertex2f(0, 0);
		glTexCoord2f(1, 0);
		glVertex2f(1, 0);
		glTexCoord2f(1, 1);
		glVertex2f(1, 1);
		glTexCoord2f(0, 1);
		glVertex2f(0, 1);
	glEnd();

	for(y = 0; y < 4; ++y) {
		for(x = 0; x < 4; ++x)
			probe_cell("test_sanity", x, y, TextureData[y][x]);
	}
91

92
	piglit_present_results();
Nicolai Hähnle's avatar
Nicolai Hähnle committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
}

static void do_test_texgen_eye(const char* testname)
{
	static GLfloat sPlane1[4] = { 1.0, 0.0, 0.0, 0.25 };
	static GLfloat sPlane2[4] = { 1.0, 0.0, 0.0, -0.25 };
	static GLfloat sPlane3[4] = { -1.0, 0.0, 0.0, 1.25 };
	int x, y;

	glClearColor(0.5, 0.5, 0.5, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);

	// Note: Modelview matrix is identity
	glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
	glTexGenfv(GL_S, GL_EYE_PLANE, sPlane1);
	glEnable(GL_TEXTURE_GEN_S);

	// Draw lower left quad
	glBegin(GL_QUADS);
		glTexCoord2f(0, 0.25); glVertex2f(0.0, 0.0);
		glTexCoord2f(0, 0.25); glVertex2f(0.5, 0.0);
		glTexCoord2f(0, 0.75); glVertex2f(0.5, 0.5);
		glTexCoord2f(0, 0.75); glVertex2f(0.0, 0.5);
	glEnd();

	// Draw lower right quad
	glTexGenfv(GL_S, GL_EYE_PLANE, sPlane2);
	glPushMatrix();
	glTranslatef(0.5, -0.5, 0.0);
	glScalef(2.0, 1.0, 1.0);
	glBegin(GL_QUADS);
		glTexCoord2f(0, 0.25); glVertex2f(0.0,  0.5);
		glTexCoord2f(0, 0.25); glVertex2f(0.25, 0.5);
		glTexCoord2f(0, 0.75); glVertex2f(0.25, 1.0);
		glTexCoord2f(0, 0.75); glVertex2f(0.0,  1.0);
	glEnd();
	glPopMatrix();

	// Draw upper left quad
	glPushMatrix();
	glTranslatef(1.0, 0.5, 0.0);
	glScalef(-1.0, 1.0, 1.0);
	glTexGenfv(GL_S, GL_EYE_PLANE, sPlane3);
	glBegin(GL_QUADS);
		glTexCoord2f(0, 0.25); glVertex2f(1.0, 0.0);
		glTexCoord2f(0, 0.25); glVertex2f(0.5, 0.0);
		glTexCoord2f(0, 0.75); glVertex2f(0.5, 0.5);
		glTexCoord2f(0, 0.75); glVertex2f(1.0, 0.5);
	glEnd();
	glPopMatrix();

	glDisable(GL_TEXTURE_GEN_S);

	for(y = 0; y < 2; ++y) {
		for(x = 0; x < 2; ++x)
			probe_cell(testname, x, y, TextureData[y+1][x+1]);
	}
150

151
	piglit_present_results();
Nicolai Hähnle's avatar
Nicolai Hähnle committed
152 153
}

Vinson Lee's avatar
Vinson Lee committed
154
static void test_texgen_eye(void)
Nicolai Hähnle's avatar
Nicolai Hähnle committed
155 156 157 158
{
	do_test_texgen_eye("test_texgen_eye");
}

Vinson Lee's avatar
Vinson Lee committed
159
static void test_texgen_eye_fp(void)
Nicolai Hähnle's avatar
Nicolai Hähnle committed
160 161 162 163 164 165 166 167 168 169
{
	if (UseFragmentProgram) {
		glEnable(GL_FRAGMENT_PROGRAM_ARB);
		do_test_texgen_eye("test_texgen_eye_fp");
		glDisable(GL_FRAGMENT_PROGRAM_ARB);
	}
}

static struct {
	const char* name;
Vinson Lee's avatar
Vinson Lee committed
170
	void (*function)(void);
Nicolai Hähnle's avatar
Nicolai Hähnle committed
171 172 173 174 175 176 177
} Tests[] = {
	{ "sanity", &test_sanity },
	{ "texgen_eye", &test_texgen_eye },
	{ "texgen_eye_fp", &test_texgen_eye_fp }
};
#define NrTests (ARRAY_SIZE(Tests))

178 179
enum piglit_result
piglit_display(void)
Nicolai Hähnle's avatar
Nicolai Hähnle committed
180
{
181
	if (piglit_automatic) {
Nicolai Hähnle's avatar
Nicolai Hähnle committed
182 183 184
		int i;
		for(i = 0; i < NrTests; ++i)
			Tests[i].function();
185
                return PIGLIT_PASS;
Nicolai Hähnle's avatar
Nicolai Hähnle committed
186 187
	} else {
		Tests[CurrentTest].function();
188
                return PIGLIT_PASS;
Nicolai Hähnle's avatar
Nicolai Hähnle committed
189 190 191 192 193 194
	}
}


static void Reshape(int width, int height)
{
195 196 197
	piglit_width = width;
	piglit_height = height;

Nicolai Hähnle's avatar
Nicolai Hähnle committed
198 199 200 201 202 203 204 205
	glViewport(0, 0, width, height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
static void Key(unsigned char key, int x, int y)
{
	(void) x;
	(void) y;
	switch (key) {
	case 't':
		CurrentTest++;
		if (CurrentTest >= NrTests)
			CurrentTest = 0;
		printf("Test: %s\n", Tests[CurrentTest].name);
		break;
	case 27:
		exit(0);
		break;
	}
	glutPostRedisplay();
}
Nicolai Hähnle's avatar
Nicolai Hähnle committed
223

224
void piglit_init(int argc, char *argv[])
Nicolai Hähnle's avatar
Nicolai Hähnle committed
225 226 227
{
	int x, y;

228 229 230 231 232
	if (!piglit_automatic) {
		printf("Press 't' to switch tests; Escape to quit\n");
		glutKeyboardFunc(Key);
	}

Nicolai Hähnle's avatar
Nicolai Hähnle committed
233 234
	if (piglit_use_fragment_program()) {
		UseFragmentProgram = 1;
235
		glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB,
Nicolai Hähnle's avatar
Nicolai Hähnle committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
			piglit_compile_program(GL_FRAGMENT_PROGRAM_ARB, TextureFP));
	}

	for(y = 0; y < 4; ++y) {
		for(x = 0; x < 4; ++x) {
			TextureData[y][x][0] = x * 0.25;
			TextureData[y][x][1] = y * 0.25;
			TextureData[y][x][2] = 0.0;
		}
	}

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 4, 4, 0, GL_RGB, GL_FLOAT, TextureData);
	glEnable(GL_TEXTURE_2D);

	glutReshapeFunc(Reshape);
255
	Reshape(piglit_width, piglit_height);
Nicolai Hähnle's avatar
Nicolai Hähnle committed
256 257
}