1 /* $Id: texobj.c,v 1.1 1999/08/19 00:55:40 jtg Exp $ */
4 * Example of using the 1.1 texture object functions.
5 * Also, this demo utilizes Mesa's fast texture map path.
7 * Brian Paul June 1996 This file is in the public domain.
13 * Revision 1.1 1999/08/19 00:55:40 jtg
16 * Revision 3.1 1999/03/28 18:24:37 brianp
19 * Revision 3.0 1998/02/14 18:42:29 brianp
30 static GLuint Window
= 0;
32 static GLuint TexObj
[2];
33 static GLfloat Angle
= 0.0f
;
34 static GLboolean HaveTexObj
= GL_FALSE
;
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)
49 static void draw( void )
51 glDepthFunc(GL_EQUAL
);
52 /* glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );*/
53 glClear( GL_COLOR_BUFFER_BIT
);
55 glColor3f( 1.0, 1.0, 1.0 );
57 /* draw first polygon */
59 glTranslatef( -1.0, 0.0, 0.0 );
60 glRotatef( Angle
, 0.0, 0.0, 1.0 );
63 glBindTexture( GL_TEXTURE_2D
, TexObj
[0] );
67 glCallList( TexObj
[0] );
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 );
77 /* draw second polygon */
79 glTranslatef( 1.0, 0.0, 0.0 );
80 glRotatef( Angle
-90.0, 0.0, 1.0, 0.0 );
83 glBindTexture( GL_TEXTURE_2D
, TexObj
[1] );
87 glCallList( TexObj
[0] );
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 );
102 static void idle( void )
110 /* change view Angle, exit upon ESC */
111 static void key(unsigned char k
, int x
, int y
)
117 #ifdef TEXTURE_OBJECT
118 glDeleteTextures( 2, TexObj
);
120 glutDestroyWindow(Window
);
127 /* new window size or exposure */
128 static void reshape( int width
, int height
)
130 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
131 glMatrixMode(GL_PROJECTION
);
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
);
137 glTranslatef( 0.0, 0.0, -8.0 );
141 static void init( void )
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 };
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 };
168 glDisable( GL_DITHER
);
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
);
176 /* generate texture object IDs */
178 #ifdef TEXTURE_OBJECT
179 glGenTextures( 2, TexObj
);
183 TexObj
[0] = glGenLists(2);
184 TexObj
[1] = TexObj
[0]+1;
187 /* setup first texture object */
189 #ifdef TEXTURE_OBJECT
190 glBindTexture( GL_TEXTURE_2D
, TexObj
[0] );
194 glNewList( TexObj
[0], GL_COMPILE
);
197 for (i
=0;i
<height
;i
++) {
198 for (j
=0;j
<width
;j
++) {
200 if (tex1
[(height
-i
-1)*width
+j
]) {
201 tex
[p
][0] = 255; tex
[p
][1] = 0; tex
[p
][2] = 0;
204 tex
[p
][0] = 255; tex
[p
][1] = 255; tex
[p
][2] = 255;
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
);
218 /* end of texture object */
220 /* setup second texture object */
222 #ifdef TEXTURE_OBJECT
223 glBindTexture( GL_TEXTURE_2D
, TexObj
[1] );
227 glNewList( TexObj
[1], GL_COMPILE
);
230 for (i
=0;i
<height
;i
++) {
231 for (j
=0;j
<width
;j
++) {
233 if (tex2
[(height
-i
-1)*width
+j
]) {
234 tex
[p
][0] = 0; tex
[p
][1] = 255; tex
[p
][2] = 0;
237 tex
[p
][0] = 0; tex
[p
][1] = 0; tex
[p
][2] = 255;
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
);
250 /* end texture object */
256 int main( int argc
, char *argv
[] )
258 glutInitWindowPosition(0, 0);
259 glutInitWindowSize(300, 300);
260 glutInitDisplayMode( GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
262 Window
= glutCreateWindow("Texture Objects");
267 /* check that renderer has the GL_EXT_texture_object extension
268 * or supports OpenGL 1.1
270 #ifdef TEXTURE_OBJECT
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
;
283 glutReshapeFunc( reshape
);
284 glutKeyboardFunc( key
);
285 glutIdleFunc( idle
);
286 glutDisplayFunc( draw
);