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