-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodelobj.cpp
76 lines (69 loc) · 1.47 KB
/
modelobj.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
#include "modelobj.h"
// Loads model from file
ModelObj::ModelObj(QString file)
{
objFile.setFileName(file);
#ifdef Q_OS_LINUX
setlocale(LC_NUMERIC, "C");
#endif
LoadMdl();
}
// Just set numeric locale to C standart
ModelObj::ModelObj()
{
#ifdef Q_OS_LINUX
setlocale(LC_NUMERIC, "C");
#endif
}
ModelObj::~ModelObj()
{
objFile.close();
}
void ModelObj::LoadMdl()
{
if(!objFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
//Error loading file
qDebug("Loading obj model failed");
return;
}
while(!objFile.atEnd()) {
QByteArray line = objFile.readLine();
switch(line.at(0))
{
case 'v':
ParceV(line);
break;
case 'f':
ParceF(line);
break;
}
}
}
vtxArray ModelObj::GetVertices()
{
//Check for vtxArr consistency
if(vtxArr.capacity() == 0) {
qDebug("Vertex array is empty:");
if(!objFile.isOpen())
qDebug("Model file not loaded!");
else
qDebug("Something goes wrong!");
}
return vtxArr;
}
faceArray ModelObj::GetFaces()
{
return fArr;
}
void ModelObj::ParceV(QByteArray line)
{
vertex tmpVtx;
sscanf(line.data(),"v %f %f %f",&tmpVtx.x,&tmpVtx.y,&tmpVtx.z);
vtxArr.push_back(tmpVtx);
}
void ModelObj::ParceF(QByteArray line)
{
face tmpFace;
sscanf(line.data(),"f %d %d %d",&tmpFace.x,&tmpFace.y,&tmpFace.z);
fArr.push_back(tmpFace);
}