-
Notifications
You must be signed in to change notification settings - Fork 0
/
OBJModel.cpp
199 lines (159 loc) · 3.77 KB
/
OBJModel.cpp
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
#include "OBJModel.h"
OBJModel::OBJModel(){}
OBJModel::OBJModel(const char *modelPath, bool useBones)
{
VBO = new VertexBuffer;
std::vector<glm::vec3> verts;
std::vector<glm::vec3> normals;
std::vector<glm::vec2> texCoords;
std::vector<int> facesVerts;
std::vector<int> facesTexCoords;
std::vector<int> facesNormals;
//load image
ILuint texName;
ilGenImages(1, &texName);
ilBindImage(texName);
char* firstWord = new char();
char* data = new char();
//Open the file
std::ifstream file;
file.open(modelPath);
if (!file.is_open())
{
std::cout << "Error opening the file " << modelPath << "\n";
return;
}
//Load data
while (!file.eof())
{
file >> firstWord;
//Vertex data
if (!_stricmp(firstWord, "v"))
{
glm::vec3 vertex;
//X data
file >> data;
vertex.x = atof(data);
//check max and min
if (vertex.x > maxVect.x)
maxVect.x = vertex.x;
else if (vertex.x < maxVect.x)
minVect.x = vertex.x;
//Y data
file >> data;
vertex.y = atof(data);
//check max and min
if (vertex.y > maxVect.y)
maxVect.y = vertex.y;
else if (vertex.y < maxVect.y)
minVect.y = vertex.y;
//Z data
file >> data;
vertex.z = atof(data);
//check max and min
if (vertex.z > maxVect.z)
maxVect.z = vertex.z;
else if (vertex.z < maxVect.z)
minVect.z = vertex.z;
//Add to verticies list
verts.push_back(vertex);
}
//Tex coord data
else if (!_stricmp(firstWord, "vt"))
{
glm::vec2 texCoord;
//X data
file >> data;
texCoord.x = atof(data);
//Y data
file >> data;
texCoord.y = atof(data);
texCoords.push_back(texCoord);
}
//normals data
else if (!_stricmp(firstWord, "vn"))
{
glm::vec3 normal;
//X data
file >> data;
normal.x = atof(data);
//Y data
file >> data;
normal.y = atof(data);
//Z data
file >> data;
normal.z = atof(data);
normals.push_back(normal);
}
//faces data
else if (!_stricmp(firstWord, "f"))
{
for (unsigned int i = 0; i < 3; ++i)
{
int facesArray[3];
for (unsigned int i = 0; i < 3; ++i)
{
if (i == 2)
file >> data;
else
file.getline(data, 256, '/');
facesArray[i] = atoi(data);
}
facesVerts.push_back(facesArray[0] - 1);
facesTexCoords.push_back(facesArray[1] - 1);
facesNormals.push_back(facesArray[2] - 1);
}
}
//skip to the end of the line
file.ignore(256, '\n');
}
//create vertex data for faces
//float* finalVerts[];
std::vector<float> finalVerts;
for (unsigned int i = 0; i < facesVerts.size(); ++i)
{
finalVerts.push_back(verts[facesVerts[i]].x);
finalVerts.push_back(verts[facesVerts[i]].y);
finalVerts.push_back(verts[facesVerts[i]].z);
}
std::vector<float> finalTexCoords;
for (unsigned int i = 0; i < facesTexCoords.size(); ++i)
{
finalTexCoords.push_back(texCoords[facesTexCoords[i]].x);
finalTexCoords.push_back(texCoords[facesTexCoords[i]].y);
this->texcoords.push_back(glm::vec2(texCoords[facesTexCoords[i]].x, texCoords[facesTexCoords[i]].y));
}
std::vector<float> finalNormals;
for (unsigned int i = 0; i < facesNormals.size(); ++i)
{
finalNormals.push_back(normals[facesNormals[i]].x);
finalNormals.push_back(normals[facesNormals[i]].y);
finalNormals.push_back(normals[facesNormals[i]].z);
}
this->verts = finalVerts;
//Init and add data
VBO->Initialize(facesVerts.size(), true, true, useBones);
VBO->AddVerticies(&finalVerts[0]);
VBO->AddNormals(&finalNormals[0]);
VBO->AddTexCoords(&finalTexCoords[0]);
}
OBJModel::~OBJModel(){}
void OBJModel::DrawOBJ()
{
VBO->ActivateAndRender();
glDisable(GL_TEXTURE_2D);
}
//Get the texture handle
GLfloat OBJModel::GetTex()
{
return texture;
}
//Get the VBO
VertexBuffer* OBJModel::GetVBO(void)
{
return VBO;
}
std::vector<float> OBJModel::GetVerts(void)
{
return verts;
}