2 ** blendeq.c - Demonstrates the use of the blend_minmax, blend_subtract,
3 ** and blend_logic_op extensions using glBlendEquationEXT.
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.
18 static int dithering
= 0;
19 static int doPrint
= 1;
23 static void DrawString(const char *string
)
27 for (i
= 0; string
[i
]; i
++)
28 glutBitmapCharacter(GLUT_BITMAP_9_BY_15
, string
[i
]);
31 static void Init(void)
35 glShadeModel(GL_FLAT
);
38 static void Reshape(int width
, int height
)
42 windH
= (GLint
)height
;
44 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
47 glMatrixMode(GL_PROJECTION
);
49 gluOrtho2D(0, windW
, 0, windH
);
50 glMatrixMode(GL_MODELVIEW
);
53 static void Key(unsigned char key
, int x
, int y
)
60 dithering
= !dithering
;
69 static void PrintColorStrings( void )
78 for (i
= windH
- deltaY
+ 4; i
> 0; i
-=deltaY
) {
79 glReadPixels(xleft
, i
+10, 1, 1, GL_RGB
, GL_UNSIGNED_BYTE
, ubbuf
);
80 sprintf(colorString
, "(0x%x, 0x%x, 0x%x)",
81 ubbuf
[0], ubbuf
[1], ubbuf
[2]);
82 glRasterPos2f(xleft
, i
);
83 DrawString(colorString
);
84 glReadPixels(xright
, i
+10, 1, 1, GL_RGB
, GL_UNSIGNED_BYTE
, ubbuf
);
85 sprintf(colorString
, "(0x%x, 0x%x, 0x%x)",
86 ubbuf
[0], ubbuf
[1], ubbuf
[2]);
87 glRasterPos2f(xright
, i
);
88 DrawString(colorString
);
92 static void Draw(void)
94 int stringOffset
= 5, stringx
= 8;
95 int x1
, x2
, xleft
, xright
;
98 (dithering
) ? glEnable(GL_DITHER
) : glDisable(GL_DITHER
);
101 glClearColor(0.5, 0.6, 0.1, 1.0);
102 glClear(GL_COLOR_BUFFER_BIT
);
104 /* Draw background */
105 glColor3f(0.1, 0.1, 1.0);
106 glRectf(0.0, 0.0, windW
/2, windH
);
109 glColor3f(0.8, 0.8, 0.0);
110 i
= windH
- deltaY
+ stringOffset
;
111 glRasterPos2f(stringx
, i
); i
-= deltaY
;
112 DrawString("SOURCE");
113 glRasterPos2f(stringx
, i
); i
-= deltaY
;
115 glRasterPos2f(stringx
, i
); i
-= deltaY
;
117 glRasterPos2f(stringx
, i
); i
-= deltaY
;
119 glRasterPos2f(stringx
, i
); i
-= deltaY
;
120 DrawString("subtract");
121 glRasterPos2f(stringx
, i
); i
-= deltaY
;
122 DrawString("reverse_subtract");
123 glRasterPos2f(stringx
, i
); i
-= deltaY
;
125 glRasterPos2f(stringx
, i
); i
-= deltaY
;
127 glRasterPos2f(stringx
, i
); i
-= deltaY
;
129 glRasterPos2f(stringx
, i
); i
-= deltaY
;
131 glRasterPos2f(stringx
, i
); i
-= deltaY
;
133 glRasterPos2f(stringx
, i
); i
-= deltaY
;
134 DrawString("invert");
135 glRasterPos2f(stringx
, i
); i
-= deltaY
;
137 glRasterPos2f(stringx
, i
); i
-= deltaY
;
145 xright
= 5 + windW
/2;
147 /* Draw foreground color for comparison */
148 glColor3f(0.9, 0.2, 0.8);
149 glRectf(x1
, i
, x2
, i
+deltaY
);
151 /* Leave one rectangle of background color */
153 /* Begin test cases */
155 glBlendFunc(GL_ONE
, GL_ONE
);
158 glBlendEquationEXT(GL_MIN_EXT
);
159 glRectf(x1
, i
, x2
, i
+deltaY
);
162 glBlendEquationEXT(GL_MAX_EXT
);
163 glRectf(x1
, i
, x2
, i
+deltaY
);
166 glBlendEquationEXT(GL_FUNC_SUBTRACT_EXT
);
167 glRectf(x1
, i
, x2
, i
+deltaY
);
170 glBlendEquationEXT(GL_FUNC_REVERSE_SUBTRACT_EXT
);
171 glRectf(x1
, i
, x2
, i
+deltaY
);
173 glBlendFunc(GL_ONE
, GL_ZERO
);
175 glBlendEquationEXT(GL_LOGIC_OP
);
177 glRectf(x1
, i
, x2
, i
+deltaY
);
180 glBlendEquationEXT(GL_LOGIC_OP
);
182 glRectf(x1
, i
, x2
, i
+deltaY
);
185 glBlendEquationEXT(GL_LOGIC_OP
);
187 glRectf(x1
, i
, x2
, i
+deltaY
);
190 glBlendEquationEXT(GL_LOGIC_OP
);
192 glRectf(x1
, i
, x2
, i
+deltaY
);
195 glBlendEquationEXT(GL_LOGIC_OP
);
197 glRectf(x1
, i
, x2
, i
+deltaY
);
200 glBlendEquationEXT(GL_LOGIC_OP
);
201 glLogicOp(GL_INVERT
);
202 glRectf(x1
, i
, x2
, i
+deltaY
);
205 glBlendEquationEXT(GL_LOGIC_OP
);
207 glRectf(x1
, i
, x2
, i
+deltaY
);
210 glBlendEquationEXT(GL_LOGIC_OP
);
212 glRectf(x1
, i
, x2
, i
+deltaY
);
213 glRectf(x1
, i
+10, x2
, i
+5);
217 glColor3f(1.0, 1.0, 1.0);
228 static GLenum
Args(int argc
, char **argv
)
232 doubleBuffer
= GL_FALSE
;
234 for (i
= 1; i
< argc
; i
++) {
235 if (strcmp(argv
[i
], "-sb") == 0) {
236 doubleBuffer
= GL_FALSE
;
237 } else if (strcmp(argv
[i
], "-db") == 0) {
238 doubleBuffer
= GL_TRUE
;
240 printf("%s (Bad option).\n", argv
[i
]);
247 int main(int argc
, char **argv
)
251 char *extName1
= "GL_EXT_blend_logic_op";
252 char *extName2
= "GL_EXT_blend_minmax";
253 char *extName3
= "GL_EXT_blend_subtract";
255 glutInit(&argc
, argv
);
257 if (Args(argc
, argv
) == GL_FALSE
) {
261 glutInitWindowPosition(0, 0); glutInitWindowSize( 800, 400);
264 type
|= (doubleBuffer
) ? GLUT_DOUBLE
: GLUT_SINGLE
;
265 glutInitDisplayMode(type
);
267 if (glutCreateWindow("Blend Equation") == GL_FALSE
) {
271 /* Make sure blend_logic_op extension is there. */
272 s
= (char *) glGetString(GL_EXTENSIONS
);
275 if (strstr(s
,extName1
) == 0) {
276 printf("Blend_logic_op extension is not present.\n");
279 if (strstr(s
,extName2
) == 0) {
280 printf("Blend_minmax extension is not present.\n");
283 if (strstr(s
,extName3
) == 0) {
284 printf("Blend_subtract extension is not present.\n");
290 glutReshapeFunc(Reshape
);
291 glutKeyboardFunc(Key
);
292 glutDisplayFunc(Draw
);