progs/tests: also test stencil INCR_WRAP mode if supported
[mesa.git] / progs / tests / texwrap.c
1
2 /*
3 * Test texture wrap modes.
4 * Press 'b' to toggle texture image borders. You should see the same
5 * rendering whether or not you're using borders.
6 *
7 * Brian Paul March 2001
8 */
9
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <math.h>
14 #include <GL/glew.h>
15 #include <GL/glut.h>
16
17
18 #ifndef GL_CLAMP_TO_BORDER
19 #define GL_CLAMP_TO_BORDER 0x812D
20 #endif
21
22 #ifndef GL_MIRRORED_REPEAT
23 #define GL_MIRRORED_REPEAT 0x8370
24 #endif
25
26 #ifndef GL_EXT_texture_mirror_clamp
27 #define GL_MIRROR_CLAMP_EXT 0x8742
28 #define GL_MIRROR_CLAMP_TO_EDGE_EXT 0x8743
29 #define GL_MIRROR_CLAMP_TO_BORDER_EXT 0x8912
30 #endif
31
32 #define BORDER_TEXTURE 1
33 #define NO_BORDER_TEXTURE 2
34 #define COLOR_TEX_CORNERS 0
35
36 #define SIZE 8
37 static GLubyte BorderImage[SIZE+2][SIZE+2][4];
38 static GLubyte NoBorderImage[SIZE][SIZE][4];
39 static GLuint Border = 0;
40
41 #define TILE_SIZE 110
42
43 #define WRAP_MODE(m) { m , # m, GL_TRUE, 1.0, { NULL, NULL } }
44 #define WRAP_EXT(m,e1,e2,v) { m , # m, GL_FALSE, v, { e1, e2 } }
45
46 struct wrap_mode {
47 GLenum mode;
48 const char * name;
49 GLboolean supported;
50 GLfloat version;
51 const char * extension_names[2];
52 };
53
54 static struct wrap_mode modes[] = {
55 WRAP_MODE( GL_REPEAT ),
56 WRAP_MODE( GL_CLAMP ),
57 WRAP_EXT ( GL_CLAMP_TO_EDGE, "GL_EXT_texture_edge_clamp",
58 "GL_SGIS_texture_edge_clamp",
59 1.2 ),
60 WRAP_EXT ( GL_CLAMP_TO_BORDER, "GL_ARB_texture_border_clamp",
61 "GL_SGIS_texture_border_clamp",
62 1.3 ),
63 WRAP_EXT ( GL_MIRRORED_REPEAT, "GL_ARB_texture_mirrored_repeat",
64 "GL_IBM_texture_mirrored_repeat",
65 1.4 ),
66 WRAP_EXT ( GL_MIRROR_CLAMP_EXT, "GL_ATI_texture_mirror_once",
67 "GL_EXT_texture_mirror_clamp",
68 999.0 ),
69 WRAP_EXT ( GL_MIRROR_CLAMP_TO_BORDER_EXT, "GL_EXT_texture_mirror_clamp",
70 NULL,
71 999.0 ),
72 WRAP_EXT ( GL_MIRROR_CLAMP_TO_EDGE_EXT, "GL_ATI_texture_mirror_once",
73 "GL_EXT_texture_mirror_clamp",
74 999.0 ),
75 { 0, NULL, GL_FALSE, 0.0, { NULL, NULL } }
76 };
77
78 static void
79 PrintString(const char *s)
80 {
81 while (*s) {
82 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
83 s++;
84 }
85 }
86
87
88 static void Display( void )
89 {
90 GLenum i, j;
91 GLint offset;
92 GLfloat version;
93
94 /* Fill in the extensions that are supported.
95 */
96
97 version = atof( (char *) glGetString( GL_VERSION ) );
98 for ( i = 0 ; modes[i].mode != 0 ; i++ ) {
99 if ( ((modes[i].extension_names[0] != NULL)
100 && glutExtensionSupported(modes[i].extension_names[0]))
101 || ((modes[i].extension_names[1] != NULL)
102 && glutExtensionSupported(modes[i].extension_names[1])) ) {
103 modes[i].supported = GL_TRUE;
104 }
105 else if ( !modes[i].supported && (modes[i].version <= version) ) {
106 fprintf( stderr, "WARNING: OpenGL library meets minimum version\n"
107 " requirement for %s, but the\n"
108 " extension string is not advertised.\n"
109 " (%s%s%s)\n",
110 modes[i].name,
111 modes[i].extension_names[0],
112 (modes[i].extension_names[1] != NULL)
113 ? " or " : "",
114 (modes[i].extension_names[1] != NULL)
115 ? modes[i].extension_names[1] : "" );
116 modes[i].supported = GL_TRUE;
117 }
118 }
119
120
121 glClearColor(0.5, 0.5, 0.5, 1.0);
122 glClear( GL_COLOR_BUFFER_BIT );
123
124 #if 0
125 /* draw texture as image */
126 glDisable(GL_TEXTURE_2D);
127 glWindowPos2iARB(1, 1);
128 glDrawPixels(6, 6, GL_RGBA, GL_UNSIGNED_BYTE, (void *) TexImage);
129 #endif
130
131 glBindTexture(GL_TEXTURE_2D, Border ? BORDER_TEXTURE : NO_BORDER_TEXTURE);
132
133
134 /* loop over min/mag filters */
135 for (i = 0; i < 2; i++) {
136 offset = 0;
137
138 if (i) {
139 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
140 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
141 }
142 else {
143 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
144 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
145 }
146
147 /* loop over border modes */
148 for (j = 0; modes[j].mode != 0; j++) {
149 const GLfloat x0 = 0, y0 = 0, x1 = (TILE_SIZE - 10), y1 = (TILE_SIZE - 10);
150 const GLfloat b = 1.2;
151 const GLfloat s0 = -b, t0 = -b, s1 = 1.0+b, t1 = 1.0+b;
152
153 if ( modes[j].supported != GL_TRUE )
154 continue;
155
156 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, modes[j].mode);
157 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, modes[j].mode);
158
159 glPushMatrix();
160 glTranslatef(offset * TILE_SIZE + 10, i * TILE_SIZE + 40, 0);
161 offset++;
162
163 glEnable(GL_TEXTURE_2D);
164 glColor3f(1, 1, 1);
165 glBegin(GL_POLYGON);
166 glTexCoord2f(s0, t0); glVertex2f(x0, y0);
167 glTexCoord2f(s1, t0); glVertex2f(x1, y0);
168 glTexCoord2f(s1, t1); glVertex2f(x1, y1);
169 glTexCoord2f(s0, t1); glVertex2f(x0, y1);
170 glEnd();
171
172 /* draw red outline showing bounds of texture at s=0,1 and t=0,1 */
173 glDisable(GL_TEXTURE_2D);
174 glColor3f(1, 0, 0);
175 glBegin(GL_LINE_LOOP);
176 glVertex2f(x0 + b * (x1-x0) / (s1-s0), y0 + b * (y1-y0) / (t1-t0));
177 glVertex2f(x1 - b * (x1-x0) / (s1-s0), y0 + b * (y1-y0) / (t1-t0));
178 glVertex2f(x1 - b * (x1-x0) / (s1-s0), y1 - b * (y1-y0) / (t1-t0));
179 glVertex2f(x0 + b * (x1-x0) / (s1-s0), y1 - b * (y1-y0) / (t1-t0));
180 glEnd();
181
182 glPopMatrix();
183 }
184 }
185
186 glDisable(GL_TEXTURE_2D);
187 glColor3f(1, 1, 1);
188 offset = 0;
189 for (i = 0; modes[i].mode != 0; i++) {
190 if ( modes[i].supported ) {
191 glWindowPos2iARB( offset * TILE_SIZE + 10, 5 + ((offset & 1) * 15) );
192 PrintString(modes[i].name);
193 offset++;
194 }
195 }
196
197 glutSwapBuffers();
198 }
199
200
201 static void Reshape( int width, int height )
202 {
203 glViewport( 0, 0, width, height );
204 glMatrixMode( GL_PROJECTION );
205 glLoadIdentity();
206 glOrtho(0, width, 0, height, -1, 1);
207 glMatrixMode( GL_MODELVIEW );
208 glLoadIdentity();
209 }
210
211
212 static void Key( unsigned char key, int x, int y )
213 {
214 (void) x;
215 (void) y;
216 switch (key) {
217 case 'b':
218 Border = !Border;
219 printf("Texture Border Size = %d\n", Border);
220 break;
221 case 27:
222 exit(0);
223 break;
224 }
225 glutPostRedisplay();
226 }
227
228
229 static void Init( void )
230 {
231 static const GLubyte border[4] = { 0, 255, 0, 255 };
232 static const GLfloat borderf[4] = { 0, 1.0, 0, 1.0 };
233 GLint i, j;
234
235 for (i = 0; i < SIZE+2; i++) {
236 for (j = 0; j < SIZE+2; j++) {
237 if (i == 0 || j == 0 || i == SIZE+1 || j == SIZE+1) {
238 /* border color */
239 BorderImage[i][j][0] = border[0];
240 BorderImage[i][j][1] = border[1];
241 BorderImage[i][j][2] = border[2];
242 BorderImage[i][j][3] = border[3];
243 }
244 else if ((i + j) & 1) {
245 /* white */
246 BorderImage[i][j][0] = 255;
247 BorderImage[i][j][1] = 255;
248 BorderImage[i][j][2] = 255;
249 BorderImage[i][j][3] = 255;
250 }
251 else {
252 /* black */
253 BorderImage[i][j][0] = 0;
254 BorderImage[i][j][1] = 0;
255 BorderImage[i][j][2] = 0;
256 BorderImage[i][j][3] = 0;
257 }
258 }
259 }
260
261 glBindTexture(GL_TEXTURE_2D, BORDER_TEXTURE);
262 #ifdef TEST_PBO_DLIST
263 /* test fetching teximage from PBO in display list */
264 {
265 GLuint b = 42, l = 10;
266
267 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER, b);
268 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER, sizeof(BorderImage),
269 BorderImage, GL_STREAM_DRAW);
270
271 glNewList(l, GL_COMPILE);
272 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SIZE+2, SIZE+2, 1,
273 GL_RGBA, GL_UNSIGNED_BYTE, (void *) 0/* BorderImage*/);
274 glEndList();
275 glCallList(l);
276 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER, 0);
277 }
278 #else
279 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SIZE+2, SIZE+2, 1,
280 GL_RGBA, GL_UNSIGNED_BYTE, (void *) BorderImage);
281 #endif
282
283 for (i = 0; i < SIZE; i++) {
284 for (j = 0; j < SIZE; j++) {
285 #if COLOR_TEX_CORNERS
286 if (i == 0 && j == 0) {
287 /* lower-left texel = RED */
288 NoBorderImage[i][j][0] = 255;
289 NoBorderImage[i][j][1] = 0;
290 NoBorderImage[i][j][2] = 0;
291 NoBorderImage[i][j][3] = 255;
292 }
293 else if (i == 0 && j == SIZE-1) {
294 /* lower-right corner = GREEN */
295 NoBorderImage[i][j][0] = 0;
296 NoBorderImage[i][j][1] = 255;
297 NoBorderImage[i][j][2] = 0;
298 NoBorderImage[i][j][3] = 255;
299 }
300 else if (i == SIZE-1 && j == 0) {
301 /* upper-left corner = BLUE */
302 NoBorderImage[i][j][0] = 0;
303 NoBorderImage[i][j][1] = 0;
304 NoBorderImage[i][j][2] = 255;
305 NoBorderImage[i][j][3] = 255;
306 }
307 else if (i == SIZE-1 && j == SIZE-1) {
308 /* upper-right corner = YELLOW */
309 NoBorderImage[i][j][0] = 255;
310 NoBorderImage[i][j][1] = 255;
311 NoBorderImage[i][j][2] = 0;
312 NoBorderImage[i][j][3] = 255;
313 }
314 else
315 #endif
316 if ((i + j) & 1) {
317 /* white */
318 NoBorderImage[i][j][0] = 255;
319 NoBorderImage[i][j][1] = 255;
320 NoBorderImage[i][j][2] = 255;
321 NoBorderImage[i][j][3] = 255;
322 }
323 else {
324 /* black */
325 NoBorderImage[i][j][0] = 0;
326 NoBorderImage[i][j][1] = 0;
327 NoBorderImage[i][j][2] = 0;
328 NoBorderImage[i][j][3] = 0;
329 }
330 }
331 }
332
333 glBindTexture(GL_TEXTURE_2D, NO_BORDER_TEXTURE);
334 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SIZE, SIZE, 0,
335 GL_RGBA, GL_UNSIGNED_BYTE, (void *) NoBorderImage);
336 glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderf);
337 }
338
339
340 int main( int argc, char *argv[] )
341 {
342 glutInit( &argc, argv );
343 glutInitWindowPosition( 0, 0 );
344 glutInitWindowSize( 1000, 270 );
345 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
346 glutCreateWindow(argv[0]);
347 glewInit();
348 glutReshapeFunc( Reshape );
349 glutKeyboardFunc( Key );
350 glutDisplayFunc( Display );
351 Init();
352 glutMainLoop();
353 return 0;
354 }