Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

blendxor.c 4.1KB

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