-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
151 lines (131 loc) · 3.59 KB
/
main.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
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
#include <halfEdge.h>
#include <Loop_subdivision.h>
#include<Collapse.h>
#include<GL\glut.h>
#define GLUT_WHEEL_UP 3 //定义滚轮操作
#define GLUT_WHEEL_DOWN 4
std::vector<face>faces;
std::vector<vertex> vers;
std::vector<half_edge> halfs;
float xRotate = 0.0f;
float yRotate = 0.0f;
int mousetate = 0; //鼠标当前的状态
GLfloat Oldx = 0.0; // 点击之前的位置
GLfloat Oldy = 0.0;
GLdouble sizeOfE = 1.0f;
double width = 700.0; //窗体的宽
double height = 700.0;//窗体的高
void onMouseMove(int x, int y) {
if (mousetate) {
//x对应y是因为对应的是法向量
yRotate += x - Oldx;
glutPostRedisplay();
Oldx = x;
xRotate += y - Oldy;
glutPostRedisplay();
Oldy = y;
}
}
void myMouse(int button, int state, int x, int y) {
if (button == GLUT_WHEEL_DOWN) {
sizeOfE -= 0.1f;
glutPostRedisplay();
}
if (button == GLUT_WHEEL_UP) {
sizeOfE += 0.1f;
glutPostRedisplay();
}
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
mousetate = 1;
Oldx = x;
Oldy = y;
}
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
mousetate = 0;
}
void LoopS(int key, int x, int y)
{
if (key == GLUT_KEY_UP) {
LoopSub *loopsub = new LoopSub(vers, halfs, faces);
loopsub->Subdivision();
faces = loopsub->upfaces;
vers = loopsub->upvers;
halfs = loopsub->uphes;
delete loopsub;
}
if (key == GLUT_KEY_DOWN) {
Collapse *col = new Collapse(vers, halfs, faces);
col->coll();
faces = col->faces;
vers = col->vers;
halfs = col->hes;
delete col;
}
glutPostRedisplay();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glScalef(sizeOfE, sizeOfE, sizeOfE); // 缩放
glRotatef(xRotate, 1.0f, 0.0f, 0.0f); // 让物体旋转的函数 第一个参数是角度大小,后面的参数是旋转的法向量
glRotatef(yRotate, 0.0f, 1.0f, 0.0f);
int num = faces.size();
for (int i = 0; i < num; i++)
{
int v1 = faces[i].v1;
int v2 = faces[i].v2;
int v3 = faces[i].v3;
glColor3f(0.0f, 1.0f, 1.0f);
glBegin(GL_LINE_LOOP);
glVertex3f(vers[v1].x, vers[v1].y, vers[v1].z);
glVertex3f(vers[v2].x, vers[v2].y, vers[v2].z);
glVertex3f(vers[v3].x, vers[v3].y, vers[v3].z);
glEnd();
/*glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_TRIANGLES);
glVertex3f(vers[v1].x, vers[v1].y, vers[v1].z);
glVertex3f(vers[v2].x, vers[v2].y, vers[v2].z);
glVertex3f(vers[v3].x, vers[v3].y, vers[v3].z);
glEnd();*/
}
glPopMatrix();
glutSwapBuffers();
}
void changeWindowSize(GLsizei w, GLsizei h)
{
GLdouble length = 1.0f;
width = (double)w;
height = (double)h;
if (h == 0) h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h) glOrtho(-length, length, -length * h / w, length * h / w, -length, length);
else glOrtho(-length * w / h, length * w / h, -length, length, -length, length);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char *argv[])
{
std::string path = "C:\\Users\\Administrator\\source\\repos\\Gl_3\\Gl_3\\bunny.off";
//std::string path = "C:\\Users\\Administrator\\Desktop\\my.off";
//std::string path = "E:\\Downloads\\octahedron.off";
halfEdge *h = new halfEdge(path);
faces = h->faces;
vers = h->vers;
halfs = h->hes;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(700, 700);
glutInitWindowPosition(100, 100);
glutCreateWindow("Half_edge");
glutDisplayFunc(display);
glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
glutReshapeFunc(changeWindowSize);
glutMouseFunc(myMouse);
glutMotionFunc(onMouseMove); // 鼠标移动的时候的函数
glutSpecialFunc(LoopS);
glutMainLoop();
system("pause");
return 0;
}