Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

blendxor.c 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. ** blendxor.c - Demonstrates the use of the blend_logic_op
  3. ** extension to draw hilights. Using XOR to draw the same
  4. ** image twice restores the background to its original value.
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #ifndef _WIN32
  9. #include <unistd.h>
  10. #endif
  11. #include <stdlib.h>
  12. #include <GL/glut.h>
  13. GLenum doubleBuffer;
  14. int dithering = 0;
  15. GLint windW, windH;
  16. static void Init(void)
  17. {
  18. glDisable(GL_DITHER);
  19. glShadeModel(GL_FLAT);
  20. }
  21. static void Reshape(int width, int height)
  22. {
  23. windW = (GLint)width;
  24. windH = (GLint)height;
  25. glViewport(0, 0, (GLint)width, (GLint)height);
  26. glMatrixMode(GL_PROJECTION);
  27. glLoadIdentity();
  28. gluOrtho2D(0, 400, 0, 400);
  29. glMatrixMode(GL_MODELVIEW);
  30. }
  31. static void Key(unsigned char key, int x, int y)
  32. {
  33. switch (key) {
  34. case 27:
  35. exit(1);
  36. case 'd':
  37. dithering = !dithering;
  38. break;
  39. default:
  40. return;
  41. }
  42. glutPostRedisplay();
  43. }
  44. static void Draw(void)
  45. {
  46. int i;
  47. glDisable(GL_BLEND);
  48. (dithering) ? glEnable(GL_DITHER) : glDisable(GL_DITHER);
  49. glClearColor(0.5, 0.6, 0.1, 1.0);
  50. glClear(GL_COLOR_BUFFER_BIT);
  51. /* Draw background prims */
  52. glColor3f(0.1, 0.1, 1.0);
  53. glBegin(GL_TRIANGLES);
  54. glVertex2i(5, 5);
  55. glVertex2i(130, 50);
  56. glVertex2i(100, 300);
  57. glEnd();
  58. glColor3f(0.5, 0.2, 0.9);
  59. glBegin(GL_TRIANGLES);
  60. glVertex2i(200, 100);
  61. glVertex2i(330, 50);
  62. glVertex2i(340, 400);
  63. glEnd();
  64. glEnable(GL_BLEND);
  65. glBlendEquationEXT(GL_LOGIC_OP);
  66. glLogicOp(GL_XOR);
  67. /* Draw a set of rectangles across the window */
  68. glColor3f(0.9, 0.2, 0.8);
  69. for(i = 0; i < 400; i+=60) {
  70. glBegin(GL_POLYGON);
  71. glVertex2i(i, 100);
  72. glVertex2i(i+50, 100);
  73. glVertex2i(i+50, 200);
  74. glVertex2i(i, 200);
  75. glEnd();
  76. }
  77. glFlush(); /* Added by Brian Paul */
  78. #ifndef _WIN32
  79. sleep(2);
  80. #endif
  81. /* Redraw the rectangles, which should erase them */
  82. for(i = 0; i < 400; i+=60) {
  83. glBegin(GL_POLYGON);
  84. glVertex2i(i, 100);
  85. glVertex2i(i+50, 100);
  86. glVertex2i(i+50, 200);
  87. glVertex2i(i, 200);
  88. glEnd();
  89. }
  90. glFlush();
  91. if (doubleBuffer) {
  92. glutSwapBuffers();
  93. }
  94. }
  95. static GLenum Args(int argc, char **argv)
  96. {
  97. GLint i;
  98. doubleBuffer = GL_FALSE;
  99. for (i = 1; i < argc; i++) {
  100. if (strcmp(argv[i], "-sb") == 0) {
  101. doubleBuffer = GL_FALSE;
  102. } else if (strcmp(argv[i], "-db") == 0) {
  103. doubleBuffer = GL_TRUE;
  104. } else {
  105. printf("%s (Bad option).\n", argv[i]);
  106. return GL_FALSE;
  107. }
  108. }
  109. return GL_TRUE;
  110. }
  111. int main(int argc, char **argv)
  112. {
  113. GLenum type;
  114. char *s;
  115. char *extName = "GL_EXT_blend_logic_op";
  116. glutInit(&argc, argv);
  117. if (Args(argc, argv) == GL_FALSE) {
  118. exit(1);
  119. }
  120. glutInitWindowPosition(0, 0); glutInitWindowSize( 400, 400);
  121. type = GLUT_RGB;
  122. type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  123. glutInitDisplayMode(type);
  124. if (glutCreateWindow("Blend XOR") == GL_FALSE) {
  125. exit(1);
  126. }
  127. /* Make sure blend_logic_op extension is there. */
  128. s = (char *) glGetString(GL_EXTENSIONS);
  129. if (!s)
  130. exit(1);
  131. if (strstr(s,extName) == 0) {
  132. printf("Blend_logic_op extension is not present.\n");
  133. exit(1);
  134. }
  135. Init();
  136. glutReshapeFunc(Reshape);
  137. glutKeyboardFunc(Key);
  138. glutDisplayFunc(Draw);
  139. glutMainLoop();
  140. return 0;
  141. }