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.3KB

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