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