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