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