progs/trivial: add dlist-flat-tri.c
[mesa.git] / progs / trivial / tri-viewport.c
1 /*
2 * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that (i) the above copyright notices and this permission notice appear in
7 * all copies of the software and related documentation, and (ii) the name of
8 * Silicon Graphics may not be used in any advertising or
9 * publicity relating to the software without the specific, prior written
10 * permission of Silicon Graphics.
11 *
12 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
13 * ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <GL/glut.h>
29
30 GLenum doubleBuffer = 1;
31 int win;
32 static float tx = 0;
33 static float ty = 0;
34 static float tw = 0;
35 static float th = 0;
36 static float z = -5;
37
38
39 static float win_width = 250;
40 static float win_height = 250;
41 static enum {
42 ORTHO,
43 FRUSTUM,
44 MODE_MAX
45 } mode = ORTHO;
46
47 static void Init(void)
48 {
49 fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
50 fprintf(stderr, "GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
51 fprintf(stderr, "GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
52 fflush(stderr);
53
54 glClearColor(0, 0, 0, 0.0);
55 }
56
57 static void Reshape(int width, int height)
58 {
59 win_width = width;
60 win_height = height;
61 glutPostRedisplay();
62 }
63
64
65 static void Key(unsigned char key, int x, int y)
66 {
67 switch (key) {
68 case 27:
69 exit(0);
70 case 'w':
71 tw += 1.0;
72 break;
73 case 'W':
74 tw -= 1.0;
75 break;
76 case 'h':
77 th += 1.0;
78 break;
79 case 'H':
80 th -= 1.0;
81 break;
82
83 case 'z':
84 z += 1.0;
85 break;
86 case 'Z':
87 z -= 1.0;
88 break;
89 case 'm':
90 mode++;
91 mode %= MODE_MAX;
92 break;
93 case ' ':
94 tw = th = tx = ty = 0;
95 z = -5;
96 mode = ORTHO;
97 break;
98 default:
99 break;
100 }
101 glutPostRedisplay();
102 }
103
104
105 static void Draw(void)
106 {
107 int i;
108 float w = tw + win_width;
109 float h = th + win_height;
110
111 fprintf(stderr, "glViewport(%f %f %f %f)\n", tx, ty, w, h);
112 fprintf(stderr, "mode: %s\n", mode == FRUSTUM ? "FRUSTUM" : "ORTHO");
113 fprintf(stderr, "z: %f\n", z);
114 fflush(stderr);
115
116 glMatrixMode(GL_PROJECTION);
117 glLoadIdentity();
118
119 switch (mode) {
120 case FRUSTUM:
121 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
122 break;
123 case ORTHO:
124 default:
125 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
126 break;
127 }
128
129 glMatrixMode(GL_MODELVIEW);
130 glLoadIdentity();
131
132
133 glClear(GL_COLOR_BUFFER_BIT);
134
135
136 /***********************************************************************
137 * Should be clipped to be no larger than the triangles:
138 */
139 glViewport(tx, ty, w, h);
140
141 glBegin(GL_POLYGON);
142 glColor3f(1,1,0);
143 glVertex3f(-100, -100, z);
144 glVertex3f(-100, 100, z);
145 glVertex3f(100, 100, z);
146 glVertex3f(100, -100, z);
147 glEnd();
148
149 glBegin(GL_POLYGON);
150 glColor3f(0,1,1);
151 glVertex3f(-10, -10, z);
152 glVertex3f(-10, 10, z);
153 glVertex3f(10, 10, z);
154 glVertex3f(10, -10, z);
155 glEnd();
156
157 glBegin(GL_POLYGON);
158 glColor3f(1,0,0);
159 glVertex3f(-2, -2, z);
160 glVertex3f(-2, 2, z);
161 glVertex3f(2, 2, z);
162 glVertex3f(2, -2, z);
163 glEnd();
164
165
166 glBegin(GL_POLYGON);
167 glColor3f(.5,.5,1);
168 glVertex3f(-1, -1, z);
169 glVertex3f(-1, 1, z);
170 glVertex3f(1, 1, z);
171 glVertex3f(1, -1, z);
172 glEnd();
173
174 /***********************************************************************
175 */
176 glViewport(0, 0, win_width, win_height);
177 glBegin(GL_LINES);
178 glColor3f(1,1,0);
179 glVertex3f(-1, 0, z);
180 glVertex3f(1, 0, z);
181
182 glVertex3f(0, -1, z);
183 glVertex3f(0, 1, z);
184 glEnd();
185
186
187 /***********************************************************************
188 */
189 glViewport(tx, ty, w, h);
190 glBegin(GL_TRIANGLES);
191 glColor3f(1,0,0);
192 glVertex3f(-1, -1, z);
193 glVertex3f(0, -1, z);
194 glVertex3f(-.5, -.5, z);
195
196 glColor3f(1,1,1);
197 glVertex3f(0, -1, z);
198 glVertex3f(1, -1, z);
199 glVertex3f(.5, -.5, z);
200
201 glVertex3f(-.5, -.5, z);
202 glVertex3f(.5, -.5, z);
203 glVertex3f(0, 0, z);
204
205
206 glColor3f(0,1,0);
207 glVertex3f(1, 1, z);
208 glVertex3f(0, 1, z);
209 glVertex3f(.5, .5, z);
210
211 glColor3f(1,1,1);
212 glVertex3f(0, 1, z);
213 glVertex3f(-1, 1, z);
214 glVertex3f(-.5, .5, z);
215
216 glVertex3f(.5, .5, z);
217 glVertex3f(-.5, .5, z);
218 glVertex3f( 0, 0, z);
219
220 glEnd();
221
222
223 glViewport(0, 0, win_width, win_height);
224
225 glBegin(GL_LINES);
226 glColor3f(.5,.5,0);
227 for (i = -10; i < 10; i++) {
228 float f = i / 10.0;
229
230 if (i == 0)
231 continue;
232
233 glVertex3f(-1, f, z);
234 glVertex3f(1, f, z);
235
236 glVertex3f(f, -1, z);
237 glVertex3f(f, 1, z);
238 }
239 glEnd();
240
241
242
243 glFlush();
244
245 if (doubleBuffer) {
246 glutSwapBuffers();
247 }
248 }
249
250 static GLenum Args(int argc, char **argv)
251 {
252 GLint i;
253
254 if (getenv("VPX"))
255 tx = atof(getenv("VPX"));
256
257 if (getenv("VPY"))
258 ty = atof(getenv("VPY"));
259
260
261 for (i = 1; i < argc; i++) {
262 if (strcmp(argv[i], "-sb") == 0) {
263 doubleBuffer = GL_FALSE;
264 } else if (strcmp(argv[i], "-db") == 0) {
265 doubleBuffer = GL_TRUE;
266 } else {
267 fprintf(stderr, "%s (Bad option).\n", argv[i]);
268 return GL_FALSE;
269 }
270 }
271 return GL_TRUE;
272 }
273
274
275 static void
276 special(int k, int x, int y)
277 {
278 switch (k) {
279 case GLUT_KEY_UP:
280 ty += 1.0;
281 break;
282 case GLUT_KEY_DOWN:
283 ty -= 1.0;
284 break;
285 case GLUT_KEY_LEFT:
286 tx -= 1.0;
287 break;
288 case GLUT_KEY_RIGHT:
289 tx += 1.0;
290 break;
291 default:
292 break;
293 }
294 glutPostRedisplay();
295 }
296
297
298 int main(int argc, char **argv)
299 {
300 GLenum type;
301
302 glutInit(&argc, argv);
303
304 if (Args(argc, argv) == GL_FALSE) {
305 exit(1);
306 }
307
308 glutInitWindowPosition(0, 0); glutInitWindowSize( 250, 250);
309
310 type = GLUT_RGB | GLUT_ALPHA;
311 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
312 glutInitDisplayMode(type);
313
314 win = glutCreateWindow(*argv);
315 if (!win) {
316 exit(1);
317 }
318
319 Init();
320
321 glutReshapeFunc(Reshape);
322 glutKeyboardFunc(Key);
323 glutSpecialFunc(special);
324 glutDisplayFunc(Draw);
325 glutMainLoop();
326 return 0;
327 }