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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #ifdef _WIN32
  13. #include <windows.h>
  14. #endif
  15. #define GL_GLEXT_LEGACY
  16. #define GL_GLEXT_PROTOTYPES
  17. #include <GL/glut.h>
  18. #include <GL/glext.h>
  19. GLenum doubleBuffer;
  20. int dithering = 0;
  21. int use11ops = 0;
  22. int supportlogops = 0;
  23. GLint windW, windH;
  24. static void Init(void)
  25. {
  26. glDisable(GL_DITHER);
  27. glShadeModel(GL_FLAT);
  28. }
  29. static void Reshape(int width, int height)
  30. {
  31. windW = (GLint)width;
  32. windH = (GLint)height;
  33. glViewport(0, 0, (GLint)width, (GLint)height);
  34. glMatrixMode(GL_PROJECTION);
  35. glLoadIdentity();
  36. gluOrtho2D(0, 400, 0, 400);
  37. glMatrixMode(GL_MODELVIEW);
  38. }
  39. static void Key(unsigned char key, int x, int y)
  40. {
  41. switch (key) {
  42. case 27:
  43. exit(1);
  44. case 'd':
  45. dithering = !dithering;
  46. break;
  47. case 'l':
  48. if (supportlogops == 3)
  49. use11ops = (!use11ops);
  50. if (use11ops)
  51. printf("Using GL 1.1 color logic ops.\n");
  52. else printf("Using GL_EXT_blend_logic_op.\n");
  53. break;
  54. default:
  55. return;
  56. }
  57. glutPostRedisplay();
  58. }
  59. static void Draw(void)
  60. {
  61. int i;
  62. glDisable(GL_BLEND);
  63. if (supportlogops & 2)
  64. glDisable(GL_COLOR_LOGIC_OP);
  65. (dithering) ? glEnable(GL_DITHER) : glDisable(GL_DITHER);
  66. glClearColor(0.5, 0.6, 0.1, 1.0);
  67. glClear(GL_COLOR_BUFFER_BIT);
  68. /* Draw background prims */
  69. glColor3f(0.1, 0.1, 1.0);
  70. glBegin(GL_TRIANGLES);
  71. glVertex2i(5, 5);
  72. glVertex2i(130, 50);
  73. glVertex2i(100, 300);
  74. glEnd();
  75. glColor3f(0.5, 0.2, 0.9);
  76. glBegin(GL_TRIANGLES);
  77. glVertex2i(200, 100);
  78. glVertex2i(330, 50);
  79. glVertex2i(340, 400);
  80. glEnd();
  81. glEnable(GL_BLEND);
  82. if (!use11ops)
  83. glBlendEquationEXT(GL_LOGIC_OP);
  84. else
  85. glEnable(GL_COLOR_LOGIC_OP);
  86. glLogicOp(GL_XOR);
  87. /* Draw a set of rectangles across the window */
  88. glColor3f(0.9, 0.2, 0.8);
  89. for(i = 0; i < 400; i+=60) {
  90. glBegin(GL_POLYGON);
  91. glVertex2i(i, 100);
  92. glVertex2i(i+50, 100);
  93. glVertex2i(i+50, 200);
  94. glVertex2i(i, 200);
  95. glEnd();
  96. }
  97. glFlush(); /* Added by Brian Paul */
  98. #ifndef _WIN32
  99. sleep(2);
  100. #endif
  101. /* Redraw the rectangles, which should erase them */
  102. for(i = 0; i < 400; i+=60) {
  103. glBegin(GL_POLYGON);
  104. glVertex2i(i, 100);
  105. glVertex2i(i+50, 100);
  106. glVertex2i(i+50, 200);
  107. glVertex2i(i, 200);
  108. glEnd();
  109. }
  110. glFlush();
  111. if (doubleBuffer) {
  112. glutSwapBuffers();
  113. }
  114. }
  115. static GLenum Args(int argc, char **argv)
  116. {
  117. GLint i;
  118. doubleBuffer = GL_FALSE;
  119. for (i = 1; i < argc; i++) {
  120. if (strcmp(argv[i], "-sb") == 0) {
  121. doubleBuffer = GL_FALSE;
  122. } else if (strcmp(argv[i], "-db") == 0) {
  123. doubleBuffer = GL_TRUE;
  124. } else {
  125. printf("%s (Bad option).\n", argv[i]);
  126. return GL_FALSE;
  127. }
  128. }
  129. return GL_TRUE;
  130. }
  131. int main(int argc, char **argv)
  132. {
  133. GLenum type;
  134. char *s;
  135. char *extName = "GL_EXT_blend_logic_op";
  136. char *version;
  137. glutInit(&argc, argv);
  138. if (Args(argc, argv) == GL_FALSE) {
  139. exit(1);
  140. }
  141. glutInitWindowPosition(0, 0); glutInitWindowSize( 400, 400);
  142. type = GLUT_RGB;
  143. type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  144. glutInitDisplayMode(type);
  145. if (glutCreateWindow("Blend XOR") == GL_FALSE) {
  146. exit(1);
  147. }
  148. /* Make sure blend_logic_op extension is there. */
  149. s = (char *) glGetString(GL_EXTENSIONS);
  150. version = (char*) glGetString(GL_VERSION);
  151. if (!s)
  152. exit(1);
  153. if (strstr(s,extName)) {
  154. supportlogops = 1;
  155. use11ops = 0;
  156. printf("blend_logic_op extension available.\n");
  157. }
  158. if (strncmp(version,"1.1",3)>=0) {
  159. supportlogops += 2;
  160. use11ops = 1;
  161. printf("1.1 color logic ops available.\n");
  162. }
  163. if (supportlogops == 0) {
  164. printf("Blend_logic_op extension and GL 1.1 not present.\n");
  165. exit(1);
  166. }
  167. Init();
  168. glutReshapeFunc(Reshape);
  169. glutKeyboardFunc(Key);
  170. glutDisplayFunc(Draw);
  171. glutMainLoop();
  172. return 0;
  173. }