-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
287 lines (268 loc) · 7.89 KB
/
Copy pathmain.cpp
File metadata and controls
287 lines (268 loc) · 7.89 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#define NDEBUG
#include <GL/glut.h>
#include <iostream>
#include "Color.h"
#include "Line.h"
#include "Circle.h"
#include "Oval.h"
#include "Polygen.h"
#include "fillArea.h"
#include "cut.h"
#include <vector>
#include <Windows.h>
// Oval oval1(200,200,500,100);
std::vector<Point> point;
enum drawType {
line_DDA, line_midpoint, line_Bre, circle_mid, polygen, fill, new_window, CohenSutherland,
SutherlandHodgman, RED, GREEN, BLUE, CLEAR, help, TODO
};
drawType type = TODO;
GLsizei winWidth = 800, winHeight = 500;
// 光栅位置参数
GLint xRaster = 25, yRaster = 150;
GLint x = 30;
bool status = false;
COLOR winColor[800][500]; //窗口像素颜色
COLOR pen;
void init( ) {
// 白色背景
glEnable(GL_BLEND);
glClearColor(1.0, 1.0, 1.0, 1.0);
glReadPixels(0, 0, winWidth, winHeight, GL_RGB, GL_UNSIGNED_BYTE, winColor);
// glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 600.0, 0.0, 500.0);
}
/********************************
* 窗口刷新回调函数
*******************************/
void winReshapeFcn(GLint newWidth, GLint newHeight);
/********************************
* 绘制函数,所有要绘制的图形都在这里
*******************************/
void Drew();
/*********************************
* 鼠标回调函数
********************************/
void mouseCB(int button, int state, int x, int y);
/*********************************
* 键盘回调函数
********************************/
void keyboardCB(unsigned char key, int x, int y);
/*******************************
* 右键菜单功能设置
******************************/
void processMenuEvents(int option);
/****************************
* 创建右键菜单
***************************/
void createGLUTMenus();
int main(int argc, char** argv) {
// 初始化GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
// 设置窗口位置
glutInitWindowPosition(100, 100);
// 设置窗口尺寸
glutInitWindowSize(winWidth, winHeight);
// 创建窗口
glutCreateWindow("Hello World");
pen.setColor(255, 0, 0);
init();
glutMouseFunc(mouseCB);
glutKeyboardFunc(keyboardCB);
glutDisplayFunc(Drew);
//glutIdleFunc(line_dda_drew);
glutReshapeFunc(winReshapeFcn);
createGLUTMenus();
glutMainLoop();
}
void winReshapeFcn(GLint newWidth, GLint newHeight) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, GLdouble(newWidth), 0.0, GLdouble(newHeight));
glClear(GL_COLOR_BUFFER_BIT);
}
void Drew(){
if(type != polygen && point.size()==2){
Point start = point[0];
Point end = point[1];
switch (type) {
case line_DDA:{
Line line1(start,end);
line1.drawByDDA();
point.clear();
break;
}
case line_midpoint:{
Line line2(start,end);
line2.drawByMidPoint();
point.clear();
break;
}
case line_Bre:{
Line line3(start,end);
line3.drawByBresenham();
point.clear();
break;
}
case circle_mid:{
int r = pow(pow(end.x - start.x,2) + pow(end.y - start.y, 2),0.5);
std::cout<<"r = "<<r<<std::endl;
Circle circle(start,r);
circle.draw();
point.clear();
break;
}
case CohenSutherland: {
Line line4(start, end);
line4.drawByCohenSutherland();
point.clear();
break;
}
case polygen: {
break;
}
case SutherlandHodgman:
break;
default:{
//Line line(start,end);
//line.drawByDDA();
point.clear();
}
}
}
/*else if (type == polygen && point.size() >= 2) {
Point start = *(point.end() - 2);
Point end = *(point.end() - 1);
Line line(start, end);
line.drawByBresenham();
}
*/
else if (type == fill && point.size() == 1) {
Point p = point[0];
fillArea(p, pen);
point.clear();
}
setPixel(0,0);
glFlush();
}
void mouseCB(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON){
if(state == GLUT_DOWN){
y = abs(y-winHeight);
Point p(x,y);
point.push_back(p);
std::cout<<"x = "<<x<<"y = "<<y<<std::endl;
}
}
}
void keyboardCB(unsigned char key, int x, int y) {
if ((type == polygen || type == SutherlandHodgman) && key == '\r') {
Polygen poly(point);
if (type == SutherlandHodgman) {
poly.drawBySutherlandHodgman();
}
poly.draw();
glFlush();
point.clear();
}
}
void processMenuEvents(int option) {
//option,就是传递过来的value的值。
switch (option) {
case line_DDA:
type = line_DDA;
break;
case line_midpoint:
type = line_midpoint;
break;
case line_Bre:
type = line_Bre;
break;
case circle_mid:
type = circle_mid;
break;
case polygen:
type = polygen;
break;
case SutherlandHodgman:
type = SutherlandHodgman;
break;
case fill:
type = fill;
break;
case new_window: {
Point a(200, 125), b(600, 125);
Line line(a, b);
line.drawByBresenham();
a.set(600, 375);
line.set(a, b);
line.drawByBresenham();
b.set(200, 375);
line.set(a, b);
line.drawByBresenham();
a.set(200, 125);
line.set(a, b);
line.drawByBresenham();
glFlush();
type = TODO;
break;
}
case CohenSutherland:
type = CohenSutherland;
break;
case RED:
pen.setColor(255, 0, 0);
break;
case GREEN:
pen.setColor(0, 255, 0);
break;
case BLUE:
pen.setColor(0, 0, 255);
break;
case CLEAR:
type = TODO;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // (or whatever buffer you want to clear)
glFlush();
glReadPixels(0, 0, winWidth, winHeight, GL_RGB, GL_UNSIGNED_BYTE, winColor);
break;
case help:
type = TODO;
MessageBoxA(0, "When drawing a polygen, press ENTER to finish", "help", MB_OK | MB_ICONEXCLAMATION);
MessageBoxA(0, "If nothing happened after choose clear, just click the mouse", "help", MB_OK | MB_ICONEXCLAMATION);
break;
default:
type = TODO;
}
}
void createGLUTMenus() {
int menu;
int submenu_Line;
int color_menu;
//创建子菜单
submenu_Line = glutCreateMenu(processMenuEvents);
//添加条目
glutAddMenuEntry("DDA", line_DDA);
glutAddMenuEntry("midpoint", line_midpoint);
glutAddMenuEntry("Bresenhem", line_Bre);
glutAddMenuEntry("CohenSutherland", CohenSutherland);
color_menu = glutCreateMenu(processMenuEvents);
glutAddMenuEntry("RED", RED);
glutAddMenuEntry("GREEN", GREEN);
glutAddMenuEntry("BLUE", BLUE);
//创建菜单并告诉GLUT,processMenuEvents处理菜单事件。
menu = glutCreateMenu(processMenuEvents);
//给菜单增加条目
glutAddSubMenu("Line", submenu_Line);
glutAddMenuEntry("Cirlce",circle_mid);
glutAddMenuEntry("Polygen", polygen);
glutAddMenuEntry("SutherlandHodgman", SutherlandHodgman);
glutAddMenuEntry("Fill", fill);
glutAddSubMenu("Color", color_menu);
glutAddMenuEntry("New Window", new_window);
glutAddMenuEntry("clear",CLEAR);
glutAddMenuEntry("Help", help);
//把菜单和鼠标右键关联起来。
glutAttachMenu(GLUT_RIGHT_BUTTON);
}