blob: 6e9fbe0c70ff61a7805172fdd9cbad3038a8a671 [file] [log] [blame]
Brian Pauld545d912005-08-25 03:09:12 +00001/* $Id: texwrap.c,v 1.8 2005/08/25 03:09:12 brianp Exp $ */
Brian Paul0bc933a2001-03-26 19:45:57 +00002
3/*
4 * Test texture wrap modes.
Brian Paul77ff5e02001-04-12 20:50:26 +00005 * Press 'b' to toggle texture image borders. You should see the same
6 * rendering whether or not you're using borders.
Brian Paul0bc933a2001-03-26 19:45:57 +00007 *
8 * Brian Paul March 2001
9 */
10
11
12#define GL_GLEXT_PROTOTYPES
13#include <stdio.h>
14#include <stdlib.h>
15#include <math.h>
16#include <GL/glut.h>
17
18
Brian Paulbc36ee22002-10-17 17:39:37 +000019#ifndef GL_CLAMP_TO_BORDER
20#define GL_CLAMP_TO_BORDER 0x812D
Brian Paul0bc933a2001-03-26 19:45:57 +000021#endif
22
Brian Paulbc36ee22002-10-17 17:39:37 +000023#ifndef GL_MIRRORED_REPEAT
24#define GL_MIRRORED_REPEAT 0x8370
25#endif
26
Ian Romanickc8363a32003-09-02 19:25:17 +000027#ifndef GL_EXT_texture_mirror_clamp
28#define GL_MIRROR_CLAMP_EXT 0x8742
29#define GL_MIRROR_CLAMP_TO_EDGE_EXT 0x8743
30#define GL_MIRROR_CLAMP_TO_BORDER_EXT 0x8912
Brian Paulbc36ee22002-10-17 17:39:37 +000031#endif
Brian Paul0bc933a2001-03-26 19:45:57 +000032
Brian Paul77ff5e02001-04-12 20:50:26 +000033#define BORDER_TEXTURE 1
34#define NO_BORDER_TEXTURE 2
35
36#define SIZE 8
37static GLubyte BorderImage[SIZE+2][SIZE+2][4];
38static GLubyte NoBorderImage[SIZE][SIZE][4];
Brian Paul785774d2003-05-30 15:30:16 +000039static GLuint Border = 0;
Brian Paul0bc933a2001-03-26 19:45:57 +000040
Brian Pauld545d912005-08-25 03:09:12 +000041#define TILE_SIZE 110
Brian Paul0bc933a2001-03-26 19:45:57 +000042
Brian Paulbc36ee22002-10-17 17:39:37 +000043#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
46struct wrap_mode {
47 GLenum mode;
48 const char * name;
49 GLboolean supported;
50 GLfloat version;
51 const char * extension_names[2];
52};
53
54static 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 ),
Ian Romanickc8363a32003-09-02 19:25:17 +000066 WRAP_EXT ( GL_MIRROR_CLAMP_EXT, "GL_ATI_texture_mirror_once",
67 "GL_EXT_texture_mirror_clamp",
Brian Paulbc36ee22002-10-17 17:39:37 +000068 999.0 ),
Ian Romanickc8363a32003-09-02 19:25:17 +000069 WRAP_EXT ( GL_MIRROR_CLAMP_TO_BORDER_EXT, "GL_EXT_texture_mirror_clamp",
70 NULL,
Brian Paulbc36ee22002-10-17 17:39:37 +000071 999.0 ),
Brian Pauld545d912005-08-25 03:09:12 +000072 WRAP_EXT ( GL_MIRROR_CLAMP_TO_EDGE_EXT, "GL_ATI_texture_mirror_once",
73 "GL_EXT_texture_mirror_clamp",
74 999.0 ),
Brian Paulbc36ee22002-10-17 17:39:37 +000075 { 0 }
76};
77
Brian Paul0bc933a2001-03-26 19:45:57 +000078static void
79PrintString(const char *s)
80{
81 while (*s) {
82 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
83 s++;
84 }
85}
86
87
88static void Display( void )
89{
Karl Schultz53d30c52002-10-18 17:47:35 +000090 GLenum i, j;
Brian Paulbc36ee22002-10-17 17:39:37 +000091 GLint offset;
92 GLfloat version;
Brian Paul0bc933a2001-03-26 19:45:57 +000093
Brian Paulbc36ee22002-10-17 17:39:37 +000094 /* 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
Brian Paul0bc933a2001-03-26 19:45:57 +0000120
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);
Brian Paul95e67dc2003-02-04 02:35:00 +0000127 glWindowPos2iARB(1, 1);
Brian Paul0bc933a2001-03-26 19:45:57 +0000128 glDrawPixels(6, 6, GL_RGBA, GL_UNSIGNED_BYTE, (void *) TexImage);
129#endif
130
Brian Paul77ff5e02001-04-12 20:50:26 +0000131 glBindTexture(GL_TEXTURE_2D, Border ? BORDER_TEXTURE : NO_BORDER_TEXTURE);
Brian Paul0bc933a2001-03-26 19:45:57 +0000132
Brian Paulbc36ee22002-10-17 17:39:37 +0000133
Brian Paul0bc933a2001-03-26 19:45:57 +0000134 /* loop over min/mag filters */
135 for (i = 0; i < 2; i++) {
Brian Paulbc36ee22002-10-17 17:39:37 +0000136 offset = 0;
137
Brian Paul0bc933a2001-03-26 19:45:57 +0000138 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 */
Brian Pauld545d912005-08-25 03:09:12 +0000148 for (j = 0; modes[j].mode != 0; j++) {
149 const GLfloat x0 = 0, y0 = 0, x1 = (TILE_SIZE - 10), y1 = (TILE_SIZE - 10);
Brian Paul785774d2003-05-30 15:30:16 +0000150 const GLfloat b = 1.2;
Brian Paul77ff5e02001-04-12 20:50:26 +0000151 const GLfloat s0 = -b, t0 = -b, s1 = 1.0+b, t1 = 1.0+b;
Brian Paulbc36ee22002-10-17 17:39:37 +0000152
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);
Brian Paul0bc933a2001-03-26 19:45:57 +0000158
159 glPushMatrix();
Brian Pauld545d912005-08-25 03:09:12 +0000160 glTranslatef(offset * TILE_SIZE + 10, i * TILE_SIZE + 40, 0);
Brian Paulbc36ee22002-10-17 17:39:37 +0000161 offset++;
Brian Paul0bc933a2001-03-26 19:45:57 +0000162
Brian Paul77ff5e02001-04-12 20:50:26 +0000163 glEnable(GL_TEXTURE_2D);
164 glColor3f(1, 1, 1);
Brian Paul0bc933a2001-03-26 19:45:57 +0000165 glBegin(GL_POLYGON);
Brian Paul77ff5e02001-04-12 20:50:26 +0000166 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));
Brian Paul0bc933a2001-03-26 19:45:57 +0000180 glEnd();
181
182 glPopMatrix();
183 }
184 }
185
186 glDisable(GL_TEXTURE_2D);
Brian Paul77ff5e02001-04-12 20:50:26 +0000187 glColor3f(1, 1, 1);
Brian Paulbc36ee22002-10-17 17:39:37 +0000188 offset = 0;
Brian Pauld545d912005-08-25 03:09:12 +0000189 for (i = 0; modes[i].mode != 0; i++) {
Brian Paulbc36ee22002-10-17 17:39:37 +0000190 if ( modes[i].supported ) {
Brian Pauld545d912005-08-25 03:09:12 +0000191 glWindowPos2iARB( offset * TILE_SIZE + 10, 5 + ((offset & 1) * 15) );
Brian Paulbc36ee22002-10-17 17:39:37 +0000192 PrintString(modes[i].name);
193 offset++;
194 }
Brian Paul0bc933a2001-03-26 19:45:57 +0000195 }
196
197 glutSwapBuffers();
198}
199
200
201static 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
212static void Key( unsigned char key, int x, int y )
213{
214 (void) x;
215 (void) y;
216 switch (key) {
Brian Paul77ff5e02001-04-12 20:50:26 +0000217 case 'b':
218 Border = !Border;
219 printf("Texture Border Size = %d\n", Border);
220 break;
Brian Paul0bc933a2001-03-26 19:45:57 +0000221 case 27:
222 exit(0);
223 break;
224 }
225 glutPostRedisplay();
226}
227
228
229static void Init( void )
230{
231 static const GLubyte border[4] = { 0, 255, 0, 255 };
Brian Paul77ff5e02001-04-12 20:50:26 +0000232 static const GLfloat borderf[4] = { 0, 1.0, 0, 1.0 };
Brian Paul0bc933a2001-03-26 19:45:57 +0000233 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 */
Brian Paul77ff5e02001-04-12 20:50:26 +0000239 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];
Brian Paul0bc933a2001-03-26 19:45:57 +0000243 }
244 else if ((i + j) & 1) {
245 /* white */
Brian Paul77ff5e02001-04-12 20:50:26 +0000246 BorderImage[i][j][0] = 255;
247 BorderImage[i][j][1] = 255;
248 BorderImage[i][j][2] = 255;
249 BorderImage[i][j][3] = 255;
Brian Paul0bc933a2001-03-26 19:45:57 +0000250 }
251 else {
252 /* black */
Brian Paul77ff5e02001-04-12 20:50:26 +0000253 BorderImage[i][j][0] = 0;
254 BorderImage[i][j][1] = 0;
255 BorderImage[i][j][2] = 0;
256 BorderImage[i][j][3] = 0;
Brian Paul0bc933a2001-03-26 19:45:57 +0000257 }
258 }
259 }
260
Brian Paul77ff5e02001-04-12 20:50:26 +0000261 glBindTexture(GL_TEXTURE_2D, BORDER_TEXTURE);
Brian Paul0bc933a2001-03-26 19:45:57 +0000262 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SIZE+2, SIZE+2, 1,
Brian Paul77ff5e02001-04-12 20:50:26 +0000263 GL_RGBA, GL_UNSIGNED_BYTE, (void *) BorderImage);
264
Brian Paul77ff5e02001-04-12 20:50:26 +0000265 for (i = 0; i < SIZE; i++) {
266 for (j = 0; j < SIZE; j++) {
267 if ((i + j) & 1) {
268 /* white */
269 NoBorderImage[i][j][0] = 255;
270 NoBorderImage[i][j][1] = 255;
271 NoBorderImage[i][j][2] = 255;
272 NoBorderImage[i][j][3] = 255;
273 }
274 else {
275 /* black */
276 NoBorderImage[i][j][0] = 0;
277 NoBorderImage[i][j][1] = 0;
278 NoBorderImage[i][j][2] = 0;
279 NoBorderImage[i][j][3] = 0;
280 }
281 }
282 }
283
284 glBindTexture(GL_TEXTURE_2D, NO_BORDER_TEXTURE);
285 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SIZE, SIZE, 0,
286 GL_RGBA, GL_UNSIGNED_BYTE, (void *) NoBorderImage);
287 glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderf);
Brian Paul0bc933a2001-03-26 19:45:57 +0000288}
289
290
291int main( int argc, char *argv[] )
292{
293 glutInit( &argc, argv );
294 glutInitWindowPosition( 0, 0 );
Brian Pauld545d912005-08-25 03:09:12 +0000295 glutInitWindowSize( 1000, 270 );
Brian Paul0bc933a2001-03-26 19:45:57 +0000296 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
297 glutCreateWindow(argv[0]);
298 glutReshapeFunc( Reshape );
299 glutKeyboardFunc( Key );
300 glutDisplayFunc( Display );
301 Init();
302 glutMainLoop();
303 return 0;
304}