1 /* $Id: texwrap.c,v 1.2 2001/04/12 20:50:26 brianp Exp $ */
4 * Test texture wrap modes.
5 * Press 'b' to toggle texture image borders. You should see the same
6 * rendering whether or not you're using borders.
8 * Brian Paul March 2001
12 #define GL_GLEXT_PROTOTYPES
19 #ifndef GL_CLAMP_TO_BORDER_ARB
20 #define GL_CLAMP_TO_BORDER_ARB 0x812D
24 #define BORDER_TEXTURE 1
25 #define NO_BORDER_TEXTURE 2
28 static GLubyte BorderImage
[SIZE
+2][SIZE
+2][4];
29 static GLubyte NoBorderImage
[SIZE
][SIZE
][4];
30 static GLuint Border
= 1;
34 PrintString(const char *s
)
37 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
43 static void Display( void )
45 static const GLenum modes
[] = {
49 GL_CLAMP_TO_BORDER_ARB
51 static const char *names
[] = {
55 "GL_CLAMP_TO_BORDER_ARB"
61 numModes
= glutExtensionSupported("GL_ARB_texture_border_clamp") ? 4 : 3;
63 glClearColor(0.5, 0.5, 0.5, 1.0);
64 glClear( GL_COLOR_BUFFER_BIT
);
67 /* draw texture as image */
68 glDisable(GL_TEXTURE_2D
);
69 glWindowPos2iMESA(1, 1);
70 glDrawPixels(6, 6, GL_RGBA
, GL_UNSIGNED_BYTE
, (void *) TexImage
);
73 glBindTexture(GL_TEXTURE_2D
, Border
? BORDER_TEXTURE
: NO_BORDER_TEXTURE
);
75 /* loop over min/mag filters */
76 for (i
= 0; i
< 2; i
++) {
78 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
79 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
82 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
83 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
86 /* loop over border modes */
87 for (j
= 0; j
< numModes
; j
++) {
88 const GLfloat x0
= 0, y0
= 0, x1
= 140, y1
= 140;
89 const GLfloat b
= 0.2;
90 const GLfloat s0
= -b
, t0
= -b
, s1
= 1.0+b
, t1
= 1.0+b
;
91 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, modes
[j
]);
92 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, modes
[j
]);
95 glTranslatef(j
* 150 + 10, i
* 150 + 25, 0);
97 glEnable(GL_TEXTURE_2D
);
100 glTexCoord2f(s0
, t0
); glVertex2f(x0
, y0
);
101 glTexCoord2f(s1
, t0
); glVertex2f(x1
, y0
);
102 glTexCoord2f(s1
, t1
); glVertex2f(x1
, y1
);
103 glTexCoord2f(s0
, t1
); glVertex2f(x0
, y1
);
106 /* draw red outline showing bounds of texture at s=0,1 and t=0,1 */
107 glDisable(GL_TEXTURE_2D
);
109 glBegin(GL_LINE_LOOP
);
110 glVertex2f(x0
+ b
* (x1
-x0
) / (s1
-s0
), y0
+ b
* (y1
-y0
) / (t1
-t0
));
111 glVertex2f(x1
- b
* (x1
-x0
) / (s1
-s0
), y0
+ b
* (y1
-y0
) / (t1
-t0
));
112 glVertex2f(x1
- b
* (x1
-x0
) / (s1
-s0
), y1
- b
* (y1
-y0
) / (t1
-t0
));
113 glVertex2f(x0
+ b
* (x1
-x0
) / (s1
-s0
), y1
- b
* (y1
-y0
) / (t1
-t0
));
120 glDisable(GL_TEXTURE_2D
);
122 for (i
= 0; i
< numModes
; i
++) {
123 glWindowPos2iMESA( i
* 150 + 10, 5);
124 PrintString(names
[i
]);
131 static void Reshape( int width
, int height
)
133 glViewport( 0, 0, width
, height
);
134 glMatrixMode( GL_PROJECTION
);
136 glOrtho(0, width
, 0, height
, -1, 1);
137 glMatrixMode( GL_MODELVIEW
);
142 static void Key( unsigned char key
, int x
, int y
)
149 printf("Texture Border Size = %d\n", Border
);
159 static void Init( void )
161 static const GLubyte border
[4] = { 0, 255, 0, 255 };
162 static const GLfloat borderf
[4] = { 0, 1.0, 0, 1.0 };
165 for (i
= 0; i
< SIZE
+2; i
++) {
166 for (j
= 0; j
< SIZE
+2; j
++) {
167 if (i
== 0 || j
== 0 || i
== SIZE
+1 || j
== SIZE
+1) {
169 BorderImage
[i
][j
][0] = border
[0];
170 BorderImage
[i
][j
][1] = border
[1];
171 BorderImage
[i
][j
][2] = border
[2];
172 BorderImage
[i
][j
][3] = border
[3];
174 else if ((i
+ j
) & 1) {
176 BorderImage
[i
][j
][0] = 255;
177 BorderImage
[i
][j
][1] = 255;
178 BorderImage
[i
][j
][2] = 255;
179 BorderImage
[i
][j
][3] = 255;
183 BorderImage
[i
][j
][0] = 0;
184 BorderImage
[i
][j
][1] = 0;
185 BorderImage
[i
][j
][2] = 0;
186 BorderImage
[i
][j
][3] = 0;
191 glBindTexture(GL_TEXTURE_2D
, BORDER_TEXTURE
);
192 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, SIZE
+2, SIZE
+2, 1,
193 GL_RGBA
, GL_UNSIGNED_BYTE
, (void *) BorderImage
);
196 for (i
= 0; i
< SIZE
; i
++) {
197 for (j
= 0; j
< SIZE
; j
++) {
200 NoBorderImage
[i
][j
][0] = 255;
201 NoBorderImage
[i
][j
][1] = 255;
202 NoBorderImage
[i
][j
][2] = 255;
203 NoBorderImage
[i
][j
][3] = 255;
207 NoBorderImage
[i
][j
][0] = 0;
208 NoBorderImage
[i
][j
][1] = 0;
209 NoBorderImage
[i
][j
][2] = 0;
210 NoBorderImage
[i
][j
][3] = 0;
215 glBindTexture(GL_TEXTURE_2D
, NO_BORDER_TEXTURE
);
216 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, SIZE
, SIZE
, 0,
217 GL_RGBA
, GL_UNSIGNED_BYTE
, (void *) NoBorderImage
);
218 glTexParameterfv(GL_TEXTURE_2D
, GL_TEXTURE_BORDER_COLOR
, borderf
);
222 int main( int argc
, char *argv
[] )
224 glutInit( &argc
, argv
);
225 glutInitWindowPosition( 0, 0 );
226 glutInitWindowSize( 650, 340 );
227 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
228 glutCreateWindow(argv
[0]);
229 glutReshapeFunc( Reshape
);
230 glutKeyboardFunc( Key
);
231 glutDisplayFunc( Display
);