Skip to content

Commit

Permalink
#123 Add viewpoint renderer, fix crash on scene controller
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Apr 26, 2021
1 parent 40493e5 commit 46cc366
Show file tree
Hide file tree
Showing 9 changed files with 370 additions and 3 deletions.
31 changes: 29 additions & 2 deletions Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -128,15 +129,18 @@ namespace Skylicht
camObj->addComponent<CCamera>();

m_viewpointCamera = camObj->getComponent<CCamera>();
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
CGameObject* gridPlane = m_viewpointZone->createEmptyObject();
gridPlane->setName(L"Grid3D");
gridPlane->addComponent<CGridPlane>();

// viewpoint
CGameObject* viewpoint = m_viewpointZone->createEmptyObject();
viewpoint->setName(L"Viewpoint");
viewpoint->addComponent<CViewpoint>();

// set scene to controller
CSceneController::getInstance()->setScene(m_scene);
}
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ namespace Skylicht

buildHierarchyNodes();

m_spaceHierarchy->setHierarchyNode(m_sceneNode);
if (m_spaceHierarchy != NULL)
m_spaceHierarchy->setHierarchyNode(m_sceneNode);
}

void CSceneController::buildHierarchyNodes()
Expand Down
58 changes: 58 additions & 0 deletions Projects/Editor/Source/EditorComponents/Viewpoint/CViewpoint.cpp
Original file line number Diff line number Diff line change
@@ -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<CViewpointData>();
m_gameObject->getEntityManager()->addRenderSystem<CViewpointRenderer>();
}

void CViewpoint::updateComponent()
{

}
}
}
45 changes: 45 additions & 0 deletions Projects/Editor/Source/EditorComponents/Viewpoint/CViewpoint.h
Original file line number Diff line number Diff line change
@@ -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();
};
}
}
Original file line number Diff line number Diff line change
@@ -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()
{

}
}
}
41 changes: 41 additions & 0 deletions Projects/Editor/Source/EditorComponents/Viewpoint/CViewpointData.h
Original file line number Diff line number Diff line change
@@ -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();
};
}
}
Original file line number Diff line number Diff line change
@@ -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<CViewpointData>();

if (viewPointData != NULL)
{
CVisibleData* visible = entity->getData<CVisibleData>();
CWorldTransformData* transformData = entity->getData<CWorldTransformData>();

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);
}
}
}
}
Loading

0 comments on commit 46cc366

Please sign in to comment.