Advertisement
Guest User

Untitled

a guest
Jan 14th, 2012
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <glut.h>
  5.  
  6. GLboolean doubleBuffer;
  7.  
  8. void draw_background()
  9. {
  10. //big blue square
  11. glColor3ub(0,255,255);
  12. glBegin(GL_POLYGON);
  13. glVertex3i(5, 5, 0);
  14. glVertex3i(-5, 5, 0);
  15. glVertex3i(-5, -5, 0);
  16. glVertex3i(5, -5, 0);
  17. glEnd();
  18. }
  19. void draw_trangle()
  20. {
  21. /* transparent triangle */
  22. glColor4ub(255, 0, 0, 255);
  23. glBegin(GL_POLYGON);
  24. glVertex3i(-4, -4, 0);
  25. glVertex3i(4, -4, 0);
  26. glVertex3i(0, 4, 0);
  27. glEnd();
  28. }
  29. void draw_blue_square()
  30. {
  31. /* blue square */
  32. glColor3ub(0, 0, 200);
  33. glBegin(GL_POLYGON);
  34. glVertex3i(3, 3, 0);
  35. glVertex3i(-3, 3, 0);
  36. glVertex3i(-3, -3, 0);
  37. glVertex3i(3, -3, 0);
  38. glEnd();
  39. }
  40.  
  41. /* ARGSUSED1 */
  42. static void
  43. Key(unsigned char key, int x, int y)
  44. {
  45. switch (key) {
  46. case 27:
  47. exit(0);
  48. }
  49. }
  50.  
  51. static void
  52. Draw(void)
  53. {
  54. glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  55.  
  56. glDisable(GL_STENCIL_TEST);
  57. glStencilMask(0x0);
  58.  
  59. draw_background();
  60.  
  61.  
  62. glStencilMask(0x1);
  63. glStencilFunc(GL_ALWAYS, 0x1, 0x1);
  64. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  65. glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  66. glDepthMask(GL_FALSE);
  67.  
  68. draw_trangle();
  69.  
  70.  
  71. glEnable(GL_STENCIL_TEST);
  72. glStencilMask(0x0);
  73. glStencilFunc(GL_NOTEQUAL, 0x1, 0x1);
  74. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  75. glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  76. glDepthMask(GL_TRUE);
  77.  
  78. draw_blue_square();
  79.  
  80. if (doubleBuffer) {
  81. glutSwapBuffers();
  82. } else {
  83. glFlush();
  84. }
  85. }
  86.  
  87. static void
  88. Args(int argc, char **argv)
  89. {
  90. GLint i;
  91.  
  92. doubleBuffer = GL_TRUE;
  93. for (i = 1; i < argc; i++) {
  94. if (strcmp(argv[i], "-sb") == 0) {
  95. doubleBuffer = GL_FALSE;
  96. } else if (strcmp(argv[i], "-db") == 0) {
  97. doubleBuffer = GL_TRUE;
  98. }
  99. }
  100. }
  101.  
  102. int
  103. main(int argc, char **argv)
  104. {
  105. GLenum type;
  106.  
  107. glutInit(&argc, argv);
  108. Args(argc, argv);
  109.  
  110. type = GLUT_RGB | GLUT_STENCIL;
  111. type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  112. glutInitDisplayMode(type);
  113. glutCreateWindow("Stencil Test");
  114.  
  115. glClearColor(0.0, 0.0, 0.0, 0.0);
  116. glClearStencil(0);
  117. glStencilMask(1);
  118. glEnable(GL_STENCIL_TEST);
  119.  
  120. glMatrixMode(GL_PROJECTION);
  121. glLoadIdentity();
  122. glOrtho(-4, 4, -4, 4, -0.1, 0.1);
  123. glMatrixMode(GL_MODELVIEW);
  124.  
  125. glutKeyboardFunc(Key);
  126. glutDisplayFunc(Draw);
  127. glutMainLoop();
  128. return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement