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 glDepthFunc(GL_EQUAL
);
38 /* glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );*/
39 glClear( GL_COLOR_BUFFER_BIT
);
41 glColor3f( 1.0, 1.0, 1.0 );
43 /* draw first polygon */
45 glTranslatef( -1.0, 0.0, 0.0 );
46 glRotatef( Angle
, 0.0, 0.0, 1.0 );
49 glBindTexture( GL_TEXTURE_2D
, TexObj
[0] );
53 glCallList( TexObj
[0] );
55 glBegin( GL_POLYGON
);
56 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 );
57 glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 );
58 glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 );
59 glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 );
63 /* draw second polygon */
65 glTranslatef( 1.0, 0.0, 0.0 );
66 glRotatef( Angle
-90.0, 0.0, 1.0, 0.0 );
69 glBindTexture( GL_TEXTURE_2D
, TexObj
[1] );
73 glCallList( TexObj
[1] );
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 );
88 static void idle( void )
96 /* change view Angle, exit upon ESC */
97 static void key(unsigned char k
, int x
, int y
)
103 #ifdef TEXTURE_OBJECT
104 glDeleteTextures( 2, TexObj
);
106 glutDestroyWindow(Window
);
113 /* new window size or exposure */
114 static void reshape( int width
, int height
)
116 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
117 glMatrixMode(GL_PROJECTION
);
119 /* glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/
120 glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 );
121 glMatrixMode(GL_MODELVIEW
);
123 glTranslatef( 0.0, 0.0, -8.0 );
127 static void init( void )
129 static int width
=8, height
=8;
130 static GLubyte tex1
[] = {
131 0, 0, 0, 0, 0, 0, 0, 0,
132 0, 0, 0, 0, 1, 0, 0, 0,
133 0, 0, 0, 1, 1, 0, 0, 0,
134 0, 0, 0, 0, 1, 0, 0, 0,
135 0, 0, 0, 0, 1, 0, 0, 0,
136 0, 0, 0, 0, 1, 0, 0, 0,
137 0, 0, 0, 1, 1, 1, 0, 0,
138 0, 0, 0, 0, 0, 0, 0, 0 };
140 static GLubyte tex2
[] = {
141 0, 0, 0, 0, 0, 0, 0, 0,
142 0, 0, 0, 2, 2, 0, 0, 0,
143 0, 0, 2, 0, 0, 2, 0, 0,
144 0, 0, 0, 0, 0, 2, 0, 0,
145 0, 0, 0, 0, 2, 0, 0, 0,
146 0, 0, 0, 2, 0, 0, 0, 0,
147 0, 0, 2, 2, 2, 2, 0, 0,
148 0, 0, 0, 0, 0, 0, 0, 0 };
154 glDisable( GL_DITHER
);
156 /* Setup texturing */
157 glEnable( GL_TEXTURE_2D
);
158 glTexEnvi( GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_DECAL
);
159 glHint( GL_PERSPECTIVE_CORRECTION_HINT
, GL_FASTEST
);
162 /* generate texture object IDs */
164 #ifdef TEXTURE_OBJECT
165 glGenTextures( 2, TexObj
);
169 TexObj
[0] = glGenLists(2);
170 TexObj
[1] = TexObj
[0]+1;
173 /* setup first texture object */
175 #ifdef TEXTURE_OBJECT
176 glBindTexture( GL_TEXTURE_2D
, TexObj
[0] );
177 assert(glIsTexture(TexObj
[0]));
181 glNewList( TexObj
[0], GL_COMPILE
);
184 for (i
=0;i
<height
;i
++) {
185 for (j
=0;j
<width
;j
++) {
187 if (tex1
[(height
-i
-1)*width
+j
]) {
188 tex
[p
][0] = 255; tex
[p
][1] = 0; tex
[p
][2] = 0;
191 tex
[p
][0] = 255; tex
[p
][1] = 255; tex
[p
][2] = 255;
196 glTexImage2D( GL_TEXTURE_2D
, 0, 3, width
, height
, 0,
197 GL_RGB
, GL_UNSIGNED_BYTE
, tex
);
198 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
199 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
200 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
201 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
205 /* end of texture object */
207 /* setup second texture object */
209 #ifdef TEXTURE_OBJECT
210 glBindTexture( GL_TEXTURE_2D
, TexObj
[1] );
211 assert(glIsTexture(TexObj
[1]));
213 assert(!glIsTexture(TexObj
[1] + 999));
216 glNewList( TexObj
[1], GL_COMPILE
);
219 for (i
=0;i
<height
;i
++) {
220 for (j
=0;j
<width
;j
++) {
222 if (tex2
[(height
-i
-1)*width
+j
]) {
223 tex
[p
][0] = 0; tex
[p
][1] = 255; tex
[p
][2] = 0;
226 tex
[p
][0] = 0; tex
[p
][1] = 0; tex
[p
][2] = 255;
230 glTexImage2D( GL_TEXTURE_2D
, 0, 3, width
, height
, 0,
231 GL_RGB
, GL_UNSIGNED_BYTE
, tex
);
232 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
233 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
234 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
235 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
239 /* end texture object */
245 int main( int argc
, char *argv
[] )
247 glutInit(&argc
, argv
);
248 glutInitWindowPosition(0, 0);
249 glutInitWindowSize(300, 300);
250 glutInitDisplayMode( GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
252 Window
= glutCreateWindow("Texture Objects");
257 /* check that renderer has the GL_EXT_texture_object extension
258 * or supports OpenGL 1.1
260 #ifdef TEXTURE_OBJECT
262 char *exten
= (char *) glGetString( GL_EXTENSIONS
);
263 char *version
= (char *) glGetString( GL_VERSION
);
264 if ( strstr( exten
, "GL_EXT_texture_object" )
265 || strncmp( version
, "1.1", 3 )==0
266 || strncmp( version
, "1.2", 3 )==0 ) {
274 glutReshapeFunc( reshape
);
275 glutKeyboardFunc( key
);
276 glutIdleFunc( idle
);
277 glutDisplayFunc( draw
);