3 * Example of using the 1.1 texture object functions.
4 * Also, this demo utilizes Mesa's fast texture map path.
6 * Brian Paul June 1996 This file is in the public domain.
16 static GLuint Window
= 0;
18 static GLuint TexObj
[2];
19 static GLfloat Angle
= 0.0f
;
20 static GLboolean UseObj
= GL_FALSE
;
23 #if defined(GL_VERSION_1_1) || defined(GL_VERSION_1_2)
24 # define TEXTURE_OBJECT 1
25 #elif defined(GL_EXT_texture_object)
26 # define TEXTURE_OBJECT 1
27 # define glBindTexture(A,B) glBindTextureEXT(A,B)
28 # define glGenTextures(A,B) glGenTexturesEXT(A,B)
29 # define glDeleteTextures(A,B) glDeleteTexturesEXT(A,B)
35 static void draw( void )
37 glClear( GL_COLOR_BUFFER_BIT
);
39 glColor3f( 1.0, 1.0, 1.0 );
41 /* draw first polygon */
43 glTranslatef( -1.0, 0.0, 0.0 );
44 glRotatef( Angle
, 0.0, 0.0, 1.0 );
47 glBindTexture( GL_TEXTURE_2D
, TexObj
[0] );
51 glCallList( TexObj
[0] );
53 glBegin( GL_POLYGON
);
54 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 );
55 glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 );
56 glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 );
57 glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 );
61 /* draw second polygon */
63 glTranslatef( 1.0, 0.0, 0.0 );
64 glRotatef( Angle
-90.0, 0.0, 1.0, 0.0 );
67 glBindTexture( GL_TEXTURE_2D
, TexObj
[1] );
71 glCallList( TexObj
[1] );
73 glBegin( GL_POLYGON
);
74 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 );
75 glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 );
76 glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 );
77 glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 );
86 static void idle( void )
94 /* change view Angle, exit upon ESC */
95 static void key(unsigned char k
, int x
, int y
)
101 #ifdef TEXTURE_OBJECT
102 glDeleteTextures( 2, TexObj
);
104 glutDestroyWindow(Window
);
111 /* new window size or exposure */
112 static void reshape( int width
, int height
)
114 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
115 glMatrixMode(GL_PROJECTION
);
117 /* glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/
118 glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 );
119 glMatrixMode(GL_MODELVIEW
);
121 glTranslatef( 0.0, 0.0, -8.0 );
125 static void init( void )
127 static int width
=8, height
=8;
128 static GLubyte tex1
[] = {
129 0, 0, 0, 0, 0, 0, 0, 0,
130 0, 0, 0, 0, 1, 0, 0, 0,
131 0, 0, 0, 1, 1, 0, 0, 0,
132 0, 0, 0, 0, 1, 0, 0, 0,
133 0, 0, 0, 0, 1, 0, 0, 0,
134 0, 0, 0, 0, 1, 0, 0, 0,
135 0, 0, 0, 1, 1, 1, 0, 0,
136 0, 0, 0, 0, 0, 0, 0, 0 };
138 static GLubyte tex2
[] = {
139 0, 0, 0, 0, 0, 0, 0, 0,
140 0, 0, 0, 2, 2, 0, 0, 0,
141 0, 0, 2, 0, 0, 2, 0, 0,
142 0, 0, 0, 0, 0, 2, 0, 0,
143 0, 0, 0, 0, 2, 0, 0, 0,
144 0, 0, 0, 2, 0, 0, 0, 0,
145 0, 0, 2, 2, 2, 2, 0, 0,
146 0, 0, 0, 0, 0, 0, 0, 0 };
152 glDisable( GL_DITHER
);
154 /* Setup texturing */
155 glEnable( GL_TEXTURE_2D
);
156 glTexEnvi( GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_DECAL
);
157 glHint( GL_PERSPECTIVE_CORRECTION_HINT
, GL_FASTEST
);
160 /* generate texture object IDs */
162 #ifdef TEXTURE_OBJECT
163 glGenTextures( 2, TexObj
);
167 TexObj
[0] = glGenLists(2);
168 TexObj
[1] = TexObj
[0]+1;
171 /* setup first texture object */
173 #ifdef TEXTURE_OBJECT
174 glBindTexture( GL_TEXTURE_2D
, TexObj
[0] );
175 assert(glIsTexture(TexObj
[0]));
179 glNewList( TexObj
[0], GL_COMPILE
);
182 for (i
=0;i
<height
;i
++) {
183 for (j
=0;j
<width
;j
++) {
185 if (tex1
[(height
-i
-1)*width
+j
]) {
186 tex
[p
][0] = 255; tex
[p
][1] = 0; tex
[p
][2] = 0;
189 tex
[p
][0] = 255; tex
[p
][1] = 255; tex
[p
][2] = 255;
194 glTexImage2D( GL_TEXTURE_2D
, 0, 3, width
, height
, 0,
195 GL_RGB
, GL_UNSIGNED_BYTE
, tex
);
196 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
197 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
198 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
199 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
203 /* end of texture object */
205 /* setup second texture object */
207 #ifdef TEXTURE_OBJECT
208 glBindTexture( GL_TEXTURE_2D
, TexObj
[1] );
209 assert(glIsTexture(TexObj
[1]));
211 assert(!glIsTexture(TexObj
[1] + 999));
214 glNewList( TexObj
[1], GL_COMPILE
);
217 for (i
=0;i
<height
;i
++) {
218 for (j
=0;j
<width
;j
++) {
220 if (tex2
[(height
-i
-1)*width
+j
]) {
221 tex
[p
][0] = 0; tex
[p
][1] = 255; tex
[p
][2] = 0;
224 tex
[p
][0] = 0; tex
[p
][1] = 0; tex
[p
][2] = 255;
228 glTexImage2D( GL_TEXTURE_2D
, 0, 3, width
, height
, 0,
229 GL_RGB
, GL_UNSIGNED_BYTE
, tex
);
230 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
231 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
232 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
233 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
237 /* end texture object */
243 int main( int argc
, char *argv
[] )
245 glutInit(&argc
, argv
);
246 glutInitWindowPosition(0, 0);
247 glutInitWindowSize(300, 300);
248 glutInitDisplayMode( GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
250 Window
= glutCreateWindow("Texture Objects");
255 /* check that renderer has the GL_EXT_texture_object extension
256 * or supports OpenGL 1.1
258 #ifdef TEXTURE_OBJECT
260 char *exten
= (char *) glGetString( GL_EXTENSIONS
);
261 char *version
= (char *) glGetString( GL_VERSION
);
262 if ( strstr( exten
, "GL_EXT_texture_object" )
263 || strncmp( version
, "1.1", 3 )==0
264 || strncmp( version
, "1.2", 3 )==0 ) {
272 glutReshapeFunc( reshape
);
273 glutKeyboardFunc( key
);
274 glutIdleFunc( idle
);
275 glutDisplayFunc( draw
);