blob: 133744235f4f83bd09ef261cf6bf93216138bf43 [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;
36
37static float win_width = 250;
38static float win_height = 250;
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +010039
40static void Init(void)
41{
42 fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
43 fprintf(stderr, "GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
44 fprintf(stderr, "GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
Keith Whitwelleb9801c2009-03-24 16:35:29 +000045 fflush(stderr);
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +010046
Keith Whitwell6bfcffa2009-04-20 17:30:53 +010047 glClearColor(0, 0, 0, 0.0);
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +010048}
49
50static void Reshape(int width, int height)
51{
Keith Whitwell6bfcffa2009-04-20 17:30:53 +010052 win_width = width;
53 win_height = height;
54 glutPostRedisplay();
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +010055}
56
Keith Whitwellc691f962009-04-20 15:50:44 +010057
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +010058static void Key(unsigned char key, int x, int y)
59{
60 switch (key) {
Keith Whitwell6bfcffa2009-04-20 17:30:53 +010061 case 27:
62 exit(0);
63 case 'w':
64 tw += 1.0;
65 break;
66 case 'W':
67 tw -= 1.0;
68 break;
69 case 'h':
70 th += 1.0;
71 break;
72 case 'H':
73 th -= 1.0;
74 break;
Keith Whitwella38f7d92009-04-20 17:32:15 +010075 case ' ':
76 tw = th = tx = ty = 0;
77 break;
Keith Whitwell6bfcffa2009-04-20 17:30:53 +010078 default:
79 break;
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +010080 }
Keith Whitwell6bfcffa2009-04-20 17:30:53 +010081 glutPostRedisplay();
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +010082}
83
Keith Whitwellc691f962009-04-20 15:50:44 +010084
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +010085static void Draw(void)
86{
Keith Whitwellc691f962009-04-20 15:50:44 +010087 int i;
Keith Whitwell6bfcffa2009-04-20 17:30:53 +010088 float w = tw + win_width;
89 float h = th + win_height;
Keith Whitwellc691f962009-04-20 15:50:44 +010090
Keith Whitwell6bfcffa2009-04-20 17:30:53 +010091 fprintf(stderr, "glViewport(%f %f %f %f)\n", tx, ty, w, h);
Keith Whitwellc691f962009-04-20 15:50:44 +010092 fflush(stderr);
93
94 glMatrixMode(GL_PROJECTION);
95 glLoadIdentity();
96 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
97 glMatrixMode(GL_MODELVIEW);
98
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +010099 glClear(GL_COLOR_BUFFER_BIT);
100
Keith Whitwellc691f962009-04-20 15:50:44 +0100101
Keith Whitwell6bfcffa2009-04-20 17:30:53 +0100102 /***********************************************************************
103 * Should be clipped to be no larger than the triangles:
104 */
105 glViewport(tx, ty, w, h);
Keith Whitwell6e052242009-04-21 10:59:54 +0100106
Keith Whitwell6bfcffa2009-04-20 17:30:53 +0100107 glBegin(GL_POLYGON);
Keith Whitwell6e052242009-04-21 10:59:54 +0100108 glColor3f(1,1,0);
109 glVertex3f(-100, -100, -30.0);
110 glVertex3f(-100, 100, -30.0);
111 glVertex3f(100, 100, -30.0);
112 glVertex3f(100, -100, -30.0);
113 glEnd();
114
115 glBegin(GL_POLYGON);
116 glColor3f(0,1,1);
117 glVertex3f(-10, -10, -30.0);
118 glVertex3f(-10, 10, -30.0);
119 glVertex3f(10, 10, -30.0);
120 glVertex3f(10, -10, -30.0);
121 glEnd();
122
123 glBegin(GL_POLYGON);
124 glColor3f(1,0,0);
Keith Whitwell6bfcffa2009-04-20 17:30:53 +0100125 glVertex3f(-2, -2, -30.0);
126 glVertex3f(-2, 2, -30.0);
127 glVertex3f(2, 2, -30.0);
128 glVertex3f(2, -2, -30.0);
129 glEnd();
130
Keith Whitwell6e052242009-04-21 10:59:54 +0100131
132 glBegin(GL_POLYGON);
133 glColor3f(.5,.5,1);
134 glVertex3f(-1, -1, -30.0);
135 glVertex3f(-1, 1, -30.0);
136 glVertex3f(1, 1, -30.0);
137 glVertex3f(1, -1, -30.0);
138 glEnd();
139
Keith Whitwell6bfcffa2009-04-20 17:30:53 +0100140 /***********************************************************************
141 */
142 glViewport(0, 0, win_width, win_height);
Keith Whitwellc691f962009-04-20 15:50:44 +0100143 glBegin(GL_LINES);
144 glColor3f(1,1,0);
145 glVertex3f(-1, 0, -30.0);
146 glVertex3f(1, 0, -30.0);
147
148 glVertex3f(0, -1, -30.0);
149 glVertex3f(0, 1, -30.0);
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +0100150 glEnd();
151
Keith Whitwellc691f962009-04-20 15:50:44 +0100152
Keith Whitwell6bfcffa2009-04-20 17:30:53 +0100153 /***********************************************************************
Keith Whitwellc691f962009-04-20 15:50:44 +0100154 */
Keith Whitwell6bfcffa2009-04-20 17:30:53 +0100155 glViewport(tx, ty, w, h);
Keith Whitwellc691f962009-04-20 15:50:44 +0100156 glBegin(GL_TRIANGLES);
157 glColor3f(1,0,0);
158 glVertex3f(-1, -1, -30.0);
159 glVertex3f(0, -1, -30.0);
160 glVertex3f(-.5, -.5, -30.0);
161
162 glColor3f(1,1,1);
163 glVertex3f(0, -1, -30.0);
164 glVertex3f(1, -1, -30.0);
165 glVertex3f(.5, -.5, -30.0);
166
167 glVertex3f(-.5, -.5, -30.0);
168 glVertex3f(.5, -.5, -30.0);
169 glVertex3f(0, 0, -30.0);
170
171
172 glColor3f(0,1,0);
173 glVertex3f(1, 1, -30.0);
174 glVertex3f(0, 1, -30.0);
175 glVertex3f(.5, .5, -30.0);
176
177 glColor3f(1,1,1);
178 glVertex3f(0, 1, -30.0);
179 glVertex3f(-1, 1, -30.0);
180 glVertex3f(-.5, .5, -30.0);
181
182 glVertex3f(.5, .5, -30.0);
183 glVertex3f(-.5, .5, -30.0);
184 glVertex3f( 0, 0, -30.0);
185
186 glEnd();
187
188
Keith Whitwell6bfcffa2009-04-20 17:30:53 +0100189 glViewport(0, 0, win_width, win_height);
Keith Whitwellc691f962009-04-20 15:50:44 +0100190
191 glBegin(GL_LINES);
192 glColor3f(.5,.5,0);
193 for (i = -10; i < 10; i++) {
194 float f = i / 10.0;
195
196 if (i == 0)
197 continue;
198
199 glVertex3f(-1, f, -30.0);
200 glVertex3f(1, f, -30.0);
201
202 glVertex3f(f, -1, -30.0);
203 glVertex3f(f, 1, -30.0);
204 }
205 glEnd();
206
207
208
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +0100209 glFlush();
210
211 if (doubleBuffer) {
212 glutSwapBuffers();
213 }
214}
215
216static GLenum Args(int argc, char **argv)
217{
218 GLint i;
219
Keith Whitwellc691f962009-04-20 15:50:44 +0100220 if (getenv("VPX"))
221 tx = atof(getenv("VPX"));
222
223 if (getenv("VPY"))
224 ty = atof(getenv("VPY"));
225
226
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +0100227 for (i = 1; i < argc; i++) {
228 if (strcmp(argv[i], "-sb") == 0) {
229 doubleBuffer = GL_FALSE;
230 } else if (strcmp(argv[i], "-db") == 0) {
231 doubleBuffer = GL_TRUE;
232 } else {
233 fprintf(stderr, "%s (Bad option).\n", argv[i]);
234 return GL_FALSE;
235 }
236 }
237 return GL_TRUE;
238}
239
Keith Whitwellc691f962009-04-20 15:50:44 +0100240
241static void
242special(int k, int x, int y)
243{
244 switch (k) {
245 case GLUT_KEY_UP:
246 ty += 1.0;
247 break;
248 case GLUT_KEY_DOWN:
249 ty -= 1.0;
250 break;
251 case GLUT_KEY_LEFT:
252 tx -= 1.0;
253 break;
254 case GLUT_KEY_RIGHT:
255 tx += 1.0;
256 break;
257 default:
Keith Whitwell6bfcffa2009-04-20 17:30:53 +0100258 break;
Keith Whitwellc691f962009-04-20 15:50:44 +0100259 }
260 glutPostRedisplay();
261}
262
263
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +0100264int main(int argc, char **argv)
265{
266 GLenum type;
267
268 glutInit(&argc, argv);
269
270 if (Args(argc, argv) == GL_FALSE) {
271 exit(1);
272 }
273
274 glutInitWindowPosition(0, 0); glutInitWindowSize( 250, 250);
275
276 type = GLUT_RGB | GLUT_ALPHA;
277 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
278 glutInitDisplayMode(type);
279
280 win = glutCreateWindow(*argv);
281 if (!win) {
282 exit(1);
283 }
284
285 Init();
286
287 glutReshapeFunc(Reshape);
Keith Whitwellc691f962009-04-20 15:50:44 +0100288 glutKeyboardFunc(Key);
289 glutSpecialFunc(special);
Jakob Bornecrantzbd2f9212009-02-21 11:58:48 +0100290 glutDisplayFunc(Draw);
291 glutMainLoop();
292 return 0;
293}