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 <math.h> |
| 29 | #include <GL/glut.h> |
| 30 | |
| 31 | |
| 32 | #define VORDER 10 |
| 33 | #define CORDER 10 |
| 34 | #define TORDER 3 |
| 35 | |
| 36 | #define VMAJOR_ORDER 2 |
| 37 | #define VMINOR_ORDER 3 |
| 38 | |
| 39 | #define CMAJOR_ORDER 2 |
| 40 | #define CMINOR_ORDER 2 |
| 41 | |
| 42 | #define TMAJOR_ORDER 2 |
| 43 | #define TMINOR_ORDER 2 |
| 44 | |
| 45 | #define VDIM 4 |
| 46 | #define CDIM 4 |
| 47 | #define TDIM 2 |
| 48 | |
| 49 | #define ONE_D 1 |
| 50 | #define TWO_D 2 |
| 51 | |
| 52 | #define EVAL 3 |
| 53 | #define MESH 4 |
| 54 | |
| 55 | |
| 56 | GLenum doubleBuffer; |
| 57 | |
| 58 | float rotX = 0.0, rotY = 0.0, translateZ = -1.0; |
| 59 | |
| 60 | GLenum arrayType = ONE_D; |
| 61 | GLenum colorType = GL_FALSE; |
| 62 | GLenum textureType = GL_FALSE; |
| 63 | GLenum polygonFilled = GL_FALSE; |
| 64 | GLenum lighting = GL_FALSE; |
| 65 | GLenum mapPoint = GL_FALSE; |
| 66 | GLenum mapType = EVAL; |
| 67 | |
| 68 | double point1[10*4] = { |
| 69 | -0.5, 0.0, 0.0, 1.0, |
| 70 | -0.4, 0.5, 0.0, 1.0, |
| 71 | -0.3,-0.5, 0.0, 1.0, |
| 72 | -0.2, 0.5, 0.0, 1.0, |
| 73 | -0.1,-0.5, 0.0, 1.0, |
| 74 | 0.0, 0.5, 0.0, 1.0, |
| 75 | 0.1,-0.5, 0.0, 1.0, |
| 76 | 0.2, 0.5, 0.0, 1.0, |
| 77 | 0.3,-0.5, 0.0, 1.0, |
| 78 | 0.4, 0.0, 0.0, 1.0, |
| 79 | }; |
| 80 | double cpoint1[10*4] = { |
| 81 | 0.0, 0.0, 1.0, 1.0, |
| 82 | 0.3, 0.0, 0.7, 1.0, |
| 83 | 0.6, 0.0, 0.3, 1.0, |
| 84 | 1.0, 0.0, 0.0, 1.0, |
| 85 | 1.0, 0.3, 0.0, 1.0, |
| 86 | 1.0, 0.6, 0.0, 1.0, |
| 87 | 1.0, 1.0, 0.0, 1.0, |
| 88 | 1.0, 1.0, 0.5, 1.0, |
| 89 | 1.0, 1.0, 1.0, 1.0, |
| 90 | }; |
| 91 | double tpoint1[11*4] = { |
| 92 | 0.0, 0.0, 0.0, 1.0, |
| 93 | 0.0, 0.1, 0.0, 1.0, |
| 94 | 0.0, 0.2, 0.0, 1.0, |
| 95 | 0.0, 0.3, 0.0, 1.0, |
| 96 | 0.0, 0.4, 0.0, 1.0, |
| 97 | 0.0, 0.5, 0.0, 1.0, |
| 98 | 0.0, 0.6, 0.0, 1.0, |
| 99 | 0.0, 0.7, 0.0, 1.0, |
| 100 | 0.0, 0.8, 0.0, 1.0, |
| 101 | 0.0, 0.9, 0.0, 1.0, |
| 102 | }; |
| 103 | double point2[2*3*4] = { |
| 104 | -0.5, -0.5, 0.5, 1.0, |
| 105 | 0.0, 1.0, 0.5, 1.0, |
| 106 | 0.5, -0.5, 0.5, 1.0, |
| 107 | -0.5, 0.5, -0.5, 1.0, |
| 108 | 0.0, -1.0, -0.5, 1.0, |
| 109 | 0.5, 0.5, -0.5, 1.0, |
| 110 | }; |
| 111 | double cpoint2[2*2*4] = { |
| 112 | 0.0, 0.0, 0.0, 1.0, |
| 113 | 0.0, 0.0, 1.0, 1.0, |
| 114 | 0.0, 1.0, 0.0, 1.0, |
| 115 | 1.0, 1.0, 1.0, 1.0, |
| 116 | }; |
| 117 | double tpoint2[2*2*2] = { |
| 118 | 0.0, 0.0, 0.0, 1.0, |
| 119 | 1.0, 0.0, 1.0, 1.0, |
| 120 | }; |
| 121 | float textureImage[4*2*4] = { |
| 122 | 1.0, 1.0, 1.0, 1.0, |
| 123 | 1.0, 0.0, 0.0, 1.0, |
| 124 | 1.0, 0.0, 0.0, 1.0, |
| 125 | 1.0, 1.0, 1.0, 1.0, |
| 126 | 1.0, 1.0, 1.0, 1.0, |
| 127 | 1.0, 0.0, 0.0, 1.0, |
| 128 | 1.0, 0.0, 0.0, 1.0, |
| 129 | 1.0, 1.0, 1.0, 1.0, |
| 130 | }; |
| 131 | |
| 132 | |
| 133 | static void Init(void) |
| 134 | { |
| 135 | static float ambient[] = {0.1, 0.1, 0.1, 1.0}; |
| 136 | static float diffuse[] = {1.0, 1.0, 1.0, 1.0}; |
| 137 | static float position[] = {0.0, 0.0, -150.0, 0.0}; |
| 138 | static float front_mat_diffuse[] = {1.0, 0.2, 1.0, 1.0}; |
| 139 | static float back_mat_diffuse[] = {1.0, 1.0, 0.2, 1.0}; |
| 140 | static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0}; |
| 141 | static float lmodel_twoside[] = {GL_TRUE}; |
| 142 | static float decal[] = {GL_DECAL}; |
| 143 | static float repeat[] = {GL_REPEAT}; |
| 144 | static float nr[] = {GL_NEAREST}; |
| 145 | |
| 146 | glFrontFace(GL_CCW); |
| 147 | |
| 148 | glEnable(GL_DEPTH_TEST); |
| 149 | |
| 150 | glMap1d(GL_MAP1_VERTEX_4, 0.0, 1.0, VDIM, VORDER, point1); |
| 151 | glMap1d(GL_MAP1_COLOR_4, 0.0, 1.0, CDIM, CORDER, cpoint1); |
| 152 | |
| 153 | glMap2d(GL_MAP2_VERTEX_4, 0.0, 1.0, VMINOR_ORDER*VDIM, VMAJOR_ORDER, 0.0, |
| 154 | 1.0, VDIM, VMINOR_ORDER, point2); |
| 155 | glMap2d(GL_MAP2_COLOR_4, 0.0, 1.0, CMINOR_ORDER*CDIM, CMAJOR_ORDER, 0.0, |
| 156 | 1.0, CDIM, CMINOR_ORDER, cpoint2); |
| 157 | glMap2d(GL_MAP2_TEXTURE_COORD_2, 0.0, 1.0, TMINOR_ORDER*TDIM, |
| 158 | TMAJOR_ORDER, 0.0, 1.0, TDIM, TMINOR_ORDER, tpoint2); |
| 159 | |
| 160 | glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); |
| 161 | glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); |
| 162 | glLightfv(GL_LIGHT0, GL_POSITION, position); |
| 163 | |
| 164 | glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse); |
| 165 | glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse); |
| 166 | |
| 167 | glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); |
| 168 | glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside); |
| 169 | |
| 170 | glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, decal); |
| 171 | glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, repeat); |
| 172 | glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, repeat); |
| 173 | glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, nr); |
| 174 | glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, nr); |
| 175 | glTexImage2D(GL_TEXTURE_2D, 0, 4, 2, 4, 0, GL_RGBA, GL_FLOAT, |
| 176 | (GLvoid *)textureImage); |
| 177 | } |
| 178 | |
| 179 | static void DrawPoints1(void) |
| 180 | { |
| 181 | GLint i; |
| 182 | |
| 183 | glColor3f(0.0, 1.0, 0.0); |
| 184 | glPointSize(2); |
| 185 | glBegin(GL_POINTS); |
| 186 | for (i = 0; i < VORDER; i++) { |
| 187 | glVertex4dv(&point1[i*4]); |
| 188 | } |
| 189 | glEnd(); |
| 190 | } |
| 191 | |
| 192 | static void DrawPoints2(void) |
| 193 | { |
| 194 | GLint i, j; |
| 195 | |
| 196 | glColor3f(1.0, 0.0, 1.0); |
| 197 | glPointSize(2); |
| 198 | glBegin(GL_POINTS); |
| 199 | for (i = 0; i < VMAJOR_ORDER; i++) { |
| 200 | for (j = 0; j < VMINOR_ORDER; j++) { |
| 201 | glVertex4dv(&point2[i*4*VMINOR_ORDER+j*4]); |
| 202 | } |
| 203 | } |
| 204 | glEnd(); |
| 205 | } |
| 206 | |
| 207 | static void DrawMapEval1(float du) |
| 208 | { |
| 209 | float u; |
| 210 | |
| 211 | glColor3f(1.0, 0.0, 0.0); |
| 212 | glBegin(GL_LINE_STRIP); |
| 213 | for (u = 0.0; u < 1.0; u += du) { |
| 214 | glEvalCoord1d(u); |
| 215 | } |
| 216 | glEvalCoord1d(1.0); |
| 217 | glEnd(); |
| 218 | } |
| 219 | |
| 220 | static void DrawMapEval2(float du, float dv) |
| 221 | { |
| 222 | float u, v, tmp; |
| 223 | |
| 224 | glColor3f(1.0, 0.0, 0.0); |
| 225 | for (v = 0.0; v < 1.0; v += dv) { |
| 226 | glBegin(GL_QUAD_STRIP); |
| 227 | for (u = 0.0; u <= 1.0; u += du) { |
| 228 | glEvalCoord2d(u,v); |
| 229 | tmp = (v + dv < 1.0) ? (v + dv) : 1.0; |
| 230 | glEvalCoord2d(u, tmp); |
| 231 | } |
| 232 | glEvalCoord2d(1.0, v); |
| 233 | glEvalCoord2d(1.0, v+dv); |
| 234 | glEnd(); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | static void RenderEval(void) |
| 239 | { |
| 240 | |
| 241 | if (colorType) { |
| 242 | glEnable(GL_MAP1_COLOR_4); |
| 243 | glEnable(GL_MAP2_COLOR_4); |
| 244 | } else { |
| 245 | glDisable(GL_MAP1_COLOR_4); |
| 246 | glDisable(GL_MAP2_COLOR_4); |
| 247 | } |
| 248 | |
| 249 | if (textureType) { |
| 250 | glEnable(GL_TEXTURE_2D); |
| 251 | glEnable(GL_MAP2_TEXTURE_COORD_2); |
| 252 | } else { |
| 253 | glDisable(GL_TEXTURE_2D); |
| 254 | glDisable(GL_MAP2_TEXTURE_COORD_2); |
| 255 | } |
| 256 | |
| 257 | if (polygonFilled) { |
| 258 | glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
| 259 | } else { |
| 260 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); |
| 261 | } |
| 262 | |
| 263 | glShadeModel(GL_SMOOTH); |
| 264 | |
| 265 | switch (mapType) { |
| 266 | case EVAL: |
| 267 | switch (arrayType) { |
| 268 | case ONE_D: |
| 269 | glDisable(GL_MAP2_VERTEX_4); |
| 270 | glEnable(GL_MAP1_VERTEX_4); |
| 271 | DrawPoints1(); |
| 272 | DrawMapEval1(0.1/VORDER); |
| 273 | break; |
| 274 | case TWO_D: |
| 275 | glDisable(GL_MAP1_VERTEX_4); |
| 276 | glEnable(GL_MAP2_VERTEX_4); |
| 277 | DrawPoints2(); |
| 278 | DrawMapEval2(0.1/VMAJOR_ORDER,0.1/VMINOR_ORDER); |
| 279 | break; |
| 280 | default: |
| 281 | break; |
| 282 | } |
| 283 | break; |
| 284 | case MESH: |
| 285 | switch (arrayType) { |
| 286 | case ONE_D: |
| 287 | DrawPoints1(); |
| 288 | glDisable(GL_MAP2_VERTEX_4); |
| 289 | glEnable (GL_MAP1_VERTEX_4); |
| 290 | glColor3f(0.0, 0.0, 1.0); |
| 291 | glMapGrid1d(40, 0.0, 1.0); |
| 292 | if (mapPoint) { |
| 293 | glPointSize(2); |
| 294 | glEvalMesh1(GL_POINT, 0, 40); |
| 295 | } else { |
| 296 | glEvalMesh1(GL_LINE, 0, 40); |
| 297 | } |
| 298 | break; |
| 299 | case TWO_D: |
| 300 | DrawPoints2(); |
| 301 | glDisable(GL_MAP1_VERTEX_4); |
| 302 | glEnable(GL_MAP2_VERTEX_4); |
| 303 | glColor3f(0.0, 0.0, 1.0); |
| 304 | glMapGrid2d(20, 0.0, 1.0, 20, 0.0, 1.0); |
| 305 | if (mapPoint) { |
| 306 | glPointSize(2); |
| 307 | glEvalMesh2(GL_POINT, 0, 20, 0, 20); |
| 308 | } else if (polygonFilled) { |
| 309 | glEvalMesh2(GL_FILL, 0, 20, 0, 20); |
| 310 | } else { |
| 311 | glEvalMesh2(GL_LINE, 0, 20, 0, 20); |
| 312 | } |
| 313 | break; |
| 314 | default: |
| 315 | break; |
| 316 | } |
| 317 | break; |
| 318 | default: |
| 319 | break; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | static void Reshape(int width, int height) |
| 324 | { |
| 325 | |
| 326 | glViewport(0, 0, (GLint)width, (GLint)height); |
| 327 | |
| 328 | glMatrixMode(GL_PROJECTION); |
| 329 | glLoadIdentity(); |
| 330 | glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 10.0); |
| 331 | glMatrixMode(GL_MODELVIEW); |
| 332 | } |
| 333 | |
| 334 | static void Key2(int key, int x, int y) |
| 335 | { |
| 336 | switch (key) { |
| 337 | case GLUT_KEY_LEFT: |
| 338 | rotY -= 30; |
| 339 | break; |
| 340 | case GLUT_KEY_RIGHT: |
| 341 | rotY += 30; |
| 342 | break; |
| 343 | case GLUT_KEY_UP: |
| 344 | rotX -= 30; |
| 345 | break; |
| 346 | case GLUT_KEY_DOWN: |
| 347 | rotX += 30; |
| 348 | break; |
| 349 | default: |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | glutPostRedisplay(); |
| 354 | } |
| 355 | |
| 356 | static void Key(unsigned char key, int x, int y) |
| 357 | { |
| 358 | switch (key) { |
| 359 | case 27: |
| 360 | exit(1); |
| 361 | case '1': |
| 362 | arrayType = ONE_D; |
| 363 | break; |
| 364 | case '2': |
| 365 | arrayType = TWO_D; |
| 366 | break; |
| 367 | case 'e': |
| 368 | mapType = EVAL; |
| 369 | break; |
| 370 | case 'm': |
| 371 | mapType = MESH; |
| 372 | break; |
| 373 | case 'f': |
| 374 | polygonFilled = !polygonFilled; |
| 375 | break; |
| 376 | case 'p': |
| 377 | mapPoint = !mapPoint; |
| 378 | break; |
| 379 | case 'c': |
| 380 | colorType = !colorType; |
| 381 | break; |
| 382 | case 't': |
| 383 | textureType = !textureType; |
| 384 | break; |
| 385 | case 'l': |
| 386 | lighting =! lighting; |
| 387 | if (lighting) { |
| 388 | glEnable(GL_LIGHTING); |
| 389 | glEnable(GL_LIGHT0); |
| 390 | glEnable(GL_AUTO_NORMAL); |
| 391 | } else { |
| 392 | glDisable(GL_LIGHTING); |
| 393 | glDisable(GL_LIGHT0); |
| 394 | glDisable(GL_AUTO_NORMAL); |
| 395 | } |
| 396 | break; |
| 397 | default: |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | glutPostRedisplay(); |
| 402 | } |
| 403 | |
| 404 | static void Draw(void) |
| 405 | { |
| 406 | |
| 407 | glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
| 408 | |
| 409 | glPushMatrix(); |
| 410 | |
| 411 | glTranslatef(0.0, 0.0 , translateZ); |
| 412 | glRotatef(rotX, 1, 0, 0); |
| 413 | glRotatef(rotY, 0, 1, 0); |
| 414 | RenderEval(); |
| 415 | |
| 416 | glPopMatrix(); |
| 417 | |
| 418 | glFlush(); |
| 419 | |
| 420 | if (doubleBuffer) { |
| 421 | glutSwapBuffers(); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | static GLenum Args(int argc, char **argv) |
| 426 | { |
| 427 | GLint i; |
| 428 | |
| 429 | doubleBuffer = GL_FALSE; |
| 430 | |
| 431 | for (i = 1; i < argc; i++) { |
| 432 | if (strcmp(argv[i], "-sb") == 0) { |
| 433 | doubleBuffer = GL_FALSE; |
| 434 | } else if (strcmp(argv[i], "-db") == 0) { |
| 435 | doubleBuffer = GL_TRUE; |
| 436 | } else { |
| 437 | printf("%s (Bad option).\n", argv[i]); |
| 438 | return GL_FALSE; |
| 439 | } |
| 440 | } |
| 441 | return GL_TRUE; |
| 442 | } |
| 443 | |
| 444 | int main(int argc, char **argv) |
| 445 | { |
| 446 | GLenum type; |
| 447 | |
| 448 | glutInit(&argc, argv); |
| 449 | |
| 450 | if (Args(argc, argv) == GL_FALSE) { |
| 451 | exit(1); |
| 452 | } |
| 453 | |
| 454 | glutInitWindowPosition(0, 0); glutInitWindowSize( 300, 300); |
| 455 | |
| 456 | type = GLUT_RGB | GLUT_DEPTH; |
| 457 | type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE; |
| 458 | glutInitDisplayMode(type); |
| 459 | |
| 460 | if (glutCreateWindow("Evaluator Test") == GL_FALSE) { |
| 461 | exit(1); |
| 462 | } |
| 463 | |
| 464 | Init(); |
| 465 | |
| 466 | glutReshapeFunc(Reshape); |
| 467 | glutKeyboardFunc(Key); |
| 468 | glutSpecialFunc(Key2); |
| 469 | glutDisplayFunc(Draw); |
| 470 | glutMainLoop(); |
| 471 | return 0; |
| 472 | } |