-
Notifications
You must be signed in to change notification settings - Fork 0
/
MeshPainter.cpp
463 lines (386 loc) · 19.4 KB
/
MeshPainter.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include "MeshPainter.h"
#include "Body.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
MeshPainter::MeshPainter() {};
MeshPainter::~MeshPainter() {};
std::vector<std::string> MeshPainter::getMeshNames() { return mesh_names; };
std::vector<TriMesh*> MeshPainter::getMeshes() { return meshes; };
std::vector<openGLObject> MeshPainter::getOpenGLObj() { return opengl_objects; };
unsigned int MeshPainter::depthMap = 0;
void MeshPainter::simplebindObjectAndData(TriMesh& mesh, openGLObject& object, const std::string& vshader, const std::string& fshader) {
// 从顶点着色器中初始化顶点的位置
object.simpleobject.program = InitShader(vshader.c_str(), fshader.c_str());
object.simpleobject.pLocation = glGetAttribLocation(object.simpleobject.program, "aPos");
glEnableVertexAttribArray(object.simpleobject.pLocation);
glVertexAttribPointer(object.simpleobject.pLocation, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
// 获得矩阵存储位置
object.simpleobject.modelLocation = glGetUniformLocation(object.simpleobject.program, "model");
object.simpleobject.lightspaceLocation = glGetUniformLocation(object.simpleobject.program, "lightSpaceMatrix");
}
void MeshPainter::bindObjectAndData(TriMesh& mesh, openGLObject& object, const std::string& texture_image, const std::string& vshader, const std::string& fshader) {
// 初始化各种对象
object.program = InitShader(vshader.c_str(), fshader.c_str());
std::vector<glm::vec3> points = mesh.getPoints();
std::vector<glm::vec3> normals = mesh.getNormals();
std::vector<glm::vec3> colors = mesh.getColors();
std::vector<glm::vec2> textures = mesh.getTextures();
// 创建顶点数组对象
#ifdef __APPLE__ // for MacOS
glGenVertexArraysAPPLE(1, &object.vao); // 分配1个顶点数组对象
glBindVertexArrayAPPLE(object.vao); // 绑定顶点数组对象
#else // for Windows
glGenVertexArrays(1, &object.vao); // 分配1个顶点数组对象
glBindVertexArray(object.vao); // 绑定顶点数组对象
#endif
// 创建并初始化顶点缓存对象
glGenBuffers(1, &object.vbo);
glBindBuffer(GL_ARRAY_BUFFER, object.vbo);
glBufferData(GL_ARRAY_BUFFER,
points.size() * sizeof(glm::vec3) +
normals.size() * sizeof(glm::vec3) +
colors.size() * sizeof(glm::vec3) +
textures.size() * sizeof(glm::vec2),
NULL, GL_STATIC_DRAW);
// 绑定顶点数据
glBufferSubData(GL_ARRAY_BUFFER, 0, points.size() * sizeof(glm::vec3), points.data());
// 绑定颜色数据
glBufferSubData(GL_ARRAY_BUFFER, points.size() * sizeof(glm::vec3), colors.size() * sizeof(glm::vec3), colors.data());
// 绑定法向量数据
glBufferSubData(GL_ARRAY_BUFFER, (points.size() + colors.size()) * sizeof(glm::vec3), normals.size() * sizeof(glm::vec3), normals.data());
// 绑定纹理数据
glBufferSubData(GL_ARRAY_BUFFER, (points.size() + normals.size() + colors.size()) * sizeof(glm::vec3), textures.size() * sizeof(glm::vec2), textures.data());
object.vshader = vshader;
object.fshader = fshader;
//obj.program = InitShader(object.vshader.c_str(), object.fshader.c_str());
//std::cout << object.program << "\n";
// 将顶点传入着色器
object.pLocation = glGetAttribLocation(object.program, "vPosition");
glEnableVertexAttribArray(object.pLocation);
glVertexAttribPointer(object.pLocation, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
// 将颜色传入着色器
object.cLocation = glGetAttribLocation(object.program, "vColor");
glEnableVertexAttribArray(object.cLocation);
glVertexAttribPointer(object.cLocation, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(points.size() * sizeof(glm::vec3)));
// 将法向量传入着色器
object.nLocation = glGetAttribLocation(object.program, "vNormal");
glEnableVertexAttribArray(object.nLocation);
glVertexAttribPointer(object.nLocation, 3,
GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET((points.size() + colors.size()) * sizeof(glm::vec3)));
// 将材质传入着色器
object.tLocation = glGetAttribLocation(object.program, "vTexture");
glEnableVertexAttribArray(object.tLocation);
glVertexAttribPointer(object.tLocation, 2,
GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET((points.size() + colors.size() + normals.size()) * sizeof(glm::vec3)));
// 获得矩阵位置
object.modelLocation = glGetUniformLocation(object.program, "model");
object.viewLocation = glGetUniformLocation(object.program, "view");
object.projectionLocation = glGetUniformLocation(object.program, "projection");
object.clipLocation = glGetUniformLocation(object.program, "clipplaneEnabled");
object.clipplaneLocation = glGetUniformLocation(object.program, "clipplane");
object.shadowLocation = glGetUniformLocation(object.program, "isShadow");
object.textureLocation= glGetUniformLocation(object.program, "useTexture");
object.lightspaceLocation = glGetUniformLocation(object.program, "lightSpaceMatrix");
object.shadowMapLocation = glGetUniformLocation(object.program, "shadowMap");
glUniform1i(object.shadowMapLocation, 1);
// 读取纹理图片数
object.texture_image = texture_image;
// 创建纹理的缓存对象
glGenTextures(1, &object.texture);
// 调用stb_image生成纹理
load_texture_STBImage(object.texture_image, object.texture);
// Clean up
glUseProgram(0);
#ifdef __APPLE__
glBindVertexArrayAPPLE(0);
#else
glBindVertexArray(0);
#endif
};
void MeshPainter::bindAgain(TriMesh& mesh, openGLObject& object) {
std::vector<glm::vec3> points = mesh.getPoints();
std::vector<glm::vec3> normals = mesh.getNormals();
std::vector<glm::vec3> colors = mesh.getColors();
std::vector<glm::vec2> textures = mesh.getTextures();
glBindBuffer(GL_ARRAY_BUFFER, object.vbo);
glBufferData(GL_ARRAY_BUFFER,
points.size() * sizeof(glm::vec3) +
normals.size() * sizeof(glm::vec3) +
colors.size() * sizeof(glm::vec3) +
textures.size() * sizeof(glm::vec2),
NULL, GL_STATIC_DRAW);
// 绑定顶点数据
glBufferSubData(GL_ARRAY_BUFFER, 0, points.size() * sizeof(glm::vec3), points.data());
// 绑定颜色数据
glBufferSubData(GL_ARRAY_BUFFER, points.size() * sizeof(glm::vec3), colors.size() * sizeof(glm::vec3), colors.data());
// 绑定法向量数据
glBufferSubData(GL_ARRAY_BUFFER, (points.size() + colors.size()) * sizeof(glm::vec3), normals.size() * sizeof(glm::vec3), normals.data());
// 绑定纹理数据
glBufferSubData(GL_ARRAY_BUFFER, (points.size() + normals.size() + colors.size()) * sizeof(glm::vec3), textures.size() * sizeof(glm::vec2), textures.data());
// 将顶点传入着色器
object.pLocation = glGetAttribLocation(object.program, "vPosition");
glEnableVertexAttribArray(object.pLocation);
glVertexAttribPointer(object.pLocation, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
// 将颜色传入着色器
object.cLocation = glGetAttribLocation(object.program, "vColor");
glEnableVertexAttribArray(object.cLocation);
glVertexAttribPointer(object.cLocation, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(points.size() * sizeof(glm::vec3)));
// 将法向量传入着色器
object.nLocation = glGetAttribLocation(object.program, "vNormal");
glEnableVertexAttribArray(object.nLocation);
glVertexAttribPointer(object.nLocation, 3,
GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET((points.size() + colors.size()) * sizeof(glm::vec3)));
// 将材质传入着色器
object.tLocation = glGetAttribLocation(object.program, "vTexture");
glEnableVertexAttribArray(object.tLocation);
glVertexAttribPointer(object.tLocation, 2,
GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET((points.size() + colors.size() + normals.size()) * sizeof(glm::vec3)));
};
void MeshPainter::bindLightAndMaterial(TriMesh& mesh, openGLObject& object, Light* light, Camera* camera) {
// 传递材质、光源等数据给着色器
// 传递相机的位置
glUniform3fv(glGetUniformLocation(object.program, "eye_position"), 1, &camera->eye[0]);
// 传递物体的材质
glm::vec4 meshAmbient = mesh.getAmbient();
glm::vec4 meshDiffuse = mesh.getDiffuse();
glm::vec4 meshSpecular = mesh.getSpecular();
float meshShininess = mesh.getShininess();
glUniform4fv(glGetUniformLocation(object.program, "material.ambient"), 1, &meshAmbient[0]);
glUniform4fv(glGetUniformLocation(object.program, "material.diffuse"), 1, &meshDiffuse[0]);
glUniform4fv(glGetUniformLocation(object.program, "material.specular"), 1, &meshSpecular[0]);
glUniform1f(glGetUniformLocation(object.program, "material.shininess"), meshShininess);
// 传递光源信息
glm::vec4 lightAmbient = light->getAmbient();
glm::vec4 lightDiffuse = light->getDiffuse();
glm::vec4 lightSpecular = light->getSpecular();
glm::vec3 lightPosition = light->getTranslation();
glUniform4fv(glGetUniformLocation(object.program, "light.ambient"), 1, &lightAmbient[0]);
glUniform4fv(glGetUniformLocation(object.program, "light.diffuse"), 1, &lightDiffuse[0]);
glUniform4fv(glGetUniformLocation(object.program, "light.specular"), 1, &lightSpecular[0]);
glUniform3fv(glGetUniformLocation(object.program, "light.position"), 1, &lightPosition[0]);
glUniform1f(glGetUniformLocation(object.program, "light.constant"), light->getConstant());
glUniform1f(glGetUniformLocation(object.program, "light.linear"), light->getLinear());
glUniform1f(glGetUniformLocation(object.program, "light.quadratic"), light->getQuadratic());
}
void MeshPainter::addMesh(TriMesh& mesh, const std::string& name, const std::string& texture_image, const std::string& vshader, const std::string& fshader) {
mesh_names.push_back(name);
meshes.push_back(&mesh);
openGLObject object;
// 绑定openGL对象,并传递顶点属性的数据
bindObjectAndData(mesh, object, texture_image, vshader, fshader);
std::string simplevshader = "shaders/shadow_mapping_depth_vshader.glsl";
std:: string simplefshader = "shaders/shadow_mapping_depth_fshader.glsl";
simplebindObjectAndData(mesh, object, simplevshader, simplefshader);
};
void MeshPainter::simpledrawMesh(TriMesh& mesh, openGLObject& object, glm::mat4 lightSpaceMatrix, bool mirror)
{
glUseProgram(object.simpleobject.program);
glBindVertexArray(object.vao);
glm::mat4 modelMatrix = mesh.getModelMatrix();
if (mirror) {
glm::vec4 norm(0, 1, 0, 0);
glUniform1i(glGetUniformLocation(object.simpleobject.program, "clipplaneEnabled"), 1);
glUniform4fv(glGetUniformLocation(object.simpleobject.program, "clipplane"), 1, &norm[0]);
}
// 传递矩阵
glUniformMatrix4fv(object.simpleobject.modelLocation, 1, GL_FALSE, &modelMatrix[0][0]);
glUniformMatrix4fv(object.simpleobject.lightspaceLocation, 1, GL_FALSE, &lightSpaceMatrix[0][0]);
// for Alpha test
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, object.texture);
glUniform1i(glGetUniformLocation(object.program, "texture"), 0);
glDrawArrays(GL_TRIANGLES, 0, mesh.getPoints().size());
#ifdef __APPLE__ // for MacOS
glBindVertexArrayAPPLE(0);
#else
glBindVertexArray(0);
#endif
}
void MeshPainter::drawMesh(TriMesh& mesh, openGLObject& object, Light* light, Camera* camera, glm::mat4 lightSpaceMatrix, bool mirror) {
// 相机矩阵计算
//camera->updateCamera();
camera->viewMatrix = camera->getViewMatrix();
camera->projMatrix = camera->getProjectionMatrix(false);
#ifdef __APPLE__ // for MacOS
glBindVertexArrayAPPLE(object.vao);
#else
glBindVertexArray(object.vao);
#endif
glUseProgram(object.program);
if (mirror) {
glm::vec4 norm(0, 1, 0, 0);
glUniform1i(glGetUniformLocation(object.program, "clipplaneEnabled"), 1);
glUniform4fv(glGetUniformLocation(object.program, "clipplane"), 1, &norm[0]);
}
// 物体的变换矩阵
glm::mat4 modelMatrix = mesh.getModelMatrix();
// 传递矩阵
glUniformMatrix4fv(object.modelLocation, 1, GL_FALSE, &modelMatrix[0][0]);
glUniformMatrix4fv(object.viewLocation, 1, GL_FALSE, &camera->viewMatrix[0][0]);
glUniformMatrix4fv(object.projectionLocation, 1, GL_FALSE, &camera->projMatrix[0][0]);
glUniformMatrix4fv(object.lightspaceLocation, 1, GL_FALSE, &lightSpaceMatrix[0][0]);
// 将着色器 isShadow 设置为0,表示正常绘制的颜色,如果是1着表示阴影
glUniform1i(object.shadowLocation, 0);
//设置是否使用材质,0则不用,1则用
if(mesh.getTextures().size()==0)
glUniform1i(object.textureLocation, 0);
else
glUniform1i(object.textureLocation, 1);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, object.texture);// 该语句必须,否则将只使用同一个纹理进行绘制
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, depthMap);
// 传递纹理数据 将生成的纹理传给shader
glUniform1i(glGetUniformLocation(object.program, "texture"), 0);
glUniform1i(glGetUniformLocation(object.program, "shadowMap"), 1);
// 将材质和光源数据传递给着色器
bindLightAndMaterial(mesh, object, light, camera);
// 绘制
glDrawArrays(GL_TRIANGLES, 0, mesh.getPoints().size());
#ifdef __APPLE__ // for MacOS
glBindVertexArrayAPPLE(0);
#else
glBindVertexArray(0);
#endif
//glUseProgram(0);
};
void MeshPainter::drawMirror(glm::mat4 PVMatrix, TriMesh& mesh, openGLObject& object, Light* light, Camera* camera, glm::mat4 lightSpaceMatrix, bool mirror_flag) {
// 相机矩阵计算
//camera->updateCamera();
camera->viewMatrix = camera->getViewMatrix();
camera->projMatrix = camera->getProjectionMatrix(false);
#ifdef __APPLE__ // for MacOS
glBindVertexArrayAPPLE(object.vao);
#else
glBindVertexArray(object.vao);
#endif
glUseProgram(object.program);
if (mirror_flag) {
glm::vec4 norm(0, 1, 0, 0);
glUniform1i(glGetUniformLocation(object.program, "clipplaneEnabled"), 1);
glUniform4fv(glGetUniformLocation(object.program, "clipplane"), 1, &norm[0]);
}
// 物体的变换矩阵
glm::mat4 modelMatrix = mesh.getModelMatrix();
glUniformMatrix4fv(glGetUniformLocation(object.program, "PVMatrix"), 1, GL_FALSE, &PVMatrix[0][0]);
// 传递矩阵
glUniformMatrix4fv(object.modelLocation, 1, GL_FALSE, &modelMatrix[0][0]);
glUniformMatrix4fv(object.viewLocation, 1, GL_FALSE, &camera->viewMatrix[0][0]);
glUniformMatrix4fv(object.projectionLocation, 1, GL_FALSE, &camera->projMatrix[0][0]);
glUniformMatrix4fv(object.lightspaceLocation, 1, GL_FALSE, &lightSpaceMatrix[0][0]);
// 将着色器 isShadow 设置为0,表示正常绘制的颜色,如果是1着表示阴影
glUniform1i(object.shadowLocation, 0);
//设置是否使用材质,0则不用,1则用
if(mesh.getTextures().size()==0)
glUniform1i(object.textureLocation, 0);
else
glUniform1i(object.textureLocation, 1);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, object.texture);// 该语句必须,否则将只使用同一个纹理进行绘制
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, depthMap);
// 传递纹理数据 将生成的纹理传给shader
glUniform1i(glGetUniformLocation(object.program, "texture"), 0);
glUniform1i(glGetUniformLocation(object.program, "shadowMap"), 1);
// 将材质和光源数据传递给着色器
bindLightAndMaterial(mesh, object, light, camera);
// 绘制
glDrawArrays(GL_TRIANGLES, 0, mesh.getPoints().size());
#ifdef __APPLE__ // for MacOS
glBindVertexArrayAPPLE(0);
#else
glBindVertexArray(0);
#endif
//glUseProgram(0);
}
Simpleobj MeshPainter::obj, MeshPainter::simpleobj;
void MeshPainter::simpledrawMeshes(glm::mat4 lightSpaceMatrix, bool mirror)
{
for (int i = 0; i < meshes.size(); i++)
{
simpledrawMesh(*meshes[i], opengl_objects[i], lightSpaceMatrix, mirror);
}
}
void MeshPainter::drawMeshes(Light* light, Camera* camera, glm::mat4 lightSpaceMatrix, bool mirror) {
for (int i = 0; i < meshes.size(); i++)
{
drawMesh(*meshes[i], opengl_objects[i], light, camera, lightSpaceMatrix, mirror);
}
};
void MeshPainter::cleanMeshes() {
// 将数据都清空释放
mesh_names.clear();
for (int i = 0; i < meshes.size(); i++)
{
meshes[i]->cleanData();
delete meshes[i];
meshes[i] = NULL;
#ifdef __APPLE__
glDeleteVertexArraysAPPLE(1, &opengl_objects[i].vao);
#else
glDeleteVertexArrays(1, &opengl_objects[i].vao);
#endif
glDeleteBuffers(1, &opengl_objects[i].vbo);
glDeleteProgram(opengl_objects[i].program);
}
meshes.clear();
opengl_objects.clear();
};
void MeshPainter::load_texture_STBImage(const std::string& file_name, GLuint& texture) {
// 读取纹理图片,并将其传递给着色器
int width, height, channels = 0;
unsigned char* pixels = NULL;
// 读取图片的时候先翻转一下图片,如果不设置的话显示出来是反过来的图片
stbi_set_flip_vertically_on_load(true);
// 读取图片数据
pixels = stbi_load(file_name.c_str(), &width, &height, &channels, 0);
printf("line %d: %s %d %d %d\n", __LINE__, file_name.c_str(), width, height, channels);
// 调整行对齐格式
if (width * channels % 4 != 0)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
GLenum format = GL_RGB;
// 设置通道格式
switch (channels)
{
case 1:
format = GL_RED;
break;
case 3:
format = GL_RGB;
break;
case 4:
format = GL_RGBA;
break;
default:
format = GL_RGB;
break;
}
// 绑定纹理对象
glBindTexture(GL_TEXTURE_2D, texture);
// 指定纹理的放大,缩小滤波,使用线性方式,即当图片放大的时候插值方式
// 将图片的rgb数据上传给opengl
glTexImage2D(
GL_TEXTURE_2D, // 指定目标纹理,这个值必须是GL_TEXTURE_2D
0, // 执行细节级别,0是最基本的图像级别,n表示第N级贴图细化级别
format, // 纹理数据的颜色格式(GPU显存)
width, // 宽度。早期的显卡不支持不规则的纹理,则宽度和高度必须是2^n
height, // 高度。早期的显卡不支持不规则的纹理,则宽度和高度必须是2^n
0, // 指定边框的宽度。必须为0
format, // 像素数据的颜色格式(CPU内存)
GL_UNSIGNED_BYTE, // 指定像素数据的数据类型
pixels // 指定内存中指向图像数据的指针
);
// 生成多级渐远纹理,多消耗1/3的显存,较小分辨率时获得更好的效果
// glGenerateMipmap(GL_TEXTURE_2D);
// 指定插值方法
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// 恢复初始对齐格式
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
// 释放图形内存
stbi_image_free(pixels);
};