-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGOFactory.cpp
More file actions
220 lines (149 loc) · 6.38 KB
/
GOFactory.cpp
File metadata and controls
220 lines (149 loc) · 6.38 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "GOFactory.h"
#include "LogManager.h"
#include "ComponentFactory.h"
#include "CameraComponent.h"
#include "ModelRenderComponent.h"
#include "DirectionalLightComponent.h"
#include "PointLightComponent.h"
#include "TeapotSpin.h"
#include "RandomMover.h"
#include "PlayerController.h"
#include "LaserController.h"
#include "CameraController.h"
#include "PerspectiveCameraComponent.h"
#include "OrthographicCameraComponent.h"
#include "GUITextureRenderComponent.h"
#include "SkyboxCameraComponent.h"
#include "ShaderManager.h"
#include "MainMenuController.h"
GOFactory GOFactory::s_xInstance;
GOFactory::GOFactory() {
}
GOFactory* GOFactory::instance() {
return &s_xInstance;
}
GameObject* GOFactory::createEmpty() {
GameObject* empty = new GameObject();
m_xProducts.push_back(empty);
return empty;
}
GameObject* GOFactory::createTeapot() {
GameObject* teapot = createEmpty();
teapot->addComponent<ModelRenderComponent>()->init("../../test/mesh_test/boss1/tris.md2", ShaderManager::instance()->getProgram(SHADER_LIGHTING_UNLIT));
//teapot->addComponent<TeapotSpin>();
//teapot->addComponent(ComponentFactory::instance()->create<RandomMover>());
return teapot;
}
GameObject* GOFactory::createLaser() {
GameObject* laser = createEmpty();
laser->addComponent<ModelRenderComponent>()->init("laser/Laser.obj", ShaderManager::instance()->getProgram(SHADER_LIGHTING_UNLIT));
laser->addComponent<LaserController>();
laser->addComponent<PointLightComponent>()->init(3.0f, glm::vec3(0.5f, 0.75f, 1.0f), 1.0f);
return laser;
}
GameObject* GOFactory::createPlayer() {
GameObject* player = createEmpty();
GameObject* ship = createEmpty();
player->addChild(ship);
ship->addComponent<ModelRenderComponent>()->init("data/models/spaceship/Spaceship.obj");
player->addComponent<PlayerController>()->init(ship);
return player;
}
GameObject* GOFactory::createSkybox() {
GameObject* skybox = createEmpty();
ModelRenderComponent* mrc = skybox->addComponent<ModelRenderComponent>();
mrc->init("skybox/skybox.obj", ShaderManager::instance()->getProgram(SHADER_LIGHTING_UNLIT));
for(unsigned int i = 0; i < mrc->getModel()->numMeshes(); ++i) {
Mesh* meshes = mrc->getModel()->getMeshes();
GLuint texId = meshes[i].getMaterial()->getTexture(meshes[i].getMaterial()->getIdsOfType(MATERIAL_DIFFUSE)[0])->getTexID();
glBindTexture(GL_TEXTURE_2D, texId);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
skybox->setPosition(glm::vec3(0, -500, 0));
skybox->setScale(glm::vec3(1000));
return skybox;
}
GameObject* GOFactory::createPlayerCamera(GameObject* player, float fov, float near, float far, float ratio) {
GameObject* camera = createEmpty();
camera->addComponent<PerspectiveCameraComponent>()->init(fov, near, far, ratio);
camera->addComponent<CameraController>()->init(player);
camera->setPosition(player->getPosition() + glm::vec3(0.0f, 0.0f, 5.0f));
return camera;
}
GameObject* GOFactory::createGUICamera(float width, float height) {
GameObject* camera = createEmpty();
camera->addComponent<OrthographicCameraComponent>()->init(0.0f, width, height, 0.0f, -1.0f, 1.0f);
return camera;
}
/*GameObject* GOFactory::createSkyCamera( GameObject* refObj, float fov, float near, float far, float ratio ) {
GameObject* camera = createEmpty();
camera->addComponent<PerspectiveCameraComponent>()->init(fov, near, far, ratio);
camera->addComponent<SkyboxCameraComponent>()->init(refObj);
return camera;
}*/
GameObject* GOFactory::createGroundPlane() {
GameObject* plane = createEmpty();
plane->addComponent<ModelRenderComponent>()->init("../../test/mesh_test/terrain_test.obj", ShaderManager::instance()->getProgram(SHADER_LIGHTING_DIFFUSE));
return plane;
}
GameObject* GOFactory::createSun(glm::vec3 direction, glm::vec3 color, float strength) {
GameObject* sun = createEmpty();
DirectionalLightComponent* lightComponent = sun->addComponent<DirectionalLightComponent>();
lightComponent->setDirection(direction);
lightComponent->setColor(color);
lightComponent->setStrength(strength);
return sun;
}
GameObject* GOFactory::createPointLight(glm::vec3 color, float radius, float strength) {
GameObject* light = createEmpty();
light->addComponent<PointLightComponent>()->init(radius, color, strength);
return light;
}
GameObject* GOFactory::createGUITest() {
GameObject* guiTest = createEmpty();
guiTest->addComponent<GUITextureRenderComponent>()->init("test/GUItest.png");
return guiTest;
}
GameObject* GOFactory::createMainMenu() {
GameObject* mainMenu = createEmpty();
GameObject* menuItemStart = createEmpty();
GameObject* menuItemQuit = createEmpty();
ModelRenderComponent* mrc_start = menuItemStart->addComponent<ModelRenderComponent>();
mrc_start->init("text/start/start.obj");
Material* matSelected = mrc_start->getModel()->getMeshes()[0].getMaterial();
ModelRenderComponent* mrc_quit = menuItemQuit->addComponent<ModelRenderComponent>();
mrc_quit->init("text/quit/quit.obj");
Material* matDeselected = mrc_quit->getModel()->getMeshes()[0].getMaterial();
matSelected->setShaderProgram(ShaderManager::instance()->getProgram(SHADER_LIGHTING_DIFFUSE));
matDeselected->setShaderProgram(ShaderManager::instance()->getProgram(SHADER_LIGHTING_DIFFUSE));
menuItemStart->setPosition(glm::vec3(0.0f, 1.0f, 0.0f));
menuItemQuit->setPosition(glm::vec3(0.0f, -1.0f, 0.0f));
menuItemStart->setRotation(glm::quat(glm::vec3(0.0f, 0.4f, 0.0f)));
menuItemQuit->setRotation(glm::quat(glm::vec3(0.0f, 0.4f, 0.0f)));
GameObject* light = createPointLight(glm::vec3(1.0f, 1.0f, 1.0f), 3.0f, 10.0f);
light->setPosition(glm::vec3(0.0f, 0.0f, 1.0f));
mainMenu->addChild(menuItemStart);
mainMenu->addChild(menuItemQuit);
mainMenu->addChild(light);
//mainMenu->addComponent<MainMenuController>()->init(mrc_start,mrc_quit,matSelected,matDeselected);
mainMenu->addComponent<MainMenuController>()->init();
return mainMenu;
}
void GOFactory::destroy(GameObject* gameObject) {
for (unsigned int i = 0; i < m_xProducts.size(); ++i) {
if (m_xProducts[i] == gameObject) {
if (gameObject->getParent() != nullptr)
gameObject->getParent()->removeChild(gameObject);
delete gameObject;
m_xProducts.erase(m_xProducts.begin() + i);
return;
}
}
Log::Warn("GOFactory: Trying to destroy unmanaged GameObject");
}
void GOFactory::cleanUp() {
for (unsigned int i = 0; i < m_xProducts.size(); ++i) {
delete m_xProducts[i];
}
}