-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathModel.cpp
More file actions
231 lines (200 loc) · 6.02 KB
/
Copy pathModel.cpp
File metadata and controls
231 lines (200 loc) · 6.02 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
#include "Model.h"
oGlModel::oGlModel()
{
}
oGlModel::oGlModel(string filename)
{
load(filename);
}
//Load a .mtl file into a struct and add it to the list
void oGlModel::loadMaterial(string matFile)
{
ifstream matf(matFile.c_str());
string line;
oGlMatInfo material;
material.name = "";
while(matf.good())
{
getline(matf, line);
vector<string> lineItems = splitString(line, ' ');
if(lineItems[0] == "newmtl")
{
if(material.name != "")
materials.push_back(material);
material.name = lineItems[1];
material.illum = 0;
material.Ns = 0;
}
else if(lineItems[0] == "Ka")
{
material.Ka.x = atof(lineItems[1].c_str());
material.Ka.y = atof(lineItems[2].c_str());
material.Ka.z = atof(lineItems[3].c_str());
}
else if(lineItems[0] == "Kd")
{
material.Kd.x = atof(lineItems[1].c_str());
material.Kd.y = atof(lineItems[2].c_str());
material.Kd.z = atof(lineItems[3].c_str());
}
else if(lineItems[0] == "Ks")
{
material.Ks.x = atof(lineItems[1].c_str());
material.Ks.y = atof(lineItems[2].c_str());
material.Ks.z = atof(lineItems[3].c_str());
}
else if(lineItems[0] == "illum")
{
material.illum = atoi(lineItems[1].c_str());
}
else if(lineItems[0] == "Ns")
{
material.Ns = atof(lineItems[1].c_str());
}
}
materials.push_back(material);
}
//Load a .obj file to the class
void oGlModel::load(string filename)
{
string line;
vector<string> lineItems;
ifstream objFile(filename.c_str());
//instead of doing this, just count how many of each thing there is and THEN resize, and load
objFile.seekg (0, ios::end);
int length = objFile.tellg();
objFile.seekg (0, ios::beg);
vertexes.reserve((int)length / 25);
faces.reserve((int)length / 25);
textureCoords.reserve((int)length / 25);
while(objFile.good())
{
getline(objFile, line);
lineItems = splitString(line, ' ');
if(lineItems[0] == "v")
{
oGlVertex v;
v.x = atof(lineItems[1].c_str());
v.y = atof(lineItems[2].c_str());
v.z = atof(lineItems[3].c_str());
vertexes.push_back(v);
}
else if (lineItems[0] == "vn")
{
oGlVertex v;
v.x = atof(lineItems[1].c_str());
v.y = atof(lineItems[2].c_str());
v.z = atof(lineItems[3].c_str());
normalVectors.push_back(v);
}
else if(lineItems[0] == "vt")
{
oGlVertex v;
v.x = atof(lineItems[1].c_str());
v.y = atof(lineItems[2].c_str());
v.z = atof(lineItems[3].c_str());
textureCoords.push_back(v);
}
else if(lineItems[0] == "f")
{
oGlFace face;
for(int i = 1; i < lineItems.size(); i++)
{
vector<string> faceVs = splitString(lineItems[i],'/');
oGlVTN vtn;
if(lineItems[i] == "")
continue;
switch(faceVs.size())
{
case 3:
vtn.norm = atoi(faceVs[2].c_str());
vtn.vert = atoi(faceVs[0].c_str());
vtn.tex = atoi(faceVs[1].c_str());
if(faceVs[1] == "")
vtn.tex = -1;
break;
case 2:
vtn.vert = atoi(faceVs[0].c_str());
vtn.tex = atoi(faceVs[1].c_str());
vtn.norm = -1;
break;
case 1:
vtn.vert = atoi(faceVs[0].c_str());
vtn.norm = -1;
vtn.tex = -1;
break;
}
face.vtnPairs.push_back(vtn);
}
faces.push_back(face);
}
else if(lineItems[0] == "mtllib")
{
int strI = filename.size() - 1;
for(;strI >= 0 && filename[strI] != '/'; strI--);
string nf = filename.substr(0, strI+1);
nf += lineItems[1];
loadMaterial(nf);
}
else if(lineItems[0] == "usemtl")
{
int mi = -1;
for(int ti = 0; ti < materials.size(); ti++)
{
if(materials[ti].name == lineItems[1])
{
mi = ti;
break;
}
}
oGlMatIndex matI;
matI.matIndex = mi;
matI.fIndex = faces.size();
matIndexes.push_back(matI);
}
}
}
//split a string by a given delimiter, i just decided this was nice to have here
vector<string> oGlModel::splitString(string s, char delim)
{
vector<string> retTok;
string temp;
for(int i = 0; i < s.length(); i++)
{
if(s[i] != delim)
temp += s[i];
else
{
retTok.push_back(temp);
temp = "";
}
}
retTok.push_back(temp);
return retTok;
}
//calculate the normal vector for a given face, cross products, boo yah
oGlVertex oGlModel::calcNormal(oGlVertex a, oGlVertex b, oGlVertex c)
{
/*
So for a triangle p1, p2, p3, if the vector U = p2 - p1 and the vector V = p3 - p1 then the normal N = U X V and can be calculated by:
Nx = UyVz - UzVy
Ny = UzVx - UxVz
Nz = UxVy - UyVx
*/
oGlVertex norm;
oGlVertex U;
oGlVertex V;
U.x = b.x - a.x;
U.y = b.y - a.y;
U.z = b.z - a.z;
V.x = c.x - a.x;
V.y = c.y - a.y;
V.z = c.z - a.z;
norm.x = (U.y * V.z) - (U.z * V.y);
norm.y = (U.z * V.x) - (U.x * V.z);
norm.z = (U.x * V.y) - (U.y * V.x);
//norm.x = (U.z * V.y) - (U.y * V.z);
//norm.y = (U.x * V.z) - (U.z * V.x);
//norm.z = (U.y * V.x) - (U.x * V.y);
return norm;
}