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

blendxor.c 3.4KB

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