blob: 3e52449b470747ea5348a70666b105e61a75ccbb [file] [log] [blame]
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +01001/*
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
30GLenum doubleBuffer = 1;
31int win;
Keith Whitwellc691f962009-04-20 15:50:44 +010032static float tx = 0;
33static float ty = 0;
Keith Whitwell6bfcffa2009-04-20 17:30:53 +010034static float tw = 0;
35static float th = 0;
Keith Whitwelle20f8372009-04-21 11:40:59 +010036static float z = -5;
37
Keith Whitwell6bfcffa2009-04-20 17:30:53 +010038
39static float win_width = 250;
40static float win_height = 250;
Keith Whitwelle20f8372009-04-21 11:40:59 +010041static enum {
42 ORTHO,
43 FRUSTUM,
44 MODE_MAX
45} mode = ORTHO;
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +010046
47static 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));
Keith Whitwelleb9801c2009-03-24 16:35:29 +000052 fflush(stderr);
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +010053
Keith Whitwell6bfcffa2009-04-20 17:30:53 +010054 glClearColor(0, 0, 0, 0.0);
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +010055}
56
57static void Reshape(int width, int height)
58{
Keith Whitwell6bfcffa2009-04-20 17:30:53 +010059 win_width = width;
60 win_height = height;
61 glutPostRedisplay();
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +010062}
63
Keith Whitwellc691f962009-04-20 15:50:44 +010064
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +010065static void Key(unsigned char key, int x, int y)
66{
67 switch (key) {
Keith Whitwell6bfcffa2009-04-20 17:30:53 +010068 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;
Keith Whitwelle20f8372009-04-21 11:40:59 +010082
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;
Keith Whitwella38f7d92009-04-20 17:32:15 +010093 case ' ':
94 tw = th = tx = ty = 0;
Keith Whitwelle20f8372009-04-21 11:40:59 +010095 z = -5;
96 mode = ORTHO;
Keith Whitwella38f7d92009-04-20 17:32:15 +010097 break;
Keith Whitwell6bfcffa2009-04-20 17:30:53 +010098 default:
99 break;
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +0100100 }
Keith Whitwell6bfcffa2009-04-20 17:30:53 +0100101 glutPostRedisplay();
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +0100102}
103
Keith Whitwellc691f962009-04-20 15:50:44 +0100104
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +0100105static void Draw(void)
106{
Keith Whitwellc691f962009-04-20 15:50:44 +0100107 int i;
Keith Whitwell6bfcffa2009-04-20 17:30:53 +0100108 float w = tw + win_width;
109 float h = th + win_height;
Keith Whitwellc691f962009-04-20 15:50:44 +0100110
Keith Whitwell6bfcffa2009-04-20 17:30:53 +0100111 fprintf(stderr, "glViewport(%f %f %f %f)\n", tx, ty, w, h);
Keith Whitwelle20f8372009-04-21 11:40:59 +0100112 fprintf(stderr, "mode: %s\n", mode == FRUSTUM ? "FRUSTUM" : "ORTHO");
113 fprintf(stderr, "z: %f\n", z);
Keith Whitwellc691f962009-04-20 15:50:44 +0100114 fflush(stderr);
115
116 glMatrixMode(GL_PROJECTION);
117 glLoadIdentity();
Keith Whitwelle20f8372009-04-21 11:40:59 +0100118
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
Keith Whitwellc691f962009-04-20 15:50:44 +0100129 glMatrixMode(GL_MODELVIEW);
Keith Whitwelle20f8372009-04-21 11:40:59 +0100130 glLoadIdentity();
131
Keith Whitwellc691f962009-04-20 15:50:44 +0100132
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +0100133 glClear(GL_COLOR_BUFFER_BIT);
134
Keith Whitwellc691f962009-04-20 15:50:44 +0100135
Keith Whitwell6bfcffa2009-04-20 17:30:53 +0100136 /***********************************************************************
137 * Should be clipped to be no larger than the triangles:
138 */
139 glViewport(tx, ty, w, h);
Keith Whitwell6e052242009-04-21 10:59:54 +0100140
Keith Whitwell6bfcffa2009-04-20 17:30:53 +0100141 glBegin(GL_POLYGON);
Keith Whitwell6e052242009-04-21 10:59:54 +0100142 glColor3f(1,1,0);
Keith Whitwelle20f8372009-04-21 11:40:59 +0100143 glVertex3f(-100, -100, z);
144 glVertex3f(-100, 100, z);
145 glVertex3f(100, 100, z);
146 glVertex3f(100, -100, z);
Keith Whitwell6e052242009-04-21 10:59:54 +0100147 glEnd();
148
149 glBegin(GL_POLYGON);
150 glColor3f(0,1,1);
Keith Whitwelle20f8372009-04-21 11:40:59 +0100151 glVertex3f(-10, -10, z);
152 glVertex3f(-10, 10, z);
153 glVertex3f(10, 10, z);
154 glVertex3f(10, -10, z);
Keith Whitwell6e052242009-04-21 10:59:54 +0100155 glEnd();
156
157 glBegin(GL_POLYGON);
158 glColor3f(1,0,0);
Keith Whitwelle20f8372009-04-21 11:40:59 +0100159 glVertex3f(-2, -2, z);
160 glVertex3f(-2, 2, z);
161 glVertex3f(2, 2, z);
162 glVertex3f(2, -2, z);
Keith Whitwell6bfcffa2009-04-20 17:30:53 +0100163 glEnd();
164
Keith Whitwell6e052242009-04-21 10:59:54 +0100165
166 glBegin(GL_POLYGON);
167 glColor3f(.5,.5,1);
Keith Whitwelle20f8372009-04-21 11:40:59 +0100168 glVertex3f(-1, -1, z);
169 glVertex3f(-1, 1, z);
170 glVertex3f(1, 1, z);
171 glVertex3f(1, -1, z);
Keith Whitwell6e052242009-04-21 10:59:54 +0100172 glEnd();
173
Keith Whitwell6bfcffa2009-04-20 17:30:53 +0100174 /***********************************************************************
175 */
176 glViewport(0, 0, win_width, win_height);
Keith Whitwellc691f962009-04-20 15:50:44 +0100177 glBegin(GL_LINES);
178 glColor3f(1,1,0);
Keith Whitwelle20f8372009-04-21 11:40:59 +0100179 glVertex3f(-1, 0, z);
180 glVertex3f(1, 0, z);
Keith Whitwellc691f962009-04-20 15:50:44 +0100181
Keith Whitwelle20f8372009-04-21 11:40:59 +0100182 glVertex3f(0, -1, z);
183 glVertex3f(0, 1, z);
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +0100184 glEnd();
185
Keith Whitwellc691f962009-04-20 15:50:44 +0100186
Keith Whitwell6bfcffa2009-04-20 17:30:53 +0100187 /***********************************************************************
Keith Whitwellc691f962009-04-20 15:50:44 +0100188 */
Keith Whitwell6bfcffa2009-04-20 17:30:53 +0100189 glViewport(tx, ty, w, h);
Keith Whitwellc691f962009-04-20 15:50:44 +0100190 glBegin(GL_TRIANGLES);
191 glColor3f(1,0,0);
Keith Whitwelle20f8372009-04-21 11:40:59 +0100192 glVertex3f(-1, -1, z);
193 glVertex3f(0, -1, z);
194 glVertex3f(-.5, -.5, z);
Keith Whitwellc691f962009-04-20 15:50:44 +0100195
196 glColor3f(1,1,1);
Keith Whitwelle20f8372009-04-21 11:40:59 +0100197 glVertex3f(0, -1, z);
198 glVertex3f(1, -1, z);
199 glVertex3f(.5, -.5, z);
Keith Whitwellc691f962009-04-20 15:50:44 +0100200
Keith Whitwelle20f8372009-04-21 11:40:59 +0100201 glVertex3f(-.5, -.5, z);
202 glVertex3f(.5, -.5, z);
203 glVertex3f(0, 0, z);
Keith Whitwellc691f962009-04-20 15:50:44 +0100204
205
206 glColor3f(0,1,0);
Keith Whitwelle20f8372009-04-21 11:40:59 +0100207 glVertex3f(1, 1, z);
208 glVertex3f(0, 1, z);
209 glVertex3f(.5, .5, z);
Keith Whitwellc691f962009-04-20 15:50:44 +0100210
211 glColor3f(1,1,1);
Keith Whitwelle20f8372009-04-21 11:40:59 +0100212 glVertex3f(0, 1, z);
213 glVertex3f(-1, 1, z);
214 glVertex3f(-.5, .5, z);
Keith Whitwellc691f962009-04-20 15:50:44 +0100215
Keith Whitwelle20f8372009-04-21 11:40:59 +0100216 glVertex3f(.5, .5, z);
217 glVertex3f(-.5, .5, z);
218 glVertex3f( 0, 0, z);
Keith Whitwellc691f962009-04-20 15:50:44 +0100219
220 glEnd();
221
222
Keith Whitwell6bfcffa2009-04-20 17:30:53 +0100223 glViewport(0, 0, win_width, win_height);
Keith Whitwellc691f962009-04-20 15:50:44 +0100224
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
Keith Whitwelle20f8372009-04-21 11:40:59 +0100233 glVertex3f(-1, f, z);
234 glVertex3f(1, f, z);
Keith Whitwellc691f962009-04-20 15:50:44 +0100235
Keith Whitwelle20f8372009-04-21 11:40:59 +0100236 glVertex3f(f, -1, z);
237 glVertex3f(f, 1, z);
Keith Whitwellc691f962009-04-20 15:50:44 +0100238 }
239 glEnd();
240
241
242
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +0100243 glFlush();
244
245 if (doubleBuffer) {
246 glutSwapBuffers();
247 }
248}
249
250static GLenum Args(int argc, char **argv)
251{
252 GLint i;
253
Keith Whitwellc691f962009-04-20 15:50:44 +0100254 if (getenv("VPX"))
255 tx = atof(getenv("VPX"));
256
257 if (getenv("VPY"))
258 ty = atof(getenv("VPY"));
259
260
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +0100261 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
Keith Whitwellc691f962009-04-20 15:50:44 +0100274
275static void
276special(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:
Keith Whitwell6bfcffa2009-04-20 17:30:53 +0100292 break;
Keith Whitwellc691f962009-04-20 15:50:44 +0100293 }
294 glutPostRedisplay();
295}
296
297
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +0100298int 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);
Keith Whitwellc691f962009-04-20 15:50:44 +0100322 glutKeyboardFunc(Key);
323 glutSpecialFunc(special);
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +0100324 glutDisplayFunc(Draw);
325 glutMainLoop();
326 return 0;
327}