-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobj.cpp
More file actions
139 lines (131 loc) · 3.85 KB
/
obj.cpp
File metadata and controls
139 lines (131 loc) · 3.85 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
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#define GLEW_STATIC
#include <GL/glew.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>
typedef unsigned int uint;
/** Mesh class with the sole purpose of converting obj files to opengl element arrays.
* That's what it's designed for.
*/
class Mesh {
std::vector<float> vertices;
std::vector<float> texCoord;
std::vector<float> vertNorm;
std::vector<uint> faces;
public:
int vertCount, texCoordCount, normCount, faceCount;
Mesh ();
Mesh (std::string filename);
void getVertexBufferArray(std::vector<GLfloat> &v, uint &stride, uint &colorOffset, uint &texOffset, uint &normalOffset);
void getElementBufferArray(std::vector<GLuint> &v);
} mesh;
Mesh::Mesh() {
}
Mesh::Mesh(const std::string filename) {
vertCount = texCoordCount = normCount = faceCount = 0;
std::string line, word;
std::ifstream objfile(filename.c_str());
if (objfile.is_open()) {
std::vector<float> tempFaces;
std::vector<float> *current;
while (getline(objfile, line)) {
std::string::size_type comment = line.find('#');
if (comment != std::string::npos)
line = line.substr(0,comment);
if (line.size() < 2)
continue;
if (line.at(0) == 'u')
continue;
std::stringstream stream(line);
for (uint i=0; stream >> word; i++) {
switch (word.at(0)) {
case 'v':
if (word.size() == 1) {
current = &vertices;
vertCount++;
}
else {
switch (word.at(1)) {
case 't':
current = &texCoord;
texCoordCount++;
break;
case 'n':
current = &vertNorm;
normCount++;
break;
}
}
break;
case 'f':
current = &tempFaces;
faceCount++;
break;
case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8':case '9':case '-':
if (current == &tempFaces) {
std::string::size_type slash = word.find_first_of("/");
if (slash == std::string::npos) {
faces.push_back(std::atoi(word.c_str()));
faces.push_back(0);
faces.push_back(0);
} else {
faces.push_back(std::atoi(word.substr(0, slash).c_str()));
word = word.substr(slash+1);
slash = word.find_first_of("/");
if (slash == std::string::npos) {
faces.push_back(std::atoi(word.c_str()));
faces.push_back(0);
} else {
if (slash == 0) {
faces.push_back(0);
faces.push_back(std::atoi(word.substr(1).c_str()));
} else {
// faces.push_back(std::atoi(word.substr(0, slash).c_str()));
faces.push_back(0);
faces.push_back(std::atoi(word.substr(slash+1).c_str()));
}
}
}
} else
current->push_back(std::atof(word.c_str()));
break;
}
}
}
}
}
void Mesh::getVertexBufferArray(std::vector<GLfloat> &v, uint &stride, uint &colorOffset, uint &texOffset, uint &normalOffset) {
uint vertSize = vertCount == 0 ? 0 : vertices.size()/vertCount;
uint texSize = texCoordCount == 0 ? 0 : texCoord.size()/texCoordCount;
uint normSize = normCount == 0 ? 0 : vertNorm.size()/normCount;
for (uint i = 0; i < faces.size(); i+=3) {
for (uint j = 0; j < vertSize; j++) {
v.push_back(vertices[(faces[i]-1)*vertSize + j]);
}
if (faces[i+1] != 0) {
for (uint j = 0; j < texSize; j++) {
v.push_back(texCoord[(faces[i+1]-1)*texSize + j]);
}
}
if (faces[i+2] != 0) {
for (uint j = 0; j < normSize; j++) {
v.push_back(vertNorm[(faces[i+2]-1)*normSize + j]);
}
}
}
stride = vertSize + texSize + normSize;
colorOffset = vertSize > 4 ? vertSize - 3 : 0;
texOffset = vertSize;
normalOffset = vertSize + texSize;
}
void Mesh::getElementBufferArray(std::vector<GLuint> &v) {
for (GLuint i = 0; i < faces.size()/3; i++) {
v.push_back(i);
}
}