Brian Paul | 77ff5e0 | 2001-04-12 20:50:26 +0000 | [diff] [blame] | 1 | /* $Id: texwrap.c,v 1.2 2001/04/12 20:50:26 brianp Exp $ */ |
Brian Paul | 0bc933a | 2001-03-26 19:45:57 +0000 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * Test texture wrap modes. |
Brian Paul | 77ff5e0 | 2001-04-12 20:50:26 +0000 | [diff] [blame] | 5 | * Press 'b' to toggle texture image borders. You should see the same |
| 6 | * rendering whether or not you're using borders. |
Brian Paul | 0bc933a | 2001-03-26 19:45:57 +0000 | [diff] [blame] | 7 | * |
| 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 | |
| 19 | #ifndef GL_CLAMP_TO_BORDER_ARB |
| 20 | #define GL_CLAMP_TO_BORDER_ARB 0x812D |
| 21 | #endif |
| 22 | |
| 23 | |
Brian Paul | 77ff5e0 | 2001-04-12 20:50:26 +0000 | [diff] [blame] | 24 | #define BORDER_TEXTURE 1 |
| 25 | #define NO_BORDER_TEXTURE 2 |
| 26 | |
| 27 | #define SIZE 8 |
| 28 | static GLubyte BorderImage[SIZE+2][SIZE+2][4]; |
| 29 | static GLubyte NoBorderImage[SIZE][SIZE][4]; |
| 30 | static GLuint Border = 1; |
Brian Paul | 0bc933a | 2001-03-26 19:45:57 +0000 | [diff] [blame] | 31 | |
| 32 | |
| 33 | static void |
| 34 | PrintString(const char *s) |
| 35 | { |
| 36 | while (*s) { |
| 37 | glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s); |
| 38 | s++; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | |
| 43 | static void Display( void ) |
| 44 | { |
| 45 | static const GLenum modes[] = { |
| 46 | GL_REPEAT, |
| 47 | GL_CLAMP, |
| 48 | GL_CLAMP_TO_EDGE, |
| 49 | GL_CLAMP_TO_BORDER_ARB |
| 50 | }; |
| 51 | static const char *names[] = { |
| 52 | "GL_REPEAT", |
| 53 | "GL_CLAMP", |
| 54 | "GL_CLAMP_TO_EDGE", |
| 55 | "GL_CLAMP_TO_BORDER_ARB" |
| 56 | }; |
| 57 | |
| 58 | GLint i, j; |
| 59 | GLint numModes; |
| 60 | |
| 61 | numModes = glutExtensionSupported("GL_ARB_texture_border_clamp") ? 4 : 3; |
| 62 | |
| 63 | glClearColor(0.5, 0.5, 0.5, 1.0); |
| 64 | glClear( GL_COLOR_BUFFER_BIT ); |
| 65 | |
| 66 | #if 0 |
| 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); |
| 71 | #endif |
| 72 | |
Brian Paul | 77ff5e0 | 2001-04-12 20:50:26 +0000 | [diff] [blame] | 73 | glBindTexture(GL_TEXTURE_2D, Border ? BORDER_TEXTURE : NO_BORDER_TEXTURE); |
Brian Paul | 0bc933a | 2001-03-26 19:45:57 +0000 | [diff] [blame] | 74 | |
| 75 | /* loop over min/mag filters */ |
| 76 | for (i = 0; i < 2; i++) { |
| 77 | if (i) { |
| 78 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 79 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 80 | } |
| 81 | else { |
| 82 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 83 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 84 | } |
| 85 | |
| 86 | /* loop over border modes */ |
| 87 | for (j = 0; j < numModes; j++) { |
Brian Paul | 77ff5e0 | 2001-04-12 20:50:26 +0000 | [diff] [blame] | 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; |
Brian Paul | 0bc933a | 2001-03-26 19:45:57 +0000 | [diff] [blame] | 91 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, modes[j]); |
| 92 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, modes[j]); |
| 93 | |
| 94 | glPushMatrix(); |
| 95 | glTranslatef(j * 150 + 10, i * 150 + 25, 0); |
| 96 | |
Brian Paul | 77ff5e0 | 2001-04-12 20:50:26 +0000 | [diff] [blame] | 97 | glEnable(GL_TEXTURE_2D); |
| 98 | glColor3f(1, 1, 1); |
Brian Paul | 0bc933a | 2001-03-26 19:45:57 +0000 | [diff] [blame] | 99 | glBegin(GL_POLYGON); |
Brian Paul | 77ff5e0 | 2001-04-12 20:50:26 +0000 | [diff] [blame] | 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); |
| 104 | glEnd(); |
| 105 | |
| 106 | /* draw red outline showing bounds of texture at s=0,1 and t=0,1 */ |
| 107 | glDisable(GL_TEXTURE_2D); |
| 108 | glColor3f(1, 0, 0); |
| 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)); |
Brian Paul | 0bc933a | 2001-03-26 19:45:57 +0000 | [diff] [blame] | 114 | glEnd(); |
| 115 | |
| 116 | glPopMatrix(); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | glDisable(GL_TEXTURE_2D); |
Brian Paul | 77ff5e0 | 2001-04-12 20:50:26 +0000 | [diff] [blame] | 121 | glColor3f(1, 1, 1); |
Brian Paul | 0bc933a | 2001-03-26 19:45:57 +0000 | [diff] [blame] | 122 | for (i = 0; i < numModes; i++) { |
| 123 | glWindowPos2iMESA( i * 150 + 10, 5); |
| 124 | PrintString(names[i]); |
| 125 | } |
| 126 | |
| 127 | glutSwapBuffers(); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | static void Reshape( int width, int height ) |
| 132 | { |
| 133 | glViewport( 0, 0, width, height ); |
| 134 | glMatrixMode( GL_PROJECTION ); |
| 135 | glLoadIdentity(); |
| 136 | glOrtho(0, width, 0, height, -1, 1); |
| 137 | glMatrixMode( GL_MODELVIEW ); |
| 138 | glLoadIdentity(); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | static void Key( unsigned char key, int x, int y ) |
| 143 | { |
| 144 | (void) x; |
| 145 | (void) y; |
| 146 | switch (key) { |
Brian Paul | 77ff5e0 | 2001-04-12 20:50:26 +0000 | [diff] [blame] | 147 | case 'b': |
| 148 | Border = !Border; |
| 149 | printf("Texture Border Size = %d\n", Border); |
| 150 | break; |
Brian Paul | 0bc933a | 2001-03-26 19:45:57 +0000 | [diff] [blame] | 151 | case 27: |
| 152 | exit(0); |
| 153 | break; |
| 154 | } |
| 155 | glutPostRedisplay(); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | static void Init( void ) |
| 160 | { |
| 161 | static const GLubyte border[4] = { 0, 255, 0, 255 }; |
Brian Paul | 77ff5e0 | 2001-04-12 20:50:26 +0000 | [diff] [blame] | 162 | static const GLfloat borderf[4] = { 0, 1.0, 0, 1.0 }; |
Brian Paul | 0bc933a | 2001-03-26 19:45:57 +0000 | [diff] [blame] | 163 | GLint i, j; |
| 164 | |
| 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) { |
| 168 | /* border color */ |
Brian Paul | 77ff5e0 | 2001-04-12 20:50:26 +0000 | [diff] [blame] | 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]; |
Brian Paul | 0bc933a | 2001-03-26 19:45:57 +0000 | [diff] [blame] | 173 | } |
| 174 | else if ((i + j) & 1) { |
| 175 | /* white */ |
Brian Paul | 77ff5e0 | 2001-04-12 20:50:26 +0000 | [diff] [blame] | 176 | BorderImage[i][j][0] = 255; |
| 177 | BorderImage[i][j][1] = 255; |
| 178 | BorderImage[i][j][2] = 255; |
| 179 | BorderImage[i][j][3] = 255; |
Brian Paul | 0bc933a | 2001-03-26 19:45:57 +0000 | [diff] [blame] | 180 | } |
| 181 | else { |
| 182 | /* black */ |
Brian Paul | 77ff5e0 | 2001-04-12 20:50:26 +0000 | [diff] [blame] | 183 | BorderImage[i][j][0] = 0; |
| 184 | BorderImage[i][j][1] = 0; |
| 185 | BorderImage[i][j][2] = 0; |
| 186 | BorderImage[i][j][3] = 0; |
Brian Paul | 0bc933a | 2001-03-26 19:45:57 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
Brian Paul | 77ff5e0 | 2001-04-12 20:50:26 +0000 | [diff] [blame] | 191 | glBindTexture(GL_TEXTURE_2D, BORDER_TEXTURE); |
Brian Paul | 0bc933a | 2001-03-26 19:45:57 +0000 | [diff] [blame] | 192 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SIZE+2, SIZE+2, 1, |
Brian Paul | 77ff5e0 | 2001-04-12 20:50:26 +0000 | [diff] [blame] | 193 | GL_RGBA, GL_UNSIGNED_BYTE, (void *) BorderImage); |
| 194 | |
| 195 | |
| 196 | for (i = 0; i < SIZE; i++) { |
| 197 | for (j = 0; j < SIZE; j++) { |
| 198 | if ((i + j) & 1) { |
| 199 | /* white */ |
| 200 | NoBorderImage[i][j][0] = 255; |
| 201 | NoBorderImage[i][j][1] = 255; |
| 202 | NoBorderImage[i][j][2] = 255; |
| 203 | NoBorderImage[i][j][3] = 255; |
| 204 | } |
| 205 | else { |
| 206 | /* black */ |
| 207 | NoBorderImage[i][j][0] = 0; |
| 208 | NoBorderImage[i][j][1] = 0; |
| 209 | NoBorderImage[i][j][2] = 0; |
| 210 | NoBorderImage[i][j][3] = 0; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 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); |
Brian Paul | 0bc933a | 2001-03-26 19:45:57 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | |
| 222 | int main( int argc, char *argv[] ) |
| 223 | { |
| 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 ); |
| 232 | Init(); |
| 233 | glutMainLoop(); |
| 234 | return 0; |
| 235 | } |