-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodelmanager.cpp
More file actions
138 lines (102 loc) · 3.33 KB
/
modelmanager.cpp
File metadata and controls
138 lines (102 loc) · 3.33 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
#include "modelmanager.h"
#include <QFile>
#include <QStringList>
#include <QDebug>
ModelManager::ModelManager(QObject *parent) : QObject(parent)
{
}
ModelManager::~ModelManager()
{
}
bool ModelManager::loadModel(const QString &filename, const QString &texturePath)
{
QFile file(filename);
if(!file.open(QIODevice::ReadOnly))
{
m_error = file.errorString();
return false;
}
while(!file.atEnd())
{
QString line = file.readLine();
if(line.left(2) == "v ")
{
// if it is vertex,process and append him to vertex buffer
// vertex struct : v x y z ,where x y z - float data of vertex
QStringList vertexData = line.split(" ");
QVector3D vec( vertexData.at(1).toDouble(),
vertexData.at(2).toDouble(),
vertexData.at(3).toDouble() );
m_vertexBuffer.append(vec);
}else if (line.left(2) == "f ") {
// if it is faces , process and append to faces buffer
// they have struct like this : f a1/a2/a3 b1/b2/b3 c1/c2/c3
// a1,b1 and c1 is three vertices of a triangle
// a2,b2,c2 - texture data
// a3,b3,c3 - normal data
QStringList facesData = line.split(" ");
QVector<FaceData> triangleData;
for(int i=1;i<=3;i++)
{
FaceData data;
data.vertex = facesData.at(i).split("/").at(0).toInt() - 1;
data.tCoord = facesData.at(i).split("/").at(1).toInt() - 1;
data.norm = facesData.at(i).split("/").at(2).toInt() - 1;
triangleData.append(data);
}
m_facesBuffer.append(triangleData);
}else if (line.left(2) == "vt") {
QStringList lineData = line.split(" ");
QVector2D vec(lineData.at(2).toFloat() , lineData.at(3).toFloat());
m_textureBuffer.append(vec);
}
}
// debug info
qDebug() << "Vertex size :" << m_vertexBuffer.size() << "Faces Size" << m_facesBuffer.size();
qDebug() << "Texture data size : " << m_textureBuffer.size();
if(!texturePath.isEmpty())
{
if(!loadTexture(texturePath))
{
qDebug() << "cant' read texture file";
return false;
}
}
return true;
}
QVector<QVector3D> *ModelManager::vertexBuffer3D()
{
return &m_vertexBuffer;
}
QVector<QVector<FaceData> > *ModelManager::facesBuffer()
{
return &m_facesBuffer;
}
QVector<QVector2D> *ModelManager::textureBuffer()
{
return &m_textureBuffer;
}
QColor ModelManager::diffuseColor(const QVector2D &coord)
{
TGAColor color = m_tData.get(coord.x(),coord.y());
QColor trColor = QColor(color[2],color[1],color[0]);
return trColor;
}
QVector2D ModelManager::uvCoord(int face, int nvert)
{
int id = m_facesBuffer.at(face).at(nvert).tCoord;
QVector2D vec( m_textureBuffer.at(id).x() * m_tData.get_width(), m_textureBuffer.at(id).y() * m_tData.get_height() );
vec.setX( (int) vec.x() );
vec.setY( (int)vec.y() );
return vec;
}
bool ModelManager::loadTexture(const QString &path)
{
bool isOk = m_tData.read_tga_file(path.toStdString().c_str());
if(isOk ) m_tData.flip_vertically();
return isOk;
}
QString ModelManager::errorString()
{
return m_error;
}