From 46cc366204b82d5f38eb5ccaed6374fc08163682 Mon Sep 17 00:00:00 2001 From: ducphamhong Date: Mon, 26 Apr 2021 22:29:09 +0700 Subject: [PATCH] #123 Add viewpoint renderer, fix crash on scene controller --- .../Source/Editor/Space/Scene/CSpaceScene.cpp | 31 ++++++- .../Source/Editor/Space/Scene/CSpaceScene.h | 2 + .../SpaceController/CSceneController.cpp | 3 +- .../EditorComponents/Viewpoint/CViewpoint.cpp | 58 ++++++++++++ .../EditorComponents/Viewpoint/CViewpoint.h | 45 +++++++++ .../Viewpoint/CViewpointData.cpp | 42 +++++++++ .../Viewpoint/CViewpointData.h | 41 +++++++++ .../Viewpoint/CViewpointRenderer.cpp | 91 +++++++++++++++++++ .../Viewpoint/CViewpointRenderer.h | 60 ++++++++++++ 9 files changed, 370 insertions(+), 3 deletions(-) create mode 100644 Projects/Editor/Source/EditorComponents/Viewpoint/CViewpoint.cpp create mode 100644 Projects/Editor/Source/EditorComponents/Viewpoint/CViewpoint.h create mode 100644 Projects/Editor/Source/EditorComponents/Viewpoint/CViewpointData.cpp create mode 100644 Projects/Editor/Source/EditorComponents/Viewpoint/CViewpointData.h create mode 100644 Projects/Editor/Source/EditorComponents/Viewpoint/CViewpointRenderer.cpp create mode 100644 Projects/Editor/Source/EditorComponents/Viewpoint/CViewpointRenderer.h diff --git a/Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.cpp b/Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.cpp index 684751f32..51428ebb4 100644 --- a/Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.cpp +++ b/Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.cpp @@ -25,6 +25,7 @@ This file is part of the "Skylicht Engine". #include "pch.h" #include "CSpaceScene.h" #include "GridPlane/CGridPlane.h" +#include "EditorComponents/Viewpoint/CViewpoint.h" #include "Editor/SpaceController/CSceneController.h" @@ -128,8 +129,6 @@ namespace Skylicht camObj->addComponent(); m_viewpointCamera = camObj->getComponent(); - m_viewpointCamera->setPosition(core::vector3df(1.5f, 1.5f, 1.5f)); - m_viewpointCamera->lookAt(core::vector3df(0.0f, 0.0f, 0.0f), core::vector3df(0.0f, 1.0f, 0.0f)); m_viewpointCamera->setAspect(1.0f); // grid @@ -137,6 +136,11 @@ namespace Skylicht gridPlane->setName(L"Grid3D"); gridPlane->addComponent(); + // viewpoint + CGameObject* viewpoint = m_viewpointZone->createEmptyObject(); + viewpoint->setName(L"Viewpoint"); + viewpoint->addComponent(); + // set scene to controller CSceneController::getInstance()->setScene(m_scene); } @@ -166,9 +170,32 @@ namespace Skylicht void CSpaceScene::update() { + updateViewpoint(); + m_scene->update(); } + void CSpaceScene::updateViewpoint() + { + const core::matrix4& view = m_editorCamera->getViewMatrix(); + + core::matrix4 cameraWorld; + view.getInverse(cameraWorld); + f32* matData = cameraWorld.pointer(); + + core::vector3df dir(matData[8], matData[9], matData[10]); + dir.normalize(); + + core::vector3df up(matData[4], matData[5], matData[6]); + up.normalize(); + + float distance = 2.0f; + core::vector3df pos = -dir * distance; + + m_viewpointCamera->setPosition(pos); + m_viewpointCamera->lookAt(core::vector3df(0.0f, 0.0f, 0.0f), up); + } + void CSpaceScene::onRender(GUI::CBase* base) { if (m_renderRP != NULL) diff --git a/Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.h b/Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.h index af040afcc..224a8ef8a 100644 --- a/Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.h +++ b/Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.h @@ -77,6 +77,8 @@ namespace Skylicht protected: + void updateViewpoint(); + void initRenderPipeline(float w, float h); void postMouseEventToScene(EMOUSE_INPUT_EVENT eventType, float x, float y); diff --git a/Projects/Editor/Source/Editor/SpaceController/CSceneController.cpp b/Projects/Editor/Source/Editor/SpaceController/CSceneController.cpp index f90d4c85b..3b48ecfd7 100644 --- a/Projects/Editor/Source/Editor/SpaceController/CSceneController.cpp +++ b/Projects/Editor/Source/Editor/SpaceController/CSceneController.cpp @@ -65,7 +65,8 @@ namespace Skylicht buildHierarchyNodes(); - m_spaceHierarchy->setHierarchyNode(m_sceneNode); + if (m_spaceHierarchy != NULL) + m_spaceHierarchy->setHierarchyNode(m_sceneNode); } void CSceneController::buildHierarchyNodes() diff --git a/Projects/Editor/Source/EditorComponents/Viewpoint/CViewpoint.cpp b/Projects/Editor/Source/EditorComponents/Viewpoint/CViewpoint.cpp new file mode 100644 index 000000000..f4b1dfd8d --- /dev/null +++ b/Projects/Editor/Source/EditorComponents/Viewpoint/CViewpoint.cpp @@ -0,0 +1,58 @@ +/* +!@ +MIT License + +CopyRight (c) 2021 Skylicht Technology CO., LTD + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files +(the "Software"), to deal in the Software without restriction, including without limitation the Rights to use, copy, modify, +merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRight HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +This file is part of the "Skylicht Engine". +https://github.com/skylicht-lab/skylicht-engine +!# +*/ + +#include "pch.h" +#include "CViewpoint.h" + +#include "GameObject/CGameObject.h" +#include "Entity/CEntityManager.h" +#include "CViewpointData.h" +#include "CViewpointRenderer.h" + +namespace Skylicht +{ + namespace Editor + { + CViewpoint::CViewpoint() + { + + } + + CViewpoint::~CViewpoint() + { + + } + + void CViewpoint::initComponent() + { + m_gameObject->getEntity()->addData(); + m_gameObject->getEntityManager()->addRenderSystem(); + } + + void CViewpoint::updateComponent() + { + + } + } +} \ No newline at end of file diff --git a/Projects/Editor/Source/EditorComponents/Viewpoint/CViewpoint.h b/Projects/Editor/Source/EditorComponents/Viewpoint/CViewpoint.h new file mode 100644 index 000000000..014d0cdb7 --- /dev/null +++ b/Projects/Editor/Source/EditorComponents/Viewpoint/CViewpoint.h @@ -0,0 +1,45 @@ +/* +!@ +MIT License + +CopyRight (c) 2021 Skylicht Technology CO., LTD + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files +(the "Software"), to deal in the Software without restriction, including without limitation the Rights to use, copy, modify, +merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRight HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +This file is part of the "Skylicht Engine". +https://github.com/skylicht-lab/skylicht-engine +!# +*/ + +#pragma once + +#include "Components/CComponentSystem.h" + +namespace Skylicht +{ + namespace Editor + { + class CViewpoint : public CComponentSystem + { + public: + CViewpoint(); + + virtual ~CViewpoint(); + + virtual void initComponent(); + + virtual void updateComponent(); + }; + } +} \ No newline at end of file diff --git a/Projects/Editor/Source/EditorComponents/Viewpoint/CViewpointData.cpp b/Projects/Editor/Source/EditorComponents/Viewpoint/CViewpointData.cpp new file mode 100644 index 000000000..7ec6bb1f1 --- /dev/null +++ b/Projects/Editor/Source/EditorComponents/Viewpoint/CViewpointData.cpp @@ -0,0 +1,42 @@ +/* +!@ +MIT License + +CopyRight (c) 2021 Skylicht Technology CO., LTD + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files +(the "Software"), to deal in the Software without restriction, including without limitation the Rights to use, copy, modify, +merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRight HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +This file is part of the "Skylicht Engine". +https://github.com/skylicht-lab/skylicht-engine +!# +*/ + +#include "pch.h" +#include "CViewpointData.h" + +namespace Skylicht +{ + namespace Editor + { + CViewpointData::CViewpointData() + { + + } + + CViewpointData::~CViewpointData() + { + + } + } +} \ No newline at end of file diff --git a/Projects/Editor/Source/EditorComponents/Viewpoint/CViewpointData.h b/Projects/Editor/Source/EditorComponents/Viewpoint/CViewpointData.h new file mode 100644 index 000000000..9f961e573 --- /dev/null +++ b/Projects/Editor/Source/EditorComponents/Viewpoint/CViewpointData.h @@ -0,0 +1,41 @@ +/* +!@ +MIT License + +CopyRight (c) 2021 Skylicht Technology CO., LTD + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files +(the "Software"), to deal in the Software without restriction, including without limitation the Rights to use, copy, modify, +merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRight HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +This file is part of the "Skylicht Engine". +https://github.com/skylicht-lab/skylicht-engine +!# +*/ + +#pragma once + +#include "Entity/IEntityData.h" + +namespace Skylicht +{ + namespace Editor + { + class CViewpointData : public IEntityData + { + public: + CViewpointData(); + + virtual ~CViewpointData(); + }; + } +} \ No newline at end of file diff --git a/Projects/Editor/Source/EditorComponents/Viewpoint/CViewpointRenderer.cpp b/Projects/Editor/Source/EditorComponents/Viewpoint/CViewpointRenderer.cpp new file mode 100644 index 000000000..f87e74b51 --- /dev/null +++ b/Projects/Editor/Source/EditorComponents/Viewpoint/CViewpointRenderer.cpp @@ -0,0 +1,91 @@ +/* +!@ +MIT License + +CopyRight (c) 2021 Skylicht Technology CO., LTD + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files +(the "Software"), to deal in the Software without restriction, including without limitation the Rights to use, copy, modify, +merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRight HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +This file is part of the "Skylicht Engine". +https://github.com/skylicht-lab/skylicht-engine +!# +*/ + +#include "pch.h" +#include "CViewpointRenderer.h" + +namespace Skylicht +{ + namespace Editor + { + CViewpointRenderer::CViewpointRenderer() + { + } + + CViewpointRenderer::~CViewpointRenderer() + { + + } + + void CViewpointRenderer::beginQuery() + { + m_viewpoints.set_used(0); + m_transforms.set_used(0); + } + + void CViewpointRenderer::onQuery(CEntityManager* entityManager, CEntity* entity) + { + CViewpointData* viewPointData = entity->getData(); + + if (viewPointData != NULL) + { + CVisibleData* visible = entity->getData(); + CWorldTransformData* transformData = entity->getData(); + + if (transformData != NULL && visible->Visible) + { + m_viewpoints.push_back(viewPointData); + m_transforms.push_back(transformData); + } + } + } + + void CViewpointRenderer::init(CEntityManager* entityManager) + { + + } + + void CViewpointRenderer::update(CEntityManager* entityManager) + { + + } + + void CViewpointRenderer::render(CEntityManager* entityManager) + { + IVideoDriver* driver = getVideoDriver(); + + CViewpointData** viewpoints = m_viewpoints.pointer(); + CWorldTransformData** transforms = m_transforms.pointer(); + + for (u32 i = 0, n = m_viewpoints.size(); i < n; i++) + { + driver->setTransform(video::ETS_WORLD, transforms[i]->World); + + // IMeshBuffer* buffer = viewpoints[i]->Buffer; + // driver->setMaterial(buffer->getMaterial()); + // driver->drawMeshBuffer(buffer); + } + } + } +} \ No newline at end of file diff --git a/Projects/Editor/Source/EditorComponents/Viewpoint/CViewpointRenderer.h b/Projects/Editor/Source/EditorComponents/Viewpoint/CViewpointRenderer.h new file mode 100644 index 000000000..36dd943f6 --- /dev/null +++ b/Projects/Editor/Source/EditorComponents/Viewpoint/CViewpointRenderer.h @@ -0,0 +1,60 @@ +/* +!@ +MIT License + +CopyRight (c) 2021 Skylicht Technology CO., LTD + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files +(the "Software"), to deal in the Software without restriction, including without limitation the Rights to use, copy, modify, +merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRight HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +This file is part of the "Skylicht Engine". +https://github.com/skylicht-lab/skylicht-engine +!# +*/ + +#pragma once + +#include "Entity/IEntityData.h" +#include "Entity/IRenderSystem.h" + +#include "CViewpointData.h" +#include "Culling/CVisibleData.h" +#include "Transform/CWorldTransformData.h" + +namespace Skylicht +{ + namespace Editor + { + class CViewpointRenderer : public IRenderSystem + { + protected: + core::array m_viewpoints; + core::array m_transforms; + + public: + CViewpointRenderer(); + + virtual ~CViewpointRenderer(); + + virtual void beginQuery(); + + virtual void onQuery(CEntityManager* entityManager, CEntity* entity); + + virtual void init(CEntityManager* entityManager); + + virtual void update(CEntityManager* entityManager); + + virtual void render(CEntityManager* entityManager); + }; + } +} \ No newline at end of file