132001f49Smrg
232001f49Smrg/*
332001f49Smrg * Example of using the 1.1 texture object functions.
432001f49Smrg * Also, this demo utilizes Mesa's fast texture map path.
532001f49Smrg *
632001f49Smrg * Brian Paul   June 1996   This file is in the public domain.
732001f49Smrg */
832001f49Smrg
932001f49Smrg#include <assert.h>
1032001f49Smrg#include <math.h>
1132001f49Smrg#include <stdio.h>
1232001f49Smrg#include <stdlib.h>
1332001f49Smrg#include <string.h>
1432001f49Smrg#include "glut_wrap.h"
1532001f49Smrg
1632001f49Smrgstatic GLuint Window = 0;
1732001f49Smrg
1832001f49Smrgstatic GLuint TexObj[2];
1932001f49Smrgstatic GLfloat Angle = 0.0f;
2032001f49Smrgstatic GLboolean UseObj = GL_FALSE;
2132001f49Smrg
2232001f49Smrg
2332001f49Smrg#if defined(GL_VERSION_1_1) || defined(GL_VERSION_1_2)
2432001f49Smrg#  define TEXTURE_OBJECT 1
2532001f49Smrg#elif defined(GL_EXT_texture_object)
2632001f49Smrg#  define TEXTURE_OBJECT 1
2732001f49Smrg#  define glBindTexture(A,B)     glBindTextureEXT(A,B)
2832001f49Smrg#  define glGenTextures(A,B)     glGenTexturesEXT(A,B)
2932001f49Smrg#  define glDeleteTextures(A,B)  glDeleteTexturesEXT(A,B)
3032001f49Smrg#endif
3132001f49Smrg
3232001f49Smrg
3332001f49Smrg
3432001f49Smrg
3532001f49Smrgstatic void draw( void )
3632001f49Smrg{
3732001f49Smrg   glClear( GL_COLOR_BUFFER_BIT );
3832001f49Smrg
3932001f49Smrg   glColor3f( 1.0, 1.0, 1.0 );
4032001f49Smrg
4132001f49Smrg   /* draw first polygon */
4232001f49Smrg   glPushMatrix();
4332001f49Smrg   glTranslatef( -1.0, 0.0, 0.0 );
4432001f49Smrg   glRotatef( Angle, 0.0, 0.0, 1.0 );
4532001f49Smrg   if (UseObj) {
4632001f49Smrg#ifdef TEXTURE_OBJECT
4732001f49Smrg      glBindTexture( GL_TEXTURE_2D, TexObj[0] );
4832001f49Smrg#endif
4932001f49Smrg   }
5032001f49Smrg   else {
5132001f49Smrg      glCallList( TexObj[0] );
5232001f49Smrg   }
5332001f49Smrg   glBegin( GL_POLYGON );
5432001f49Smrg   glTexCoord2f( 0.0, 0.0 );   glVertex2f( -1.0, -1.0 );
5532001f49Smrg   glTexCoord2f( 1.0, 0.0 );   glVertex2f(  1.0, -1.0 );
5632001f49Smrg   glTexCoord2f( 1.0, 1.0 );   glVertex2f(  1.0,  1.0 );
5732001f49Smrg   glTexCoord2f( 0.0, 1.0 );   glVertex2f( -1.0,  1.0 );
5832001f49Smrg   glEnd();
5932001f49Smrg   glPopMatrix();
6032001f49Smrg
6132001f49Smrg   /* draw second polygon */
6232001f49Smrg   glPushMatrix();
6332001f49Smrg   glTranslatef( 1.0, 0.0, 0.0 );
6432001f49Smrg   glRotatef( Angle-90.0, 0.0, 1.0, 0.0 );
6532001f49Smrg   if (UseObj) {
6632001f49Smrg#ifdef TEXTURE_OBJECT
6732001f49Smrg      glBindTexture( GL_TEXTURE_2D, TexObj[1] );
6832001f49Smrg#endif
6932001f49Smrg   }
7032001f49Smrg   else {
7132001f49Smrg      glCallList( TexObj[1] );
7232001f49Smrg   }
7332001f49Smrg   glBegin( GL_POLYGON );
7432001f49Smrg   glTexCoord2f( 0.0, 0.0 );   glVertex2f( -1.0, -1.0 );
7532001f49Smrg   glTexCoord2f( 1.0, 0.0 );   glVertex2f(  1.0, -1.0 );
7632001f49Smrg   glTexCoord2f( 1.0, 1.0 );   glVertex2f(  1.0,  1.0 );
7732001f49Smrg   glTexCoord2f( 0.0, 1.0 );   glVertex2f( -1.0,  1.0 );
7832001f49Smrg   glEnd();
7932001f49Smrg   glPopMatrix();
8032001f49Smrg
8132001f49Smrg   glutSwapBuffers();
8232001f49Smrg}
8332001f49Smrg
8432001f49Smrg
8532001f49Smrg
8632001f49Smrgstatic void idle( void )
8732001f49Smrg{
8832001f49Smrg   static double t0 = -1.;
8932001f49Smrg   double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
9032001f49Smrg   if (t0 < 0.0)
9132001f49Smrg      t0 = t;
9232001f49Smrg   dt = t - t0;
9332001f49Smrg   t0 = t;
9432001f49Smrg   Angle += 120.0*dt;
9532001f49Smrg   glutPostRedisplay();
9632001f49Smrg}
9732001f49Smrg
9832001f49Smrg
9932001f49Smrg
10032001f49Smrg/* change view Angle, exit upon ESC */
10132001f49Smrgstatic void key(unsigned char k, int x, int y)
10232001f49Smrg{
10332001f49Smrg   (void) x;
10432001f49Smrg   (void) y;
10532001f49Smrg   switch (k) {
10632001f49Smrg      case 27:
10732001f49Smrg#ifdef TEXTURE_OBJECT
10832001f49Smrg         glDeleteTextures( 2, TexObj );
10932001f49Smrg#endif
11032001f49Smrg         glutDestroyWindow(Window);
11132001f49Smrg         exit(0);
11232001f49Smrg   }
11332001f49Smrg}
11432001f49Smrg
11532001f49Smrg
11632001f49Smrg
11732001f49Smrg/* new window size or exposure */
11832001f49Smrgstatic void reshape( int width, int height )
11932001f49Smrg{
12032001f49Smrg   glViewport(0, 0, (GLint)width, (GLint)height);
12132001f49Smrg   glMatrixMode(GL_PROJECTION);
12232001f49Smrg   glLoadIdentity();
12332001f49Smrg   /*   glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/
12432001f49Smrg   glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 );
12532001f49Smrg   glMatrixMode(GL_MODELVIEW);
12632001f49Smrg   glLoadIdentity();
12732001f49Smrg   glTranslatef( 0.0, 0.0, -8.0 );
12832001f49Smrg}
12932001f49Smrg
13032001f49Smrg
13132001f49Smrgstatic void init( void )
13232001f49Smrg{
13332001f49Smrg   static int width=8, height=8;
13432001f49Smrg   static GLubyte tex1[] = {
13532001f49Smrg     0, 0, 0, 0, 0, 0, 0, 0,
13632001f49Smrg     0, 0, 0, 0, 1, 0, 0, 0,
13732001f49Smrg     0, 0, 0, 1, 1, 0, 0, 0,
13832001f49Smrg     0, 0, 0, 0, 1, 0, 0, 0,
13932001f49Smrg     0, 0, 0, 0, 1, 0, 0, 0,
14032001f49Smrg     0, 0, 0, 0, 1, 0, 0, 0,
14132001f49Smrg     0, 0, 0, 1, 1, 1, 0, 0,
14232001f49Smrg     0, 0, 0, 0, 0, 0, 0, 0 };
14332001f49Smrg
14432001f49Smrg   static GLubyte tex2[] = {
14532001f49Smrg     0, 0, 0, 0, 0, 0, 0, 0,
14632001f49Smrg     0, 0, 0, 2, 2, 0, 0, 0,
14732001f49Smrg     0, 0, 2, 0, 0, 2, 0, 0,
14832001f49Smrg     0, 0, 0, 0, 0, 2, 0, 0,
14932001f49Smrg     0, 0, 0, 0, 2, 0, 0, 0,
15032001f49Smrg     0, 0, 0, 2, 0, 0, 0, 0,
15132001f49Smrg     0, 0, 2, 2, 2, 2, 0, 0,
15232001f49Smrg     0, 0, 0, 0, 0, 0, 0, 0 };
15332001f49Smrg
15432001f49Smrg   GLubyte tex[64][3];
15532001f49Smrg   GLint i, j;
15632001f49Smrg
15732001f49Smrg
15832001f49Smrg   glDisable( GL_DITHER );
15932001f49Smrg
16032001f49Smrg   /* Setup texturing */
16132001f49Smrg   glEnable( GL_TEXTURE_2D );
16232001f49Smrg   glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
16332001f49Smrg   glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
16432001f49Smrg
16532001f49Smrg
16632001f49Smrg   /* generate texture object IDs */
16732001f49Smrg   if (UseObj) {
16832001f49Smrg#ifdef TEXTURE_OBJECT
16932001f49Smrg      glGenTextures( 2, TexObj );
17032001f49Smrg#endif
17132001f49Smrg   }
17232001f49Smrg   else {
17332001f49Smrg      TexObj[0] = glGenLists(2);
17432001f49Smrg      TexObj[1] = TexObj[0]+1;
17532001f49Smrg   }
17632001f49Smrg
17732001f49Smrg   /* setup first texture object */
17832001f49Smrg   if (UseObj) {
17932001f49Smrg#ifdef TEXTURE_OBJECT
18032001f49Smrg      glBindTexture( GL_TEXTURE_2D, TexObj[0] );
18132001f49Smrg      assert(glIsTexture(TexObj[0]));
18232001f49Smrg#endif
18332001f49Smrg   }
18432001f49Smrg   else {
18532001f49Smrg      glNewList( TexObj[0], GL_COMPILE );
18632001f49Smrg   }
18732001f49Smrg   /* red on white */
18832001f49Smrg   for (i=0;i<height;i++) {
18932001f49Smrg      for (j=0;j<width;j++) {
19032001f49Smrg         int p = i*width+j;
19132001f49Smrg         if (tex1[(height-i-1)*width+j]) {
19232001f49Smrg            tex[p][0] = 255;   tex[p][1] = 0;     tex[p][2] = 0;
19332001f49Smrg         }
19432001f49Smrg         else {
19532001f49Smrg            tex[p][0] = 255;   tex[p][1] = 255;   tex[p][2] = 255;
19632001f49Smrg         }
19732001f49Smrg      }
19832001f49Smrg   }
19932001f49Smrg
20032001f49Smrg   glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
20132001f49Smrg                 GL_RGB, GL_UNSIGNED_BYTE, tex );
20232001f49Smrg   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
20332001f49Smrg   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
20432001f49Smrg   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
20532001f49Smrg   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
20632001f49Smrg   if (!UseObj) {
20732001f49Smrg      glEndList();
20832001f49Smrg   }
20932001f49Smrg   /* end of texture object */
21032001f49Smrg
21132001f49Smrg   /* setup second texture object */
21232001f49Smrg   if (UseObj) {
21332001f49Smrg#ifdef TEXTURE_OBJECT
21432001f49Smrg      glBindTexture( GL_TEXTURE_2D, TexObj[1] );
21532001f49Smrg      assert(glIsTexture(TexObj[1]));
21632001f49Smrg#endif
21732001f49Smrg      assert(!glIsTexture(TexObj[1] + 999));
21832001f49Smrg   }
21932001f49Smrg   else {
22032001f49Smrg      glNewList( TexObj[1], GL_COMPILE );
22132001f49Smrg   }
22232001f49Smrg   /* green on blue */
22332001f49Smrg   for (i=0;i<height;i++) {
22432001f49Smrg      for (j=0;j<width;j++) {
22532001f49Smrg         int p = i*width+j;
22632001f49Smrg         if (tex2[(height-i-1)*width+j]) {
22732001f49Smrg            tex[p][0] = 0;     tex[p][1] = 255;   tex[p][2] = 0;
22832001f49Smrg         }
22932001f49Smrg         else {
23032001f49Smrg            tex[p][0] = 0;     tex[p][1] = 0;     tex[p][2] = 255;
23132001f49Smrg         }
23232001f49Smrg      }
23332001f49Smrg   }
23432001f49Smrg   glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
23532001f49Smrg                 GL_RGB, GL_UNSIGNED_BYTE, tex );
23632001f49Smrg   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
23732001f49Smrg   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
23832001f49Smrg   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
23932001f49Smrg   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
24032001f49Smrg   if (!UseObj) {
24132001f49Smrg      glEndList();
24232001f49Smrg   }
24332001f49Smrg   /* end texture object */
24432001f49Smrg
24532001f49Smrg}
24632001f49Smrg
24732001f49Smrg
24832001f49Smrg
24932001f49Smrgint main( int argc, char *argv[] )
25032001f49Smrg{
25132001f49Smrg   glutInit(&argc, argv);
25232001f49Smrg   glutInitWindowPosition(0, 0);
25332001f49Smrg   glutInitWindowSize(300, 300);
25432001f49Smrg   glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
25532001f49Smrg
25632001f49Smrg   Window = glutCreateWindow("Texture Objects");
25732001f49Smrg   if (!Window) {
25832001f49Smrg      exit(1);
25932001f49Smrg   }
26032001f49Smrg
26132001f49Smrg   /* check that renderer has the GL_EXT_texture_object extension
26232001f49Smrg    * or supports OpenGL 1.1
26332001f49Smrg    */
26432001f49Smrg#ifdef TEXTURE_OBJECT
26532001f49Smrg   {
26632001f49Smrg      char *exten = (char *) glGetString( GL_EXTENSIONS );
26732001f49Smrg      char *version = (char *) glGetString( GL_VERSION );
26832001f49Smrg      if (   strstr( exten, "GL_EXT_texture_object" )
26932001f49Smrg          || strncmp( version, "1.1", 3 )==0
27032001f49Smrg          || strncmp( version, "1.2", 3 )==0 ) {
27132001f49Smrg         UseObj = GL_TRUE;
27232001f49Smrg      }
27332001f49Smrg   }
27432001f49Smrg#endif
27532001f49Smrg
27632001f49Smrg   init();
27732001f49Smrg
27832001f49Smrg   glutReshapeFunc( reshape );
27932001f49Smrg   glutKeyboardFunc( key );
28032001f49Smrg   glutIdleFunc( idle );
28132001f49Smrg   glutDisplayFunc( draw );
28232001f49Smrg   glutMainLoop();
28332001f49Smrg   return 0;
28432001f49Smrg}
285