-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjloader.cpp
More file actions
69 lines (60 loc) · 1.97 KB
/
Copy pathobjloader.cpp
File metadata and controls
69 lines (60 loc) · 1.97 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
#include "objloader.hpp"
#ifndef __EMSCRIPTEN__
// Original Assimp-based loader for desktop
bool loadAssimpModel(const char *path, std::vector<glm::vec3> &out_vertices,
std::vector<glm::vec2> &out_uvs,
std::vector<glm::vec3> &out_normals) {
Assimp::Importer importer;
const aiScene *scene =
importer.ReadFile(path, aiProcess_Triangulate | aiProcess_GenNormals |
aiProcess_CalcTangentSpace);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE ||
!scene->mRootNode) {
std::cerr << "Assimp error: " << importer.GetErrorString() << std::endl;
return false;
}
if (scene->mNumMeshes < 1) {
std::cerr << "No meshes found in file" << std::endl;
return false;
}
const aiMesh *mesh = scene->mMeshes[0];
// Vertices
out_vertices.reserve(mesh->mNumVertices);
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
glm::vec3 vertex;
vertex.x = mesh->mVertices[i].x;
vertex.y = mesh->mVertices[i].y;
vertex.z = mesh->mVertices[i].z;
out_vertices.push_back(vertex);
}
// UVs
if (mesh->mTextureCoords[0]) {
out_uvs.reserve(mesh->mNumVertices);
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
glm::vec2 uv;
uv.x = mesh->mTextureCoords[0][i].x;
uv.y = mesh->mTextureCoords[0][i].y;
out_uvs.push_back(uv);
}
}
// Normals
if (mesh->mNormals) {
out_normals.reserve(mesh->mNumVertices);
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
glm::vec3 normal;
normal.x = mesh->mNormals[i].x;
normal.y = mesh->mNormals[i].y;
normal.z = mesh->mNormals[i].z;
out_normals.push_back(normal);
}
}
return true;
}
#else
// WebGL-compatible OBJ loader for Emscripten
bool loadAssimpModel(const char *path, std::vector<glm::vec3> &out_vertices,
std::vector<glm::vec2> &out_uvs,
std::vector<glm::vec3> &out_normals) {
return false;
}
#endif