Brian Paul | 0bc933a | 2001-03-26 19:45:57 +0000 | [diff] [blame^] | 1 | /* $Id: texwrap.c,v 1.1 2001/03/26 19:45:57 brianp Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Test texture wrap modes. |
| 5 | * |
| 6 | * Brian Paul March 2001 |
| 7 | */ |
| 8 | |
| 9 | |
| 10 | #define GL_GLEXT_PROTOTYPES |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <math.h> |
| 14 | #include <GL/glut.h> |
| 15 | |
| 16 | |
| 17 | #ifndef GL_CLAMP_TO_BORDER_ARB |
| 18 | #define GL_CLAMP_TO_BORDER_ARB 0x812D |
| 19 | #endif |
| 20 | |
| 21 | |
| 22 | #define SIZE 4 |
| 23 | static GLubyte TexImage[SIZE+2][SIZE+2][4]; |
| 24 | |
| 25 | |
| 26 | static void |
| 27 | PrintString(const char *s) |
| 28 | { |
| 29 | while (*s) { |
| 30 | glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s); |
| 31 | s++; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | |
| 36 | static void Display( void ) |
| 37 | { |
| 38 | static const GLenum modes[] = { |
| 39 | GL_REPEAT, |
| 40 | GL_CLAMP, |
| 41 | GL_CLAMP_TO_EDGE, |
| 42 | GL_CLAMP_TO_BORDER_ARB |
| 43 | }; |
| 44 | static const char *names[] = { |
| 45 | "GL_REPEAT", |
| 46 | "GL_CLAMP", |
| 47 | "GL_CLAMP_TO_EDGE", |
| 48 | "GL_CLAMP_TO_BORDER_ARB" |
| 49 | }; |
| 50 | |
| 51 | GLint i, j; |
| 52 | GLint numModes; |
| 53 | |
| 54 | numModes = glutExtensionSupported("GL_ARB_texture_border_clamp") ? 4 : 3; |
| 55 | |
| 56 | glClearColor(0.5, 0.5, 0.5, 1.0); |
| 57 | glClear( GL_COLOR_BUFFER_BIT ); |
| 58 | |
| 59 | #if 0 |
| 60 | /* draw texture as image */ |
| 61 | glDisable(GL_TEXTURE_2D); |
| 62 | glWindowPos2iMESA(1, 1); |
| 63 | glDrawPixels(6, 6, GL_RGBA, GL_UNSIGNED_BYTE, (void *) TexImage); |
| 64 | #endif |
| 65 | |
| 66 | glEnable(GL_TEXTURE_2D); |
| 67 | |
| 68 | /* loop over min/mag filters */ |
| 69 | for (i = 0; i < 2; i++) { |
| 70 | if (i) { |
| 71 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 72 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 73 | } |
| 74 | else { |
| 75 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 76 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 77 | } |
| 78 | |
| 79 | /* loop over border modes */ |
| 80 | for (j = 0; j < numModes; j++) { |
| 81 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, modes[j]); |
| 82 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, modes[j]); |
| 83 | |
| 84 | glPushMatrix(); |
| 85 | glTranslatef(j * 150 + 10, i * 150 + 25, 0); |
| 86 | |
| 87 | glBegin(GL_POLYGON); |
| 88 | glTexCoord2f(-0.2, -0.2); glVertex2f( 0, 0); |
| 89 | glTexCoord2f( 1.2, -0.2); glVertex2f(140, 0); |
| 90 | glTexCoord2f( 1.2, 1.2); glVertex2f(140, 140); |
| 91 | glTexCoord2f(-0.2, 1.2); glVertex2f( 0, 140); |
| 92 | glEnd(); |
| 93 | |
| 94 | glPopMatrix(); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | glDisable(GL_TEXTURE_2D); |
| 99 | for (i = 0; i < numModes; i++) { |
| 100 | glWindowPos2iMESA( i * 150 + 10, 5); |
| 101 | PrintString(names[i]); |
| 102 | } |
| 103 | |
| 104 | glutSwapBuffers(); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | static void Reshape( int width, int height ) |
| 109 | { |
| 110 | glViewport( 0, 0, width, height ); |
| 111 | glMatrixMode( GL_PROJECTION ); |
| 112 | glLoadIdentity(); |
| 113 | glOrtho(0, width, 0, height, -1, 1); |
| 114 | glMatrixMode( GL_MODELVIEW ); |
| 115 | glLoadIdentity(); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | static void Key( unsigned char key, int x, int y ) |
| 120 | { |
| 121 | (void) x; |
| 122 | (void) y; |
| 123 | switch (key) { |
| 124 | case 27: |
| 125 | exit(0); |
| 126 | break; |
| 127 | } |
| 128 | glutPostRedisplay(); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | static void Init( void ) |
| 133 | { |
| 134 | static const GLubyte border[4] = { 0, 255, 0, 255 }; |
| 135 | GLint i, j; |
| 136 | |
| 137 | for (i = 0; i < SIZE+2; i++) { |
| 138 | for (j = 0; j < SIZE+2; j++) { |
| 139 | if (i == 0 || j == 0 || i == SIZE+1 || j == SIZE+1) { |
| 140 | /* border color */ |
| 141 | TexImage[i][j][0] = border[0]; |
| 142 | TexImage[i][j][1] = border[1]; |
| 143 | TexImage[i][j][2] = border[2]; |
| 144 | TexImage[i][j][3] = border[3]; |
| 145 | } |
| 146 | else if ((i + j) & 1) { |
| 147 | /* white */ |
| 148 | TexImage[i][j][0] = 255; |
| 149 | TexImage[i][j][1] = 255; |
| 150 | TexImage[i][j][2] = 255; |
| 151 | TexImage[i][j][3] = 255; |
| 152 | } |
| 153 | else { |
| 154 | /* black */ |
| 155 | TexImage[i][j][0] = 0; |
| 156 | TexImage[i][j][1] = 0; |
| 157 | TexImage[i][j][2] = 0; |
| 158 | TexImage[i][j][3] = 0; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SIZE+2, SIZE+2, 1, |
| 164 | GL_RGBA, GL_UNSIGNED_BYTE, (void *) TexImage); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | int main( int argc, char *argv[] ) |
| 169 | { |
| 170 | glutInit( &argc, argv ); |
| 171 | glutInitWindowPosition( 0, 0 ); |
| 172 | glutInitWindowSize( 650, 340 ); |
| 173 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE ); |
| 174 | glutCreateWindow(argv[0]); |
| 175 | glutReshapeFunc( Reshape ); |
| 176 | glutKeyboardFunc( Key ); |
| 177 | glutDisplayFunc( Display ); |
| 178 | Init(); |
| 179 | glutMainLoop(); |
| 180 | return 0; |
| 181 | } |