-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeshResource.cpp
More file actions
93 lines (78 loc) · 3.23 KB
/
MeshResource.cpp
File metadata and controls
93 lines (78 loc) · 3.23 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
#include <MeshResource.hpp>
#include <stdio.h> // for printf
// You need to implement this or move it here
extern GLuint loadBMP_custom(const char* path);
extern bool loadOBJ(
const char* path,
std::vector<glm::vec3>& vertices,
std::vector<glm::vec2>& uvs,
std::vector<glm::vec3>& normals
);
// A simple (and fragile) way to get texture paths.
// You had this in your code, so I'm just creating a stub.
// You should find the .mtl file and parse it.
// For now, this just swaps .obj for .bmp (as an example)
std::string MeshResource::getTextureNameFromObj(const char* obj_path) {
std::string s = obj_path;
s = s.substr(0, s.find_last_of('.'));
// HACK: Assuming texture has same name + .bmp
// A real solution would parse the .mtl file.
return s + ".bmp";
}
MeshResource::MeshResource(const char* obj_path) {
printf("\n=== Loading MESH RESOURCE: %s ===\n", obj_path);
// Initialize
this->VAO = 0;
this->vertexBuffer = 0;
this->uvBuffer = 0;
this->normalBuffer = 0;
this->textureID = 0;
this->vertexCount = 0;
// Load texture
std::string texture_filename = getTextureNameFromObj(obj_path);
if (!texture_filename.empty()) {
printf("Loading texture: %s\n", texture_filename.c_str());
this->textureID = loadBMP_custom(texture_filename.c_str());
printf("Texture ID: %d\n", this->textureID);
}
// Load OBJ
std::vector<glm::vec3> vertices;
std::vector<glm::vec2> uvs;
std::vector<glm::vec3> normals;
bool res = loadOBJ(obj_path, vertices, uvs, normals);
if (!res || vertices.empty()) {
printf("ERROR: Failed to load OBJ!\n");
return;
}
this->vertexCount = vertices.size();
printf("Vertices: %zu\n", this->vertexCount);
// Create VAO and Buffers
glGenVertexArrays(1, &this->VAO);
glBindVertexArray(this->VAO);
printf("VAO: %d\n", this->VAO);
glGenBuffers(1, &this->vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glGenBuffers(1, &this->uvBuffer);
glBindBuffer(GL_ARRAY_BUFFER, this->uvBuffer);
glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
glGenBuffers(1, &this->normalBuffer);
glBindBuffer(GL_ARRAY_BUFFER, this->normalBuffer);
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), &normals[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glBindVertexArray(0); // Unbind
printf("MeshResource created successfully!\n");
}
MeshResource::~MeshResource() {
printf("Destroying MeshResource (VAO: %d)\n", this->VAO);
if (this->vertexBuffer) glDeleteBuffers(1, &this->vertexBuffer);
if (this->uvBuffer) glDeleteBuffers(1, &this->uvBuffer);
if (this->normalBuffer) glDeleteBuffers(1, &this->normalBuffer);
if (this->textureID) glDeleteTextures(1, &this->textureID);
if (this->VAO) glDeleteVertexArrays(1, &this->VAO);
}