Clone of mesa.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

blendeq.c 6.7KB

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