-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.cpp
79 lines (59 loc) · 1.74 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
#include "model.h"
#include <fstream>
#include <cassert>
Model::Model() : m_vertices(0), m_indices(0), m_vertexCount(0), m_indexCount(0)
{
}
Model::~Model()
{
}
bool Model::Load(const std::string& key, const OptionList& options)
{
std::ifstream tmd(key.c_str());
if(!tmd.is_open())
return false;
tmd >> m_vertexCount >> m_indexCount;
assert(m_vertexCount < 65535); // Mesh currently uses 16 bit for indexes
std::cout << "vcount: " << m_vertexCount << ", icount: " << m_indexCount << std::endl;
m_vertices = new Vertex[m_vertexCount];
m_indices = new Index[m_indexCount];
for(int i = 0; i < m_vertexCount; ++i)
{
Vertex& v = m_vertices[i];
tmd >> v.coord.x;
tmd >> v.coord.y;
tmd >> v.coord.z;
tmd >> v.normal.x;
tmd >> v.normal.y;
tmd >> v.normal.z;
}
for(int i = 0; i < m_indexCount; ++i)
{
tmd >> m_indices[i];
}
tmd.close();
return true;
}
bool Model::Release()
{
if(m_vertices)
delete [] m_vertices;
if(m_indices)
delete [] m_indices;
return true;
}
Mesh<VertexData3dNormal>* Model::GetMesh()
{
assert(m_vertexCount > 0 && m_indexCount > 0);
VertexData3dNormal* vd = new VertexData3dNormal[m_vertexCount];
for(int i = 0; i < m_vertexCount; ++i)
{
Vertex& v = m_vertices[i];
vd[i] = Mesh<VertexData3dNormal>::VertexData(v.coord.x, v.coord.y, v.coord.z, v.normal.x, v.normal.y, v.normal.z, 1.f, 1.f, 1.f, 0, 0);
}
Mesh<VertexData3dNormal>* mesh = new Mesh<VertexData3dNormal>(Mesh<VertexData3dNormal>::MT_TRIANGLE);
mesh->SetMeshData(vd, m_vertexCount, m_indices, m_indexCount);
//mesh->SetMeshData(vd, m_vertexCount);
delete [] vd;
return mesh;
}