CSEngine
Loading...
Searching...
No Matches
FirstDemoScene.cpp
1#include "../Util/Render/SEnvironmentMgr.h"
2#include "FirstDemoScene.h"
3#include "../Component/LightComponent.h"
4#include "../Component/DrawableSkinnedMeshComponent.h"
5#include "../Component/CameraComponent.h"
6#include "../../Assets/teapot_smooth.h"
7#include "../Component/RenderComponent.h"
8#include "../Component/CustomComponent.h"
9#include "../Manager/GameObjectMgr.h"
10
11#include "../Util/Loader/DAE/DAELoader.h"
12#include "../Util/AssetsDef.h"
13#include "../Manager/SCloneFactory.h"
14#include "../Util/Loader/SCENE/SSceneLoader.h"
15#include "../Util/Render/SFrameBuffer.h"
16#include "../Util/Render/SMaterial.h"
17
18using namespace CSE;
19
20FirstDemoScene::FirstDemoScene() = default;
21
22
23FirstDemoScene::~FirstDemoScene() = default;
24
25
26void FirstDemoScene::Init() {
27 //===============
28
29 //DAE test
30// SPrefab* jack = MeshLoader::LoadModel("jack.model");
31 SPrefab* jack = SResource::Create<SPrefab>("jack.prefab");
32 SPrefab* plane = SResource::Create<SPrefab>("plane_circle.prefab");
33 SPrefab* roomPrefab = SResource::Create<SPrefab>("room.prefab");
34
35// daeLoader->GeneratePrefab();
36
37// SGameObject* aa = new SGameObject();
38//
39//
40//
41// aa->CreateComponent<DrawableStaticMeshComponent>();
42// aa->GetComponent<DrawableStaticMeshComponent>()->SetMesh(*(daeLoader->GetMesh()));
43// aa->CreateComponent<MaterialComponent>();
44// aa->GetComponent<MaterialComponent>()->SetMaterialAmbient(vec3{ 1, 1, 0 });
45// aa->GetComponent<MaterialComponent>()->SetShininess(40);
46//
47// aa->CreateComponent<RenderComponent>();
48// aa->GetComponent<RenderComponent>()->SetShaderHandle(0);
49// float scale = 0.1f;
50// aa->GetTransform()->m_scale = vec3{scale, scale, scale};
51 // aa->GetTransform()->m_rotation.x = 90.f;
52// aa->GetTransform()->m_position.y = -0.3f;
53// aa->CreateComponent<CustomComponent>();
54// aa->GetComponent<CustomComponent>()->SetClassName("TestScript");
55// aa->SetName("dae mesh");
56// aa->GetComponent<RenderComponent>()->SetIsEnable(false);
57
58 // cube = daeLoader->GetMesh();
59 // cube->SetUndestroyable(false);
60
61
62 SGameObject* root = new SGameObject("root");
63 root->GetTransform()->m_position = vec3{0, -1, 0};
64
65 auto room = roomPrefab->Clone(vec3{0, 0.2f, 0}, root);
66 room->GetTransform()->m_scale = vec3{ 4.f, 4.f, 4.f };
67 room->GetTransform()->m_rotation.Rotate(Quaternion::AngleAxis(vec3{0, 1, 0}, 3.14f / 2));
68
69 const auto& room_children = room->GetChildren();
70 for (const auto& gameObject : room_children) {
71 const auto& component = gameObject->GetComponent<RenderComponent>();
72 if(component == nullptr) continue;
73 component->SetMaterial(SResource::Create<SMaterial>("File:Material/Room.mat"));
74 }
75
76 //Managing Memory Test
77 SGameObject* b = new SGameObject();
78 b->SetParent(root);
79 d = new SGameObject();
80 d->SetParent(root);
81// STexture* empty = new STexture();
82// empty->LoadEmpty();
83
84 cube = new MeshSurface(CH02::teapot_smoothNumVerts, CH02::teapot_smoothVerts, CH02::teapot_smoothNormals);
85 cube->SetUndestroyable(false);
86 SFrameBuffer* buffer = new SFrameBuffer();
87 buffer->GenerateFramebuffer(SFrameBuffer::PLANE, 512, 512);
88 const auto& buf_tex = buffer->GenerateTexturebuffer(SFrameBuffer::RENDER, GL_RGB);
89 buf_tex->SetName("framebuffer_test");
90 buf_tex->SetAbsoluteID("framebuffer_test");
91 buffer->RasterizeFramebuffer();
92 b->Destroy();
93
94 SGameObject* testing = new SGameObject("root of mesh");
95 root->AddChild(testing);
96
97 auto* camera_gm = new SGameObject("ct");
98 camera_gm->SetParent(testing);
99 camera_gm->GetTransform()->m_position = vec3{ 0, -0.3f, 0 };
100 {
101 const auto& sc = camera_gm->CreateComponent<CustomComponent>();
102 sc->SetClassName("testScript.script");
103 }
104
105 auto* light_gm = new SGameObject("lt");
106 light_gm->SetParent(testing);
107 light_gm->GetTransform()->m_scale = vec3{ 0.2f, 0.2f, 0.2f };
108 {
109 const auto& sc = light_gm->CreateComponent<CustomComponent>();
110 sc->SetClassName("testScriptFast.script");
111 }
112
113 SGameObject* a = new SGameObject("camera");
114 a->SetParent(camera_gm);
115
116 SGameObject* ab = jack->Clone(vec3{ 0, -0.15f, 0 }, testing);
117 SGameObject* ab2 = jack->Clone(vec3{ 0.1f, -0.45f, 0.12f }, testing);
118 ab->GetTransform()->m_scale = vec3{ 0.0002f, 0.0002f, 0.0002f };
119 ab2->GetTransform()->m_scale = vec3{ 0.0002f, 0.0002f, 0.0002f };
120 ab2->GetTransform()->m_rotation.Rotate(Quaternion::AngleAxis(vec3{1, 0, 0}, 3.14f / 2));
121 ab2->GetTransform()->m_rotation.Rotate(Quaternion::AngleAxis(vec3{0, 1, 0}, 1.f));
122// ab->CreateComponent<CustomComponent>();
123// ab->GetComponent<CustomComponent>()->SetClassName("testScript.script");
124// auto ab_m = ab->GetComponent<RenderComponent>()->GetMaterial();
125 {
126 const auto& ab_children = ab->GetChildren();
127 for (const auto& gameObject : ab_children) {
128 const auto& component = gameObject->GetComponent<RenderComponent>();
129 if (component == nullptr) continue;
130 if(gameObject->GetName() == "Mesh-mesh") {
131 const auto& material = SResource::Create<SMaterial>("File:Material/Jack.mat");
132 material->SetOrderLayer(5500);
133 component->SetMaterial(material);
134 }
135 else {
136 const auto& material = SResource::Create<SMaterial>("File:Material/Label.mat");
137 component->SetMaterial(material);
138 }
139 }
140 }
141 {
142 const auto& ab_children = ab2->GetChildren();
143 for (const auto& gameObject : ab_children) {
144 const auto& component = gameObject->GetComponent<RenderComponent>();
145 if (component == nullptr) continue;
146 if(gameObject->GetName() == "Mesh-mesh") {
147 const auto& material = SResource::Create<SMaterial>("File:Material/Jack.mat");
148 material->SetOrderLayer(5500);
149 component->SetMaterial(material);
150 }
151 else {
152 const auto& material = SResource::Create<SMaterial>("File:Material/Label.mat");
153 component->SetMaterial(material);
154 }
155 }
156 }
157
158 c2 = new SGameObject();
159 c2->SetParent(testing);
160 // c2->AddComponent(c->GetComponent<DrawableStaticMeshComponent>());
161 c2->CreateComponent<DrawableStaticMeshComponent>();
162 c2->GetComponent<DrawableStaticMeshComponent>()->SetMesh(*cube);
163// c2->GetComponent<MaterialComponent>()->SetMaterialAmbient(vec3{ 1, 0, 0 });
164// c2->GetComponent<MaterialComponent>()->SetAlbedoTexture(empty);
165 c2->GetTransform()->m_position = vec3{ -0.2f, -0.3f, -0.2f };
166 c2->GetTransform()->m_scale = vec3{ 0.5f, 0.5f, 0.5f };
167 {
168 const auto& render = c2->CreateComponent<RenderComponent>();
169 const auto& material = SResource::Create<SMaterial>("File:Material/SSS_PBR.mat");
170 material->SetOrderLayer(6000);
171 render->SetMaterial(material);
172 }
173// c2->GetComponent<RenderComponent>()->SetShaderHandle("PBR.shader");
174
175 c3 = new SGameObject("testscript2 object");
176 c3->SetParent(root);
177 c3->GetTransform()->m_position.y = -0.7f;
178
179 c3->CreateComponent<CustomComponent>();
180 c3->GetComponent<CustomComponent>()->SetClassName("testScript2.script");
181// {
182// std::vector<std::string> variable = {
183// "materialComp", c3->GetID(c3->GetComponent<MaterialComponent>()), "comp"
184// };
185// c3->GetComponent<CustomComponent>()->SetValue("__variable__", variable);
186// }
187// {
188// std::vector<std::string> variable = {
189// "testScript", c->GetID(c->GetComponent<CustomComponent>()), "nut"
190// };
191// c3->GetComponent<CustomComponent>()->SetValue("__variable__", variable);
192// }
193
194
195// c3->GetComponent<RenderComponent>()->SetShaderHandle("PBR.shader");
196
197 auto c4 = SCloneFactory::Clone(c3, c3);
198// c4->GetComponent<MaterialComponent>()->SetMaterialAmbient(vec3{ 1, 0, 1 });
199// c4->GetComponent<MaterialComponent>()->SetAlbedoTexture(empty);
200 c4->GetTransform()->m_position.y = -0.1f;
201 c4->GetTransform()->m_position.x = -2.f;
202 c4->GetTransform()->m_scale.Set(0.5, 0.5, 0.5);
203 c4->DeleteComponent(c4->GetComponent<CustomComponent>());
204
205
206 SGameObject* direction = new SGameObject();
207 direction->SetParent(root);
208 direction->SetName("directional");
209 direction->GetTransform()->m_position = vec3{ 0.f, 1.f, 0.f };
210 direction->CreateComponent<LightComponent>();
211// direction->GetComponent<LightComponent>()->SetColorAmbient(vec4{ 0.07f, 0.07f, 0.07f, 1 });
212// direction->GetComponent<LightComponent>()->SetColorDiffuse(vec4{ 0.3f, 0.5f, 0.5f, 1 });
213 direction->GetComponent<LightComponent>()->SetSunrising(true);
214 direction->GetComponent<LightComponent>()->SetLightType(LightComponent::DIRECTIONAL);
215 direction->GetComponent<LightComponent>()->SetDirection(vec4{ 0.0f, 1.0f, 1, 0 });
216 direction->GetComponent<LightComponent>()->SetShadow(true);
217
218 direction->CreateComponent<CustomComponent>();
219 direction->GetComponent<CustomComponent>()->SetClassName("directionalLight.script");
220//
221//
222// SComponent* test_comp = direction->GetComponent<CustomComponent>();
223//
224// auto auto_comp = test_comp->GetComponent();
225
226 d->SetName("light");
227 d->CreateComponent<LightComponent>();
228 d->GetTransform()->m_position = vec3{ -2.8f, -1.5f, 0 };
229 light_gm->AddChild(d);
230 d->GetComponent<LightComponent>()->SetDirection(vec4{ 1.f, 0.5f, 1.f, 1.0f });
231 d->GetComponent<LightComponent>()->SetColor(vec3{ 5, 5, 5 });
232 d->GetComponent<LightComponent>()->SetLightType(LightComponent::POINT);
233 d->GetComponent<LightComponent>()->SetLightRadius(1);
234 d->GetComponent<LightComponent>()->SetShadow(false);
235// d->CreateComponent<DrawableStaticMeshComponent>();
236// d->GetComponent<DrawableStaticMeshComponent>()->SetMesh(*cube);
237// d->CreateComponent<MaterialComponent>();
238// d->GetComponent<MaterialComponent>()->AttachMaterials("File:Material/DefaultPBR.mat");
239// d->GetComponent<MaterialComponent>()->SetDiffuseMaterial(vec4{ 1, 1, 1, 1 });
240// d->GetComponent<MaterialComponent>()->SetAlbedo(vec3{ 1, 1, 1 });
241// d->GetComponent<MaterialComponent>()->SetAlbedoTexture(empty);
242// auto d_r = d->CreateComponent<RenderComponent>();
243// d_r->GetMaterial()->SetVec3("FLOAT_ALBEDO", vec3{ 0, 1, 0 });
244// d->GetComponent<RenderComponent>()->SetShaderHandle("PBR.shader");
245 //d->GetComponent<LightComponent>()->DisableDiffuse = true;
246
247 a->CreateComponent<CameraComponent>();
248// a->GetComponent<CameraComponent>()->SetOrtho(-3.f, 3.f, -3.f, 3.f);
249// a->GetComponent<CameraComponent>()->SetCameraType(CSE::CameraComponent::ORTHO);
250 a->GetTransform()->m_position = vec3{ 0, 0.2f, 0.8f };
251// a->GetTransform()->m_position = vec3{ 0.f, 2.f, 4.f };
252// a->GetTransform()->m_scale = vec3{ 2.f, 2.f, 2.f };
253// a->GetComponent<CameraComponent>()->SetTarget(vec3{ 0.0f, -1.0f, -1.f });
254 auto a_cam = a->GetComponent<CameraComponent>();
255 a_cam->SetTarget(camera_gm);
256 a_cam->SetBackgroundSkybox();
257 a_cam->SetBackgroundType(CameraBase::SKYBOX);
258// a->CreateComponent<CustomComponent>();
259// a->GetComponent<CustomComponent>()->SetClassName("testcamerar.script");
260
261 c4->CreateComponent<CameraComponent>();
262 c4->GetComponent<CameraComponent>()->SetTarget(d);
263 c4->GetComponent<CameraComponent>()->SetBackgroundSkybox();
264 c4->GetComponent<CameraComponent>()->SetBackgroundType(CameraBase::SKYBOX);
265 c4->GetComponent<CameraComponent>()->SetFrameBuffer(
266 SResource::Create<SFrameBuffer>("File:Texture/TestFrameBuffer.framebuffer"));
267 //===============
268
269 SSceneLoader::SavePrefab(root, CSE::NativeAssetsPath() + "Scene/test_scene.scene");
270}
271
272
273void FirstDemoScene::Tick(float elapsedTime) {
274
275 if (startTIme == 0) {
276 startTIme = elapsedTime;
277 }
278
279 //===============
280// SGameObject* d = GameObjectMgr::getInstance()->Find("dae mesh");
281// d->GetTransform()->m_position.x = sinf(elapsedTime*0.001) * 1.f;
282// d->GetTransform()->m_position.z = cosf(elapsedTime*0.001) * 1.f;
283 //c->GetTransform()->m_rotation.y += 0.1f;
284// c->GetTransform()->m_position.y = sinf(elapsedTime * 0.001) * 0.1f;
285 //===============
286
287 if (elapsedTime - startTIme > 3000) {
288 startTIme = elapsedTime;
289 isInvisible = !isInvisible;
290// switchingObject();
291 }
292}
293
294
295void FirstDemoScene::Destroy() {
296}
297
298
299void FirstDemoScene::switchingObject() {
300
301 c3->SetIsEnable(!isInvisible);
302
303 if (isInvisible) {
304 c2->Destroy();
305// d->GetComponent<LightComponent>()->SetLightType(LightComponent::POINT);
306
307 } else {
308 c2 = new SGameObject();
309 c2->CreateComponent<DrawableStaticMeshComponent>();
310 c2->GetComponent<DrawableStaticMeshComponent>()->SetMesh(*cube);
311// c2->CreateComponent<MaterialComponent>();
312// c2->GetComponent<MaterialComponent>()->SetMaterialAmbient(vec3{ 1, 0, 0 });
313// c2->GetComponent<MaterialComponent>()->SetMaterialSpecular(vec3{ 0, 0, 0 });
314 c2->GetTransform()->m_position.y = 0.5f;
315
316
317 c2->CreateComponent<RenderComponent>();
318// c2->GetComponent<RenderComponent>()->SetShaderHandle("PBR.shader");
319
320// d->GetComponent<LightComponent>()->SetLightType(LightComponent::DIRECTIONAL);
321 }
322
323}
STexture * GenerateTexturebuffer(BufferType type, int channel, int level=0)
void GenerateFramebuffer(BufferDimension dimension, int width, int height)
void Destroy() override
자동 삭제가 아닌 특정한 상황에서 삭제될 때 호출되는 함수