-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scene.cpp
206 lines (176 loc) · 5.14 KB
/
Scene.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "Scene.hpp"
#include <QTime>
#include "lib/CS123ISceneParser.h"
Scene::ScenePrimitive::ScenePrimitive() : shape(PRIMITIVE_NONE), material()
{
}
Scene::ScenePrimitive::~ScenePrimitive()
{
if (material.textureMap)
{
delete material.textureMap;
}
if (material.bumpMap)
{
delete material.bumpMap;
}
}
Scene::Scene() :
m_global(),
m_lights(),
m_primitives()
{
}
Scene::~Scene()
{
}
void Scene::parse(Scene *sceneToFill, CS123ISceneParser *parser)
{
// TODO: load scene into sceneToFill using setGlobal(), addLight(), addPrimitive(), and finishParsing()
if (!sceneToFill || !parser)
{
return;
}
QTime t;
t.start();
std::cerr << "Parsing start\n";
CS123SceneGlobalData global;
if (parser->getGlobalData(global))
{
sceneToFill->setGlobal(global);
}
int light_count = parser->getNumLights();
for (int i=0; i<light_count; i++)
{
CS123SceneLightData light;
if (parser->getLightData(i, light))
{
sceneToFill->addLight(light);
}
}
const CS123SceneNode * root = parser->getRootNode();
if (root)
{
parseNode(*root, *sceneToFill);
}
//normalized the light vectors (just in case)
for (QVector<LightData>::iterator light=sceneToFill->m_lights.begin(); light!=sceneToFill->m_lights.end(); light++)
{
if (light->location.w!=0.0)
{
light->location.normalize();
}
}
std::cerr << "Parsing end: " << t.elapsed() << "\n";
}
void Scene::addPrimitive(const CS123ScenePrimitive &scenePrimitive, const Matrix4x4 &matrix)
{
//append node
m_primitives.append(ScenePrimitive());
ScenePrimitive & primitive = m_primitives.last();
//get shape
primitive.shape = scenePrimitive.type;
if (!primitive.shape)
{ //failed to create primitive shape
m_primitives.removeLast();
return;
}
//copy materials
//primitive.material = scenePrimitive.material;
memcpy(&(primitive.material), &(scenePrimitive.material), sizeof(CS123SceneMaterial));
primitive.material.textureMap = NULL;
primitive.material.bumpMap = NULL;
if (scenePrimitive.material.textureMap)
{
primitive.material.textureMap = new CS123SceneFileMap();
(*primitive.material.textureMap) = (*scenePrimitive.material.textureMap);
}
primitive.T = matrix;
}
void Scene::addLight(const CS123SceneLightData &sceneLight)
{
LightData data;
if (sceneLight.type==LIGHT_POINT)
{
data.location.x = sceneLight.pos.x;
data.location.y = sceneLight.pos.y;
data.location.z = sceneLight.pos.z;
data.location.w = sceneLight.pos.w;
data.spot = Vector4f(0.0f, 0.0f, 0.0f, 0.0f);
}
else if (sceneLight.type==LIGHT_DIRECTIONAL)
{
data.location.x = sceneLight.dir.x;
data.location.y = sceneLight.dir.y;
data.location.z = sceneLight.dir.z;
data.location.w = sceneLight.dir.w;
data.location.normalize(); //direction vector
data.spot = Vector4f(0.0f, 0.0f, 0.0f, 0.0f);
}
else if (sceneLight.type==LIGHT_SPOT)
{
data.location.x = sceneLight.pos.x;
data.location.y = sceneLight.pos.y;
data.location.z = sceneLight.pos.z;
data.location.w = sceneLight.pos.w;
data.spot.x = sceneLight.dir.x;
data.spot.y = sceneLight.dir.y;
data.spot.z = sceneLight.dir.z;
data.spot.w = 0.0f;
data.spot.normalize(); //direction vector
data.spot.w = sceneLight.penumbra;
}
else
{ //unsupported type
return;
}
data.color.x = sceneLight.color.r;
data.color.y = sceneLight.color.g;
data.color.z = sceneLight.color.b;
data.color.w = sceneLight.color.a;
data.attenuation.x = sceneLight.function.x;
data.attenuation.y = sceneLight.function.y;
data.attenuation.z = sceneLight.function.z;
data.attenuation.w = 0.f;
m_lights.push_back(data);
}
void Scene::setGlobal(const CS123SceneGlobalData &global)
{
m_global.x = global.ka;
m_global.y = global.kd;
m_global.z = global.ks;
m_global.w = global.kt;
}
void Scene::parseNode(CS123SceneNode const& node, Scene & scene, Matrix4x4 T)
{
//collect the transformations
for (std::vector<CS123SceneTransformation*>::const_iterator iter=node.transformations.begin();
iter!=node.transformations.end(); iter++)
{
const CS123SceneTransformation * st = *iter;
if (st)
{
T *= st->MatrixTransform();
}
}
//parse primitives
for (std::vector<CS123ScenePrimitive*>::const_iterator iter=node.primitives.begin();
iter!=node.primitives.end(); iter++)
{
const CS123ScenePrimitive * primitive = *iter;
if (primitive)
{
scene.addPrimitive(*primitive, T);
}
}
//parse children
for (std::vector<CS123SceneNode*>::const_iterator iter=node.children.begin();
iter!=node.children.end(); iter++)
{
const CS123SceneNode * childNode = *iter;
if (childNode)
{
parseNode(*childNode, scene, T);
}
}
}