2 * Test rubber-band selection box w/ logicops and blend.
12 #define IMAGE_FILE "../images/arch.rgb"
14 static int ImgWidth
, ImgHeight
;
15 static GLenum ImgFormat
;
16 static GLubyte
*Image
= NULL
;
19 static int Width
= 512, Height
= 512;
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
42 DrawRect(const struct rect
*r
)
44 glDrawBuffer(GL_FRONT
);
48 glEnable(GL_COLOR_LOGIC_OP
);
52 glBlendFunc(GL_ONE
, GL_ONE
);
53 glBlendEquation(GL_FUNC_SUBTRACT
);
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
);
67 glDisable(GL_COLOR_LOGIC_OP
);
70 glDrawBuffer(GL_BACK
);
75 PrintString(const char *s
)
78 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
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);
107 if (RedrawBackground
) {
112 if (!RedrawBackground
)
113 DrawRect(&OldRect
); /* erase old */
115 DrawRect(&NewRect
); /* draw new */
120 RedrawBackground
= GL_FALSE
;
125 Reshape(int width
, int height
)
130 glViewport(0, 0, width
, height
);
132 glMatrixMode(GL_PROJECTION
);
134 glOrtho(0, Width
, Height
, 0, -1, 1); /* Inverted Y! */
136 glMatrixMode(GL_MODELVIEW
);
139 RedrawBackground
= GL_TRUE
;
144 Key(unsigned char key
, int x
, int y
)
158 glutDestroyWindow(Win
);
162 RedrawBackground
= GL_TRUE
;
168 SpecialKey(int key
, int x
, int y
)
187 MouseMotion(int x
, int y
)
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
;
207 else if (button
== GLUT_LEFT_BUTTON
&& state
== GLUT_UP
) {
208 ButtonDown
= GL_FALSE
;
217 Image
= LoadRGBImage(IMAGE_FILE
, &ImgWidth
, &ImgHeight
, &ImgFormat
);
219 printf("Couldn't read %s\n", IMAGE_FILE
);
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]);
236 glutReshapeFunc(Reshape
);
237 glutKeyboardFunc(Key
);
238 glutSpecialFunc(SpecialKey
);
239 glutMotionFunc(MouseMotion
);
240 glutMouseFunc(MouseButton
);
241 glutDisplayFunc(Draw
);