gallium: change remaining util functions to use cso sampler views
[mesa/mesa-lb.git] / progs / tests / rubberband.c
blob866a0f519e50aa513f909af68b26c288942d2696
1 /**
2 * Test rubber-band selection box w/ logicops and blend.
3 */
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"
12 #define IMAGE_FILE "../images/arch.rgb"
14 static int ImgWidth, ImgHeight;
15 static GLenum ImgFormat;
16 static GLubyte *Image = NULL;
18 static int Win;
19 static int Width = 512, Height = 512;
21 struct rect
23 int x0, y0, x1, y1;
26 static struct rect OldRect, NewRect;
28 static GLboolean ButtonDown = GL_FALSE;
29 static GLboolean LogicOp = 0*GL_TRUE;
31 static GLboolean RedrawBackground = GL_TRUE;
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};
39 * Draw rubberband box in front buffer
41 static void
42 DrawRect(const struct rect *r)
44 glDrawBuffer(GL_FRONT);
46 if (LogicOp) {
47 glLogicOp(GL_XOR);
48 glEnable(GL_COLOR_LOGIC_OP);
50 else {
51 glEnable(GL_BLEND);
52 glBlendFunc(GL_ONE, GL_ONE);
53 glBlendEquation(GL_FUNC_SUBTRACT);
56 glColor3f(1, 1, 1);
58 glLineWidth(3.0);
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();
67 glDisable(GL_COLOR_LOGIC_OP);
68 glDisable(GL_BLEND);
70 glDrawBuffer(GL_BACK);
74 static void
75 PrintString(const char *s)
77 while (*s) {
78 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
79 s++;
84 static void
85 DrawBackground(void)
87 char s[100];
89 sprintf(s, "[L/B] %s mode. Use mouse to make selection box.",
90 LogicOp ? "LogicOp" : "Blend");
92 glClear(GL_COLOR_BUFFER_BIT);
94 glWindowPos2i((Width - ImgWidth) / 2, (Height - ImgHeight) / 2);
95 glDrawPixels(ImgWidth, ImgHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
97 glWindowPos2i(10, 10);
98 PrintString(s);
100 glutSwapBuffers();
104 static void
105 Draw(void)
107 if (RedrawBackground) {
108 DrawBackground();
111 if (ButtonDown) {
112 if (!RedrawBackground)
113 DrawRect(&OldRect); /* erase old */
115 DrawRect(&NewRect); /* draw new */
117 OldRect = NewRect;
120 RedrawBackground = GL_FALSE;
124 static void
125 Reshape(int width, int height)
127 Width = width;
128 Height = height;
130 glViewport(0, 0, width, height);
132 glMatrixMode(GL_PROJECTION);
133 glLoadIdentity();
134 glOrtho(0, Width, Height, 0, -1, 1); /* Inverted Y! */
136 glMatrixMode(GL_MODELVIEW);
137 glLoadIdentity();
139 RedrawBackground = GL_TRUE;
143 static void
144 Key(unsigned char key, int x, int y)
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;
162 RedrawBackground = GL_TRUE;
163 glutPostRedisplay();
167 static void
168 SpecialKey(int key, int x, int y)
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;
182 glutPostRedisplay();
186 static void
187 MouseMotion(int x, int y)
189 if (ButtonDown) {
190 NewRect.x1 = x;
191 NewRect.y1 = y;
192 glutPostRedisplay();
197 static void
198 MouseButton(int button, int state, int x, int y)
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;
207 else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
208 ButtonDown = GL_FALSE;
210 glutPostRedisplay();
214 static void
215 Init(void)
217 Image = LoadRGBImage(IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat);
218 if (!Image) {
219 printf("Couldn't read %s\n", IMAGE_FILE);
220 exit(0);
223 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
224 glPixelStorei(GL_PACK_ALIGNMENT, 1);
229 main(int argc, char *argv[])
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;