1#include <stdio.h> 2#include <string.h> 3#include <stdlib.h> 4#include "glut_wrap.h" 5 6 7#define PIXEL_CENTER(x) ((long)(x) + 0.5) 8 9#define GAP 10 10#define ROWS 3 11#define COLS 4 12 13#define OPENGL_WIDTH 48 14#define OPENGL_HEIGHT 13 15 16 17GLenum rgb, doubleBuffer, windType; 18GLint windW, windH; 19 20GLenum mode1, mode2; 21GLint boxW, boxH; 22GLubyte OpenGL_bits[] = { 23 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 24 0x7f, 0xfb, 0xff, 0xff, 0xff, 0x01, 25 0x7f, 0xfb, 0xff, 0xff, 0xff, 0x01, 26 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 27 0x3e, 0x8f, 0xb7, 0xf9, 0xfc, 0x01, 28 0x63, 0xdb, 0xb0, 0x8d, 0x0d, 0x00, 29 0x63, 0xdb, 0xb7, 0x8d, 0x0d, 0x00, 30 0x63, 0xdb, 0xb6, 0x8d, 0x0d, 0x00, 31 0x63, 0x8f, 0xf3, 0xcc, 0x0d, 0x00, 32 0x63, 0x00, 0x00, 0x0c, 0x4c, 0x0a, 33 0x63, 0x00, 0x00, 0x0c, 0x4c, 0x0e, 34 0x63, 0x00, 0x00, 0x8c, 0xed, 0x0e, 35 0x3e, 0x00, 0x00, 0xf8, 0x0c, 0x00, 36}; 37 38 39#include "tkmap.c" 40 41static void Init(void) 42{ 43 44 mode1 = GL_TRUE; 45 mode2 = GL_TRUE; 46} 47 48static void Reshape(int width, int height) 49{ 50 51 windW = (GLint)width; 52 windH = (GLint)height; 53} 54 55static void RotateColorMask(void) 56{ 57 static GLint rotation = 0; 58 59 rotation = (rotation + 1) & 0x3; 60 switch (rotation) { 61 case 0: 62 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 63 glIndexMask( 0xff ); 64 break; 65 case 1: 66 glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE); 67 glIndexMask(0xFE); 68 break; 69 case 2: 70 glColorMask(GL_TRUE, GL_FALSE, GL_TRUE, GL_TRUE); 71 glIndexMask(0xFD); 72 break; 73 case 3: 74 glColorMask(GL_TRUE, GL_TRUE, GL_FALSE, GL_TRUE); 75 glIndexMask(0xFB); 76 break; 77 } 78} 79 80static void Key(unsigned char key, int x, int y) 81{ 82 83 switch (key) { 84 case 27: 85 exit(1); 86 case '1': 87 mode1 = !mode1; 88 break; 89 case '2': 90 mode2 = !mode2; 91 break; 92 case '3': 93 RotateColorMask(); 94 break; 95 default: 96 return; 97 } 98 99 glutPostRedisplay(); 100} 101 102static void Viewport(GLint row, GLint column) 103{ 104 GLint x, y; 105 106 boxW = (windW - (COLS + 1) * GAP) / COLS; 107 boxH = (windH - (ROWS + 1) * GAP) / ROWS; 108 109 x = GAP + column * (boxW + GAP); 110 y = GAP + row * (boxH + GAP); 111 112 glViewport(x, y, boxW, boxH); 113 114 glMatrixMode(GL_PROJECTION); 115 glLoadIdentity(); 116 glOrtho(-boxW/2, boxW/2, -boxH/2, boxH/2, 0.0, 1.0); 117 glMatrixMode(GL_MODELVIEW); 118 119 glEnable(GL_SCISSOR_TEST); 120 glScissor(x, y, boxW, boxH); 121} 122 123static void Point(void) 124{ 125 GLint i; 126 127 glBegin(GL_POINTS); 128 SetColor(COLOR_WHITE); 129 glVertex2i(0, 0); 130 for (i = 1; i < 8; i++) { 131 GLint j = i * 2; 132 SetColor(COLOR_BLACK+i); 133 glVertex2i(-j, -j); 134 glVertex2i(-j, 0); 135 glVertex2i(-j, j); 136 glVertex2i(0, j); 137 glVertex2i(j, j); 138 glVertex2i(j, 0); 139 glVertex2i(j, -j); 140 glVertex2i(0, -j); 141 } 142 glEnd(); 143} 144 145static void Lines(void) 146{ 147 GLint i; 148 149 glPushMatrix(); 150 151 glTranslatef(-12, 0, 0); 152 for (i = 1; i < 8; i++) { 153 SetColor(COLOR_BLACK+i); 154 glBegin(GL_LINES); 155 glVertex2i(-boxW/4, -boxH/4); 156 glVertex2i(boxW/4, boxH/4); 157 glEnd(); 158 glTranslatef(4, 0, 0); 159 } 160 161 glPopMatrix(); 162 163 glBegin(GL_LINES); 164 glVertex2i(0, 0); 165 glEnd(); 166} 167 168static void LineStrip(void) 169{ 170 171 glBegin(GL_LINE_STRIP); 172 SetColor(COLOR_RED); 173 glVertex2f(PIXEL_CENTER(-boxW/4), PIXEL_CENTER(-boxH/4)); 174 SetColor(COLOR_GREEN); 175 glVertex2f(PIXEL_CENTER(-boxW/4), PIXEL_CENTER(boxH/4)); 176 SetColor(COLOR_BLUE); 177 glVertex2f(PIXEL_CENTER(boxW/4), PIXEL_CENTER(boxH/4)); 178 SetColor(COLOR_WHITE); 179 glVertex2f(PIXEL_CENTER(boxW/4), PIXEL_CENTER(-boxH/4)); 180 glEnd(); 181 182 glBegin(GL_LINE_STRIP); 183 glVertex2i(0, 0); 184 glEnd(); 185} 186 187static void LineLoop(void) 188{ 189 190 glBegin(GL_LINE_LOOP); 191 SetColor(COLOR_RED); 192 glVertex2f(PIXEL_CENTER(-boxW/4), PIXEL_CENTER(-boxH/4)); 193 SetColor(COLOR_GREEN); 194 glVertex2f(PIXEL_CENTER(-boxW/4), PIXEL_CENTER(boxH/4)); 195 SetColor(COLOR_BLUE); 196 glVertex2f(PIXEL_CENTER(boxW/4), PIXEL_CENTER(boxH/4)); 197 SetColor(COLOR_WHITE); 198 glVertex2f(PIXEL_CENTER(boxW/4), PIXEL_CENTER(-boxH/4)); 199 glEnd(); 200 201 glEnable(GL_LOGIC_OP); 202 glLogicOp(GL_XOR); 203 204 glEnable(GL_BLEND); 205 glBlendFunc(GL_ONE, GL_ONE); 206 207 SetColor(COLOR_MAGENTA); 208 glBegin(GL_LINE_LOOP); 209 glVertex2f(PIXEL_CENTER(-boxW/8), PIXEL_CENTER(-boxH/8)); 210 glVertex2f(PIXEL_CENTER(-boxW/8), PIXEL_CENTER(boxH/8)); 211 glEnd(); 212 glBegin(GL_LINE_LOOP); 213 glVertex2f(PIXEL_CENTER(-boxW/8), PIXEL_CENTER(boxH/8+5)); 214 glVertex2f(PIXEL_CENTER(boxW/8), PIXEL_CENTER(boxH/8+5)); 215 glEnd(); 216 glDisable(GL_LOGIC_OP); 217 glDisable(GL_BLEND); 218 219 SetColor(COLOR_GREEN); 220 glBegin(GL_POINTS); 221 glVertex2i(0, 0); 222 glEnd(); 223 224 glBegin(GL_LINE_LOOP); 225 glVertex2i(0, 0); 226 glEnd(); 227} 228 229static void Bitmap(void) 230{ 231 232 glBegin(GL_LINES); 233 SetColor(COLOR_GREEN); 234 glVertex2i(-boxW/2, 0); 235 glVertex2i(boxW/2, 0); 236 glVertex2i(0, -boxH/2); 237 glVertex2i(0, boxH/2); 238 SetColor(COLOR_RED); 239 glVertex2i(0, -3); 240 glVertex2i(0, -3+OPENGL_HEIGHT); 241 SetColor(COLOR_BLUE); 242 glVertex2i(0, -3); 243 glVertex2i(OPENGL_WIDTH, -3); 244 glEnd(); 245 246 SetColor(COLOR_GREEN); 247 248 glPixelStorei(GL_UNPACK_LSB_FIRST, GL_TRUE); 249 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 250 251 glRasterPos2i(0, 0); 252 glBitmap(OPENGL_WIDTH, OPENGL_HEIGHT, 0, 3, 0.0, 0.0, OpenGL_bits); 253} 254 255static void Triangles(void) 256{ 257 258 glBegin(GL_TRIANGLES); 259 SetColor(COLOR_GREEN); 260 glVertex2i(-boxW/4, -boxH/4); 261 SetColor(COLOR_RED); 262 glVertex2i(-boxW/8, -boxH/16); 263 SetColor(COLOR_BLUE); 264 glVertex2i(boxW/8, -boxH/16); 265 266 SetColor(COLOR_GREEN); 267 glVertex2i(-boxW/4, boxH/4); 268 SetColor(COLOR_RED); 269 glVertex2i(-boxW/8, boxH/16); 270 SetColor(COLOR_BLUE); 271 glVertex2i(boxW/8, boxH/16); 272 glEnd(); 273 274 glBegin(GL_TRIANGLES); 275 glVertex2i(0, 0); 276 glVertex2i(-100, 100); 277 glEnd(); 278} 279 280static void TriangleStrip(void) 281{ 282 283 glBegin(GL_TRIANGLE_STRIP); 284 SetColor(COLOR_GREEN); 285 glVertex2i(-boxW/4, -boxH/4); 286 SetColor(COLOR_RED); 287 glVertex2i(-boxW/4, boxH/4); 288 SetColor(COLOR_BLUE); 289 glVertex2i(0, -boxH/4); 290 SetColor(COLOR_WHITE); 291 glVertex2i(0, boxH/4); 292 SetColor(COLOR_CYAN); 293 glVertex2i(boxW/4, -boxH/4); 294 SetColor(COLOR_YELLOW); 295 glVertex2i(boxW/4, boxH/4); 296 glEnd(); 297 298 glBegin(GL_TRIANGLE_STRIP); 299 glVertex2i(0, 0); 300 glVertex2i(-100, 100); 301 glEnd(); 302} 303 304static void TriangleFan(void) 305{ 306 GLint vx[8][2]; 307 GLint x0, y0, x1, y1, x2, y2, x3, y3; 308 GLint i; 309 310 y0 = -boxH/4; 311 y1 = y0 + boxH/2/3; 312 y2 = y1 + boxH/2/3; 313 y3 = boxH/4; 314 x0 = -boxW/4; 315 x1 = x0 + boxW/2/3; 316 x2 = x1 + boxW/2/3; 317 x3 = boxW/4; 318 319 vx[0][0] = x0; vx[0][1] = y1; 320 vx[1][0] = x0; vx[1][1] = y2; 321 vx[2][0] = x1; vx[2][1] = y3; 322 vx[3][0] = x2; vx[3][1] = y3; 323 vx[4][0] = x3; vx[4][1] = y2; 324 vx[5][0] = x3; vx[5][1] = y1; 325 vx[6][0] = x2; vx[6][1] = y0; 326 vx[7][0] = x1; vx[7][1] = y0; 327 328 glBegin(GL_TRIANGLE_FAN); 329 SetColor(COLOR_WHITE); 330 glVertex2i(0, 0); 331 for (i = 0; i < 8; i++) { 332 SetColor(COLOR_WHITE-i); 333 glVertex2iv(vx[i]); 334 } 335 glEnd(); 336 337 glBegin(GL_TRIANGLE_FAN); 338 glVertex2i(0, 0); 339 glVertex2i(-100, 100); 340 glEnd(); 341} 342 343static void Rect(void) 344{ 345 346 SetColor(COLOR_GREEN); 347 glRecti(-boxW/4, -boxH/4, boxW/4, boxH/4); 348} 349 350static void PolygonFunc(void) 351{ 352 GLint vx[8][2]; 353 GLint x0, y0, x1, y1, x2, y2, x3, y3; 354 GLint i; 355 356 y0 = -boxH/4; 357 y1 = y0 + boxH/2/3; 358 y2 = y1 + boxH/2/3; 359 y3 = boxH/4; 360 x0 = -boxW/4; 361 x1 = x0 + boxW/2/3; 362 x2 = x1 + boxW/2/3; 363 x3 = boxW/4; 364 365 vx[0][0] = x0; vx[0][1] = y1; 366 vx[1][0] = x0; vx[1][1] = y2; 367 vx[2][0] = x1; vx[2][1] = y3; 368 vx[3][0] = x2; vx[3][1] = y3; 369 vx[4][0] = x3; vx[4][1] = y2; 370 vx[5][0] = x3; vx[5][1] = y1; 371 vx[6][0] = x2; vx[6][1] = y0; 372 vx[7][0] = x1; vx[7][1] = y0; 373 374 glBegin(GL_POLYGON); 375 for (i = 0; i < 8; i++) { 376 SetColor(COLOR_WHITE-i); 377 glVertex2iv(vx[i]); 378 } 379 glEnd(); 380 381 glBegin(GL_POLYGON); 382 glVertex2i(0, 0); 383 glVertex2i(100, 100); 384 glEnd(); 385} 386 387static void Quads(void) 388{ 389 390 glBegin(GL_QUADS); 391 SetColor(COLOR_GREEN); 392 glVertex2i(-boxW/4, -boxH/4); 393 SetColor(COLOR_RED); 394 glVertex2i(-boxW/8, -boxH/16); 395 SetColor(COLOR_BLUE); 396 glVertex2i(boxW/8, -boxH/16); 397 SetColor(COLOR_WHITE); 398 glVertex2i(boxW/4, -boxH/4); 399 400 SetColor(COLOR_GREEN); 401 glVertex2i(-boxW/4, boxH/4); 402 SetColor(COLOR_RED); 403 glVertex2i(-boxW/8, boxH/16); 404 SetColor(COLOR_BLUE); 405 glVertex2i(boxW/8, boxH/16); 406 SetColor(COLOR_WHITE); 407 glVertex2i(boxW/4, boxH/4); 408 glEnd(); 409 410 glBegin(GL_QUADS); 411 glVertex2i(0, 0); 412 glVertex2i(100, 100); 413 glVertex2i(-100, 100); 414 glEnd(); 415} 416 417static void QuadStrip(void) 418{ 419 420 glBegin(GL_QUAD_STRIP); 421 SetColor(COLOR_GREEN); 422 glVertex2i(-boxW/4, -boxH/4); 423 SetColor(COLOR_RED); 424 glVertex2i(-boxW/4, boxH/4); 425 SetColor(COLOR_BLUE); 426 glVertex2i(0, -boxH/4); 427 SetColor(COLOR_WHITE); 428 glVertex2i(0, boxH/4); 429 SetColor(COLOR_CYAN); 430 glVertex2i(boxW/4, -boxH/4); 431 SetColor(COLOR_YELLOW); 432 glVertex2i(boxW/4, boxH/4); 433 glEnd(); 434 435 glBegin(GL_QUAD_STRIP); 436 glVertex2i(0, 0); 437 glVertex2i(100, 100); 438 glVertex2i(-100, 100); 439 glEnd(); 440} 441 442static void Draw(void) 443{ 444 445 glViewport(0, 0, windW, windH); 446 glDisable(GL_SCISSOR_TEST); 447 448 glPushAttrib(GL_COLOR_BUFFER_BIT); 449 450 glColorMask(1, 1, 1, 1); 451 glIndexMask(~0); 452 453 glClearColor(0.0, 0.0, 0.0, 0.0); 454 glClear(GL_COLOR_BUFFER_BIT); 455 456 glPopAttrib(); 457 458 if (mode1) { 459 glShadeModel(GL_SMOOTH); 460 } else { 461 glShadeModel(GL_FLAT); 462 } 463 464 if (mode2) { 465 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); 466 } else { 467 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 468 } 469 470 Viewport(0, 0); Point(); 471 Viewport(0, 1); Lines(); 472 Viewport(0, 2); LineStrip(); 473 Viewport(0, 3); LineLoop(); 474 475 Viewport(1, 0); Bitmap(); 476 Viewport(1, 1); TriangleFan(); 477 Viewport(1, 2); Triangles(); 478 Viewport(1, 3); TriangleStrip(); 479 480 Viewport(2, 0); Rect(); 481 Viewport(2, 1); PolygonFunc(); 482 Viewport(2, 2); Quads(); 483 Viewport(2, 3); QuadStrip(); 484 485 glFlush(); 486 487 if (doubleBuffer) { 488 glutSwapBuffers(); 489 } 490} 491 492static GLenum Args(int argc, char **argv) 493{ 494 GLint i; 495 496 rgb = GL_TRUE; 497 doubleBuffer = GL_FALSE; 498 499 for (i = 1; i < argc; i++) { 500 if (strcmp(argv[i], "-ci") == 0) { 501 rgb = GL_FALSE; 502 } else if (strcmp(argv[i], "-rgb") == 0) { 503 rgb = GL_TRUE; 504 } else if (strcmp(argv[i], "-sb") == 0) { 505 doubleBuffer = GL_FALSE; 506 } else if (strcmp(argv[i], "-db") == 0) { 507 doubleBuffer = GL_TRUE; 508 } else { 509 printf("%s (Bad option).\n", argv[i]); 510 return GL_FALSE; 511 } 512 } 513 return GL_TRUE; 514} 515 516int main(int argc, char **argv) 517{ 518 glutInit(&argc, argv); 519 520 if (Args(argc, argv) == GL_FALSE) { 521 exit(1); 522 } 523 524 windW = 600; 525 windH = 300; 526 glutInitWindowPosition(0, 0); glutInitWindowSize( windW, windH); 527 528 windType = (rgb) ? GLUT_RGB : GLUT_INDEX; 529 windType |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE; 530 glutInitDisplayMode(windType); 531 532 if (glutCreateWindow("Primitive Test") == GL_FALSE) { 533 exit(1); 534 } 535 536 InitMap(); 537 538 Init(); 539 540 glutReshapeFunc(Reshape); 541 glutKeyboardFunc(Key); 542 glutDisplayFunc(Draw); 543 glutMainLoop(); 544 return 0; 545} 546