-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawLine.cpp
63 lines (58 loc) · 1.25 KB
/
drawLine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<gl\glut.h>
int ww=600, wh=400;
int first=0;
int xi,yi,xf,yf;
void init() {
glViewport(0,0,ww,wh);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)wh);
glMatrixMode(GL_MODELVIEW);
}
void drawLine(int x1, int y1, int x2, int y2) {
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(5);
glBegin(GL_LINES);
glVertex2i(x1,y1);
glVertex2i(x2,y2);
glEnd();
glFlush();
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(5.0);
glBegin(GL_LINES);
glColor3f(1.0,1.0,0.0);
glVertex2f(0.5,0.5);
glVertex2f(-0.5,0.5);
glEnd();
glFlush();
}
void mouse(int btn, int state, int x, int y) {
if(btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
switch(first) {
case 0 :
xi = x;
yi = wh - y;
first = 1;
break;
case 1 :
xf = x;
yf = wh -y;
drawLine(xi, yi, xf, yf);
first = 0;
break;
}
}
}
int main(int argc, char** argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(ww,wh);
glutCreateWindow("Draw Line With Mouse Click");
glutDisplayFunc(display);
init();
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}