/
loaddrawable.cpp
147 lines (132 loc) · 3.42 KB
/
loaddrawable.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "loaddrawable.h"
#include "contentmanager.h"
#include "textureinfo.h"
#include "cfg/ptree.h"
LoadDrawable::LoadDrawable(
const std::string & path,
const int anisotropy,
ContentManager & content,
std::list<std::tr1::shared_ptr<MODEL> > & modellist,
std::ostream & error) :
path(path),
anisotropy(anisotropy),
content(content),
modellist(modellist),
error(error)
{
// ctor
}
bool LoadDrawable::operator()(
const PTree & cfg,
SCENENODE & topnode,
keyed_container<SCENENODE>::handle * nodehandle,
keyed_container<DRAWABLE>::handle * drawhandle)
{
std::vector<std::string> texname;
if (!cfg.get("texture", texname)) return true;
std::string meshname;
if (!cfg.get("mesh", meshname, error)) return false;
return operator()(meshname, texname, cfg, topnode, nodehandle, drawhandle);
}
bool LoadDrawable::operator()(
const std::string & meshname,
const std::vector<std::string> & texname,
const PTree & cfg,
SCENENODE & topnode,
keyed_container<SCENENODE>::handle * nodeptr,
keyed_container<DRAWABLE>::handle * drawptr)
{
DRAWABLE drawable;
// set textures
TEXTUREINFO texinfo;
texinfo.mipmap = true;
texinfo.anisotropy = anisotropy;
std::tr1::shared_ptr<TEXTURE> tex;
if (texname.size() == 0)
{
error << "No texture defined" << std::endl;
return false;
}
if (texname.size() > 0)
{
if (!content.load(path, texname[0], texinfo, tex)) return false;
drawable.SetDiffuseMap(tex);
}
if (texname.size() > 1)
{
if (!content.load(path, texname[1], texinfo, tex)) return false;
drawable.SetMiscMap1(tex);
}
if (texname.size() > 2)
{
if (!content.load(path, texname[2], texinfo, tex)) return false;
drawable.SetMiscMap2(tex);
}
// set mesh
std::tr1::shared_ptr<MODEL> mesh;
if (!content.load(path, meshname, mesh)) return false;
std::string scalestr;
if (cfg.get("scale", scalestr) &&
!content.get(path, meshname + scalestr, mesh))
{
MATHVECTOR<float, 3> scale;
std::stringstream s(scalestr);
s >> scale;
VERTEXARRAY meshva = mesh->GetVertexArray();
meshva.Scale(scale[0], scale[1], scale[2]);
content.load(path, meshname + scalestr, meshva, mesh);
}
drawable.SetModel(*mesh);
modellist.push_back(mesh);
// set color
MATHVECTOR<float, 4> col(1);
if (cfg.get("color", col))
{
drawable.SetColor(col[0], col[1], col[2], col[3]);
}
// set node
SCENENODE * node = &topnode;
if (nodeptr != 0)
{
if (!nodeptr->valid())
{
*nodeptr = topnode.AddNode();
assert(nodeptr->valid());
}
node = &topnode.GetNode(*nodeptr);
}
MATHVECTOR<float, 3> pos, rot;
if (cfg.get("position", pos) | cfg.get("rotation", rot))
{
if (node == &topnode)
{
// position relative to parent, create child node
keyed_container <SCENENODE>::handle nodehandle = topnode.AddNode();
node = &topnode.GetNode(nodehandle);
}
node->GetTransform().SetTranslation(pos);
node->GetTransform().SetRotation(QUATERNION<float>(rot[0]/180*M_PI, rot[1]/180*M_PI, rot[2]/180*M_PI));
}
// set drawable
keyed_container<DRAWABLE>::handle drawtemp;
keyed_container<DRAWABLE>::handle * draw = &drawtemp;
if (drawptr != 0) draw = drawptr;
std::string drawtype;
if (cfg.get("draw", drawtype))
{
if (drawtype == "emissive")
{
drawable.SetDecal(true);
*draw = node->GetDrawlist().lights_emissive.insert(drawable);
}
else if (drawtype == "transparent")
{
*draw = node->GetDrawlist().normal_blend.insert(drawable);
}
}
else
{
*draw = node->GetDrawlist().car_noblend.insert(drawable);
}
return true;
}