Skip to content

Commit

Permalink
#80 Add scene debug render (to debug collision), move line/polygon fr…
Browse files Browse the repository at this point in the history
…om Component to Engine
  • Loading branch information
ducphamhong committed Oct 17, 2021
1 parent 614638b commit d0d0ce8
Show file tree
Hide file tree
Showing 23 changed files with 656 additions and 13 deletions.
12 changes: 1 addition & 11 deletions Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,7 @@ namespace Skylicht

CDirectionalLight* directionalLight = lightObj->addComponent<CDirectionalLight>();
SColor c(255, 255, 244, 214);
directionalLight->setColor(SColorf(c));

lightObj = zone->createEmptyObject();
lightObj->setName(L"DirectionLight 1");

lightTransform = lightObj->getTransformEuler();
lightTransform->setPosition(core::vector3df(120.0f, 2.0f, 120.0f));

directionalLight = lightObj->addComponent<CDirectionalLight>();
c = SColor(255, 255, 244, 214);
directionalLight->setColor(SColorf(c));
directionalLight->setColor(SColorf(c));

// update search index
m_scene->updateAddRemoveObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ namespace Skylicht
{
g->onGizmos();
}

CCollisionController::getInstance()->getBBCollision()->drawDebug();
}

void CSceneController::refresh()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,21 @@ namespace Skylicht
addLinkComponent(m_sprite);

CBBCollisionManager* bbCollision = CCollisionController::getInstance()->getBBCollision();
m_collisionNode = bbCollision->addBBCollision(m_gameObject, core::aabbox3df(core::vector3df(-2.0f, -2.0f, -2.0f), core::vector3df(2.0f, 2.0f, 2.0f)));
m_collisionNode = bbCollision->addBBCollision(m_gameObject, core::aabbox3df(core::vector3df(-1.0f, -1.0f, -1.0f), core::vector3df(1.0f, 1.0f, 1.0f)));
}

void CGDirectionLight::updateComponent()
{
// update color of light
SColor lightColor = m_directionLight->getColor().toSColor();

m_sprite->setColor(lightColor);

// draw light direction arrow
CHandles::getInstance()->drawArrowInViewSpace(m_gameObject->getPosition(), m_directionLight->getDirection(), 0.5f, 0.05f, lightColor);

// update collision bbox
float boxScale = m_sprite->getViewScale() * 10.0f;
m_collisionNode->updateBBox(core::aabbox3df(core::vector3df(-1.0f, -1.0f, -1.0f) * boxScale, core::vector3df(1.0f, 1.0f, 1.0f) * boxScale));
}
}
}
22 changes: 22 additions & 0 deletions Projects/Skylicht/Collision/Source/Collision/CBBoxPatchBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This file is part of the "Skylicht Engine".

#include "pch.h"
#include "CBBoxPatchBuilder.h"
#include "Debug/CSceneDebug.h"

namespace Skylicht
{
Expand Down Expand Up @@ -259,6 +260,27 @@ namespace Skylicht
return ret;
}

void CBBoxPatchBuilder::drawDebug()
{
/*
CSceneDebug* debug = CSceneDebug::getInstance();
debug->addBoudingBox(m_global.BBox, SColor(255, 255, 0, 255));
for (u32 i = 0, n = m_collisionPatchs.size(); i < n; i++)
{
debug->addBoudingBox(m_collisionPatchs[i]->BBox, SColor(255, 255, 0, 255));
SPatch* patch = m_collisionPatchs[i];
for (u32 j = 0, m = patch->Collisions.size(); j < m; j++)
{
CCollisionNode* node = patch->Collisions[j];
debug->addBoudingBox(node->getTransformBBox(), SColor(255, 255, 255, 0));
}
}
*/
}

