added multitexture and line stipple
[mesa.git] / progs / tests / texline.c
1 /* $Id: texline.c,v 1.4 2002/08/17 00:30:36 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 GLint Texture = 1;
22 static GLboolean Stipple = GL_FALSE;
23 static GLfloat LineWidth = 1.0;
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 static GLfloat Scale = 1.0;
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 glScalef(Scale, Scale, Scale);
50
51 if (Texture)
52 glColor3f(1, 1, 1);
53
54 if (Points) {
55 glBegin(GL_POINTS);
56 for (t = 0.0; t <= 1.0; t += 0.025) {
57 for (s = 0.0; s <= 1.0; s += 0.025) {
58 x = s * 2.0 - 1.0;
59 y = t * 2.0 - 1.0;
60 if (!Texture)
61 glColor3f(1, 0, 1);
62 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, t, s);
63 glTexCoord2f(s, t);
64 glVertex2f(x, y);
65 }
66 }
67 glEnd();
68 }
69 else {
70 glBegin(GL_LINES);
71 for (t = 0.0; t <= 1.0; t += 0.025) {
72 x = t * 2.0 - 1.0;
73 if (!Texture)
74 glColor3f(1, 0, 1);
75 glTexCoord2f(t, 0.0);
76 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, t);
77 glVertex2f(x, -1.0);
78 if (!Texture)
79 glColor3f(0, 1, 0);
80 glTexCoord2f(t, 1.0);
81 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, t);
82 glVertex2f(x, 1.0);
83 }
84 glEnd();
85 }
86
87 glPopMatrix();
88
89 glutSwapBuffers();
90 }
91
92
93 static void Reshape( int width, int height )
94 {
95 GLfloat ar = (float) width / height;
96 glViewport( 0, 0, width, height );
97 glMatrixMode( GL_PROJECTION );
98 glLoadIdentity();
99 glFrustum( -ar, ar, -1.0, 1.0, 10.0, 100.0 );
100 glMatrixMode( GL_MODELVIEW );
101 glLoadIdentity();
102 glTranslatef( 0.0, 0.0, -12.0 );
103 }
104
105
106 static void Key( unsigned char key, int x, int y )
107 {
108 (void) x;
109 (void) y;
110 switch (key) {
111 case 'a':
112 Antialias = !Antialias;
113 if (Antialias) {
114 glEnable(GL_LINE_SMOOTH);
115 glEnable(GL_POINT_SMOOTH);
116 glEnable(GL_BLEND);
117 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
118 }
119 else {
120 glDisable(GL_LINE_SMOOTH);
121 glDisable(GL_POINT_SMOOTH);
122 glDisable(GL_BLEND);
123 }
124 break;
125 case 't':
126 Texture++;
127 if (Texture > 2)
128 Texture = 0;
129 if (Texture == 0) {
130 glActiveTextureARB(GL_TEXTURE0_ARB);
131 glDisable(GL_TEXTURE_2D);
132 glActiveTextureARB(GL_TEXTURE1_ARB);
133 glDisable(GL_TEXTURE_2D);
134 }
135 else if (Texture == 1) {
136 glActiveTextureARB(GL_TEXTURE0_ARB);
137 glEnable(GL_TEXTURE_2D);
138 glActiveTextureARB(GL_TEXTURE1_ARB);
139 glDisable(GL_TEXTURE_2D);
140 }
141 else {
142 glActiveTextureARB(GL_TEXTURE0_ARB);
143 glEnable(GL_TEXTURE_2D);
144 glActiveTextureARB(GL_TEXTURE1_ARB);
145 glEnable(GL_TEXTURE_2D);
146 }
147 break;
148 case 'w':
149 LineWidth -= 0.25;
150 if (LineWidth < 0.25)
151 LineWidth = 0.25;
152 glLineWidth(LineWidth);
153 glPointSize(LineWidth);
154 break;
155 case 'W':
156 LineWidth += 0.25;
157 if (LineWidth > 8.0)
158 LineWidth = 8.0;
159 glLineWidth(LineWidth);
160 glPointSize(LineWidth);
161 break;
162 case 'p':
163 Points = !Points;
164 break;
165 case 's':
166 Stipple = !Stipple;
167 if (Stipple)
168 glEnable(GL_LINE_STIPPLE);
169 else
170 glDisable(GL_LINE_STIPPLE);
171 break;
172 case ' ':
173 Animate = !Animate;
174 if (Animate)
175 glutIdleFunc(Idle);
176 else
177 glutIdleFunc(NULL);
178 break;
179 case 27:
180 exit(0);
181 break;
182 }
183 printf("LineWidth, PointSize = %f\n", LineWidth);
184 glutPostRedisplay();
185 }
186
187
188 static void SpecialKey( int key, int x, int y )
189 {
190 float step = 3.0;
191 (void) x;
192 (void) y;
193
194 switch (key) {
195 case GLUT_KEY_UP:
196 Xrot += step;
197 break;
198 case GLUT_KEY_DOWN:
199 Xrot -= step;
200 break;
201 case GLUT_KEY_LEFT:
202 Yrot += step;
203 break;
204 case GLUT_KEY_RIGHT:
205 Yrot -= step;
206 break;
207 }
208 glutPostRedisplay();
209 }
210
211
212 static void Init( int argc, char *argv[] )
213 {
214 GLuint u;
215 for (u = 0; u < 2; u++) {
216 glActiveTextureARB(GL_TEXTURE0_ARB + u);
217 glBindTexture(GL_TEXTURE_2D, 10+u);
218 if (u == 0)
219 glEnable(GL_TEXTURE_2D);
220 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
221 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
222 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
223 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
224
225 if (u == 0)
226 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
227 else
228 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
229
230 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
231 if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
232 printf("Error: couldn't load texture image\n");
233 exit(1);
234 }
235 }
236
237 glLineStipple(1, 0xff);
238
239 if (argc > 1 && strcmp(argv[1], "-info")==0) {
240 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
241 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
242 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
243 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
244 }
245 }
246
247
248 int main( int argc, char *argv[] )
249 {
250 glutInit( &argc, argv );
251 glutInitWindowSize( 400, 300 );
252
253 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
254
255 glutCreateWindow(argv[0] );
256
257 Init(argc, argv);
258
259 glutReshapeFunc( Reshape );
260 glutKeyboardFunc( Key );
261 glutSpecialFunc( SpecialKey );
262 glutDisplayFunc( Display );
263 if (Animate)
264 glutIdleFunc( Idle );
265
266 glutMainLoop();
267 return 0;
268 }