-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOBJloader.h
More file actions
131 lines (119 loc) · 4.69 KB
/
OBJloader.h
File metadata and controls
131 lines (119 loc) · 4.69 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
// Provided by the class
#include <iostream>
#include "glm.hpp"
#include <vector>
#include <stdio.h>
#include <stdlib.h>
using namespace glm;
using namespace std;
bool loadOBJ(
const char * path,
vector<vec3> & out_vertices,
vector<vec3> & out_normals,
vector<vec2> & out_uvs) {
vector<int> vertexIndices, uvIndices, normalIndices;
vector<vec3> temp_vertices;
vector<vec2> temp_uvs;
vector<vec3> temp_normals;
FILE * file;
file = fopen(path, "r"); // &file, path, "r"
if (!file) {
printf("Impossible to open the file ! Are you in the right path ?\n");
exit(-1);
}
while (1) {
char lineHeader[128];
// read the first word of the line
int res = fscanf(file, "%s", lineHeader);
if (res == EOF)
break; // EOF = End Of File. Quit the loop.
// else : parse lineHeader
if (strcmp(lineHeader, "v") == 0) {
vec3 vertex;
fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z);
temp_vertices.push_back(vertex);
}
else if (strcmp(lineHeader, "vt") == 0) {
vec2 uv;
fscanf(file, "%f %f\n", &uv.x, &uv.y);
uv.y = -uv.y; // Invert V coordinate since we will only use DDS texture, which are inverted. Remove if you want to use TGA or BMP loaders.
temp_uvs.push_back(uv);
}
else if (strcmp(lineHeader, "vn") == 0) {
vec3 normal;
fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z);
temp_normals.push_back(normal);
}
else if (strcmp(lineHeader, "f") == 0) {
int vertexIndex[3], uvIndex[3], normalIndex[3];
bool uv = true;
bool norm = true;
char line[128];
fgets(line, 128, file);
//vertex, uv, norm
int matches = sscanf(line, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex[0], &uvIndex[0], &normalIndex[0], &vertexIndex[1], &uvIndex[1], &normalIndex[1], &vertexIndex[2], &uvIndex[2], &normalIndex[2]);
if (matches != 9) {
//vertex, norm
matches = sscanf(line, "%d//%d %d//%d %d//%d\n", &vertexIndex[0], &normalIndex[0], &vertexIndex[1], &normalIndex[1], &vertexIndex[2], &normalIndex[2]);
if (matches != 6) {
//vertex, uv
matches = sscanf(line, "%d/%d %d/%d %d/%d\n", &vertexIndex[0], &uvIndex[0], &vertexIndex[1], &uvIndex[1], &vertexIndex[2], &uvIndex[2]);
if (matches != 6) {
//vertex
matches = sscanf(line, "%d %d %d\n", &vertexIndex[0], &vertexIndex[1], &vertexIndex[2]);
if (matches != 6) {
printf("File can't be read by our simple parser. 'f' format expected: d/d/d d/d/d d/d/d || d/d d/d d/d || d//d d//d d//d\n");
printf("Character at %ld", ftell(file));
return false;
}
norm = false;
}
else {
norm = false;
}
}
else {
uv = false;
}
}
vertexIndices.push_back(vertexIndex[0]);
vertexIndices.push_back(vertexIndex[1]);
vertexIndices.push_back(vertexIndex[2]);
if (norm) {
normalIndices.push_back(normalIndex[0]);
normalIndices.push_back(normalIndex[1]);
normalIndices.push_back(normalIndex[2]);
}
if (uv) {
uvIndices.push_back(uvIndex[0]);
uvIndices.push_back(uvIndex[1]);
uvIndices.push_back(uvIndex[2]);
}
}
else {
char clear[1000];
fgets(clear, 1000, file);
}
}
// For each vertex of each triangle
for (unsigned int i = 0; i < vertexIndices.size(); i++) {
if (uvIndices.size() != 0) {
if (i < uvIndices.size()) {
unsigned int uvIndex = abs(uvIndices[i]);
vec2 uv = temp_uvs[uvIndex - 1];
out_uvs.push_back(uv);
}
}
if (normalIndices.size() != 0) {
if (i < normalIndices.size()) {
unsigned int normalIndex = abs(normalIndices[i]);
vec3 normal = temp_normals[normalIndex - 1];
out_normals.push_back(normal);
}
}
unsigned int vertexIndex = abs(vertexIndices[i]);
vec3 vertex = temp_vertices[vertexIndex - 1];
out_vertices.push_back(vertex);
}
return true;
}