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