-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
393 lines (336 loc) · 17.9 KB
/
Model.cpp
File metadata and controls
393 lines (336 loc) · 17.9 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
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
#include <iostream>
#include <fstream>
#include <string>
#include "Model.h"
#include "Texture.h"
#define print(x) //std::cout << x << "\n"
Model::Model(const std::filesystem::path& path_main, const std::filesystem::path& path_tex, bool is_height_map)
{
if (!is_height_map) {
LoadOBJFile(path_main);
GLuint texture_id = TextureInit(path_tex.string().c_str());
mesh = Mesh(GL_TRIANGLES, mesh_vertices, mesh_vertex_indices, texture_id);
}
else {
//HeightMap_Load(path_main);
HeightMap_Load2(path_main);
GLuint texture_id = TextureInit(path_tex.string().c_str());
mesh = Mesh(GL_TRIANGLES, mesh_vertices, mesh_vertex_indices, texture_id);
}
}
Model::Model(std::string _name, const std::filesystem::path& path_main, const std::filesystem::path& path_tex, glm::vec3 _position, glm::vec3 _scale, glm::vec4 _rotation, bool is_height_map, bool use_aabb) :
name(_name),
position(_position),
scale(_scale),
rotation(_rotation)
//use_aabb(use_aabb)
{
if (!is_height_map) {
LoadOBJFile(path_main);
}
else {
HeightMap_Load2(path_main);
}
GLuint texture_id = TextureInit(path_tex.string().c_str());
mesh = Mesh(GL_TRIANGLES, mesh_vertices, mesh_vertex_indices, texture_id);
}
void Model::Draw(Shader& shader)
{
mx_model = glm::identity<glm::mat4>();
mx_model = glm::translate(mx_model, position);
mx_model = glm::scale(mx_model, scale);
rotation_axes = glm::vec3(rotation.x, rotation.y, rotation.z);
mx_model = glm::rotate(mx_model, glm::radians(rotation.w), rotation_axes);
mesh.draw(shader, mx_model);
//mesh.draw(shader);
}
void Model::FillFileLines(const std::filesystem::path& file_name)
{
file_lines.clear();
std::ifstream file_reader(file_name);
while (getline(file_reader, file_line)) {
file_lines.push_back(file_line);
}
file_reader.close();
}
void Model::LoadOBJFile(const std::filesystem::path& file_name)
{
FillFileLines(file_name);
mesh_vertices.clear();
mesh_vertex_indices.clear();
std::vector<glm::vec3> vertices;
std::vector<glm::vec2> texture_coordinates;
std::vector<glm::vec3> vertex_normals;
std::vector<GLuint> indices_vertex, indices_texture_coordinate, indices_vertex_normal;
std::string first_two_chars, first_three_chars;
glm::vec2 uv;
glm::vec3 vertex_or_normal;
bool line_success;
for (const std::string& line : file_lines) {
if (!line.empty()) {
line_success = true;
first_two_chars = line.substr(0, 2);
first_three_chars = line.substr(0, 3);
// v -1.183220029 4.784470081 47.4618988
if (first_two_chars == "v ") {
vertex_or_normal = {};
(void)sscanf_s(line.c_str(), "v %f %f %f", &vertex_or_normal.x, &vertex_or_normal.y, &vertex_or_normal.z);
vertices.push_back(vertex_or_normal);
}
// vt 0.5000 0.7500
else if (first_three_chars == "vt ") {
uv = {};
(void)sscanf_s(line.c_str(), "vt %f %f", &uv.y, &uv.x);
texture_coordinates.push_back(uv);
}
// vn 0.7235898972 -0.6894102097 -0.03363365307
else if (first_three_chars == "vn ") {
vertex_or_normal = {};
(void)sscanf_s(line.c_str(), "vn %f %f %f", &vertex_or_normal.x, &vertex_or_normal.y, &vertex_or_normal.z);
vertex_normals.push_back(vertex_or_normal);
}
else if (first_two_chars == "f ") {
auto n = std::count(line.begin(), line.end(), '/');
// f 1 2 3
if (n == 0) {
unsigned int indices_temp[3]{};
(void)sscanf_s(line.c_str(), "f %d %d %d", &indices_temp[0], &indices_temp[1], &indices_temp[2]);
indices_vertex.insert(indices_vertex.end(), { indices_temp[0], indices_temp[1], indices_temp[2] });
}
// f 3/1 4/2 5/3
else if (n == 3) {
unsigned int indices_temp[6]{};
(void)sscanf_s(line.c_str(), "f %d/%d %d/%d %d/%d", &indices_temp[0], &indices_temp[3], &indices_temp[1], &indices_temp[4], &indices_temp[2], &indices_temp[5]);
indices_vertex.insert(indices_vertex.end(), { indices_temp[0], indices_temp[1], indices_temp[2] });
indices_texture_coordinate.insert(indices_texture_coordinate.end(), { indices_temp[3], indices_temp[4], indices_temp[5] });
}
else if (n == 6) {
// f 7//1 8//2 9//3
if (line.find("//") != std::string::npos) {
unsigned int indices_temp[6]{};
(void)sscanf_s(line.c_str(), "f %d//%d %d//%d %d//%d", &indices_temp[0], &indices_temp[3], &indices_temp[1], &indices_temp[4], &indices_temp[2], &indices_temp[5]);
indices_vertex.insert(indices_vertex.end(), { indices_temp[0], indices_temp[1], indices_temp[2] });
indices_vertex_normal.insert(indices_vertex_normal.end(), { indices_temp[3], indices_temp[4], indices_temp[5] });
}
// f 6/4/1 3/5/3 7/6/5
else {
unsigned int indices_temp[9]{};
(void)sscanf_s(line.c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d", &indices_temp[0], &indices_temp[3], &indices_temp[6], &indices_temp[1], &indices_temp[4], &indices_temp[7], &indices_temp[2], &indices_temp[5], &indices_temp[8]);
indices_vertex.insert(indices_vertex.end(), { indices_temp[0], indices_temp[1], indices_temp[2] });
indices_texture_coordinate.insert(indices_texture_coordinate.end(), { indices_temp[3], indices_temp[4], indices_temp[5] });
indices_vertex_normal.insert(indices_vertex_normal.end(), { indices_temp[6], indices_temp[7], indices_temp[8] });
}
}
// f 1/1/1 2/2/2 22/23/3 21/22/4
else if (n == 8) {
unsigned int v[4]{};
unsigned int vt[4]{};
unsigned int vn[4]{};
(void)sscanf_s(line.c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d", &v[0], &vt[0], &vn[0], &v[1], &vt[1], &vn[1], &v[2], &vt[2], &vn[2], &v[3], &vt[3], &vn[3]);
indices_vertex.insert(indices_vertex.end(), { v[0], v[1], v[2], v[0], v[2], v[3] });
indices_texture_coordinate.insert(indices_texture_coordinate.end(), { vt[0], vt[1], vt[2], vt[0], vt[2], vt[3] });
indices_vertex_normal.insert(indices_vertex_normal.end(), { vn[0], vn[1], vn[2], vn[0], vn[2], vn[3] });
}
else {
line_success = false;
}
}
else {
line_success = false;
}
if (!line_success && first_two_chars != "# ") {
print("LoadOBJFile: Ignoring line '" << line << "' in file '" << file_name << "'");
}
}
}
// RETARDED DRAW ? 2.0
std::vector<glm::vec3> vertices_direct;
std::vector<glm::vec2> texture_coordinates_direct;
std::vector<glm::vec3> vertex_normals_direct;
for (unsigned int u = 0; u < indices_vertex.size(); u++) {
vertices_direct.push_back(vertices[indices_vertex[u] - 1]);
}
for (unsigned int u = 0; u < indices_texture_coordinate.size(); u++) {
texture_coordinates_direct.push_back(texture_coordinates[indices_texture_coordinate[u] - 1]);
}
for (unsigned int u = 0; u < indices_vertex_normal.size(); u++) {
vertex_normals_direct.push_back(vertex_normals[indices_vertex_normal[u] - 1]);
}
///* Uncomment these if you don't like to live dangerously
auto n_direct_uvs = texture_coordinates_direct.size();
auto n_direct_normals = vertex_normals_direct.size();
/**/
for (unsigned int u = 0; u < vertices_direct.size(); u++) {
Vertex vertex{};
vertex.Position = vertices_direct[u];
if (u < n_direct_uvs) vertex.UVs = texture_coordinates_direct[u];
if (u < n_direct_normals) vertex.Normal = vertex_normals_direct[u];
mesh_vertices.push_back(vertex);
mesh_vertex_indices.push_back(u);
}
// What's said is said, what's done is done.
print("LoadOBJFile: Loaded OBJ file " << file_name << "\n");
}
void Model::LoadMTLFile(const std::filesystem::path& file_name)
{
FillFileLines(file_name);
//TODO: Model::LoadMTLFile
}
void Model::HeightMap_Load(const std::filesystem::path& file_name)
{
mesh_vertices.clear();
mesh_vertex_indices.clear();
cv::Mat hmap = cv::imread(file_name.u8string(), cv::IMREAD_GRAYSCALE);
if (hmap.empty()) std::cerr << "HeightMap: [!] Height map empty? File: " << file_name << "\n";
const unsigned int mesh_step_size = 10;
print("HeightMap: heightmap size: " << hmap.size << ", channels: " << hmap.channels());
if (hmap.channels() != 1) std::cerr << "HeightMap: [!] requested 1 channel, got: " << hmap.channels() << "\n";
// Create heightmap mesh from TRIANGLES in XZ plane, Y is UP (right hand rule)
//
// 3-----2
// | /|
// | / |
// |/ |
// 0-----1
//
// 012,023
//
glm::vec3 normal{};
unsigned int indices_counter = 0;
for (unsigned int x_coord = 0; x_coord < (hmap.cols - mesh_step_size); x_coord += mesh_step_size) {
for (unsigned int z_coord = 0; z_coord < (hmap.rows - mesh_step_size); z_coord += mesh_step_size) {
// Get The (X, Y, Z) Value For The Bottom Left Vertex = 0
glm::vec3 p0(x_coord, hmap.at<uchar>(cv::Point(x_coord, z_coord)), z_coord);
// Get The (X, Y, Z) Value For The Bottom Right Vertex = 1
glm::vec3 p1(x_coord + mesh_step_size, hmap.at<uchar>(cv::Point(x_coord + mesh_step_size, z_coord)), z_coord);
// Get The (X, Y, Z) Value For The Top Right Vertex = 2
glm::vec3 p2(x_coord + mesh_step_size, hmap.at<uchar>(cv::Point(x_coord + mesh_step_size, z_coord + mesh_step_size)), z_coord + mesh_step_size);
// Get The (X, Y, Z) Value For The Top Left Vertex = 3
glm::vec3 p3(x_coord, hmap.at<uchar>(cv::Point(x_coord, z_coord + mesh_step_size)), z_coord + mesh_step_size);
// Get max normalized height for tile, set texture accordingly
// Grayscale image returns 0..256, normalize to 0.0f..1.0f by dividing by 256 (255 ?)
float max_h = std::max(hmap.at<uchar>(cv::Point(x_coord, z_coord)) / 255.0f,
std::max(hmap.at<uchar>(cv::Point(x_coord, z_coord + mesh_step_size)) / 255.0f,
std::max(hmap.at<uchar>(cv::Point(x_coord + mesh_step_size, z_coord + mesh_step_size)) / 255.0f,
hmap.at<uchar>(cv::Point(x_coord + mesh_step_size, z_coord)) / 255.0f
)));
// Get texture coords in vertices, bottom left of geometry == bottom left of texture
glm::vec2 tc0 = HeightMap_GetSubtexByHeight(max_h);
glm::vec2 tc1 = tc0 + glm::vec2(1.0f / 16, 0.0f); // add offset for bottom right corner
glm::vec2 tc2 = tc0 + glm::vec2(1.0f / 16, 1.0f / 16); // add offset for top right corner
glm::vec2 tc3 = tc0 + glm::vec2(0.0f, 1.0f / 16); // add offset for bottom left corner
// RETARDED HEIGHT MAP ? 1.1
// calculate normal vector
normal = glm::normalize(glm::cross(p1 - p0, p2 - p0));
// place vertices and ST to mesh
mesh_vertices.emplace_back(Vertex{ p0, normal, tc0 });
mesh_vertices.emplace_back(Vertex{ p1, normal, tc1 });
mesh_vertices.emplace_back(Vertex{ p2, normal, tc2 });
mesh_vertices.emplace_back(Vertex{ p3, normal, tc3 });
// place indices
indices_counter += 4;
mesh_vertex_indices.emplace_back(indices_counter - 4);
mesh_vertex_indices.emplace_back(indices_counter - 2);
mesh_vertex_indices.emplace_back(indices_counter - 3);
mesh_vertex_indices.emplace_back(indices_counter - 4);
mesh_vertex_indices.emplace_back(indices_counter - 1);
mesh_vertex_indices.emplace_back(indices_counter - 2);
}
}
print("HeightMap: height map vertices: " << mesh_vertices.size());
}
void Model::HeightMap_Load2(const std::filesystem::path& file_name)
{
mesh_vertices.clear();
mesh_vertex_indices.clear();
cv::Mat hmap = cv::imread(file_name.u8string(), cv::IMREAD_GRAYSCALE);
if (hmap.empty()) std::cerr << "HeightMap: [!] Height map empty? File: " << file_name << "\n";
const unsigned int mesh_step_size = 10;
print("HeightMap: heightmap size: " << hmap.size << ", channels: " << hmap.channels());
if (hmap.channels() != 1) std::cerr << "HeightMap: [!] requested 1 channel, got: " << hmap.channels() << "\n";
// Create heightmap mesh from TRIANGLES in XZ plane, Y is UP (right hand rule)
//
// 3-----2
// | /|
// | / |
// |/ |
// 0-----1
//
// 012,023
glm::vec3 normalA{};
glm::vec3 normalB{};
glm::vec3 normal{};
unsigned int indices_counter = 0;
std::map<std::pair<unsigned int, unsigned int>, glm::vec3> normal_sums;
std::pair<unsigned int, unsigned int> pair, pair0, pair1, pair2, pair3;
for (unsigned int x_coord = 0; x_coord < (hmap.cols - mesh_step_size); x_coord += mesh_step_size) {
for (unsigned int z_coord = 0; z_coord < (hmap.rows - mesh_step_size); z_coord += mesh_step_size) {
// Get The (X, Y, Z) Value For The Bottom Left Vertex = 0
glm::vec3 p0(x_coord, hmap.at<uchar>(cv::Point(x_coord, z_coord)), z_coord);
// Get The (X, Y, Z) Value For The Bottom Right Vertex = 1
glm::vec3 p1(x_coord + mesh_step_size, hmap.at<uchar>(cv::Point(x_coord + mesh_step_size, z_coord)), z_coord);
// Get The (X, Y, Z) Value For The Top Right Vertex = 2
glm::vec3 p2(x_coord + mesh_step_size, hmap.at<uchar>(cv::Point(x_coord + mesh_step_size, z_coord + mesh_step_size)), z_coord + mesh_step_size);
// Get The (X, Y, Z) Value For The Top Left Vertex = 3
glm::vec3 p3(x_coord, hmap.at<uchar>(cv::Point(x_coord, z_coord + mesh_step_size)), z_coord + mesh_step_size);
// Get max normalized height for tile, set texture accordingly
// Grayscale image returns 0..256, normalize to 0.0f..1.0f by dividing by 256 (255 ?)
float max_h = std::max(hmap.at<uchar>(cv::Point(x_coord, z_coord)) / 255.0f,
std::max(hmap.at<uchar>(cv::Point(x_coord, z_coord + mesh_step_size)) / 255.0f,
std::max(hmap.at<uchar>(cv::Point(x_coord + mesh_step_size, z_coord + mesh_step_size)) / 255.0f,
hmap.at<uchar>(cv::Point(x_coord + mesh_step_size, z_coord)) / 255.0f
)));
// Get texture coords in vertices, bottom left of geometry == bottom left of texture
glm::vec2 tc0 = HeightMap_GetSubtexByHeight(max_h);
glm::vec2 tc1 = tc0 + glm::vec2((1.0f / 16), 0.0f); // add offset for bottom right corner
glm::vec2 tc2 = tc0 + glm::vec2((1.0f / 16), (1.0f / 16)); // add offset for top right corner
glm::vec2 tc3 = tc0 + glm::vec2(0.0f, (1.0f / 16)); // add offset for bottom left corner
// RETARDED HEIGHT MAP 2.0
// - calculate normal vector
normalA = glm::normalize(glm::cross(p1 - p0, p2 - p0));
normalB = glm::normalize(glm::cross(p2 - p0, p3 - p0));
normal = (normalA + normalB) / 2.0f;
// - place vertices and ST to mesh
mesh_vertices.emplace_back(Vertex{ p0, -normal, tc0 });
mesh_vertices.emplace_back(Vertex{ p1, -normal, tc1 });
mesh_vertices.emplace_back(Vertex{ p2, -normal, tc2 });
mesh_vertices.emplace_back(Vertex{ p3, -normal, tc3 });
// - place indices
indices_counter += 4;
mesh_vertex_indices.emplace_back(indices_counter - 4);
mesh_vertex_indices.emplace_back(indices_counter - 2);
mesh_vertex_indices.emplace_back(indices_counter - 3);
mesh_vertex_indices.emplace_back(indices_counter - 4);
mesh_vertex_indices.emplace_back(indices_counter - 1);
mesh_vertex_indices.emplace_back(indices_counter - 2);
// - normal averaging
pair0 = { x_coord, z_coord };
pair1 = { x_coord + mesh_step_size, z_coord };
pair2 = { x_coord + mesh_step_size, z_coord + mesh_step_size };
pair3 = { x_coord, z_coord + mesh_step_size };
normal_sums[pair0] -= normal;
normal_sums[pair1] -= normal;
normal_sums[pair2] -= normal;
normal_sums[pair3] -= normal;
}
}
// - normal averaging, 2nd iter
for (auto& vertex : mesh_vertices) {
pair = { static_cast<unsigned int>(vertex.Position.x), static_cast<unsigned int>(vertex.Position.z) };
vertex.Normal = glm::normalize(normal_sums[pair]); // no need to divide by four, we can just normalize
_heights[{vertex.Position.x* HEGHTMAP_SCALE, vertex.Position.z* HEGHTMAP_SCALE}] = vertex.Position.y; // for heightmap collision
}
print("HeightMap: height map vertices: " << mesh_vertices.size());
}
glm::vec2 Model::HeightMap_GetSubtexST(const int x, const int y)
{
return glm::vec2(x * 1.0f / 16, y * 1.0f / 16);
}
glm::vec2 Model::HeightMap_GetSubtexByHeight(float height)
{
if (height > 0.9) return HeightMap_GetSubtexST(2, 4); //snow
else if (height > 0.8) return HeightMap_GetSubtexST(3, 4); //ice
else if (height > 0.5) return HeightMap_GetSubtexST(5, 0); //rock
else if (height > 0.3) return HeightMap_GetSubtexST(0, 0); //soil
else return HeightMap_GetSubtexST(2, 0); //grass
}