openGL 实现绘制、修改多边形

Polyline Editor

Using a mouse and keyboard to do polyline editing.

  • A house in the process of being drawn. The user has just clicked at the position drawn, and a line has been drawn from the previous point to the one by the mouse.
  • Moving a point. The user positions the cursor near the vertex of some polyline, presses down the mouse button, and “drags” the chosen point to some other location before releasing the button. Upon release of the button, the previous lines connected to this point are erased, and new lines are drawn to it.
  • A point is deleted from a polyline. The user clicks near the vertex of some polyline, and the two line segments connected to that vertex are erased. Then the two other endpoints of the segments just erased are connected with a line segment.

The functionality of the program should include the following “actions”:

  • Begin (‘b’) (create a new polyline)
  • Delete (‘d’) (delete the next point pointed to)
  • Move (‘m’) (drag the point pointed to to a new location)
  • Refresh (‘r’) (erase the screen and redraw all the polylines)
  • Quit (‘q’) (exit from the program)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// ConsoleApplication1-openGL-lab1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

struct point {
int x;
int y;
// 一个多边形的起始点
bool polygonStart;
};
std::vector<point> points;
bool newPolygon = true;
bool deleteMode = false;
bool moveMode = false;
// 用于标记正在拖动
bool dragging = false;
// 被拖动的点的编号
int draggedNum = -1;

void RenderSence() {
// 将窗口颜色的设置显示出来
glClear(GL_COLOR_BUFFER_BIT);
// 内容显示颜色
glColor3f(1, 1, 1);
for (int i = 0; i < points.size(); i++) {
if (points[i].polygonStart) {
if (i != 0) {
glEnd();
}
glBegin(GL_LINE_STRIP);
}
// 注意y值的改变
glVertex2f(points[i].x, 600 - points[i].y);
}
glEnd();
glutSwapBuffers();
}

// -1 when no points
int findNearPointNum(int x, int y) {
if (points.size() == 0) {
return -1;
}
int near = 0;
double mix = -1;
for (int i = 0; i < points.size(); i++) {
double distence = (points[i].x - x)*(points[i].x - x) + (points[i].y - y)*(points[i].y - y);
if (mix == -1) {
mix = distence;
}
else if (mix > distence) {
mix = distence;
near = i;
}
}
return near;
}

void processMouse(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON&&state == GLUT_DOWN) {
if (deleteMode) {
int near = findNearPointNum(x, y);
if (near == -1) {
return;
}
if (points[near].polygonStart) {
if (points.size() > near + 1) {
points[near + 1].polygonStart = true;
}
}
points.erase(points.begin() + near, points.begin() + near + 1);
return;
}
if (moveMode) {
dragging = true;
draggedNum = findNearPointNum(x, y);
return;
}
point t{ x,y,false };
if (newPolygon) {
newPolygon = false;
t.polygonStart = true;
}
points.push_back(t);
}
else if (button == GLUT_LEFT_BUTTON&&state == GLUT_UP) {
if (!dragging) {
return;
}
dragging = false;
if (draggedNum != -1) {
points[draggedNum].x = x;
points[draggedNum].y = y;
}
}
}

void processKey(unsigned char key, int x, int y) {
if (key == 'b') {
newPolygon = true;
}
if (key == 'd') {
deleteMode = true;
}
if (key == 'm') {
moveMode = true;
}
if (key == 'r') {
points.clear();
}
if (key == 'q') {
exit(0);
}
}

int main(int argc, char* argv[])
{
glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);

glutInitWindowSize(1000, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Hello GLUT");

glutDisplayFunc(RenderSence);
glutIdleFunc(RenderSence);

gluOrtho2D(0, 1000, 0, 600);

glutMouseFunc(processMouse);
glutKeyboardFunc(processKey);

glutMainLoop();

return 0;
}