1 /* $Id: texobj.c,v 1.5 2000/07/19 23:57:24 brianp 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.
15 static GLuint Window
= 0;
17 static GLuint TexObj
[2];
18 static GLfloat Angle
= 0.0f
;
19 static GLboolean UseObj
= GL_FALSE
;
22 #if defined(GL_VERSION_1_1) || defined(GL_VERSION_1_2)
23 # define TEXTURE_OBJECT 1
24 #elif defined(GL_EXT_texture_object)
25 # define TEXTURE_OBJECT 1
26 # define glBindTexture(A,B) glBindTextureEXT(A,B)
27 # define glGenTextures(A,B) glGenTexturesEXT(A,B)
28 # define glDeleteTextures(A,B) glDeleteTexturesEXT(A,B)
34 static void draw( void )
36 glDepthFunc(GL_EQUAL
);
37 /* glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );*/
38 glClear( GL_COLOR_BUFFER_BIT
);
40 glColor3f( 1.0, 1.0, 1.0 );
42 /* draw first polygon */
44 glTranslatef( -1.0, 0.0, 0.0 );
45 glRotatef( Angle
, 0.0, 0.0, 1.0 );
48 glBindTexture( GL_TEXTURE_2D
, TexObj
[0] );
52 glCallList( TexObj
[0] );
54 glBegin( GL_POLYGON
);
55 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 );
56 glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 );
57 glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 );
58 glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 );
62 /* draw second polygon */
64 glTranslatef( 1.0, 0.0, 0.0 );
65 glRotatef( Angle
-90.0, 0.0, 1.0, 0.0 );
68 glBindTexture( GL_TEXTURE_2D
, TexObj
[1] );
72 glCallList( TexObj
[1] );
74 glBegin( GL_POLYGON
);
75 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 );
76 glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 );
77 glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 );
78 glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 );
87 static void idle( void )
95 /* change view Angle, exit upon ESC */
96 static void key(unsigned char k
, int x
, int y
)
102 #ifdef TEXTURE_OBJECT
103 glDeleteTextures( 2, TexObj
);
105 glutDestroyWindow(Window
);
112 /* new window size or exposure */
113 static void reshape( int width
, int height
)
115 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
116 glMatrixMode(GL_PROJECTION
);
118 /* glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/
119 glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 );
120 glMatrixMode(GL_MODELVIEW
);
122 glTranslatef( 0.0, 0.0, -8.0 );
126 static void init( void )
128 static int width
=8, height
=8;
129 static GLubyte tex1
[] = {
130 0, 0, 0, 0, 0, 0, 0, 0,
131 0, 0, 0, 0, 1, 0, 0, 0,
132 0, 0, 0, 1, 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, 0, 1, 0, 0, 0,
136 0, 0, 0, 1, 1, 1, 0, 0,
137 0, 0, 0, 0, 0, 0, 0, 0 };
139 static GLubyte tex2
[] = {
140 0, 0, 0, 0, 0, 0, 0, 0,
141 0, 0, 0, 2, 2, 0, 0, 0,
142 0, 0, 2, 0, 0, 2, 0, 0,
143 0, 0, 0, 0, 0, 2, 0, 0,
144 0, 0, 0, 0, 2, 0, 0, 0,
145 0, 0, 0, 2, 0, 0, 0, 0,
146 0, 0, 2, 2, 2, 2, 0, 0,
147 0, 0, 0, 0, 0, 0, 0, 0 };
153 glDisable( GL_DITHER
);
155 /* Setup texturing */
156 glEnable( GL_TEXTURE_2D
);
157 glTexEnvi( GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_DECAL
);
158 glHint( GL_PERSPECTIVE_CORRECTION_HINT
, GL_FASTEST
);
161 /* generate texture object IDs */
163 #ifdef TEXTURE_OBJECT
164 glGenTextures( 2, TexObj
);
168 TexObj
[0] = glGenLists(2);
169 TexObj
[1] = TexObj
[0]+1;
172 /* setup first texture object */
174 #ifdef TEXTURE_OBJECT
175 glBindTexture( GL_TEXTURE_2D
, 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] );
212 glNewList( TexObj
[1], GL_COMPILE
);
215 for (i
=0;i
<height
;i
++) {
216 for (j
=0;j
<width
;j
++) {
218 if (tex2
[(height
-i
-1)*width
+j
]) {
219 tex
[p
][0] = 0; tex
[p
][1] = 255; tex
[p
][2] = 0;
222 tex
[p
][0] = 0; tex
[p
][1] = 0; tex
[p
][2] = 255;
226 glTexImage2D( GL_TEXTURE_2D
, 0, 3, width
, height
, 0,
227 GL_RGB
, GL_UNSIGNED_BYTE
, tex
);
228 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
229 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
230 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
231 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
235 /* end texture object */
241 int main( int argc
, char *argv
[] )
243 glutInit(&argc
, argv
);
244 glutInitWindowPosition(0, 0);
245 glutInitWindowSize(300, 300);
246 glutInitDisplayMode( GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
248 Window
= glutCreateWindow("Texture Objects");
253 /* check that renderer has the GL_EXT_texture_object extension
254 * or supports OpenGL 1.1
256 #ifdef TEXTURE_OBJECT
258 char *exten
= (char *) glGetString( GL_EXTENSIONS
);
259 char *version
= (char *) glGetString( GL_VERSION
);
260 if ( strstr( exten
, "GL_EXT_texture_object" )
261 || strncmp( version
, "1.1", 3 )==0
262 || strncmp( version
, "1.2", 3 )==0 ) {
270 glutReshapeFunc( reshape
);
271 glutKeyboardFunc( key
);
272 glutIdleFunc( idle
);
273 glutDisplayFunc( draw
);