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 "glut_wrap.h"
15
16static GLuint Window = 0;
17
18static GLuint TexObj[2];
19static GLfloat Angle = 0.0f;
20static 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
35static void draw( void )
36{
37   glClear( GL_COLOR_BUFFER_BIT );
38
39   glColor3f( 1.0, 1.0, 1.0 );
40
41   /* draw first polygon */
42   glPushMatrix();
43   glTranslatef( -1.0, 0.0, 0.0 );
44   glRotatef( Angle, 0.0, 0.0, 1.0 );
45   if (UseObj) {
46#ifdef TEXTURE_OBJECT
47      glBindTexture( GL_TEXTURE_2D, TexObj[0] );
48#endif
49   }
50   else {
51      glCallList( TexObj[0] );
52   }
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 );
58   glEnd();
59   glPopMatrix();
60
61   /* draw second polygon */
62   glPushMatrix();
63   glTranslatef( 1.0, 0.0, 0.0 );
64   glRotatef( Angle-90.0, 0.0, 1.0, 0.0 );
65   if (UseObj) {
66#ifdef TEXTURE_OBJECT
67      glBindTexture( GL_TEXTURE_2D, TexObj[1] );
68#endif
69   }
70   else {
71      glCallList( TexObj[1] );
72   }
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 );
78   glEnd();
79   glPopMatrix();
80
81   glutSwapBuffers();
82}
83
84
85
86static void idle( void )
87{
88   static double t0 = -1.;
89   double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
90   if (t0 < 0.0)
91      t0 = t;
92   dt = t - t0;
93   t0 = t;
94   Angle += 120.0*dt;
95   glutPostRedisplay();
96}
97
98
99
100/* change view Angle, exit upon ESC */
101static void key(unsigned char k, int x, int y)
102{
103   (void) x;
104   (void) y;
105   switch (k) {
106      case 27:
107#ifdef TEXTURE_OBJECT
108         glDeleteTextures( 2, TexObj );
109#endif
110         glutDestroyWindow(Window);
111         exit(0);
112   }
113}
114
115
116
117/* new window size or exposure */
118static void reshape( int width, int height )
119{
120   glViewport(0, 0, (GLint)width, (GLint)height);
121   glMatrixMode(GL_PROJECTION);
122   glLoadIdentity();
123   /*   glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/
124   glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 );
125   glMatrixMode(GL_MODELVIEW);
126   glLoadIdentity();
127   glTranslatef( 0.0, 0.0, -8.0 );
128}
129
130
131static void init( void )
132{
133   static int width=8, height=8;
134   static GLubyte tex1[] = {
135     0, 0, 0, 0, 0, 0, 0, 0,
136     0, 0, 0, 0, 1, 0, 0, 0,
137     0, 0, 0, 1, 1, 0, 0, 0,
138     0, 0, 0, 0, 1, 0, 0, 0,
139     0, 0, 0, 0, 1, 0, 0, 0,
140     0, 0, 0, 0, 1, 0, 0, 0,
141     0, 0, 0, 1, 1, 1, 0, 0,
142     0, 0, 0, 0, 0, 0, 0, 0 };
143
144   static GLubyte tex2[] = {
145     0, 0, 0, 0, 0, 0, 0, 0,
146     0, 0, 0, 2, 2, 0, 0, 0,
147     0, 0, 2, 0, 0, 2, 0, 0,
148     0, 0, 0, 0, 0, 2, 0, 0,
149     0, 0, 0, 0, 2, 0, 0, 0,
150     0, 0, 0, 2, 0, 0, 0, 0,
151     0, 0, 2, 2, 2, 2, 0, 0,
152     0, 0, 0, 0, 0, 0, 0, 0 };
153
154   GLubyte tex[64][3];
155   GLint i, j;
156
157
158   glDisable( GL_DITHER );
159
160   /* Setup texturing */
161   glEnable( GL_TEXTURE_2D );
162   glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
163   glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
164
165
166   /* generate texture object IDs */
167   if (UseObj) {
168#ifdef TEXTURE_OBJECT
169      glGenTextures( 2, TexObj );
170#endif
171   }
172   else {
173      TexObj[0] = glGenLists(2);
174      TexObj[1] = TexObj[0]+1;
175   }
176
177   /* setup first texture object */
178   if (UseObj) {
179#ifdef TEXTURE_OBJECT
180      glBindTexture( GL_TEXTURE_2D, TexObj[0] );
181      assert(glIsTexture(TexObj[0]));
182#endif
183   }
184   else {
185      glNewList( TexObj[0], GL_COMPILE );
186   }
187   /* red on white */
188   for (i=0;i<height;i++) {
189      for (j=0;j<width;j++) {
190         int p = i*width+j;
191         if (tex1[(height-i-1)*width+j]) {
192            tex[p][0] = 255;   tex[p][1] = 0;     tex[p][2] = 0;
193         }
194         else {
195            tex[p][0] = 255;   tex[p][1] = 255;   tex[p][2] = 255;
196         }
197      }
198   }
199
200   glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
201                 GL_RGB, GL_UNSIGNED_BYTE, tex );
202   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
203   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
204   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
205   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
206   if (!UseObj) {
207      glEndList();
208   }
209   /* end of texture object */
210
211   /* setup second texture object */
212   if (UseObj) {
213#ifdef TEXTURE_OBJECT
214      glBindTexture( GL_TEXTURE_2D, TexObj[1] );
215      assert(glIsTexture(TexObj[1]));
216#endif
217      assert(!glIsTexture(TexObj[1] + 999));
218   }
219   else {
220      glNewList( TexObj[1], GL_COMPILE );
221   }
222   /* green on blue */
223   for (i=0;i<height;i++) {
224      for (j=0;j<width;j++) {
225         int p = i*width+j;
226         if (tex2[(height-i-1)*width+j]) {
227            tex[p][0] = 0;     tex[p][1] = 255;   tex[p][2] = 0;
228         }
229         else {
230            tex[p][0] = 0;     tex[p][1] = 0;     tex[p][2] = 255;
231         }
232      }
233   }
234   glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
235                 GL_RGB, GL_UNSIGNED_BYTE, tex );
236   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
237   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
238   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
239   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
240   if (!UseObj) {
241      glEndList();
242   }
243   /* end texture object */
244
245}
246
247
248
249int main( int argc, char *argv[] )
250{
251   glutInit(&argc, argv);
252   glutInitWindowPosition(0, 0);
253   glutInitWindowSize(300, 300);
254   glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
255
256   Window = glutCreateWindow("Texture Objects");
257   if (!Window) {
258      exit(1);
259   }
260
261   /* check that renderer has the GL_EXT_texture_object extension
262    * or supports OpenGL 1.1
263    */
264#ifdef TEXTURE_OBJECT
265   {
266      char *exten = (char *) glGetString( GL_EXTENSIONS );
267      char *version = (char *) glGetString( GL_VERSION );
268      if (   strstr( exten, "GL_EXT_texture_object" )
269          || strncmp( version, "1.1", 3 )==0
270          || strncmp( version, "1.2", 3 )==0 ) {
271         UseObj = GL_TRUE;
272      }
273   }
274#endif
275
276   init();
277
278   glutReshapeFunc( reshape );
279   glutKeyboardFunc( key );
280   glutIdleFunc( idle );
281   glutDisplayFunc( draw );
282   glutMainLoop();
283   return 0;
284}
285