added multitexture support
[mesa.git] / progs / tests / texline.c
1 /* $Id: texline.c,v 1.3 2001/05/21 17:45:25 brianp Exp $ */
2
3 /*
4 * Test textured lines.
5 *
6 * Brian Paul
7 * September 2000
8 */
9
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <math.h>
14 #include <GL/glut.h>
15 #include "../util/readtex.c" /* I know, this is a hack. */
16
17 #define TEXTURE_FILE "../images/girl.rgb"
18
19 static GLboolean Antialias = GL_FALSE;
20 static GLboolean Animate = GL_FALSE;
21 static GLboolean Texture = GL_TRUE;
22 static GLfloat LineWidth = 1.0;
23 static GLboolean Multitex = GL_FALSE;
24
25 static GLfloat Xrot = -60.0, Yrot = 0.0, Zrot = 0.0;
26 static GLfloat DYrot = 1.0;
27 static GLboolean Points = GL_FALSE;
28
29
30 static void Idle( void )
31 {
32 if (Animate) {
33 Zrot += DYrot;
34 glutPostRedisplay();
35 }
36 }
37
38
39 static void Display( void )
40 {
41 GLfloat x, y, s, t;
42
43 glClear( GL_COLOR_BUFFER_BIT );
44
45 glPushMatrix();
46 glRotatef(Xrot, 1.0, 0.0, 0.0);
47 glRotatef(Yrot, 0.0, 1.0, 0.0);
48 glRotatef(Zrot, 0.0, 0.0, 1.0);
49
50 if (Texture)
51 glColor3f(1, 1, 1);
52
53 if (Points) {
54 glBegin(GL_POINTS);
55 for (t = 0.0; t <= 1.0; t += 0.025) {
56 for (s = 0.0; s <= 1.0; s += 0.025) {
57 x = s * 2.0 - 1.0;
58 y = t * 2.0 - 1.0;
59 if (!Texture)
60 glColor3f(1, 0, 1);
61 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, t, s);
62 glTexCoord2f(s, t);
63 glVertex2f(x, y);
64 }
65 }
66 glEnd();
67 }
68 else {
69 glBegin(GL_LINES);
70 for (t = 0.0; t <= 1.0; t += 0.025) {
71 x = t * 2.0 - 1.0;
72 if (!Texture)
73 glColor3f(1, 0, 1);
74 glTexCoord2f(t, 0.0);
75 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, t);
76 glVertex2f(x, -1.0);
77 if (!Texture)
78 glColor3f(0, 1, 0);
79 glTexCoord2f(t, 1.0);
80 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, t);
81 glVertex2f(x, 1.0);
82 }
83 glEnd();
84 }
85
86 glPopMatrix();
87
88 glutSwapBuffers();
89 }
90
91
92 static void Reshape( int width, int height )
93 {
94 GLfloat ar = (float) width / height;
95 glViewport( 0, 0, width, height );
96 glMatrixMode( GL_PROJECTION );
97 glLoadIdentity();
98 glFrustum( -ar, ar, -1.0, 1.0, 10.0, 100.0 );
99 glMatrixMode( GL_MODELVIEW );
100 glLoadIdentity();
101 glTranslatef( 0.0, 0.0, -12.0 );
102 }
103
104
105 static void Key( unsigned char key, int x, int y )
106 {
107 (void) x;
108 (void) y;
109 switch (key) {
110 case 'a':
111 Antialias = !Antialias;
112 if (Antialias) {
113 glEnable(GL_LINE_SMOOTH);
114 glEnable(GL_POINT_SMOOTH);
115 glEnable(GL_BLEND);
116 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
117 }
118 else {
119 glDisable(GL_LINE_SMOOTH);
120 glDisable(GL_POINT_SMOOTH);
121 glDisable(GL_BLEND);
122 }
123 break;
124 case 't':
125 Texture = !Texture;
126 if (Texture)
127 glEnable(GL_TEXTURE_2D);
128 else
129 glDisable(GL_TEXTURE_2D);
130 break;
131 case 'w':
132 LineWidth -= 0.25;
133 if (LineWidth < 0.25)
134 LineWidth = 0.25;
135 glLineWidth(LineWidth);
136 glPointSize(LineWidth);
137 break;
138 case 'W':
139 LineWidth += 0.25;
140 if (LineWidth > 8.0)
141 LineWidth = 8.0;
142 glLineWidth(LineWidth);
143 glPointSize(LineWidth);
144 break;
145 case 'm':
146 Multitex = !Multitex;
147 if (Multitex) {
148 glEnable(GL_TEXTURE_2D);
149 }
150 else {
151 glDisable(GL_TEXTURE_2D);
152 }
153 break;
154 case 'p':
155 Points = !Points;
156 break;
157 case ' ':
158 Animate = !Animate;
159 if (Animate)
160 glutIdleFunc(Idle);
161 else
162 glutIdleFunc(NULL);
163 break;
164 case 27:
165 exit(0);
166 break;
167 }
168 printf("LineWidth, PointSize = %f\n", LineWidth);
169 glutPostRedisplay();
170 }
171
172
173 static void SpecialKey( int key, int x, int y )
174 {
175 float step = 3.0;
176 (void) x;
177 (void) y;
178
179 switch (key) {
180 case GLUT_KEY_UP:
181 Xrot += step;
182 break;
183 case GLUT_KEY_DOWN:
184 Xrot -= step;
185 break;
186 case GLUT_KEY_LEFT:
187 Yrot += step;
188 break;
189 case GLUT_KEY_RIGHT:
190 Yrot -= step;
191 break;
192 }
193 glutPostRedisplay();
194 }
195
196
197 static void Init( int argc, char *argv[] )
198 {
199 GLuint u;
200 for (u = 0; u < 2; u++) {
201 glActiveTextureARB(GL_TEXTURE0_ARB + u);
202 glBindTexture(GL_TEXTURE_2D, 10+u);
203 if (u == 0 || Multitex)
204 glEnable(GL_TEXTURE_2D);
205 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
206 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
207 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
208 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
209
210 if (u == 0)
211 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
212 else
213 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
214
215 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
216 if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
217 printf("Error: couldn't load texture image\n");
218 exit(1);
219 }
220 }
221
222 if (argc > 1 && strcmp(argv[1], "-info")==0) {
223 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
224 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
225 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
226 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
227 }
228 }
229
230
231 int main( int argc, char *argv[] )
232 {
233 glutInit( &argc, argv );
234 glutInitWindowSize( 400, 300 );
235
236 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
237
238 glutCreateWindow(argv[0] );
239
240 Init(argc, argv);
241
242 glutReshapeFunc( Reshape );
243 glutKeyboardFunc( Key );
244 glutSpecialFunc( SpecialKey );
245 glutDisplayFunc( Display );
246 if (Animate)
247 glutIdleFunc( Idle );
248
249 glutMainLoop();
250 return 0;
251 }