-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
169 lines (138 loc) · 5.5 KB
/
Model.cpp
File metadata and controls
169 lines (138 loc) · 5.5 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
#include "Model.h"
#include "Shader.h"
#include <iostream>
#include <glm/gtc/type_ptr.hpp>
// These must be defined exactly once in the project (in mp.cpp)
// so we only declare the loaders here
#define TINYOBJLOADER_IMPLEMENTATION
#include "tiny_obj_loader.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
using namespace std;
using namespace glm;
// ---- Load OBJ using tinyobjloader ----
void Model::loadOBJ(const string& objPath) {
tinyobj::attrib_t attrib;
vector<tinyobj::shape_t> shapes;
vector<tinyobj::material_t> materials;
string err;
bool ok = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, objPath.c_str());
if (!ok || shapes.empty()) {
cerr << "[Model] Failed to load OBJ: " << objPath << endl;
if (!err.empty()) cerr << " Error: " << err << endl;
return;
}
cout << "[Model] Loaded " << shapes.size() << " shapes from " << objPath << endl;
// Pack interleaved: X Y Z | Nx Ny Nz | U V (8 floats per vertex)
vector<float> data;
for (auto& shape : shapes) { // ← loop ALL shapes
for (auto& idx : shape.mesh.indices) {
// Position
data.push_back(attrib.vertices[idx.vertex_index * 3]);
data.push_back(attrib.vertices[idx.vertex_index * 3 + 1]);
data.push_back(attrib.vertices[idx.vertex_index * 3 + 2]);
// Normal
if (idx.normal_index >= 0) {
data.push_back(attrib.normals[idx.normal_index * 3]);
data.push_back(attrib.normals[idx.normal_index * 3 + 1]);
data.push_back(attrib.normals[idx.normal_index * 3 + 2]);
} else {
data.push_back(0.f); data.push_back(1.f); data.push_back(0.f);
}
// UV
if (idx.texcoord_index >= 0) {
data.push_back(attrib.texcoords[idx.texcoord_index * 2]);
data.push_back(attrib.texcoords[idx.texcoord_index * 2 + 1]);
} else {
data.push_back(0.f); data.push_back(0.f);
}
}
} // end shape loop
vertexCount = (int)(data.size() / 8);
cout << "[Model] Total vertices: " << vertexCount << endl;
setupMesh(data);
}
// ---- Upload vertex data to GPU ----
void Model::setupMesh(const vector<float>& data) {
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER,
sizeof(float) * data.size(),
data.data(), GL_STATIC_DRAW);
// attrib 0: position (3 floats, offset 0)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// attrib 1: normal (3 floats, offset 12)
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,
8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// attrib 2: uv (2 floats, offset 24)
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE,
8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
glBindVertexArray(0);
}
// ---- Load texture from file ----
void Model::loadTexture(const string& texPath) {
int w, h, ch;
unsigned char* data = stbi_load(texPath.c_str(), &w, &h, &ch, 0);
stbi_set_flip_vertically_on_load(true);
if (!data) {
cerr << "[Model] Failed to load texture: " << texPath << "\n";
// Generate a 1x1 white fallback texture
unsigned char white[3] = { 255, 255, 255 };
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,
GL_RGB, GL_UNSIGNED_BYTE, white);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
glGenTextures(1, &textureID);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureID);
GLenum fmt = (ch == 4) ? GL_RGBA : GL_RGB;
glTexImage2D(GL_TEXTURE_2D, 0, fmt, w, h, 0, fmt, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
stbi_image_free(data);
}
// ---- Constructor ----
Model::Model(const string& objPath, const string& texPath)
: VAO(0), VBO(0), textureID(0), vertexCount(0)
{
loadOBJ(objPath);
loadTexture(texPath);
}
// ---- Destructor ----
Model::~Model() {
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteTextures(1, &textureID);
}
// ---- Build model matrix from position / scale / rotation ----
mat4 Model::getModelMatrix() const {
mat4 m = mat4(1.f);
m = glm::translate(m, position);
// Fix sideways model
m = glm::rotate(m, radians(90.f), vec3(0.f, 1.f, 0.f));
m = glm::rotate(m, radians(rotation.y), vec3(0.f, 1.f, 0.f)); // yaw
m = glm::rotate(m, radians(rotation.x), vec3(1.f, 0.f, 0.f)); // pitch
m = glm::rotate(m, radians(rotation.z), vec3(0.f, 0.f, 1.f)); // roll
m = glm::scale(m, this->scale);
return m;
}
// ---- Draw ----
void Model::draw() {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureID);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, vertexCount);
glBindVertexArray(0);
}