Advertisement
Guest User

Untitled

a guest
Jan 14th, 2012
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <GL/glut.h>
  5.  
  6. GLboolean doubleBuffer = GL_TRUE;
  7.  
  8. void draw_background()
  9. {
  10.     //big blue square
  11.     glColor3ub(0,255,255);
  12.     glBegin(GL_QUADS);
  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.     glColor3ub(255, 0, 0);
  23.     glBegin(GL_TRIANGLES);
  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_QUADS);
  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.     int win_width, win_height;
  55.     win_width = glutGet(GLUT_WINDOW_WIDTH);
  56.     win_height = glutGet(GLUT_WINDOW_HEIGHT);
  57.  
  58.     glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  59.  
  60.     glViewport(0, 0, win_width, win_height);
  61.  
  62.     glMatrixMode(GL_PROJECTION);
  63.     glLoadIdentity();
  64.     glOrtho(-10, 10, -10, 10, -1, 1);
  65.  
  66.     glMatrixMode(GL_MODELVIEW);
  67.     glLoadIdentity();
  68.  
  69.     glDisable(GL_STENCIL_TEST);
  70.     glStencilMask(0x0);
  71.    
  72.     draw_background();
  73.    
  74.     glEnable(GL_STENCIL_TEST);
  75.     glStencilMask(0x1);
  76.     glStencilFunc(GL_ALWAYS, 0x1, 0x1);
  77.     glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  78.     glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  79.     glDepthMask(GL_FALSE);
  80.    
  81.     draw_trangle();
  82.    
  83.     glStencilMask(0x1);
  84.     glStencilFunc(GL_NOTEQUAL, 0x1, 0x1);
  85.     glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  86.     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  87.     glDepthMask(GL_TRUE);
  88.    
  89.     draw_blue_square();
  90.    
  91.     if (doubleBuffer) {
  92.         glutSwapBuffers();
  93.     } else {
  94.         glFlush();
  95.     }
  96. }
  97.  
  98. static void
  99. Args(int argc, char **argv)
  100. {
  101.     GLint i;
  102.    
  103.     doubleBuffer = GL_TRUE;
  104.     for (i = 1; i < argc; i++) {
  105.         if (strcmp(argv[i], "-sb") == 0) {
  106.             doubleBuffer = GL_FALSE;
  107.         } else if (strcmp(argv[i], "-db") == 0) {
  108.             doubleBuffer = GL_TRUE;
  109.         }
  110.     }
  111. }
  112.  
  113. int
  114. main(int argc, char **argv)
  115. {
  116.     GLenum type;
  117.    
  118.     glutInit(&argc, argv);
  119.     Args(argc, argv);
  120.    
  121.     type = GLUT_RGB | GLUT_STENCIL;
  122.     type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  123.     glutInitDisplayMode(type);
  124.     glutCreateWindow("Stencil Test");
  125.    
  126.     glClearColor(0.0, 0.0, 0.0, 0.0);
  127.    
  128.     glutKeyboardFunc(Key);
  129.     glutDisplayFunc(Draw);
  130.     glutMainLoop();
  131.     return 0;    
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement