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