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; |
| 75 | |
| 76 | default: |
| 77 | break; |
Jakob Bornecrantz | bd2f921 | 2009-02-21 11:58:48 +0100 | [diff] [blame] | 78 | } |
Keith Whitwell | 6bfcffa | 2009-04-20 17:30:53 +0100 | [diff] [blame^] | 79 | glutPostRedisplay(); |
Jakob Bornecrantz | bd2f921 | 2009-02-21 11:58:48 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 82 | |
Jakob Bornecrantz | bd2f921 | 2009-02-21 11:58:48 +0100 | [diff] [blame] | 83 | static void Draw(void) |
| 84 | { |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 85 | int i; |
Keith Whitwell | 6bfcffa | 2009-04-20 17:30:53 +0100 | [diff] [blame^] | 86 | float w = tw + win_width; |
| 87 | float h = th + win_height; |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 88 | |
Keith Whitwell | 6bfcffa | 2009-04-20 17:30:53 +0100 | [diff] [blame^] | 89 | fprintf(stderr, "glViewport(%f %f %f %f)\n", tx, ty, w, h); |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 90 | fflush(stderr); |
| 91 | |
| 92 | glMatrixMode(GL_PROJECTION); |
| 93 | glLoadIdentity(); |
| 94 | glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0); |
| 95 | glMatrixMode(GL_MODELVIEW); |
| 96 | |
Jakob Bornecrantz | bd2f921 | 2009-02-21 11:58:48 +0100 | [diff] [blame] | 97 | glClear(GL_COLOR_BUFFER_BIT); |
| 98 | |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 99 | |
Keith Whitwell | 6bfcffa | 2009-04-20 17:30:53 +0100 | [diff] [blame^] | 100 | /*********************************************************************** |
| 101 | * Should be clipped to be no larger than the triangles: |
| 102 | */ |
| 103 | glViewport(tx, ty, w, h); |
| 104 | glBegin(GL_POLYGON); |
| 105 | glColor3f(.5,.5,1); |
| 106 | glVertex3f(-2, -2, -30.0); |
| 107 | glVertex3f(-2, 2, -30.0); |
| 108 | glVertex3f(2, 2, -30.0); |
| 109 | glVertex3f(2, -2, -30.0); |
| 110 | glEnd(); |
| 111 | |
| 112 | /*********************************************************************** |
| 113 | */ |
| 114 | glViewport(0, 0, win_width, win_height); |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 115 | glBegin(GL_LINES); |
| 116 | glColor3f(1,1,0); |
| 117 | glVertex3f(-1, 0, -30.0); |
| 118 | glVertex3f(1, 0, -30.0); |
| 119 | |
| 120 | glVertex3f(0, -1, -30.0); |
| 121 | glVertex3f(0, 1, -30.0); |
Jakob Bornecrantz | bd2f921 | 2009-02-21 11:58:48 +0100 | [diff] [blame] | 122 | glEnd(); |
| 123 | |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 124 | |
Keith Whitwell | 6bfcffa | 2009-04-20 17:30:53 +0100 | [diff] [blame^] | 125 | /*********************************************************************** |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 126 | */ |
Keith Whitwell | 6bfcffa | 2009-04-20 17:30:53 +0100 | [diff] [blame^] | 127 | glViewport(tx, ty, w, h); |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 128 | glBegin(GL_TRIANGLES); |
| 129 | glColor3f(1,0,0); |
| 130 | glVertex3f(-1, -1, -30.0); |
| 131 | glVertex3f(0, -1, -30.0); |
| 132 | glVertex3f(-.5, -.5, -30.0); |
| 133 | |
| 134 | glColor3f(1,1,1); |
| 135 | glVertex3f(0, -1, -30.0); |
| 136 | glVertex3f(1, -1, -30.0); |
| 137 | glVertex3f(.5, -.5, -30.0); |
| 138 | |
| 139 | glVertex3f(-.5, -.5, -30.0); |
| 140 | glVertex3f(.5, -.5, -30.0); |
| 141 | glVertex3f(0, 0, -30.0); |
| 142 | |
| 143 | |
| 144 | glColor3f(0,1,0); |
| 145 | glVertex3f(1, 1, -30.0); |
| 146 | glVertex3f(0, 1, -30.0); |
| 147 | glVertex3f(.5, .5, -30.0); |
| 148 | |
| 149 | glColor3f(1,1,1); |
| 150 | glVertex3f(0, 1, -30.0); |
| 151 | glVertex3f(-1, 1, -30.0); |
| 152 | glVertex3f(-.5, .5, -30.0); |
| 153 | |
| 154 | glVertex3f(.5, .5, -30.0); |
| 155 | glVertex3f(-.5, .5, -30.0); |
| 156 | glVertex3f( 0, 0, -30.0); |
| 157 | |
| 158 | glEnd(); |
| 159 | |
| 160 | |
Keith Whitwell | 6bfcffa | 2009-04-20 17:30:53 +0100 | [diff] [blame^] | 161 | glViewport(0, 0, win_width, win_height); |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 162 | |
| 163 | glBegin(GL_LINES); |
| 164 | glColor3f(.5,.5,0); |
| 165 | for (i = -10; i < 10; i++) { |
| 166 | float f = i / 10.0; |
| 167 | |
| 168 | if (i == 0) |
| 169 | continue; |
| 170 | |
| 171 | glVertex3f(-1, f, -30.0); |
| 172 | glVertex3f(1, f, -30.0); |
| 173 | |
| 174 | glVertex3f(f, -1, -30.0); |
| 175 | glVertex3f(f, 1, -30.0); |
| 176 | } |
| 177 | glEnd(); |
| 178 | |
| 179 | |
| 180 | |
Jakob Bornecrantz | bd2f921 | 2009-02-21 11:58:48 +0100 | [diff] [blame] | 181 | glFlush(); |
| 182 | |
| 183 | if (doubleBuffer) { |
| 184 | glutSwapBuffers(); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | static GLenum Args(int argc, char **argv) |
| 189 | { |
| 190 | GLint i; |
| 191 | |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 192 | if (getenv("VPX")) |
| 193 | tx = atof(getenv("VPX")); |
| 194 | |
| 195 | if (getenv("VPY")) |
| 196 | ty = atof(getenv("VPY")); |
| 197 | |
| 198 | |
Jakob Bornecrantz | bd2f921 | 2009-02-21 11:58:48 +0100 | [diff] [blame] | 199 | for (i = 1; i < argc; i++) { |
| 200 | if (strcmp(argv[i], "-sb") == 0) { |
| 201 | doubleBuffer = GL_FALSE; |
| 202 | } else if (strcmp(argv[i], "-db") == 0) { |
| 203 | doubleBuffer = GL_TRUE; |
| 204 | } else { |
| 205 | fprintf(stderr, "%s (Bad option).\n", argv[i]); |
| 206 | return GL_FALSE; |
| 207 | } |
| 208 | } |
| 209 | return GL_TRUE; |
| 210 | } |
| 211 | |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 212 | |
| 213 | static void |
| 214 | special(int k, int x, int y) |
| 215 | { |
| 216 | switch (k) { |
| 217 | case GLUT_KEY_UP: |
| 218 | ty += 1.0; |
| 219 | break; |
| 220 | case GLUT_KEY_DOWN: |
| 221 | ty -= 1.0; |
| 222 | break; |
| 223 | case GLUT_KEY_LEFT: |
| 224 | tx -= 1.0; |
| 225 | break; |
| 226 | case GLUT_KEY_RIGHT: |
| 227 | tx += 1.0; |
| 228 | break; |
| 229 | default: |
Keith Whitwell | 6bfcffa | 2009-04-20 17:30:53 +0100 | [diff] [blame^] | 230 | break; |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 231 | } |
| 232 | glutPostRedisplay(); |
| 233 | } |
| 234 | |
| 235 | |
Jakob Bornecrantz | bd2f921 | 2009-02-21 11:58:48 +0100 | [diff] [blame] | 236 | int main(int argc, char **argv) |
| 237 | { |
| 238 | GLenum type; |
| 239 | |
| 240 | glutInit(&argc, argv); |
| 241 | |
| 242 | if (Args(argc, argv) == GL_FALSE) { |
| 243 | exit(1); |
| 244 | } |
| 245 | |
| 246 | glutInitWindowPosition(0, 0); glutInitWindowSize( 250, 250); |
| 247 | |
| 248 | type = GLUT_RGB | GLUT_ALPHA; |
| 249 | type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE; |
| 250 | glutInitDisplayMode(type); |
| 251 | |
| 252 | win = glutCreateWindow(*argv); |
| 253 | if (!win) { |
| 254 | exit(1); |
| 255 | } |
| 256 | |
| 257 | Init(); |
| 258 | |
| 259 | glutReshapeFunc(Reshape); |
Keith Whitwell | c691f96 | 2009-04-20 15:50:44 +0100 | [diff] [blame] | 260 | glutKeyboardFunc(Key); |
| 261 | glutSpecialFunc(special); |
Jakob Bornecrantz | bd2f921 | 2009-02-21 11:58:48 +0100 | [diff] [blame] | 262 | glutDisplayFunc(Draw); |
| 263 | glutMainLoop(); |
| 264 | return 0; |
| 265 | } |