ff701e98c3b4bd5de6a38bbd88880301d595658f
[mesa.git] / progs / demos / texobj.c
1
2 /*
3 * Example of using the 1.1 texture object functions.
4 * Also, this demo utilizes Mesa's fast texture map path.
5 *
6 * Brian Paul June 1996 This file is in the public domain.
7 */
8
9 #include <assert.h>
10 #include <math.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include "GL/glut.h"
15
16 static GLuint Window = 0;
17
18 static GLuint TexObj[2];
19 static GLfloat Angle = 0.0f;
20 static GLboolean UseObj = GL_FALSE;
21
22
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)
30 #endif
31
32
33
34
35 static void draw( void )
36 {
37 glDepthFunc(GL_EQUAL);
38 /* glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );*/
39 glClear( GL_COLOR_BUFFER_BIT );
40
41 glColor3f( 1.0, 1.0, 1.0 );
42
43 /* draw first polygon */
44 glPushMatrix();
45 glTranslatef( -1.0, 0.0, 0.0 );
46 glRotatef( Angle, 0.0, 0.0, 1.0 );
47 if (UseObj) {
48 #ifdef TEXTURE_OBJECT
49 glBindTexture( GL_TEXTURE_2D, TexObj[0] );
50 #endif
51 }
52 else {
53 glCallList( TexObj[0] );
54 }
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 );
60 glEnd();
61 glPopMatrix();
62
63 /* draw second polygon */
64 glPushMatrix();
65 glTranslatef( 1.0, 0.0, 0.0 );
66 glRotatef( Angle-90.0, 0.0, 1.0, 0.0 );
67 if (UseObj) {
68 #ifdef TEXTURE_OBJECT
69 glBindTexture( GL_TEXTURE_2D, TexObj[1] );
70 #endif
71 }
72 else {
73 glCallList( TexObj[1] );
74 }
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 );
80 glEnd();
81 glPopMatrix();
82
83 glutSwapBuffers();
84 }
85
86
87
88 static void idle( void )
89 {
90 Angle += 2.0;
91 glutPostRedisplay();
92 }
93
94
95
96 /* change view Angle, exit upon ESC */
97 static void key(unsigned char k, int x, int y)
98 {
99 (void) x;
100 (void) y;
101 switch (k) {
102 case 27:
103 #ifdef TEXTURE_OBJECT
104 glDeleteTextures( 2, TexObj );
105 #endif
106 glutDestroyWindow(Window);
107 exit(0);
108 }
109 }
110
111
112
113 /* new window size or exposure */
114 static void reshape( int width, int height )
115 {
116 glViewport(0, 0, (GLint)width, (GLint)height);
117 glMatrixMode(GL_PROJECTION);
118 glLoadIdentity();
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);
122 glLoadIdentity();
123 glTranslatef( 0.0, 0.0, -8.0 );
124 }
125
126
127 static void init( void )
128 {
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 };
139
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 };
149
150 GLubyte tex[64][3];
151 GLint i, j;
152
153
154 glDisable( GL_DITHER );
155
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 );
160
161
162 /* generate texture object IDs */
163 if (UseObj) {
164 #ifdef TEXTURE_OBJECT
165 glGenTextures( 2, TexObj );
166 #endif
167 }
168 else {
169 TexObj[0] = glGenLists(2);
170 TexObj[1] = TexObj[0]+1;
171 }
172
173 /* setup first texture object */
174 if (UseObj) {
175 #ifdef TEXTURE_OBJECT
176 glBindTexture( GL_TEXTURE_2D, TexObj[0] );
177 assert(glIsTexture(TexObj[0]));
178 #endif
179 }
180 else {
181 glNewList( TexObj[0], GL_COMPILE );
182 }
183 /* red on white */
184 for (i=0;i<height;i++) {
185 for (j=0;j<width;j++) {
186 int p = i*width+j;
187 if (tex1[(height-i-1)*width+j]) {
188 tex[p][0] = 255; tex[p][1] = 0; tex[p][2] = 0;
189 }
190 else {
191 tex[p][0] = 255; tex[p][1] = 255; tex[p][2] = 255;
192 }
193 }
194 }
195
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 );
202 if (!UseObj) {
203 glEndList();
204 }
205 /* end of texture object */
206
207 /* setup second texture object */
208 if (UseObj) {
209 #ifdef TEXTURE_OBJECT
210 glBindTexture( GL_TEXTURE_2D, TexObj[1] );
211 assert(glIsTexture(TexObj[1]));
212 #endif
213 assert(!glIsTexture(TexObj[1] + 999));
214 }
215 else {
216 glNewList( TexObj[1], GL_COMPILE );
217 }
218 /* green on blue */
219 for (i=0;i<height;i++) {
220 for (j=0;j<width;j++) {
221 int p = i*width+j;
222 if (tex2[(height-i-1)*width+j]) {
223 tex[p][0] = 0; tex[p][1] = 255; tex[p][2] = 0;
224 }
225 else {
226 tex[p][0] = 0; tex[p][1] = 0; tex[p][2] = 255;
227 }
228 }
229 }
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 );
236 if (!UseObj) {
237 glEndList();
238 }
239 /* end texture object */
240
241 }
242
243
244
245 int main( int argc, char *argv[] )
246 {
247 glutInit(&argc, argv);
248 glutInitWindowPosition(0, 0);
249 glutInitWindowSize(300, 300);
250 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
251
252 Window = glutCreateWindow("Texture Objects");
253 if (!Window) {
254 exit(1);
255 }
256
257 /* check that renderer has the GL_EXT_texture_object extension
258 * or supports OpenGL 1.1
259 */
260 #ifdef TEXTURE_OBJECT
261 {
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 ) {
267 UseObj = GL_TRUE;
268 }
269 }
270 #endif
271
272 init();
273
274 glutReshapeFunc( reshape );
275 glutKeyboardFunc( key );
276 glutIdleFunc( idle );
277 glutDisplayFunc( draw );
278 glutMainLoop();
279 return 0;
280 }