tex1d.c revision 32001f49
1
2/* Exercise 1D textures
3 */
4
5#include <assert.h>
6#include <math.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include "GL/glew.h"
11#include "glut_wrap.h"
12
13static GLuint Window = 0;
14static GLuint TexObj[2];
15static GLfloat Angle = 0.0f;
16
17
18static void draw( void )
19{
20   glClear( GL_COLOR_BUFFER_BIT );
21
22   glColor3f( 1.0, 1.0, 1.0 );
23
24   /* draw first polygon */
25   glPushMatrix();
26   glTranslatef( -1.0, 0.0, 0.0 );
27   glRotatef( Angle, 0.0, 0.0, 1.0 );
28   glBindTexture( GL_TEXTURE_1D, TexObj[0] );
29   glBegin( GL_POLYGON );
30   glTexCoord1f( 0.0 );   glVertex2f( -1.0, -1.0 );
31   glTexCoord1f( 1.0 );   glVertex2f(  1.0, -1.0 );
32   glTexCoord1f( 1.0 );   glVertex2f(  1.0,  1.0 );
33   glTexCoord1f( 0.0 );   glVertex2f( -1.0,  1.0 );
34   glEnd();
35   glPopMatrix();
36
37   glutSwapBuffers();
38}
39
40
41
42/*
43static void idle( void )
44{
45   Angle += 2.0;
46   glutPostRedisplay();
47}
48*/
49
50
51
52/* change view Angle, exit upon ESC */
53static void key(unsigned char k, int x, int y)
54{
55   (void) x;
56   (void) y;
57   switch (k) {
58      case 27:
59         exit(0);
60   }
61}
62
63
64
65/* new window size or exposure */
66static void reshape( int width, int height )
67{
68   glViewport(0, 0, (GLint)width, (GLint)height);
69   glMatrixMode(GL_PROJECTION);
70   glLoadIdentity();
71   /*   glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/
72   glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 );
73   glMatrixMode(GL_MODELVIEW);
74   glLoadIdentity();
75   glTranslatef( 0.0, 0.0, -8.0 );
76}
77
78
79static void init( void )
80{
81   GLubyte tex[256][3];
82   GLint i;
83
84
85   glDisable( GL_DITHER );
86
87   /* Setup texturing */
88   glEnable( GL_TEXTURE_1D );
89   glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
90
91
92   /* generate texture object IDs */
93   glGenTextures( 2, TexObj );
94
95   /* setup first texture object */
96   glBindTexture( GL_TEXTURE_1D, TexObj[0] );
97
98
99   for (i = 0; i < 256; i++) {
100      GLfloat f;
101
102      /* map 0..255 to -PI .. PI */
103      f = ((i / 255.0) - .5) * (3.141592 * 2);
104
105      f = sin(f);
106
107      /* map -1..1 to 0..255 */
108      tex[i][0] = (f+1.0)/2.0 * 255.0;
109      tex[i][1] = 0;
110      tex[i][2] = 0;
111   }
112
113   glTexImage1D( GL_TEXTURE_1D, 0, 3, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, tex );
114   glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
115   glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
116   glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT );
117   glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_REPEAT );
118}
119
120
121
122int main( int argc, char *argv[] )
123{
124   glutInit(&argc, argv);
125   glutInitWindowPosition(0, 0);
126   glutInitWindowSize(300, 300);
127   glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
128
129   Window = glutCreateWindow("Texture Objects");
130   glewInit();
131   if (!Window) {
132      exit(1);
133   }
134
135   init();
136
137   glutReshapeFunc( reshape );
138   glutKeyboardFunc( key );
139/*    glutIdleFunc( idle ); */
140   glutDisplayFunc( draw );
141   glutMainLoop();
142   return 0;
143}
144