-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.cpp
125 lines (113 loc) · 3.41 KB
/
Model.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
#include "Model.h"
void Model::render() const
{
glPushMatrix();
glTranslatef(getPosition().getX(), getPosition().getY(), getPosition().getZ());
glRotatef(getOrientation().getX(), 1.0, 0.0, 0.0);
glRotatef(getOrientation().getY(), 0.0, 1.0, 0.0);
glRotatef(getOrientation().getZ(), 0.0, 0.0, 1.0);
glColor3f(getColor().getRed(), getColor().getGreen(), getColor().getBlue());
for (Triangle* triangle : this->triangles)
{
triangle->render();
}
glPopMatrix();
}
void Model::loadModel(const string& filename)
{
string line;
ifstream modelFile(filename.c_str());
if (modelFile.is_open())
{
while (modelFile.good())
{
getline(modelFile, line);
if (line.c_str()[0] == 'v' && line.c_str()[1] == 't')
{
//cout << ">>> Esto es una coordenada de textura de un vértice: \n";
//cout << line << "\n";
line[0] = ' ';
line[1] = ' ';
this->textures.push_back(this->parseLineToVector3D(line));
//cout << ">>>>> tamaño de texture: " << this->textures.size() << "\n";
}
else if (line.c_str()[0] == 'v' && line.c_str()[1] == 'n')
{
//cout << ">>> Esto es la normal de un vértice: \n";
//cout << line << "\n";
line[0] = ' ';
line[1] = ' ';
this->normals.push_back(this->parseLineToVector3D(line));
//cout << ">>>>> tamaño de normals: " << this->normals.size() << "\n";
}
else if (line.c_str()[0] == 'v')
{
//cout << ">>> Esto es un vértice: \n";
//cout << line << "\n";
line[0] = ' ';
this->vertexes.push_back(this->parseLineToVector3D(line));
//cout << ">>>>> tamaño de vertexes: " << this->vertexes.size() << "\n";
}
else if (line.c_str()[0] == 'f')
{
//cout << ">>> Esto es una cara: \n";
//cout << line << "\n";
line[0] = ' ';
this->triangles.push_back(this->parseFace(line));
}
else if (line.c_str()[0] == '#')
{
//cout << ">>> Esto es un comentario: \n";
//cout << line << "\n";
}
else
{
//cout << ">>> Esto no se sabe lo que es: \n";
//cout << line << "\n";
}
}
modelFile.close();
}
}
Vector3D* Model::parseLineToVector3D(string& line)
{
istringstream lineManager(line);
float x = 0.0;
float y = 0.0;
float z = 0.0;
lineManager >> x;
lineManager >> y;
lineManager >> z;
Vector3D* vector = new(nothrow) Vector3D(x, y, z);
return vector;
}
Triangle* Model::parseFace(string& line)
{
istringstream lineManager(line);
int firstVertexIndex = 0;
//int firstVertexTexture = 0;
int firstVertexNormal = 0;
int secondVertexIndex = 0;
//int secondVertexTexture = 0;
int secondVertexNormal = 0;
int thirdVertexIndex = 0;
//int thirdVertexTexture = 0;
int thirdVertexNormal = 0;
char c;
lineManager >> firstVertexIndex >> c >> c >> firstVertexNormal;
lineManager >> secondVertexIndex >> c >> c >> secondVertexNormal;
lineManager >> thirdVertexIndex >> c >> c >> thirdVertexNormal;
//cout << firstVertexIndex << "/" << "/" << firstVertexNormal << endl;
//cout << secondVertexIndex << "/" << "/" << secondVertexNormal << endl;
//cout << thirdVertexIndex << "/" << "/" << thirdVertexNormal << endl;
Vector3D triangleVertex1 = *this->vertexes[--firstVertexIndex];
Vector3D triangleVertex2 = *this->vertexes[--secondVertexIndex];
Vector3D triangleVertex3 = *this->vertexes[--thirdVertexIndex];
Vector3D triangleNormal = *this->normals[--firstVertexNormal];
Triangle* triangle = new(nothrow) Triangle(
triangleVertex1,
triangleVertex2,
triangleVertex3,
triangleNormal);
return triangle;
}