-
Notifications
You must be signed in to change notification settings - Fork 1
/
freeHandLine.cpp
89 lines (70 loc) · 1.67 KB
/
freeHandLine.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <GL/glut.h>
#include <windows.h>
#include <stdio.h>
#include <vector>
using namespace std;
int windowWidth = 320, windowHeight = 240;
int windowPosX = 50, windowPosY = 50;
int k;
struct Point {
int x, y;
Point(int x1, int y1) : x(x1), y(y1) {}
Point() : x(0), y(0) {}
} curr;
vector<Point> points;
void display() {
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINE_STRIP);
for (int i = 0; i < points.size(); ++i) {
glVertex2i(points[i].x, points[i].y);
}
glEnd();
glFlush();
}
void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
//glMatrixMode(GL_MODELVIEW);
}
void keyboard(unsigned char key, int x, int y) {
if (key == 27) {
exit(0);
}
}
void mouse(int button, int state, int x, int y) {
int yy = glutGet(GLUT_WINDOW_HEIGHT);
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
k = 1;
}
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
k = 0;
points.clear();
}
if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
glClear(GL_COLOR_BUFFER_BIT);
}
}
void moveMouse(int x, int y) {
if (k) {
int yy = glutGet(GLUT_WINDOW_HEIGHT);
points.push_back(Point(x, yy - y));
glutPostRedisplay();
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(windowWidth, windowHeight);
glutInitWindowPosition(windowPosX, windowPosY);
glutCreateWindow("Draw lines using mouse");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(moveMouse);
//glClearColor(0.0, 0.0, 0.0, 1.0);
glutMainLoop();
return 0;
}