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 7.1KB

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