bool CBBoxPatchBuilder::getCollisionPoint(
const core::line3d<f32>& ray,
f32& outBestDistanceSquared,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ namespace Skylicht

public:

virtual void drawDebug();

virtual bool getCollisionPoint(
const core::line3d<f32>& ray,
f32& outBestDistanceSquared,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ namespace Skylicht

virtual void build() = 0;

virtual void drawDebug() {}

virtual CGameObject* getObjectWithRay(
const core::line3d<f32>& ray,
f32& outBestDistanceSquared,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ namespace Skylicht
}
}

void CCollisionNode::updateBBox(const core::aabbox3df& box)
{
if (Selector != NULL)
{
Selector->updateBBox(box);
}
}

core::aabbox3df CCollisionNode::getTransformBBox()
{
core::aabbox3df ret;
Expand Down
2 changes: 2 additions & 0 deletions Projects/Skylicht/Collision/Source/Collision/CCollisionNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ namespace Skylicht

void updateTransform();

void updateBBox(const core::aabbox3df& box);

core::aabbox3df getTransformBBox();
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ namespace Skylicht

return m_bbox;
}

void CTriangleSelector::updateBBox(const core::aabbox3df& box)
{
m_bbox = box;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ namespace Skylicht

virtual const core::aabbox3df& getBBox();

virtual void updateBBox(const core::aabbox3df& box);

inline u32 getTriangleCount()
{
return m_triangles.size();
Expand Down
10 changes: 10 additions & 0 deletions Projects/Skylicht/Components/Source/SpriteDraw/CSprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ namespace Skylicht
m_data->Color = color;
}

inline float getScale()
{
return m_data->Scale;
}

inline float getViewScale()
{
return m_data->ViewScale;
}

void setFrame(SFrame* frame, float scale, const SColor& color);

void setCenter(bool b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ namespace Skylicht

float Scale;

float ViewScale;

bool Center;

bool Billboard;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,12 @@ namespace Skylicht

// scale
float scale = m_sprites[i]->Scale;
m_sprites[i]->ViewScale = scale;
if (m_sprites[i]->AutoScaleInViewSpace)
{
scale = scale * 0.0015f / CProjective::getSegmentLengthClipSpace(camera, pos, pos + right);
m_sprites[i]->ViewScale = scale;
}

// render buffer
currentTexture = frame->Image->Texture;
Expand Down
117 changes: 117 additions & 0 deletions Projects/Skylicht/Engine/Source/Debug/CDebugRenderer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
!@
MIT License
Copyright (c) 2019 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 "CDebugRenderer.h"
#include "CSceneDebug.h"

namespace Skylicht
{
CDebugRenderer::CDebugRenderer()
{
m_drawData = new CLineDrawData();
}

CDebugRenderer::~CDebugRenderer()
{
delete m_drawData;
}

void CDebugRenderer::beginQuery()
{

}

void CDebugRenderer::onQuery(CEntityManager* entityManager, CEntity* entity)
{

}

void CDebugRenderer::init(CEntityManager* entityManager)
{

}

void CDebugRenderer::update(CEntityManager* entityManager)
{

}

void CDebugRenderer::render(CEntityManager* entityManager)
{

}

void CDebugRenderer::postRender(CEntityManager* entityManager)
{
IVideoDriver* videoDriver = getVideoDriver();
CSceneDebug* debug = CSceneDebug::getInstance();

for (int i = 0, n = debug->getLinesCount(); i < n; i++)
{
const CSceneDebug::SLineDebug& line = debug->getLine(i);

m_drawData->addLine(line.line.start, line.line.end, line.color);
}

for (int i = 0, n = debug->getBoxCount(); i < n; i++)
{
m_drawData->add3DBox(debug->getBox(i).box, debug->getBox(i).color);
}

for (int i = 0, n = debug->getTriCount(); i < n; i++)
{
core::vector3df normal = debug->getTri(i).tri.getNormal();
normal.normalize();
normal *= 2.0f;

core::vector3df a = debug->getTri(i).tri.pointA + normal;
core::vector3df b = debug->getTri(i).tri.pointB + normal;
core::vector3df c = debug->getTri(i).tri.pointC + normal;
SColor color = debug->getTri(i).color;

m_drawData->addLine(a, b, color);
m_drawData->addLine(b, c, color);
m_drawData->addLine(c, a, color);
}

for (int i = 0; i < debug->getLineStripCount(); i++)
{
const core::array<core::vector3df>& listPoint = debug->getLineStrip(i).point;
m_drawData->addPolyline(listPoint.const_pointer(), listPoint.size(), false, debug->getLineStrip(i).color);
}

m_drawData->updateBuffer();

IMeshBuffer* buffer = m_drawData->LineBuffer;
if (buffer->getPrimitiveCount() > 0)
{
videoDriver->setMaterial(buffer->getMaterial());
videoDriver->drawMeshBuffer(buffer);
}

debug->clear();
m_drawData->clearBuffer();
}
}
54 changes: 54 additions & 0 deletions Projects/Skylicht/Engine/Source/Debug/CDebugRenderer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
!@
MIT License
Copyright (c) 2019 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/IRenderSystem.h"
#include "LineDraw/CLineDrawData.h"

namespace Skylicht
{
class CDebugRenderer : public IRenderSystem
{
protected:
CLineDrawData* m_drawData;

public:
CDebugRenderer();

virtual ~CDebugRenderer();

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

virtual void postRender(CEntityManager* entityManager);
};
}
Loading

0 comments on commit d0d0ce8

Please sign in to comment.