-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.cpp
More file actions
252 lines (208 loc) · 8.13 KB
/
demo.cpp
File metadata and controls
252 lines (208 loc) · 8.13 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
#include "Angel.h"
#include <vector>
#include <fstream>
#include <string>
using namespace std;
int window;
// 三角面片中的顶点序列
typedef struct vIndex {
unsigned int a, b, c;
vIndex(int ia, int ib, int ic) : a(ia), b(ib), c(ic) {}
} vec3i;
std::vector<glm::vec3> vertices;
std::vector<vec3i> faces;
int nVertices = 0;
int nFaces = 0;
int nEdges = 0;
const int XAXIS = 0;
const int YAXIS = 1;
const int ZAXIS = 2;
int activeAXIS = 1;
int XAngle = 0;
int YAngle = 0;
int ZAngle = 0;
bool rotateActive = false;
GLuint program;
std::vector<glm::vec3> points; // 传入着色器的绘制点
std::vector<glm::vec3> colors; // 传入着色器的颜色
const int NUM_VERTICES = 8;
const glm::vec3 vertex_colors[NUM_VERTICES] = {
glm::vec3(1.0, 1.0, 1.0), // White
glm::vec3(1.0, 1.0, 0.0), // Yellow
glm::vec3(0.0, 1.0, 0.0), // Green
glm::vec3(0.0, 1.0, 1.0), // Cyan
glm::vec3(1.0, 0.0, 1.0), // Magenta
glm::vec3(1.0, 0.0, 0.0), // Red
glm::vec3(0.0, 0.0, 0.0), // Black
glm::vec3(0.0, 0.0, 1.0) // Blue
};
void read_off(const std::string filename) {
// fin打开文件读取文件信息
if (filename.empty()) {
return;
}
std::ifstream fin;
fin.open(filename);
// @TODO: Task1:修改此函数读取OFF文件中三维模型的信息
if (!fin) {
printf("文件有误\n");
return;
} else {
printf("文件打开成功\n");
vertices.clear();
faces.clear();
// 读取OFF字符串
string str;
fin >> str;
// 读取文件中顶点数、面片数、边数
fin >> nVertices >> nFaces >> nEdges;
// 根据顶点数,循环读取每个顶点坐标,将其保存到vertices
for (int i = 0; i < nVertices; i++) {
double x, y, z;
fin >> x >> y >> z;
vertices.emplace_back(x, y, z);
}
// 根据面片数,循环读取每个面片信息,并用构建的vec3i结构体保存到faces
for (int i = 0; i < nFaces; i++) {
int count, index1, index2, index3;
fin >> count >> index1 >> index2 >> index3;
faces.emplace_back(index1, index2, index3);
}
}
fin.close();
}
void storeFacesPoints() {
points.clear();
colors.clear();
// @TODO: Task1:修改此函数在points和colors容器中存储每个三角面片的各个点和颜色信息
// 在points容器中,依次添加每个面片的顶点,并在colors容器中,添加该点的颜色信息
// 比如一个正方形由两个三角形构成,那么vertices会由4个顶点的数据构成,faces会记录两个三角形的顶点下标,
// 而points就是记录这2个三角形的顶点,总共6个顶点的数据。
// colors容器则是和points的顶点一一对应,保存这个顶点的颜色,这里我们可以使用顶点坐标或者自己设定的颜色赋值。
int k = 0;
for (int i = 0; i < nFaces; i++) {
unsigned int i1 = faces[i].a, i2 = faces[i].b, i3 = faces[i].c;
points.emplace_back(vertices[i1]);
points.emplace_back(vertices[i2]);
points.emplace_back(vertices[i3]);
// if (i % 2 == 0 && i != 0) k++;
// colors.emplace_back(vertex_colors[k]);
// colors.emplace_back(vertex_colors[k]);
// colors.emplace_back(vertex_colors[k]);
// // 让顶点颜色不同
colors.push_back(vertices[i1] + glm::vec3(0.5, 0.5, 0.5));
colors.push_back(vertices[i2] + glm::vec3(0.5, 0.5, 0.5));
colors.push_back(vertices[i3] + glm::vec3(0.5, 0.5, 0.5));
}
}
void init() {
// 读取off模型文件
read_off("./assets/cow.off");
storeFacesPoints();
// 创建顶点数组对象
GLuint vao[1];
glGenVertexArrays(1, vao); // 分配1个顶点数组对象
glBindVertexArray(vao[0]); // 绑定顶点数组对象
// 创建并初始化顶点缓存对象
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, points.size() * sizeof(glm::vec3) + colors.size() * sizeof(glm::vec3), NULL,
GL_DYNAMIC_DRAW);
// @TODO: Task1:修改完成后再打开下面注释,否则程序会报错
// 分别读取数据
glBufferSubData(GL_ARRAY_BUFFER, 0, points.size() * sizeof(glm::vec3), &points[0]);
glBufferSubData(GL_ARRAY_BUFFER, points.size() * sizeof(glm::vec3), colors.size() * sizeof(glm::vec3), &colors[0]);
// 读取着色器并使用
std::string vshader, fshader;
vshader = "lab/labShaders/lab2HomeWork/vshader.glsl";
fshader = "lab/labShaders/lab2HomeWork/fshader.glsl";
program = InitShader(vshader.c_str(), fshader.c_str());
glUseProgram(program);
// 从顶点着色器中初始化顶点的位置
GLuint pLocation = glGetAttribLocation(program, "vPosition");
glEnableVertexAttribArray(pLocation);
glVertexAttribPointer(pLocation, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
// 从顶点着色器中初始化顶点的颜色
GLuint cLocation = glGetAttribLocation(program, "vColor");
glEnableVertexAttribArray(cLocation);
glVertexAttribPointer(cLocation, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(points.size() * sizeof(glm::vec3)));
// 黑色背景
glClearColor(0.0, 0.0, 0.0, 1.0);
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
GLuint location = glGetUniformLocation(program, "matrix");
glm::mat4 mat = glm::mat4(1.0f);
if (rotateActive) {
mat = glm::rotate(mat, glm::radians((float) XAngle), glm::vec3(1, 0, 0));
mat = glm::rotate(mat, glm::radians((float) YAngle), glm::vec3(0, 1, 0));
mat = glm::rotate(mat, glm::radians((float) ZAngle), glm::vec3(0, 0, 1));
if (activeAXIS == 0) XAngle++;
if (activeAXIS == 1) YAngle++;
if (activeAXIS == 2) ZAngle++;
}
glUniformMatrix4fv(static_cast<GLint> (location), 1, GL_FALSE, glm::value_ptr(mat));
glDrawArrays(GL_TRIANGLES, 0, points.size());
}
// 窗口键盘回调函数。
void key_callback(GLFWwindow *window, int key, int scancode, int action, int mode) {
switch (key) {
case GLFW_KEY_X:
if (action == GLFW_PRESS) activeAXIS = 0;
break;
case GLFW_KEY_Y:
if (action == GLFW_PRESS) activeAXIS = 1;
break;
case GLFW_KEY_Z:
if (action == GLFW_PRESS) activeAXIS = 2;
break;
}
}
void mouse_callback(GLFWwindow *window, int action, int button, int mods) {
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
rotateActive = true;
}
if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) {
rotateActive = false;
}
}
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
glViewport(0, 0, width, height);
}
int main(int argc, char **argv) {
// 初始化GLFW库,必须是应用程序调用的第一个GLFW函数
glfwInit();
// 配置GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// 配置窗口属性
GLFWwindow *window = glfwCreateWindow(600, 600, "模型", NULL, NULL);
if (window == NULL) {
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
glfwSetMouseButtonCallback(window, mouse_callback);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// 调用任何OpenGL的函数之前初始化GLAD
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
init();
while (!glfwWindowShouldClose(window)) {
display();
// 交换颜色缓冲 以及 检查有没有触发什么事件(比如键盘输入、鼠标移动等)
glfwSwapBuffers(window);
glfwPollEvents();
}
return 0;
}