Jakob Bornecrantz | bd2f921 | 2009-02-21 11:58:48 +0100 | [diff] [blame] | 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; |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 32 | static float tx = 0; |
| 33 | static float ty = 0; |
Keith Whitwell | 6bfcffa | 2009-04-20 17:30:53 +0100 | [diff] [blame] | 34 | static float tw = 0; |
| 35 | static float th = 0; |
| 36 | |
| 37 | static float win_width = 250; |
| 38 | static float win_height = 250; |
Jakob Bornecrantz | bd2f921 | 2009-02-21 11:58:48 +0100 | [diff] [blame] | 39 | |
| 40 | static 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 Whitwell | eb9801c | 2009-03-24 16:35:29 +0000 | [diff] [blame] | 45 | fflush(stderr); |
Jakob Bornecrantz | bd2f921 | 2009-02-21 11:58:48 +0100 | [diff] [blame] | 46 | |
Keith Whitwell | 6bfcffa | 2009-04-20 17:30:53 +0100 | [diff] [blame] | 47 | glClearColor(0, 0, 0, 0.0); |
Jakob Bornecrantz | bd2f921 | 2009-02-21 11:58:48 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static void Reshape(int width, int height) |
| 51 | { |
Keith Whitwell | 6bfcffa | 2009-04-20 17:30:53 +0100 | [diff] [blame] | 52 | win_width = width; |
| 53 | win_height = height; |
| 54 | glutPostRedisplay(); |
Jakob Bornecrantz | bd2f921 | 2009-02-21 11:58:48 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 57 | |
Jakob Bornecrantz | bd2f921 | 2009-02-21 11:58:48 +0100 | [diff] [blame] | 58 | static void Key(unsigned char key, int x, int y) |
| 59 | { |
| 60 | switch (key) { |
Keith Whitwell | 6bfcffa | 2009-04-20 17:30:53 +0100 | [diff] [blame] | 61 | 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 Whitwell | a38f7d9 | 2009-04-20 17:32:15 +0100 | [diff] [blame] | 75 | case ' ': |
| 76 | tw = th = tx = ty = 0; |
| 77 | break; |
Keith Whitwell | 6bfcffa | 2009-04-20 17:30:53 +0100 | [diff] [blame] | 78 | default: |
| 79 | break; |
Jakob Bornecrantz | bd2f921 | 2009-02-21 11:58:48 +0100 | [diff] [blame] | 80 | } |
Keith Whitwell | 6bfcffa | 2009-04-20 17:30:53 +0100 | [diff] [blame] | 81 | glutPostRedisplay(); |
Jakob Bornecrantz | bd2f921 | 2009-02-21 11:58:48 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 84 | |
Jakob Bornecrantz | bd2f921 | 2009-02-21 11:58:48 +0100 | [diff] [blame] | 85 | static void Draw(void) |
| 86 | { |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 87 | int i; |
Keith Whitwell | 6bfcffa | 2009-04-20 17:30:53 +0100 | [diff] [blame] | 88 | float w = tw + win_width; |
| 89 | float h = th + win_height; |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 90 | |
Keith Whitwell | 6bfcffa | 2009-04-20 17:30:53 +0100 | [diff] [blame] | 91 | fprintf(stderr, "glViewport(%f %f %f %f)\n", tx, ty, w, h); |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 92 | 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 Bornecrantz | bd2f921 | 2009-02-21 11:58:48 +0100 | [diff] [blame] | 99 | glClear(GL_COLOR_BUFFER_BIT); |
| 100 | |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 101 | |
Keith Whitwell | 6bfcffa | 2009-04-20 17:30:53 +0100 | [diff] [blame] | 102 | /*********************************************************************** |
| 103 | * Should be clipped to be no larger than the triangles: |
| 104 | */ |
| 105 | glViewport(tx, ty, w, h); |
Keith Whitwell | 6e05224 | 2009-04-21 10:59:54 +0100 | [diff] [blame^] | 106 | |
Keith Whitwell | 6bfcffa | 2009-04-20 17:30:53 +0100 | [diff] [blame] | 107 | glBegin(GL_POLYGON); |
Keith Whitwell | 6e05224 | 2009-04-21 10:59:54 +0100 | [diff] [blame^] | 108 | 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 Whitwell | 6bfcffa | 2009-04-20 17:30:53 +0100 | [diff] [blame] | 125 | 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 Whitwell | 6e05224 | 2009-04-21 10:59:54 +0100 | [diff] [blame^] | 131 | |
| 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 Whitwell | 6bfcffa | 2009-04-20 17:30:53 +0100 | [diff] [blame] | 140 | /*********************************************************************** |
| 141 | */ |
| 142 | glViewport(0, 0, win_width, win_height); |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 143 | 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 Bornecrantz | bd2f921 | 2009-02-21 11:58:48 +0100 | [diff] [blame] | 150 | glEnd(); |
| 151 | |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 152 | |
Keith Whitwell | 6bfcffa | 2009-04-20 17:30:53 +0100 | [diff] [blame] | 153 | /*********************************************************************** |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 154 | */ |
Keith Whitwell | 6bfcffa | 2009-04-20 17:30:53 +0100 | [diff] [blame] | 155 | glViewport(tx, ty, w, h); |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 156 | 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 Whitwell | 6bfcffa | 2009-04-20 17:30:53 +0100 | [diff] [blame] | 189 | glViewport(0, 0, win_width, win_height); |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 190 | |
| 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 Bornecrantz | bd2f921 | 2009-02-21 11:58:48 +0100 | [diff] [blame] | 209 | glFlush(); |
| 210 | |
| 211 | if (doubleBuffer) { |
| 212 | glutSwapBuffers(); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | static GLenum Args(int argc, char **argv) |
| 217 | { |
| 218 | GLint i; |
| 219 | |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 220 | if (getenv("VPX")) |
| 221 | tx = atof(getenv("VPX")); |
| 222 | |
| 223 | if (getenv("VPY")) |
| 224 | ty = atof(getenv("VPY")); |
| 225 | |
| 226 | |
Jakob Bornecrantz | bd2f921 | 2009-02-21 11:58:48 +0100 | [diff] [blame] | 227 | 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 Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 240 | |
| 241 | static void |
| 242 | special(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 Whitwell | 6bfcffa | 2009-04-20 17:30:53 +0100 | [diff] [blame] | 258 | break; |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 259 | } |
| 260 | glutPostRedisplay(); |
| 261 | } |
| 262 | |
| 263 | |
Jakob Bornecrantz | bd2f921 | 2009-02-21 11:58:48 +0100 | [diff] [blame] | 264 | int 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 Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 288 | glutKeyboardFunc(Key); |
| 289 | glutSpecialFunc(special); |
Jakob Bornecrantz | bd2f921 | 2009-02-21 11:58:48 +0100 | [diff] [blame] | 290 | glutDisplayFunc(Draw); |
| 291 | glutMainLoop(); |
| 292 | return 0; |
| 293 | } |