blob: 6f34e28461c8138aa283184d3ddb74debfab8f9e [file] [log] [blame]
jtgafb833d1999-08-19 00:55:39 +00001/*
2** blendxor.c - Demonstrates the use of the blend_logic_op
3** extension to draw hilights. Using XOR to draw the same
4** image twice restores the background to its original value.
5*/
6
7#include <stdio.h>
8#include <string.h>
9#ifndef _WIN32
10#include <unistd.h>
11#endif
12#include <stdlib.h>
Karl Schultz3d587f62002-01-16 00:57:54 +000013#ifdef _WIN32
14#include <windows.h>
15#endif
16#define GL_GLEXT_LEGACY
jtgafb833d1999-08-19 00:55:39 +000017#include <GL/glut.h>
18
19
20GLenum doubleBuffer;
21int dithering = 0;
Roland Scheidegger9e295362004-05-21 17:03:38 +000022int use11ops = 0;
23int supportlogops = 0;
jtgafb833d1999-08-19 00:55:39 +000024GLint windW, windH;
25
26static void Init(void)
27{
28 glDisable(GL_DITHER);
29 glShadeModel(GL_FLAT);
30}
31
32static void Reshape(int width, int height)
33{
34
35 windW = (GLint)width;
36 windH = (GLint)height;
37
38 glViewport(0, 0, (GLint)width, (GLint)height);
39
40 glMatrixMode(GL_PROJECTION);
41 glLoadIdentity();
42 gluOrtho2D(0, 400, 0, 400);
43 glMatrixMode(GL_MODELVIEW);
44}
45
46static void Key(unsigned char key, int x, int y)
47{
48
49 switch (key) {
50 case 27:
51 exit(1);
52 case 'd':
53 dithering = !dithering;
54 break;
Roland Scheidegger9e295362004-05-21 17:03:38 +000055 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;
jtgafb833d1999-08-19 00:55:39 +000062 default:
63 return;
64 }
65
66 glutPostRedisplay();
67}
68
69static void Draw(void)
70{
71 int i;
72
73 glDisable(GL_BLEND);
Roland Scheidegger9e295362004-05-21 17:03:38 +000074 if (supportlogops & 2)
75 glDisable(GL_COLOR_LOGIC_OP);
jtgafb833d1999-08-19 00:55:39 +000076
77 (dithering) ? glEnable(GL_DITHER) : glDisable(GL_DITHER);
78
79 glClearColor(0.5, 0.6, 0.1, 1.0);
80 glClear(GL_COLOR_BUFFER_BIT);
81
82 /* Draw background prims */
83 glColor3f(0.1, 0.1, 1.0);
84 glBegin(GL_TRIANGLES);
85 glVertex2i(5, 5);
86 glVertex2i(130, 50);
87 glVertex2i(100, 300);
88 glEnd();
89 glColor3f(0.5, 0.2, 0.9);
90 glBegin(GL_TRIANGLES);
91 glVertex2i(200, 100);
92 glVertex2i(330, 50);
93 glVertex2i(340, 400);
94 glEnd();
95
96 glEnable(GL_BLEND);
Roland Scheidegger9e295362004-05-21 17:03:38 +000097 if (!use11ops)
98 glBlendEquationEXT(GL_LOGIC_OP);
99 else
100 glEnable(GL_COLOR_LOGIC_OP);
jtgafb833d1999-08-19 00:55:39 +0000101 glLogicOp(GL_XOR);
102
103 /* Draw a set of rectangles across the window */
104 glColor3f(0.9, 0.2, 0.8);
105 for(i = 0; i < 400; i+=60) {
106 glBegin(GL_POLYGON);
107 glVertex2i(i, 100);
108 glVertex2i(i+50, 100);
109 glVertex2i(i+50, 200);
110 glVertex2i(i, 200);
111 glEnd();
112 }
113 glFlush(); /* Added by Brian Paul */
114#ifndef _WIN32
115 sleep(2);
116#endif
117
118 /* Redraw the rectangles, which should erase them */
119 for(i = 0; i < 400; i+=60) {
120 glBegin(GL_POLYGON);
121 glVertex2i(i, 100);
122 glVertex2i(i+50, 100);
123 glVertex2i(i+50, 200);
124 glVertex2i(i, 200);
125 glEnd();
126 }
127 glFlush();
128
129
130 if (doubleBuffer) {
131 glutSwapBuffers();
132 }
133}
134
135static GLenum Args(int argc, char **argv)
136{
137 GLint i;
138
139 doubleBuffer = GL_FALSE;
140
141 for (i = 1; i < argc; i++) {
142 if (strcmp(argv[i], "-sb") == 0) {
143 doubleBuffer = GL_FALSE;
144 } else if (strcmp(argv[i], "-db") == 0) {
145 doubleBuffer = GL_TRUE;
146 } else {
147 printf("%s (Bad option).\n", argv[i]);
148 return GL_FALSE;
149 }
150 }
151 return GL_TRUE;
152}
153
154int main(int argc, char **argv)
155{
156 GLenum type;
157 char *s;
158 char *extName = "GL_EXT_blend_logic_op";
Roland Scheidegger9e295362004-05-21 17:03:38 +0000159 char *version;
jtgafb833d1999-08-19 00:55:39 +0000160
161 glutInit(&argc, argv);
162
163 if (Args(argc, argv) == GL_FALSE) {
164 exit(1);
165 }
166
167 glutInitWindowPosition(0, 0); glutInitWindowSize( 400, 400);
168
169 type = GLUT_RGB;
170 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
171 glutInitDisplayMode(type);
172
173 if (glutCreateWindow("Blend XOR") == GL_FALSE) {
174 exit(1);
175 }
176
177 /* Make sure blend_logic_op extension is there. */
178 s = (char *) glGetString(GL_EXTENSIONS);
Roland Scheidegger9e295362004-05-21 17:03:38 +0000179 version = (char*) glGetString(GL_VERSION);
jtgafb833d1999-08-19 00:55:39 +0000180 if (!s)
181 exit(1);
Roland Scheidegger9e295362004-05-21 17:03:38 +0000182 if (strstr(s,extName)) {
183 supportlogops = 1;
184 use11ops = 0;
185 printf("blend_logic_op extension available.\n");
186 }
187 if (strncmp(version,"1.1",3)>=0) {
188 supportlogops += 2;
189 use11ops = 1;
190 printf("1.1 color logic ops available.\n");
191 }
192 if (supportlogops == 0) {
193 printf("Blend_logic_op extension and GL 1.1 not present.\n");
jtgafb833d1999-08-19 00:55:39 +0000194 exit(1);
195 }
196
197 Init();
198
199 glutReshapeFunc(Reshape);
200 glutKeyboardFunc(Key);
201 glutDisplayFunc(Draw);
202 glutMainLoop();
203 return 0;
204}