132001f49Smrg
232001f49Smrg/* Exercise 1D textures
332001f49Smrg */
432001f49Smrg
532001f49Smrg#include <assert.h>
632001f49Smrg#include <math.h>
732001f49Smrg#include <stdio.h>
832001f49Smrg#include <stdlib.h>
932001f49Smrg#include <string.h>
1032001f49Smrg#include "GL/glew.h"
1132001f49Smrg#include "glut_wrap.h"
1232001f49Smrg
1332001f49Smrgstatic GLuint Window = 0;
1432001f49Smrgstatic GLuint TexObj[2];
1532001f49Smrgstatic GLfloat Angle = 0.0f;
1632001f49Smrg
1732001f49Smrg
1832001f49Smrgstatic void draw( void )
1932001f49Smrg{
2032001f49Smrg   glClear( GL_COLOR_BUFFER_BIT );
2132001f49Smrg
2232001f49Smrg   glColor3f( 1.0, 1.0, 1.0 );
2332001f49Smrg
2432001f49Smrg   /* draw first polygon */
2532001f49Smrg   glPushMatrix();
2632001f49Smrg   glTranslatef( -1.0, 0.0, 0.0 );
2732001f49Smrg   glRotatef( Angle, 0.0, 0.0, 1.0 );
2832001f49Smrg   glBindTexture( GL_TEXTURE_1D, TexObj[0] );
2932001f49Smrg   glBegin( GL_POLYGON );
3032001f49Smrg   glTexCoord1f( 0.0 );   glVertex2f( -1.0, -1.0 );
3132001f49Smrg   glTexCoord1f( 1.0 );   glVertex2f(  1.0, -1.0 );
3232001f49Smrg   glTexCoord1f( 1.0 );   glVertex2f(  1.0,  1.0 );
3332001f49Smrg   glTexCoord1f( 0.0 );   glVertex2f( -1.0,  1.0 );
3432001f49Smrg   glEnd();
3532001f49Smrg   glPopMatrix();
3632001f49Smrg
3732001f49Smrg   glutSwapBuffers();
3832001f49Smrg}
3932001f49Smrg
4032001f49Smrg
4132001f49Smrg
4232001f49Smrg/*
4332001f49Smrgstatic void idle( void )
4432001f49Smrg{
4532001f49Smrg   Angle += 2.0;
4632001f49Smrg   glutPostRedisplay();
4732001f49Smrg}
4832001f49Smrg*/
4932001f49Smrg
5032001f49Smrg
5132001f49Smrg
5232001f49Smrg/* change view Angle, exit upon ESC */
5332001f49Smrgstatic void key(unsigned char k, int x, int y)
5432001f49Smrg{
5532001f49Smrg   (void) x;
5632001f49Smrg   (void) y;
5732001f49Smrg   switch (k) {
5832001f49Smrg      case 27:
5932001f49Smrg         exit(0);
6032001f49Smrg   }
6132001f49Smrg}
6232001f49Smrg
6332001f49Smrg
6432001f49Smrg
6532001f49Smrg/* new window size or exposure */
6632001f49Smrgstatic void reshape( int width, int height )
6732001f49Smrg{
6832001f49Smrg   glViewport(0, 0, (GLint)width, (GLint)height);
6932001f49Smrg   glMatrixMode(GL_PROJECTION);
7032001f49Smrg   glLoadIdentity();
7132001f49Smrg   /*   glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/
7232001f49Smrg   glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 );
7332001f49Smrg   glMatrixMode(GL_MODELVIEW);
7432001f49Smrg   glLoadIdentity();
7532001f49Smrg   glTranslatef( 0.0, 0.0, -8.0 );
7632001f49Smrg}
7732001f49Smrg
7832001f49Smrg
7932001f49Smrgstatic void init( void )
8032001f49Smrg{
8132001f49Smrg   GLubyte tex[256][3];
8232001f49Smrg   GLint i;
8332001f49Smrg
8432001f49Smrg
8532001f49Smrg   glDisable( GL_DITHER );
8632001f49Smrg
8732001f49Smrg   /* Setup texturing */
8832001f49Smrg   glEnable( GL_TEXTURE_1D );
8932001f49Smrg   glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
9032001f49Smrg
9132001f49Smrg
9232001f49Smrg   /* generate texture object IDs */
9332001f49Smrg   glGenTextures( 2, TexObj );
9432001f49Smrg
9532001f49Smrg   /* setup first texture object */
9632001f49Smrg   glBindTexture( GL_TEXTURE_1D, TexObj[0] );
9732001f49Smrg
9832001f49Smrg
9932001f49Smrg   for (i = 0; i < 256; i++) {
10032001f49Smrg      GLfloat f;
10132001f49Smrg
10232001f49Smrg      /* map 0..255 to -PI .. PI */
10332001f49Smrg      f = ((i / 255.0) - .5) * (3.141592 * 2);
10432001f49Smrg
10532001f49Smrg      f = sin(f);
10632001f49Smrg
10732001f49Smrg      /* map -1..1 to 0..255 */
10832001f49Smrg      tex[i][0] = (f+1.0)/2.0 * 255.0;
10932001f49Smrg      tex[i][1] = 0;
11032001f49Smrg      tex[i][2] = 0;
11132001f49Smrg   }
11232001f49Smrg
11332001f49Smrg   glTexImage1D( GL_TEXTURE_1D, 0, 3, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, tex );
11432001f49Smrg   glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
11532001f49Smrg   glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
11632001f49Smrg   glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT );
11732001f49Smrg   glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_REPEAT );
11832001f49Smrg}
11932001f49Smrg
12032001f49Smrg
12132001f49Smrg
12232001f49Smrgint main( int argc, char *argv[] )
12332001f49Smrg{
12432001f49Smrg   glutInit(&argc, argv);
12532001f49Smrg   glutInitWindowPosition(0, 0);
12632001f49Smrg   glutInitWindowSize(300, 300);
12732001f49Smrg   glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
12832001f49Smrg
12932001f49Smrg   Window = glutCreateWindow("Texture Objects");
13032001f49Smrg   glewInit();
13132001f49Smrg   if (!Window) {
13232001f49Smrg      exit(1);
13332001f49Smrg   }
13432001f49Smrg
13532001f49Smrg   init();
13632001f49Smrg
13732001f49Smrg   glutReshapeFunc( reshape );
13832001f49Smrg   glutKeyboardFunc( key );
13932001f49Smrg/*    glutIdleFunc( idle ); */
14032001f49Smrg   glutDisplayFunc( draw );
14132001f49Smrg   glutMainLoop();
14232001f49Smrg   return 0;
14332001f49Smrg}
144