jtg | afb833d | 1999-08-19 00:55:39 +0000 | [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 | |
| 31 | #define SOLID 1 |
| 32 | #define LINE 2 |
| 33 | #define POINT 3 |
| 34 | |
| 35 | |
| 36 | GLenum rgb, doubleBuffer, windType; |
| 37 | GLint windW, windH; |
| 38 | |
| 39 | GLenum dithering = GL_TRUE; |
| 40 | GLenum showVerticies = GL_TRUE; |
| 41 | GLenum hideBottomTriangle = GL_FALSE; |
| 42 | GLenum outline = GL_TRUE; |
| 43 | GLenum culling = GL_FALSE; |
| 44 | GLenum winding = GL_FALSE; |
| 45 | GLenum face = GL_FALSE; |
| 46 | GLenum state = SOLID; |
| 47 | GLenum aaMode = GL_FALSE; |
| 48 | GLenum shade = GL_TRUE; |
| 49 | |
| 50 | GLint color1, color2, color3; |
| 51 | |
| 52 | float zRotation = 90.0; |
| 53 | float zoom = 1.0; |
| 54 | |
| 55 | float boxA[3] = {-100, -100, 0}; |
| 56 | float boxB[3] = { 100, -100, 0}; |
| 57 | float boxC[3] = { 100, 100, 0}; |
| 58 | float boxD[3] = {-100, 100, 0}; |
| 59 | |
| 60 | float p0[3] = {-125,-80, 0}; |
| 61 | float p1[3] = {-125, 80, 0}; |
| 62 | float p2[3] = { 172, 0, 0}; |
| 63 | |
| 64 | |
| 65 | #include "tkmap.c" |
| 66 | |
| 67 | static void Init(void) |
| 68 | { |
| 69 | float r, g, b; |
| 70 | float percent1, percent2; |
| 71 | GLint i, j; |
| 72 | |
| 73 | glClearColor(0.0, 0.0, 0.0, 0.0); |
| 74 | |
| 75 | glLineStipple(1, 0xF0F0); |
| 76 | |
| 77 | glEnable(GL_SCISSOR_TEST); |
| 78 | |
| 79 | if (!rgb) { |
| 80 | for (j = 0; j <= 12; j++) { |
| 81 | if (j <= 6) { |
| 82 | percent1 = j / 6.0; |
| 83 | r = 1.0 - 0.8 * percent1; |
| 84 | g = 0.2 + 0.8 * percent1; |
| 85 | b = 0.2; |
| 86 | } else { |
| 87 | percent1 = (j - 6) / 6.0; |
| 88 | r = 0.2; |
| 89 | g = 1.0 - 0.8 * percent1; |
| 90 | b = 0.2 + 0.8 * percent1; |
| 91 | } |
| 92 | glutSetColor(j+18, r, g, b); |
| 93 | for (i = 0; i < 16; i++) { |
| 94 | percent2 = i / 15.0; |
| 95 | glutSetColor(j*16+1+32, r*percent2, g*percent2, b*percent2); |
| 96 | } |
| 97 | } |
| 98 | color1 = 18; |
| 99 | color2 = 24; |
| 100 | color3 = 30; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | static void Reshape(int width, int height) |
| 105 | { |
| 106 | |
| 107 | windW = (GLint)width; |
| 108 | windH = (GLint)height; |
| 109 | } |
| 110 | |
| 111 | static void Key2(int key, int x, int y) |
| 112 | { |
| 113 | |
| 114 | switch (key) { |
| 115 | case GLUT_KEY_LEFT: |
| 116 | zRotation += 0.5; |
| 117 | break; |
| 118 | case GLUT_KEY_RIGHT: |
| 119 | zRotation -= 0.5; |
| 120 | break; |
| 121 | default: |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | glutPostRedisplay(); |
| 126 | } |
| 127 | |
| 128 | static void Key(unsigned char key, int x, int y) |
| 129 | { |
| 130 | |
| 131 | switch (key) { |
| 132 | case 27: |
| 133 | exit(1); |
| 134 | case 'Z': |
| 135 | zoom *= 0.75; |
| 136 | break; |
| 137 | case 'z': |
| 138 | zoom /= 0.75; |
| 139 | if (zoom > 10) { |
| 140 | zoom = 10; |
| 141 | } |
| 142 | break; |
| 143 | case '1': |
| 144 | glPolygonMode(GL_FRONT_AND_BACK, GL_POINT); |
| 145 | break; |
| 146 | case '2': |
| 147 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); |
| 148 | break; |
| 149 | case '3': |
| 150 | glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
| 151 | break; |
| 152 | case '4': |
| 153 | state = POINT; |
| 154 | break; |
| 155 | case '5': |
| 156 | state = LINE; |
| 157 | break; |
| 158 | case '6': |
| 159 | state = SOLID; |
| 160 | break; |
| 161 | case '7': |
| 162 | culling = !culling; |
| 163 | break; |
| 164 | case '8': |
| 165 | winding = !winding; |
| 166 | break; |
| 167 | case '9': |
| 168 | face = !face; |
| 169 | break; |
| 170 | case 'v': |
| 171 | showVerticies = !showVerticies; |
| 172 | break; |
| 173 | case 's': |
| 174 | shade = !shade; |
| 175 | (shade) ? glShadeModel(GL_SMOOTH) : glShadeModel(GL_FLAT); |
| 176 | break; |
| 177 | case 'h': |
| 178 | hideBottomTriangle = !hideBottomTriangle; |
| 179 | break; |
| 180 | case 'o': |
| 181 | outline = !outline; |
| 182 | break; |
| 183 | case 'm': |
| 184 | dithering = !dithering; |
| 185 | break; |
| 186 | case '0': |
| 187 | aaMode = !aaMode; |
| 188 | if (aaMode) { |
| 189 | glEnable(GL_POLYGON_SMOOTH); |
| 190 | glEnable(GL_BLEND); |
| 191 | glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
| 192 | if (!rgb) { |
| 193 | color1 = 32; |
| 194 | color2 = 128; |
| 195 | color3 = 224; |
| 196 | } |
| 197 | } else { |
| 198 | glDisable(GL_POLYGON_SMOOTH); |
| 199 | glDisable(GL_BLEND); |
| 200 | if (!rgb) { |
| 201 | color1 = 18; |
| 202 | color2 = 24; |
| 203 | color3 = 30; |
| 204 | } |
| 205 | } |
| 206 | break; |
| 207 | default: |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | glutPostRedisplay(); |
| 212 | } |
| 213 | |
| 214 | static void BeginPrim(void) |
| 215 | { |
| 216 | |
| 217 | switch (state) { |
| 218 | case SOLID: |
| 219 | glBegin(GL_POLYGON); |
| 220 | break; |
| 221 | case LINE: |
| 222 | glBegin(GL_LINE_LOOP); |
| 223 | break; |
| 224 | case POINT: |
| 225 | glBegin(GL_POINTS); |
| 226 | break; |
| 227 | default: |
| 228 | break; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | static void EndPrim(void) |
| 233 | { |
| 234 | |
| 235 | glEnd(); |
| 236 | } |
| 237 | |
| 238 | static void Draw(void) |
| 239 | { |
| 240 | float scaleX, scaleY; |
| 241 | |
| 242 | glViewport(0, 0, windW, windH); |
| 243 | |
| 244 | glMatrixMode(GL_PROJECTION); |
| 245 | glLoadIdentity(); |
| 246 | gluOrtho2D(-175, 175, -175, 175); |
| 247 | glMatrixMode(GL_MODELVIEW); |
| 248 | |
| 249 | glScissor(0, 0, windW, windH); |
| 250 | |
| 251 | (culling) ? glEnable(GL_CULL_FACE) : glDisable(GL_CULL_FACE); |
| 252 | (winding) ? glFrontFace(GL_CCW) : glFrontFace(GL_CW); |
| 253 | (face) ? glCullFace(GL_FRONT) : glCullFace(GL_BACK); |
| 254 | |
| 255 | (dithering) ? glEnable(GL_DITHER) : glDisable(GL_DITHER); |
| 256 | |
| 257 | glClear(GL_COLOR_BUFFER_BIT); |
| 258 | |
| 259 | SetColor(COLOR_GREEN); |
| 260 | glBegin(GL_LINE_LOOP); |
| 261 | glVertex3fv(boxA); |
| 262 | glVertex3fv(boxB); |
| 263 | glVertex3fv(boxC); |
| 264 | glVertex3fv(boxD); |
| 265 | glEnd(); |
| 266 | |
| 267 | if (!hideBottomTriangle) { |
| 268 | glPushMatrix(); |
| 269 | |
| 270 | glScalef(zoom, zoom, zoom); |
| 271 | glRotatef(zRotation, 0, 0, 1); |
| 272 | |
| 273 | SetColor(COLOR_BLUE); |
| 274 | BeginPrim(); |
| 275 | glVertex3fv(p0); |
| 276 | glVertex3fv(p1); |
| 277 | glVertex3fv(p2); |
| 278 | EndPrim(); |
| 279 | |
| 280 | if (showVerticies) { |
| 281 | (rgb) ? glColor3fv(RGBMap[COLOR_RED]) : glIndexf(color1); |
| 282 | glRectf(p0[0]-2, p0[1]-2, p0[0]+2, p0[1]+2); |
| 283 | (rgb) ? glColor3fv(RGBMap[COLOR_GREEN]) : glIndexf(color2); |
| 284 | glRectf(p1[0]-2, p1[1]-2, p1[0]+2, p1[1]+2); |
| 285 | (rgb) ? glColor3fv(RGBMap[COLOR_BLUE]) : glIndexf(color3); |
| 286 | glRectf(p2[0]-2, p2[1]-2, p2[0]+2, p2[1]+2); |
| 287 | } |
| 288 | |
| 289 | glPopMatrix(); |
| 290 | } |
| 291 | |
| 292 | scaleX = (float)(windW - 20) / 2 / 175 * (175 - 100) + 10; |
| 293 | scaleY = (float)(windH - 20) / 2 / 175 * (175 - 100) + 10; |
| 294 | |
| 295 | glViewport(scaleX, scaleY, windW-2*scaleX, windH-2*scaleY); |
| 296 | |
| 297 | glMatrixMode(GL_PROJECTION); |
| 298 | glLoadIdentity(); |
| 299 | gluOrtho2D(-100, 100, -100, 100); |
| 300 | glMatrixMode(GL_MODELVIEW); |
| 301 | |
| 302 | glScissor(scaleX, scaleY, windW-2*scaleX, windH-2*scaleY); |
| 303 | |
| 304 | glPushMatrix(); |
| 305 | |
| 306 | glScalef(zoom, zoom, zoom); |
| 307 | glRotatef(zRotation, 0,0,1); |
| 308 | |
| 309 | glPointSize(10); |
| 310 | glLineWidth(5); |
| 311 | glEnable(GL_POINT_SMOOTH); |
| 312 | glEnable(GL_LINE_STIPPLE); |
| 313 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 314 | |
| 315 | SetColor(COLOR_RED); |
| 316 | BeginPrim(); |
| 317 | (rgb) ? glColor3fv(RGBMap[COLOR_RED]) : glIndexf(color1); |
| 318 | glVertex3fv(p0); |
| 319 | (rgb) ? glColor3fv(RGBMap[COLOR_GREEN]) : glIndexf(color2); |
| 320 | glVertex3fv(p1); |
| 321 | (rgb) ? glColor3fv(RGBMap[COLOR_BLUE]) : glIndexf(color3); |
| 322 | glVertex3fv(p2); |
| 323 | EndPrim(); |
| 324 | |
| 325 | glPointSize(1); |
| 326 | glLineWidth(1); |
| 327 | glDisable(GL_POINT_SMOOTH); |
| 328 | glDisable(GL_LINE_STIPPLE); |
| 329 | glBlendFunc(GL_ONE, GL_ZERO); |
| 330 | |
| 331 | if (outline) { |
| 332 | SetColor(COLOR_WHITE); |
| 333 | glBegin(GL_LINE_LOOP); |
| 334 | glVertex3fv(p0); |
| 335 | glVertex3fv(p1); |
| 336 | glVertex3fv(p2); |
| 337 | glEnd(); |
| 338 | } |
| 339 | |
| 340 | glPopMatrix(); |
| 341 | |
| 342 | glFlush(); |
| 343 | |
| 344 | if (doubleBuffer) { |
| 345 | glutSwapBuffers(); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | static GLenum Args(int argc, char **argv) |
| 350 | { |
| 351 | GLint i; |
| 352 | |
| 353 | rgb = GL_TRUE; |
| 354 | doubleBuffer = GL_FALSE; |
| 355 | |
| 356 | for (i = 1; i < argc; i++) { |
| 357 | if (strcmp(argv[i], "-ci") == 0) { |
| 358 | rgb = GL_FALSE; |
| 359 | } else if (strcmp(argv[i], "-rgb") == 0) { |
| 360 | rgb = GL_TRUE; |
| 361 | } else if (strcmp(argv[i], "-sb") == 0) { |
| 362 | doubleBuffer = GL_FALSE; |
| 363 | } else if (strcmp(argv[i], "-db") == 0) { |
| 364 | doubleBuffer = GL_TRUE; |
| 365 | } else { |
| 366 | printf("%s (Bad option).\n", argv[i]); |
| 367 | return GL_FALSE; |
| 368 | } |
| 369 | } |
| 370 | return GL_TRUE; |
| 371 | } |
| 372 | |
| 373 | int main(int argc, char **argv) |
| 374 | { |
| 375 | glutInit(&argc, argv); |
| 376 | |
| 377 | if (Args(argc, argv) == GL_FALSE) { |
| 378 | exit(1); |
| 379 | } |
| 380 | |
| 381 | windW = 600; |
| 382 | windH = 300; |
| 383 | glutInitWindowPosition(0, 0); glutInitWindowSize( windW, windH); |
| 384 | |
| 385 | windType = (rgb) ? GLUT_RGB : GLUT_INDEX; |
| 386 | windType |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE; |
| 387 | glutInitDisplayMode(windType); |
| 388 | |
| 389 | if (glutCreateWindow("Triangle Test") == GL_FALSE) { |
| 390 | exit(1); |
| 391 | } |
| 392 | |
| 393 | InitMap(); |
| 394 | |
| 395 | Init(); |
| 396 | |
| 397 | glutReshapeFunc(Reshape); |
| 398 | glutKeyboardFunc(Key); |
| 399 | glutSpecialFunc(Key2); |
| 400 | glutDisplayFunc(Draw); |
| 401 | glutMainLoop(); |
| 402 | return 0; |
| 403 | } |