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.

blendeq.c 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. ** blendeq.c - Demonstrates the use of the blend_minmax, blend_subtract,
  3. ** and blend_logic_op extensions using glBlendEquationEXT.
  4. **
  5. ** Over a two-color backround, draw rectangles using twelve blend
  6. ** options. The values are read back as UNSIGNED_BYTE and printed
  7. ** in hex over each value. These values are useful for logic
  8. ** op comparisons when channels are 8 bits deep.
  9. */
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <GL/glut.h>
  14. GLenum doubleBuffer;
  15. static int dithering = 0;
  16. static int doPrint = 1;
  17. static int deltaY;
  18. GLint windW, windH;
  19. static void DrawString(const char *string)
  20. {
  21. int i;
  22. for (i = 0; string[i]; i++)
  23. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, string[i]);
  24. }
  25. static void Init(void)
  26. {
  27. glDisable(GL_DITHER);
  28. glShadeModel(GL_FLAT);
  29. }
  30. static void Reshape(int width, int height)
  31. {
  32. windW = (GLint)width;
  33. windH = (GLint)height;
  34. glViewport(0, 0, (GLint)width, (GLint)height);
  35. deltaY = windH /16;
  36. glMatrixMode(GL_PROJECTION);
  37. glLoadIdentity();
  38. gluOrtho2D(0, windW, 0, windH);
  39. glMatrixMode(GL_MODELVIEW);
  40. }
  41. static void Key(unsigned char key, int x, int y)
  42. {
  43. switch (key) {
  44. case 27:
  45. exit(1);
  46. case 'd':
  47. dithering = !dithering;
  48. break;
  49. default:
  50. return;
  51. }
  52. glutPostRedisplay();
  53. }
  54. static void PrintColorStrings( void )
  55. {
  56. GLubyte ubbuf[3];
  57. int i, xleft, xright;
  58. char colorString[18];
  59. xleft = 5 + windW/4;
  60. xright = 5 + windW/2;
  61. for (i = windH - deltaY + 4; i > 0; i-=deltaY) {
  62. glReadPixels(xleft, i+10, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, ubbuf);
  63. sprintf(colorString, "(0x%x, 0x%x, 0x%x)",
  64. ubbuf[0], ubbuf[1], ubbuf[2]);
  65. glRasterPos2f(xleft, i);
  66. DrawString(colorString);
  67. glReadPixels(xright, i+10, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, ubbuf);
  68. sprintf(colorString, "(0x%x, 0x%x, 0x%x)",
  69. ubbuf[0], ubbuf[1], ubbuf[2]);
  70. glRasterPos2f(xright, i);
  71. DrawString(colorString);
  72. }
  73. }
  74. static void Draw(void)
  75. {
  76. int stringOffset = 5, stringx = 8;
  77. int x1, x2, xleft, xright;
  78. int i;
  79. (dithering) ? glEnable(GL_DITHER) : glDisable(GL_DITHER);
  80. glDisable(GL_BLEND);
  81. glClearColor(0.5, 0.6, 0.1, 1.0);
  82. glClear(GL_COLOR_BUFFER_BIT);
  83. /* Draw background */
  84. glColor3f(0.1, 0.1, 1.0);
  85. glRectf(0.0, 0.0, windW/2, windH);
  86. /* Draw labels */
  87. glColor3f(0.8, 0.8, 0.0);
  88. i = windH - deltaY + stringOffset;
  89. glRasterPos2f(stringx, i); i -= deltaY;
  90. DrawString("SOURCE");
  91. glRasterPos2f(stringx, i); i -= deltaY;
  92. DrawString("DEST");
  93. glRasterPos2f(stringx, i); i -= deltaY;
  94. DrawString("min");
  95. glRasterPos2f(stringx, i); i -= deltaY;
  96. DrawString("max");
  97. glRasterPos2f(stringx, i); i -= deltaY;
  98. DrawString("subtract");
  99. glRasterPos2f(stringx, i); i -= deltaY;
  100. DrawString("reverse_subtract");
  101. glRasterPos2f(stringx, i); i -= deltaY;
  102. DrawString("clear");
  103. glRasterPos2f(stringx, i); i -= deltaY;
  104. DrawString("set");
  105. glRasterPos2f(stringx, i); i -= deltaY;
  106. DrawString("copy");
  107. glRasterPos2f(stringx, i); i -= deltaY;
  108. DrawString("noop");
  109. glRasterPos2f(stringx, i); i -= deltaY;
  110. DrawString("and");
  111. glRasterPos2f(stringx, i); i -= deltaY;
  112. DrawString("invert");
  113. glRasterPos2f(stringx, i); i -= deltaY;
  114. DrawString("or");
  115. glRasterPos2f(stringx, i); i -= deltaY;
  116. DrawString("xor");
  117. i = windH - deltaY;
  118. x1 = windW/4;
  119. x2 = 3 * windW/4;
  120. xleft = 5 + windW/4;
  121. xright = 5 + windW/2;
  122. /* Draw foreground color for comparison */
  123. glColor3f(0.9, 0.2, 0.8);
  124. glRectf(x1, i, x2, i+deltaY);
  125. /* Leave one rectangle of background color */
  126. /* Begin test cases */
  127. glEnable(GL_BLEND);
  128. glBlendFunc(GL_ONE, GL_ONE);
  129. i -= 2*deltaY;
  130. glBlendEquationEXT(GL_MIN_EXT);
  131. glRectf(x1, i, x2, i+deltaY);
  132. i -= deltaY;
  133. glBlendEquationEXT(GL_MAX_EXT);
  134. glRectf(x1, i, x2, i+deltaY);
  135. i -= deltaY;
  136. glBlendEquationEXT(GL_FUNC_SUBTRACT_EXT);
  137. glRectf(x1, i, x2, i+deltaY);
  138. i -= deltaY;
  139. glBlendEquationEXT(GL_FUNC_REVERSE_SUBTRACT_EXT);
  140. glRectf(x1, i, x2, i+deltaY);
  141. glBlendFunc(GL_ONE, GL_ZERO);
  142. i -= deltaY;
  143. glBlendEquationEXT(GL_LOGIC_OP);
  144. glLogicOp(GL_CLEAR);
  145. glRectf(x1, i, x2, i+deltaY);
  146. i -= deltaY;
  147. glBlendEquationEXT(GL_LOGIC_OP);
  148. glLogicOp(GL_SET);
  149. glRectf(x1, i, x2, i+deltaY);
  150. i -= deltaY;
  151. glBlendEquationEXT(GL_LOGIC_OP);
  152. glLogicOp(GL_COPY);
  153. glRectf(x1, i, x2, i+deltaY);
  154. i -= deltaY;
  155. glBlendEquationEXT(GL_LOGIC_OP);
  156. glLogicOp(GL_NOOP);
  157. glRectf(x1, i, x2, i+deltaY);
  158. i -= deltaY;
  159. glBlendEquationEXT(GL_LOGIC_OP);
  160. glLogicOp(GL_AND);
  161. glRectf(x1, i, x2, i+deltaY);
  162. i -= deltaY;
  163. glBlendEquationEXT(GL_LOGIC_OP);
  164. glLogicOp(GL_INVERT);
  165. glRectf(x1, i, x2, i+deltaY);
  166. i -= deltaY;
  167. glBlendEquationEXT(GL_LOGIC_OP);
  168. glLogicOp(GL_OR);
  169. glRectf(x1, i, x2, i+deltaY);
  170. i -= deltaY;
  171. glBlendEquationEXT(GL_LOGIC_OP);
  172. glLogicOp(GL_XOR);
  173. glRectf(x1, i, x2, i+deltaY);
  174. glRectf(x1, i+10, x2, i+5);
  175. if (doPrint) {
  176. glDisable(GL_BLEND);
  177. glColor3f(1.0, 1.0, 1.0);
  178. PrintColorStrings();
  179. }
  180. glFlush();
  181. if (doubleBuffer) {
  182. glutSwapBuffers();
  183. }
  184. }
  185. static GLenum Args(int argc, char **argv)
  186. {
  187. GLint i;
  188. doubleBuffer = GL_FALSE;
  189. for (i = 1; i < argc; i++) {
  190. if (strcmp(argv[i], "-sb") == 0) {
  191. doubleBuffer = GL_FALSE;
  192. } else if (strcmp(argv[i], "-db") == 0) {
  193. doubleBuffer = GL_TRUE;
  194. } else {
  195. printf("%s (Bad option).\n", argv[i]);
  196. return GL_FALSE;
  197. }
  198. }
  199. return GL_TRUE;
  200. }
  201. int main(int argc, char **argv)
  202. {
  203. GLenum type;
  204. char *s;
  205. char *extName1 = "GL_EXT_blend_logic_op";
  206. char *extName2 = "GL_EXT_blend_minmax";
  207. char *extName3 = "GL_EXT_blend_subtract";
  208. glutInit(&argc, argv);
  209. if (Args(argc, argv) == GL_FALSE) {
  210. exit(1);
  211. }
  212. glutInitWindowPosition(0, 0); glutInitWindowSize( 800, 400);
  213. type = GLUT_RGB;
  214. type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  215. glutInitDisplayMode(type);
  216. if (glutCreateWindow("Blend Equation") == GL_FALSE) {
  217. exit(1);
  218. }
  219. /* Make sure blend_logic_op extension is there. */
  220. s = (char *) glGetString(GL_EXTENSIONS);
  221. if (!s)
  222. exit(1);
  223. if (strstr(s,extName1) == 0) {
  224. printf("Blend_logic_op extension is not present.\n");
  225. exit(1);
  226. }
  227. if (strstr(s,extName2) == 0) {
  228. printf("Blend_minmax extension is not present.\n");
  229. exit(1);
  230. }
  231. if (strstr(s,extName3) == 0) {
  232. printf("Blend_subtract extension is not present.\n");
  233. exit(1);
  234. }
  235. Init();
  236. glutReshapeFunc(Reshape);
  237. glutKeyboardFunc(Key);
  238. glutDisplayFunc(Draw);
  239. glutMainLoop();
  240. return 0;
  241. }