Use glad instead of GLEW.
[mesa-demos.git] / src / tests / blendxor.c
blob302164dde34d6c5d374f1f5b3579e0d31337afad
1 /**
2 * Test XOR emulation with blending.
4 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <math.h>
9 #include <glad/glad.h>
10 #include "glut_wrap.h"
11 #include "readtex.c"
13 #define IMAGE_FILE DEMOS_DATA_DIR "arch.rgb"
15 static int ImgWidth, ImgHeight;
16 static GLenum ImgFormat;
17 static GLubyte *Image = NULL;
19 static int Win;
20 static int Width = 600, Height = 600;
22 struct rect
24 int x0, y0, x1, y1;
27 static struct rect OldRect, NewRect;
29 static GLboolean ButtonDown = GL_FALSE;
30 static GLboolean LogicOp = 0*GL_TRUE;
32 static void
33 PrintString(const char *s)
35 while (*s) {
36 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
37 s++;
42 static void
43 Draw(void)
45 glClear(GL_COLOR_BUFFER_BIT);
47 glWindowPos2i((Width - ImgWidth) / 2, (Height - ImgHeight) / 2);
48 glDrawPixels(ImgWidth, ImgHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
51 * Draw 2D XOR rects
53 glColor3f(1, 1, 1);
55 glWindowPos2i(100, Height - 20);
56 PrintString("XOR LogicOp:");
57 glLogicOp(GL_XOR);
58 glEnable(GL_COLOR_LOGIC_OP);
59 glRecti(100, 30, 250, Height - 30);
60 glDisable(GL_COLOR_LOGIC_OP);
62 glWindowPos2i(Width/2 + 10, Height - 20);
63 PrintString("Invert Blending:");
64 glBlendFunc(GL_ONE, GL_ONE);
65 glBlendEquation(GL_FUNC_SUBTRACT);
66 glEnable(GL_BLEND);
67 glRecti(Width / 2, 30, Width / 2 + 150, Height - 30);
68 glDisable(GL_BLEND);
70 glutSwapBuffers();
74 static void
75 Reshape(int width, int height)
77 Width = width;
78 Height = height;
79 glViewport(0, 0, width, height);
80 glMatrixMode(GL_PROJECTION);
81 glLoadIdentity();
82 glOrtho(0, Width, 0, Height, -1, 1);
83 glMatrixMode(GL_MODELVIEW);
84 glLoadIdentity();
88 static void
89 Key(unsigned char key, int x, int y)
91 (void) x;
92 (void) y;
93 switch (key) {
94 case 'b':
95 case 'B':
96 LogicOp = GL_FALSE;
97 break;
98 case 'l':
99 case 'L':
100 LogicOp = GL_TRUE;
101 break;
102 case 27:
103 glutDestroyWindow(Win);
104 exit(0);
105 break;
107 glutPostRedisplay();
111 static void
112 SpecialKey(int key, int x, int y)
114 (void) x;
115 (void) y;
116 switch (key) {
117 case GLUT_KEY_UP:
118 break;
119 case GLUT_KEY_DOWN:
120 break;
121 case GLUT_KEY_LEFT:
122 break;
123 case GLUT_KEY_RIGHT:
124 break;
126 glutPostRedisplay();
130 static void
131 MouseMotion(int x, int y)
133 if (ButtonDown) {
134 NewRect.x1 = x;
135 NewRect.y1 = y;
136 glutPostRedisplay();
141 static void
142 MouseButton(int button, int state, int x, int y)
144 if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
145 ButtonDown = GL_TRUE;
146 NewRect.x0 = NewRect.x1 = x;
147 NewRect.y0 = NewRect.y1 = y;
148 OldRect = NewRect;
150 else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
151 ButtonDown = GL_FALSE;
156 static void
157 Init(void)
160 * Load image and scale if needed.
162 Image = LoadRGBImage(IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat);
163 if (!Image) {
164 printf("Couldn't read %s\n", IMAGE_FILE);
165 exit(0);
168 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
169 glPixelStorei(GL_PACK_ALIGNMENT, 1);
174 main(int argc, char *argv[])
176 glutInit(&argc, argv);
177 glutInitWindowSize(Width, Height);
178 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
179 Win = glutCreateWindow(argv[0]);
180 gladLoadGL();
181 glutReshapeFunc(Reshape);
182 glutKeyboardFunc(Key);
183 glutSpecialFunc(SpecialKey);
184 glutMotionFunc(MouseMotion);
185 glutMouseFunc(MouseButton);
186 glutDisplayFunc(Draw);
187 Init();
188 glutPostRedisplay();
189 glutMainLoop();
190 return 0;