Brian Paul | 058a3ab | 2000-03-01 03:36:40 +0000 | [diff] [blame^] | 1 | /* $Id: texobj.c,v 1.3 2000/03/01 03:36:40 brianp Exp $ */ |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * Example of using the 1.1 texture object functions. |
| 5 | * Also, this demo utilizes Mesa's fast texture map path. |
| 6 | * |
| 7 | * Brian Paul June 1996 This file is in the public domain. |
| 8 | */ |
| 9 | |
| 10 | |
| 11 | /* |
| 12 | * $Log: texobj.c,v $ |
Brian Paul | 058a3ab | 2000-03-01 03:36:40 +0000 | [diff] [blame^] | 13 | * Revision 1.3 2000/03/01 03:36:40 brianp |
| 14 | * test for GL 1.2 |
| 15 | * |
Brian Paul | 4ceb561 | 2000-02-25 23:24:06 +0000 | [diff] [blame] | 16 | * Revision 1.2 2000/02/25 23:24:06 brianp |
| 17 | * fixed bug when using display lists |
| 18 | * |
| 19 | * Revision 1.1.1.1 1999/08/19 00:55:40 jtg |
| 20 | * Imported sources |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 21 | * |
| 22 | * Revision 3.1 1999/03/28 18:24:37 brianp |
| 23 | * minor clean-up |
| 24 | * |
| 25 | * Revision 3.0 1998/02/14 18:42:29 brianp |
| 26 | * initial rev |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | |
| 31 | #include <math.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include "GL/glut.h" |
| 35 | |
| 36 | static GLuint Window = 0; |
| 37 | |
| 38 | static GLuint TexObj[2]; |
| 39 | static GLfloat Angle = 0.0f; |
| 40 | static GLboolean HaveTexObj = GL_FALSE; |
| 41 | |
| 42 | |
Brian Paul | 058a3ab | 2000-03-01 03:36:40 +0000 | [diff] [blame^] | 43 | #if defined(GL_VERSION_1_1) || defined(GL_VERSION_1_2) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 44 | # define TEXTURE_OBJECT 1 |
| 45 | #elif defined(GL_EXT_texture_object) |
| 46 | # define TEXTURE_OBJECT 1 |
| 47 | # define glBindTexture(A,B) glBindTextureEXT(A,B) |
| 48 | # define glGenTextures(A,B) glGenTexturesEXT(A,B) |
| 49 | # define glDeleteTextures(A,B) glDeleteTexturesEXT(A,B) |
| 50 | #endif |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | static void draw( void ) |
| 56 | { |
| 57 | glDepthFunc(GL_EQUAL); |
| 58 | /* glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );*/ |
| 59 | glClear( GL_COLOR_BUFFER_BIT ); |
| 60 | |
| 61 | glColor3f( 1.0, 1.0, 1.0 ); |
| 62 | |
| 63 | /* draw first polygon */ |
| 64 | glPushMatrix(); |
| 65 | glTranslatef( -1.0, 0.0, 0.0 ); |
| 66 | glRotatef( Angle, 0.0, 0.0, 1.0 ); |
| 67 | if (HaveTexObj) { |
| 68 | #ifdef TEXTURE_OBJECT |
| 69 | glBindTexture( GL_TEXTURE_2D, TexObj[0] ); |
| 70 | #endif |
| 71 | } |
| 72 | else { |
| 73 | glCallList( TexObj[0] ); |
| 74 | } |
| 75 | glBegin( GL_POLYGON ); |
| 76 | glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 ); |
| 77 | glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 ); |
| 78 | glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 ); |
| 79 | glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 ); |
| 80 | glEnd(); |
| 81 | glPopMatrix(); |
| 82 | |
| 83 | /* draw second polygon */ |
| 84 | glPushMatrix(); |
| 85 | glTranslatef( 1.0, 0.0, 0.0 ); |
| 86 | glRotatef( Angle-90.0, 0.0, 1.0, 0.0 ); |
| 87 | if (HaveTexObj) { |
| 88 | #ifdef TEXTURE_OBJECT |
| 89 | glBindTexture( GL_TEXTURE_2D, TexObj[1] ); |
| 90 | #endif |
| 91 | } |
| 92 | else { |
Brian Paul | 4ceb561 | 2000-02-25 23:24:06 +0000 | [diff] [blame] | 93 | glCallList( TexObj[1] ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 94 | } |
| 95 | glBegin( GL_POLYGON ); |
| 96 | glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 ); |
| 97 | glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 ); |
| 98 | glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 ); |
| 99 | glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 ); |
| 100 | glEnd(); |
| 101 | glPopMatrix(); |
| 102 | |
| 103 | glutSwapBuffers(); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | |
| 108 | static void idle( void ) |
| 109 | { |
| 110 | Angle += 2.0; |
| 111 | glutPostRedisplay(); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | |
| 116 | /* change view Angle, exit upon ESC */ |
| 117 | static void key(unsigned char k, int x, int y) |
| 118 | { |
| 119 | (void) x; |
| 120 | (void) y; |
| 121 | switch (k) { |
| 122 | case 27: |
| 123 | #ifdef TEXTURE_OBJECT |
| 124 | glDeleteTextures( 2, TexObj ); |
| 125 | #endif |
| 126 | glutDestroyWindow(Window); |
| 127 | exit(0); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | |
| 132 | |
| 133 | /* new window size or exposure */ |
| 134 | static void reshape( int width, int height ) |
| 135 | { |
| 136 | glViewport(0, 0, (GLint)width, (GLint)height); |
| 137 | glMatrixMode(GL_PROJECTION); |
| 138 | glLoadIdentity(); |
| 139 | /* glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/ |
| 140 | glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 ); |
| 141 | glMatrixMode(GL_MODELVIEW); |
| 142 | glLoadIdentity(); |
| 143 | glTranslatef( 0.0, 0.0, -8.0 ); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | static void init( void ) |
| 148 | { |
| 149 | static int width=8, height=8; |
| 150 | static GLubyte tex1[] = { |
| 151 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 152 | 0, 0, 0, 0, 1, 0, 0, 0, |
| 153 | 0, 0, 0, 1, 1, 0, 0, 0, |
| 154 | 0, 0, 0, 0, 1, 0, 0, 0, |
| 155 | 0, 0, 0, 0, 1, 0, 0, 0, |
| 156 | 0, 0, 0, 0, 1, 0, 0, 0, |
| 157 | 0, 0, 0, 1, 1, 1, 0, 0, |
| 158 | 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 159 | |
| 160 | static GLubyte tex2[] = { |
| 161 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 162 | 0, 0, 0, 2, 2, 0, 0, 0, |
| 163 | 0, 0, 2, 0, 0, 2, 0, 0, |
| 164 | 0, 0, 0, 0, 0, 2, 0, 0, |
| 165 | 0, 0, 0, 0, 2, 0, 0, 0, |
| 166 | 0, 0, 0, 2, 0, 0, 0, 0, |
| 167 | 0, 0, 2, 2, 2, 2, 0, 0, |
| 168 | 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 169 | |
| 170 | GLubyte tex[64][3]; |
| 171 | GLint i, j; |
| 172 | |
| 173 | |
| 174 | glDisable( GL_DITHER ); |
| 175 | |
| 176 | /* Setup texturing */ |
| 177 | glEnable( GL_TEXTURE_2D ); |
| 178 | glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL ); |
| 179 | glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST ); |
| 180 | |
| 181 | |
| 182 | /* generate texture object IDs */ |
| 183 | if (HaveTexObj) { |
| 184 | #ifdef TEXTURE_OBJECT |
| 185 | glGenTextures( 2, TexObj ); |
| 186 | #endif |
| 187 | } |
| 188 | else { |
| 189 | TexObj[0] = glGenLists(2); |
| 190 | TexObj[1] = TexObj[0]+1; |
| 191 | } |
| 192 | |
| 193 | /* setup first texture object */ |
| 194 | if (HaveTexObj) { |
| 195 | #ifdef TEXTURE_OBJECT |
| 196 | glBindTexture( GL_TEXTURE_2D, TexObj[0] ); |
| 197 | #endif |
| 198 | } |
| 199 | else { |
| 200 | glNewList( TexObj[0], GL_COMPILE ); |
| 201 | } |
| 202 | /* red on white */ |
| 203 | for (i=0;i<height;i++) { |
| 204 | for (j=0;j<width;j++) { |
| 205 | int p = i*width+j; |
| 206 | if (tex1[(height-i-1)*width+j]) { |
| 207 | tex[p][0] = 255; tex[p][1] = 0; tex[p][2] = 0; |
| 208 | } |
| 209 | else { |
| 210 | tex[p][0] = 255; tex[p][1] = 255; tex[p][2] = 255; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0, |
| 216 | GL_RGB, GL_UNSIGNED_BYTE, tex ); |
| 217 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); |
| 218 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); |
| 219 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); |
| 220 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); |
| 221 | if (!HaveTexObj) { |
| 222 | glEndList(); |
| 223 | } |
| 224 | /* end of texture object */ |
| 225 | |
| 226 | /* setup second texture object */ |
| 227 | if (HaveTexObj) { |
| 228 | #ifdef TEXTURE_OBJECT |
| 229 | glBindTexture( GL_TEXTURE_2D, TexObj[1] ); |
| 230 | #endif |
| 231 | } |
| 232 | else { |
| 233 | glNewList( TexObj[1], GL_COMPILE ); |
| 234 | } |
| 235 | /* green on blue */ |
| 236 | for (i=0;i<height;i++) { |
| 237 | for (j=0;j<width;j++) { |
| 238 | int p = i*width+j; |
| 239 | if (tex2[(height-i-1)*width+j]) { |
| 240 | tex[p][0] = 0; tex[p][1] = 255; tex[p][2] = 0; |
| 241 | } |
| 242 | else { |
| 243 | tex[p][0] = 0; tex[p][1] = 0; tex[p][2] = 255; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0, |
| 248 | GL_RGB, GL_UNSIGNED_BYTE, tex ); |
| 249 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); |
| 250 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); |
| 251 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); |
| 252 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); |
| 253 | if (!HaveTexObj) { |
| 254 | glEndList(); |
| 255 | } |
| 256 | /* end texture object */ |
| 257 | |
| 258 | } |
| 259 | |
| 260 | |
| 261 | |
| 262 | int main( int argc, char *argv[] ) |
| 263 | { |
| 264 | glutInitWindowPosition(0, 0); |
| 265 | glutInitWindowSize(300, 300); |
| 266 | glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE ); |
| 267 | |
| 268 | Window = glutCreateWindow("Texture Objects"); |
| 269 | if (!Window) { |
| 270 | exit(1); |
| 271 | } |
| 272 | |
| 273 | /* check that renderer has the GL_EXT_texture_object extension |
| 274 | * or supports OpenGL 1.1 |
| 275 | */ |
| 276 | #ifdef TEXTURE_OBJECT |
| 277 | { |
| 278 | char *exten = (char *) glGetString( GL_EXTENSIONS ); |
| 279 | char *version = (char *) glGetString( GL_VERSION ); |
| 280 | if ( strstr( exten, "GL_EXT_texture_object" ) |
Brian Paul | 058a3ab | 2000-03-01 03:36:40 +0000 | [diff] [blame^] | 281 | || strncmp( version, "1.1", 3 )==0 |
| 282 | || strncmp( version, "1.2", 3 )==0 ) { |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 283 | HaveTexObj = GL_TRUE; |
| 284 | } |
| 285 | } |
| 286 | #endif |
| 287 | |
| 288 | init(); |
| 289 | |
| 290 | glutReshapeFunc( reshape ); |
| 291 | glutKeyboardFunc( key ); |
| 292 | glutIdleFunc( idle ); |
| 293 | glutDisplayFunc( draw ); |
| 294 | glutMainLoop(); |
| 295 | return 0; |
| 296 | } |