-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene.cpp
147 lines (107 loc) · 3.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
#include "scene.hpp"
#include "./hierarchy.hpp"
#include "./game-object.hpp"
#include "../apis/base/iengine.hpp"
Scene::Scene() {
loader = IEngine::Loader;
input = IEngine::Input;
}
Scene::~Scene() = default;
// ===
// Getters
// ===
Camera* Scene::getCamera() const {
return camera;
}
void Scene::setCamera(Camera* camera) {
this->camera = camera;
}
const vector<Mesh*>& Scene::getMeshes() {
return meshes;
}
const map<const Material*, vector<Mesh*>> Scene::getMeshesByMaterial() const {
return meshesByMaterial;
}
const vector<Terrain*>& Scene::getTerrains() {
return terrains;
}
const vector<CubicPatch*> &Scene::getCubicPatches() {
return cubicPatches;
}
const vector<PointLight*>& Scene::getPointLights() {
return pointLights;
}
const vector<DirectLight*>& Scene::getDirectLights() {
return directLights;
}
const vector<SpotLight*> &Scene::getSpotLights() {
return spotLights;
}
// ===
// Events
// ===
void Scene::setup() {
processHierarchy();
Hierarchy::updateTransformTree();
}
void Scene::beforeRender() {
Hierarchy::updateTransformTree();
}
void Scene::afterRender() { }
void Scene::finish() { }
void Scene::createCamera() {
IWindow* window = IEngine::Window;
auto* cameraObject = Hierarchy::createGameObjectInTree();
Transform* cameraTransform = cameraObject->transform;
camera = new Camera(radians(45.0f), 0.1f, 1000.0f);
camera->cubeMap = loader->loadCubeMap("textures/skybox");
cameraObject->components.add(camera);
cameraTransform->translate(vec3(-10, 25, -30));
cameraTransform->rotate(quat(vec3(0, radians(-15.0), 0)));
camera->setScreenSizes(window->screenWidth, window->screenHeight);
Hierarchy::addToHierarchy(cameraObject);
}
// ===
void Scene::processHierarchy() {
loadComponents(&meshes);
loadComponents(&terrains);
loadComponents(&cubicPatches);
loadComponents(&directLights);
loadComponents(&pointLights);
loadComponents(&spotLights);
meshesByMaterial = processMeshesByMaterial();
}
map<const Material*, vector<Mesh*>> Scene::processMeshesByMaterial() const {
map<const Material*, vector<Mesh*>> result;
for(auto* mesh: meshes) {
result[mesh->material].push_back(mesh);
}
return result;
}
template<typename T>
void Scene::loadComponents(vector<T*>* array) {
array->clear();
map<int, GameObject*>* gameObjects = Hierarchy::getGameObjects();
for(auto & [id, gameObject] : *gameObjects) {
vector<T*> components = gameObject->components.getAll<T>();
array->insert(array->end(), components.begin(), components.end());
}
}
// ===
// Basic shapes
// ===
GameObject* Scene::createCube() {
return IEngine::Loader->loadModel("models/shapes/cube.obj");
}
GameObject* Scene::createSphere() {
return IEngine::Loader->loadModel("models/shapes/sphere.obj");
}
GameObject* Scene::createPlane() {
return IEngine::Loader->loadModel("models/shapes/plane.obj");
}
GameObject* Scene::createCylinder() {
return IEngine::Loader->loadModel("models/shapes/cylinder.obj");
}
GameObject* Scene::createCone() {
return IEngine::Loader->loadModel("models/shapes/cone.obj");
}