progs/tests: compile with SCons and glew
[mesa.git] / progs / tests / rubberband.c
1 /**
2 * Test rubber-band selection box w/ logicops and blend.
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <math.h>
8 #include <GL/glew.h>
9 #include <GL/glut.h>
10 #include "readtex.c"
11
12 #define IMAGE_FILE "../images/arch.rgb"
13
14 static int ImgWidth, ImgHeight;
15 static GLenum ImgFormat;
16 static GLubyte *Image = NULL;
17
18 static int Win;
19 static int Width = 512, Height = 512;
20
21 struct rect
22 {
23 int x0, y0, x1, y1;
24 };
25
26 static struct rect OldRect, NewRect;
27
28 static GLboolean ButtonDown = GL_FALSE;
29 static GLboolean LogicOp = 0*GL_TRUE;
30
31 static GLboolean RedrawBackground = GL_TRUE;
32
33 static const GLfloat red[4] = {1.0, 0.2, 0.2, 1.0};
34 static const GLfloat green[4] = {0.2, 1.0, 0.2, 1.0};
35 static const GLfloat blue[4] = {0.2, 0.2, 1.0, 1.0};
36
37
38 /*
39 * Draw rubberband box in front buffer
40 */
41 static void
42 DrawRect(const struct rect *r)
43 {
44 glDrawBuffer(GL_FRONT);
45
46 if (LogicOp) {
47 glLogicOp(GL_XOR);
48 glEnable(GL_COLOR_LOGIC_OP);
49 }
50 else {
51 glEnable(GL_BLEND);
52 glBlendFunc(GL_ONE, GL_ONE);
53 glBlendEquation(GL_FUNC_SUBTRACT);
54 }
55
56 glColor3f(1, 1, 1);
57
58 glLineWidth(3.0);
59
60 glBegin(GL_LINE_LOOP);
61 glVertex2i(r->x0, r->y0);
62 glVertex2i(r->x1, r->y0);
63 glVertex2i(r->x1, r->y1);
64 glVertex2i(r->x0, r->y1);
65 glEnd();
66
67 glDisable(GL_COLOR_LOGIC_OP);
68 glDisable(GL_BLEND);
69
70 glDrawBuffer(GL_BACK);
71 }
72
73
74 static void
75 PrintString(const char *s)
76 {
77 while (*s) {
78 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
79 s++;
80 }
81 }
82
83
84 static void
85 DrawBackground(void)
86 {
87 char s[100];
88
89 sprintf(s, "[L/B] %s mode. Use mouse to make selection box.",
90 LogicOp ? "LogicOp" : "Blend");
91
92 glClear(GL_COLOR_BUFFER_BIT);
93
94 glWindowPos2i((Width - ImgWidth) / 2, (Height - ImgHeight) / 2);
95 glDrawPixels(ImgWidth, ImgHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
96
97 glWindowPos2i(10, 10);
98 PrintString(s);
99
100 glutSwapBuffers();
101 }
102
103
104 static void
105 Draw(void)
106 {
107 if (RedrawBackground) {
108 DrawBackground();
109 }
110
111 if (ButtonDown) {
112 if (!RedrawBackground)
113 DrawRect(&OldRect); /* erase old */
114
115 DrawRect(&NewRect); /* draw new */
116
117 OldRect = NewRect;
118 }
119
120 RedrawBackground = GL_FALSE;
121 }
122
123
124 static void
125 Reshape(int width, int height)
126 {
127 Width = width;
128 Height = height;
129
130 glViewport(0, 0, width, height);
131
132 glMatrixMode(GL_PROJECTION);
133 glLoadIdentity();
134 glOrtho(0, Width, Height, 0, -1, 1); /* Inverted Y! */
135
136 glMatrixMode(GL_MODELVIEW);
137 glLoadIdentity();
138
139 RedrawBackground = GL_TRUE;
140 }
141
142
143 static void
144 Key(unsigned char key, int x, int y)
145 {
146 (void) x;
147 (void) y;
148 switch (key) {
149 case 'b':
150 case 'B':
151 LogicOp = GL_FALSE;
152 break;
153 case 'l':
154 case 'L':
155 LogicOp = GL_TRUE;
156 break;
157 case 27:
158 glutDestroyWindow(Win);
159 exit(0);
160 break;
161 }
162 RedrawBackground = GL_TRUE;
163 glutPostRedisplay();
164 }
165
166
167 static void
168 SpecialKey(int key, int x, int y)
169 {
170 (void) x;
171 (void) y;
172 switch (key) {
173 case GLUT_KEY_UP:
174 break;
175 case GLUT_KEY_DOWN:
176 break;
177 case GLUT_KEY_LEFT:
178 break;
179 case GLUT_KEY_RIGHT:
180 break;
181 }
182 glutPostRedisplay();
183 }
184
185
186 static void
187 MouseMotion(int x, int y)
188 {
189 if (ButtonDown) {
190 NewRect.x1 = x;
191 NewRect.y1 = y;
192 glutPostRedisplay();
193 }
194 }
195
196
197 static void
198 MouseButton(int button, int state, int x, int y)
199 {
200 if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
201 ButtonDown = GL_TRUE;
202 RedrawBackground = GL_TRUE;
203 NewRect.x0 = NewRect.x1 = x;
204 NewRect.y0 = NewRect.y1 = y;
205 OldRect = NewRect;
206 }
207 else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
208 ButtonDown = GL_FALSE;
209 }
210 glutPostRedisplay();
211 }
212
213
214 static void
215 Init(void)
216 {
217 Image = LoadRGBImage(IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat);
218 if (!Image) {
219 printf("Couldn't read %s\n", IMAGE_FILE);
220 exit(0);
221 }
222
223 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
224 glPixelStorei(GL_PACK_ALIGNMENT, 1);
225 }
226
227
228 int
229 main(int argc, char *argv[])
230 {
231 glutInit(&argc, argv);
232 glutInitWindowSize(Width, Height);
233 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
234 Win = glutCreateWindow(argv[0]);
235 glewInit();
236 glutReshapeFunc(Reshape);
237 glutKeyboardFunc(Key);
238 glutSpecialFunc(SpecialKey);
239 glutMotionFunc(MouseMotion);
240 glutMouseFunc(MouseButton);
241 glutDisplayFunc(Draw);
242 Init();
243 glutPostRedisplay();
244 glutMainLoop();
245 return 0;
246 }