From d0d0ce8dc3b099d425f864c5f5afad5eb34f73a0 Mon Sep 17 00:00:00 2001 From: Pham Hong Duc Date: Sun, 17 Oct 2021 23:19:21 +0700 Subject: [PATCH] #80 Add scene debug render (to debug collision), move line/polygon from Component to Engine --- .../Source/Editor/Space/Scene/CSpaceScene.cpp | 12 +- .../SpaceController/CSceneController.cpp | 2 + .../DirectionLight/CGDirectionLight.cpp | 9 +- .../Source/Collision/CBBoxPatchBuilder.cpp | 22 ++ .../Source/Collision/CBBoxPatchBuilder.h | 2 + .../Source/Collision/CCollisionBuilder.h | 2 + .../Source/Collision/CCollisionNode.cpp | 8 + .../Source/Collision/CCollisionNode.h | 2 + .../Source/Collision/CTriangleSelector.cpp | 5 + .../Source/Collision/CTriangleSelector.h | 2 + .../Components/Source/SpriteDraw/CSprite.h | 10 + .../Source/SpriteDraw/CSpriteDrawData.h | 2 + .../Source/SpriteDraw/CSpriteRenderer.cpp | 4 + .../Engine/Source/Debug/CDebugRenderer.cpp | 117 +++++++++ .../Engine/Source/Debug/CDebugRenderer.h | 54 ++++ .../Engine/Source/Debug/CSceneDebug.cpp | 243 ++++++++++++++++++ .../Engine/Source/Debug/CSceneDebug.h | 165 ++++++++++++ .../Engine/Source/Entity/CEntityManager.cpp | 2 + .../Source/LineDraw/CLineDrawData.cpp | 0 .../Source/LineDraw/CLineDrawData.h | 0 .../Source/PolygonDraw/CPolygonDrawData.cpp | 0 .../Source/PolygonDraw/CPolygonDrawData.h | 0 Projects/Skylicht/Engine/Source/Skylicht.cpp | 6 + 23 files changed, 656 insertions(+), 13 deletions(-) create mode 100644 Projects/Skylicht/Engine/Source/Debug/CDebugRenderer.cpp create mode 100644 Projects/Skylicht/Engine/Source/Debug/CDebugRenderer.h create mode 100644 Projects/Skylicht/Engine/Source/Debug/CSceneDebug.cpp create mode 100644 Projects/Skylicht/Engine/Source/Debug/CSceneDebug.h rename Projects/Skylicht/{Components => Engine}/Source/LineDraw/CLineDrawData.cpp (100%) rename Projects/Skylicht/{Components => Engine}/Source/LineDraw/CLineDrawData.h (100%) rename Projects/Skylicht/{Components => Engine}/Source/PolygonDraw/CPolygonDrawData.cpp (100%) rename Projects/Skylicht/{Components => Engine}/Source/PolygonDraw/CPolygonDrawData.h (100%) diff --git a/Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.cpp b/Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.cpp index 57acba271..916678585 100644 --- a/Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.cpp +++ b/Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.cpp @@ -277,17 +277,7 @@ namespace Skylicht CDirectionalLight* directionalLight = lightObj->addComponent(); 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(); - c = SColor(255, 255, 244, 214); - directionalLight->setColor(SColorf(c)); + directionalLight->setColor(SColorf(c)); // update search index m_scene->updateAddRemoveObject(); diff --git a/Projects/Editor/Source/Editor/SpaceController/CSceneController.cpp b/Projects/Editor/Source/Editor/SpaceController/CSceneController.cpp index 6682f6720..103a8a21a 100644 --- a/Projects/Editor/Source/Editor/SpaceController/CSceneController.cpp +++ b/Projects/Editor/Source/Editor/SpaceController/CSceneController.cpp @@ -310,6 +310,8 @@ namespace Skylicht { g->onGizmos(); } + + CCollisionController::getInstance()->getBBCollision()->drawDebug(); } void CSceneController::refresh() diff --git a/Projects/Editor/Source/EditorComponents/DirectionLight/CGDirectionLight.cpp b/Projects/Editor/Source/EditorComponents/DirectionLight/CGDirectionLight.cpp index 7b5a4dfad..da3d4548e 100644 --- a/Projects/Editor/Source/EditorComponents/DirectionLight/CGDirectionLight.cpp +++ b/Projects/Editor/Source/EditorComponents/DirectionLight/CGDirectionLight.cpp @@ -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)); } } } \ No newline at end of file diff --git a/Projects/Skylicht/Collision/Source/Collision/CBBoxPatchBuilder.cpp b/Projects/Skylicht/Collision/Source/Collision/CBBoxPatchBuilder.cpp index c1d522d17..5f7488e7c 100644 --- a/Projects/Skylicht/Collision/Source/Collision/CBBoxPatchBuilder.cpp +++ b/Projects/Skylicht/Collision/Source/Collision/CBBoxPatchBuilder.cpp @@ -24,6 +24,7 @@ This file is part of the "Skylicht Engine". #include "pch.h" #include "CBBoxPatchBuilder.h" +#include "Debug/CSceneDebug.h" namespace Skylicht { @@ -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& ray, f32& outBestDistanceSquared, diff --git a/Projects/Skylicht/Collision/Source/Collision/CBBoxPatchBuilder.h b/Projects/Skylicht/Collision/Source/Collision/CBBoxPatchBuilder.h index 4135e74fa..0708d0552 100644 --- a/Projects/Skylicht/Collision/Source/Collision/CBBoxPatchBuilder.h +++ b/Projects/Skylicht/Collision/Source/Collision/CBBoxPatchBuilder.h @@ -76,6 +76,8 @@ namespace Skylicht public: + virtual void drawDebug(); + virtual bool getCollisionPoint( const core::line3d& ray, f32& outBestDistanceSquared, diff --git a/Projects/Skylicht/Collision/Source/Collision/CCollisionBuilder.h b/Projects/Skylicht/Collision/Source/Collision/CCollisionBuilder.h index 2bf4dfd89..1a7675e74 100644 --- a/Projects/Skylicht/Collision/Source/Collision/CCollisionBuilder.h +++ b/Projects/Skylicht/Collision/Source/Collision/CCollisionBuilder.h @@ -48,6 +48,8 @@ namespace Skylicht virtual void build() = 0; + virtual void drawDebug() {} + virtual CGameObject* getObjectWithRay( const core::line3d& ray, f32& outBestDistanceSquared, diff --git a/Projects/Skylicht/Collision/Source/Collision/CCollisionNode.cpp b/Projects/Skylicht/Collision/Source/Collision/CCollisionNode.cpp index c14e8136a..9eec8dd67 100644 --- a/Projects/Skylicht/Collision/Source/Collision/CCollisionNode.cpp +++ b/Projects/Skylicht/Collision/Source/Collision/CCollisionNode.cpp @@ -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; diff --git a/Projects/Skylicht/Collision/Source/Collision/CCollisionNode.h b/Projects/Skylicht/Collision/Source/Collision/CCollisionNode.h index 6cd94f490..4e61ec329 100644 --- a/Projects/Skylicht/Collision/Source/Collision/CCollisionNode.h +++ b/Projects/Skylicht/Collision/Source/Collision/CCollisionNode.h @@ -50,6 +50,8 @@ namespace Skylicht void updateTransform(); + void updateBBox(const core::aabbox3df& box); + core::aabbox3df getTransformBBox(); }; } \ No newline at end of file diff --git a/Projects/Skylicht/Collision/Source/Collision/CTriangleSelector.cpp b/Projects/Skylicht/Collision/Source/Collision/CTriangleSelector.cpp index acbc0404b..0c7c21a22 100644 --- a/Projects/Skylicht/Collision/Source/Collision/CTriangleSelector.cpp +++ b/Projects/Skylicht/Collision/Source/Collision/CTriangleSelector.cpp @@ -68,4 +68,9 @@ namespace Skylicht return m_bbox; } + + void CTriangleSelector::updateBBox(const core::aabbox3df& box) + { + m_bbox = box; + } } \ No newline at end of file diff --git a/Projects/Skylicht/Collision/Source/Collision/CTriangleSelector.h b/Projects/Skylicht/Collision/Source/Collision/CTriangleSelector.h index 119c68560..28c77be19 100644 --- a/Projects/Skylicht/Collision/Source/Collision/CTriangleSelector.h +++ b/Projects/Skylicht/Collision/Source/Collision/CTriangleSelector.h @@ -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(); diff --git a/Projects/Skylicht/Components/Source/SpriteDraw/CSprite.h b/Projects/Skylicht/Components/Source/SpriteDraw/CSprite.h index 2ea1d8887..e2133f9f6 100644 --- a/Projects/Skylicht/Components/Source/SpriteDraw/CSprite.h +++ b/Projects/Skylicht/Components/Source/SpriteDraw/CSprite.h @@ -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); diff --git a/Projects/Skylicht/Components/Source/SpriteDraw/CSpriteDrawData.h b/Projects/Skylicht/Components/Source/SpriteDraw/CSpriteDrawData.h index 8c249df4b..9eb1471dc 100644 --- a/Projects/Skylicht/Components/Source/SpriteDraw/CSpriteDrawData.h +++ b/Projects/Skylicht/Components/Source/SpriteDraw/CSpriteDrawData.h @@ -42,6 +42,8 @@ namespace Skylicht float Scale; + float ViewScale; + bool Center; bool Billboard; diff --git a/Projects/Skylicht/Components/Source/SpriteDraw/CSpriteRenderer.cpp b/Projects/Skylicht/Components/Source/SpriteDraw/CSpriteRenderer.cpp index e57bddb6b..517083504 100644 --- a/Projects/Skylicht/Components/Source/SpriteDraw/CSpriteRenderer.cpp +++ b/Projects/Skylicht/Components/Source/SpriteDraw/CSpriteRenderer.cpp @@ -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; diff --git a/Projects/Skylicht/Engine/Source/Debug/CDebugRenderer.cpp b/Projects/Skylicht/Engine/Source/Debug/CDebugRenderer.cpp new file mode 100644 index 000000000..2ddb85af2 --- /dev/null +++ b/Projects/Skylicht/Engine/Source/Debug/CDebugRenderer.cpp @@ -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& 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(); + } +} \ No newline at end of file diff --git a/Projects/Skylicht/Engine/Source/Debug/CDebugRenderer.h b/Projects/Skylicht/Engine/Source/Debug/CDebugRenderer.h new file mode 100644 index 000000000..07cec02af --- /dev/null +++ b/Projects/Skylicht/Engine/Source/Debug/CDebugRenderer.h @@ -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); + }; +} \ No newline at end of file diff --git a/Projects/Skylicht/Engine/Source/Debug/CSceneDebug.cpp b/Projects/Skylicht/Engine/Source/Debug/CSceneDebug.cpp new file mode 100644 index 000000000..300f2a79d --- /dev/null +++ b/Projects/Skylicht/Engine/Source/Debug/CSceneDebug.cpp @@ -0,0 +1,243 @@ +/* +!@ +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 "CSceneDebug.h" + +namespace Skylicht +{ + CSceneDebug::CSceneDebug() : + m_nLine(0), + m_nBox(0), + m_nTri(0) + { + + } + + CSceneDebug::~CSceneDebug() + { + } + + void CSceneDebug::addCircle(const core::vector3df& pos, float radius, const core::vector3df& normal, const SColor& color) + { + if (m_nLineStrip >= MAX_DEBUGGEOMETRY) + return; + + core::quaternion q; + core::vector3df up(0.0f, 1.0f, 0.0f); + core::vector3df n = normal; + n.normalize(); + q.rotationFromTo(up, n); + + int step = 30; + float rad = 0; + float radInc = core::PI * 2 / step; + + core::vector3df point; + + // clear point list + m_linestrip[m_nLineStrip].point.set_used(0); + m_linestrip[m_nLineStrip].color = color; + + for (int i = 0; i <= step; i++) + { + point.Y = 0.0; + point.X = radius * sinf(rad); + point.Z = radius * cosf(rad); + + // rotate + point = q * point; + + // translate + point += pos; + + // add point + m_linestrip[m_nLineStrip].point.push_back(point); + + // inc rad + rad = rad + radInc; + } + + m_nLineStrip++; + } + + void CSceneDebug::addEclipse(const core::vector3df& pos, float radiusZ, float radiusX, const core::vector3df& normal, const SColor& color) + { + if (m_nLineStrip >= MAX_DEBUGGEOMETRY) + return; + + core::quaternion q; + core::vector3df up(0.0f, 1.0f, 0.0f); + core::vector3df n = normal; + n.normalize(); + q.rotationFromTo(up, n); + + int step = 30; + float rad = 0; + float radInc = core::PI * 2 / step; + + core::vector3df point; + + // clear point list + m_linestrip[m_nLineStrip].point.set_used(0); + m_linestrip[m_nLineStrip].color = color; + + for (int i = 0; i <= step; i++) + { + point.Y = 0.0; + point.X = radiusX * sinf(rad); + point.Z = radiusZ * cosf(rad); + + // rotate + point = q * point; + + // translate + point += pos; + + // add point + m_linestrip[m_nLineStrip].point.push_back(point); + + // inc rad + rad = rad + radInc; + } + + m_nLineStrip++; + } + + void CSceneDebug::addSphere(const core::vector3df& pos, float radius, const SColor& color) + { + core::vector3df normal; + + normal = core::vector3df(0.0f, 1.0f, 0.0f); + addCircle(pos, radius, normal, color); + + + normal = core::vector3df(0.0f, 0.0f, 1.0f); + addCircle(pos, radius, normal, color); + } + + void CSceneDebug::addTri(const core::triangle3df& tri, const SColor& color) + { + if (m_nTri >= MAX_DEBUGGEOMETRY) + return; + + m_tri[m_nTri].tri = tri; + m_tri[m_nTri].color = color; + m_nTri++; + } + + void CSceneDebug::addLine(const core::vector3df& v1, const core::vector3df& v2, const SColor& color) + { + if (m_nLine >= MAX_DEBUGGEOMETRY) + return; + + core::line3df line(v1, v2); + m_lines[m_nLine].line = line; + m_lines[m_nLine].color = color; + m_nLine++; + } + + void CSceneDebug::addLine(const core::line3df& line, const SColor& color) + { + if (m_nLine >= MAX_DEBUGGEOMETRY) + return; + + m_lines[m_nLine].line = line; + m_lines[m_nLine].color = color; + m_nLine++; + } + + void CSceneDebug::addLinestrip(const std::vector& point, const SColor& color) + { + if (m_nLineStrip >= MAX_DEBUGGEOMETRY) + return; + + m_linestrip[m_nLineStrip].point.set_used(0); + for (int i = 0, n = (int)point.size(); i < n; i++) + m_linestrip[m_nLineStrip].point.push_back(point[i]); + + m_linestrip[m_nLineStrip].color = color; + m_nLineStrip++; + } + + void CSceneDebug::addLinestrip(const core::array& point, const SColor& color) + { + if (m_nLineStrip >= MAX_DEBUGGEOMETRY) + return; + + m_linestrip[m_nLineStrip].point = point; + m_linestrip[m_nLineStrip].color = color; + m_nLineStrip++; + } + + void CSceneDebug::addBoudingBox(const core::aabbox3df& box, const SColor& color) + { + if (m_nBox >= MAX_DEBUGGEOMETRY) + return; + + m_boxs[m_nBox].box = box; + m_boxs[m_nBox].color = color; + m_nBox++; + } + + void CSceneDebug::addTransformBBox(const core::aabbox3df& box, const SColor& color, const core::matrix4& mat) + { + core::vector3df edges[8]; + box.getEdges(edges); + + for (int i = 0; i < 8; i++) + mat.transformVect(edges[i]); + + addLine(edges[5], edges[1], color); + addLine(edges[1], edges[3], color); + addLine(edges[3], edges[7], color); + addLine(edges[7], edges[5], color); + addLine(edges[0], edges[2], color); + addLine(edges[2], edges[6], color); + addLine(edges[6], edges[4], color); + addLine(edges[4], edges[0], color); + addLine(edges[1], edges[0], color); + addLine(edges[3], edges[2], color); + addLine(edges[7], edges[6], color); + addLine(edges[5], edges[4], color); + } + + void CSceneDebug::addTransform(const core::matrix4& mat, float vectorLength) + { + core::vector3df pos = mat.getTranslation(); + core::vector3df scale = mat.getScale(); + + core::vector3df y(0.0f, 1.0f, 0.0f); + core::vector3df x(1.0f, 0.0f, 0.0f); + core::vector3df z(0.0f, 0.0f, 1.0f); + + mat.rotateVect(x); + mat.rotateVect(y); + mat.rotateVect(z); + + addLine(pos, pos + x * vectorLength * scale.X, SColor(255, 255, 0, 0)); + addLine(pos, pos + y * vectorLength * scale.Y, SColor(255, 0, 255, 0)); + addLine(pos, pos + z * vectorLength * scale.Z, SColor(255, 0, 0, 255)); + } +} \ No newline at end of file diff --git a/Projects/Skylicht/Engine/Source/Debug/CSceneDebug.h b/Projects/Skylicht/Engine/Source/Debug/CSceneDebug.h new file mode 100644 index 000000000..12f51d1ba --- /dev/null +++ b/Projects/Skylicht/Engine/Source/Debug/CSceneDebug.h @@ -0,0 +1,165 @@ +/* +!@ +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 "Utils/CGameSingleton.h" + +#define MAX_DEBUGGEOMETRY 2000 + +namespace Skylicht +{ + class CSceneDebug : public CGameSingleton + { + public: + struct SLineDebug + { + core::line3df line; + SColor color; + }; + + struct SLineStripDebug + { + core::array point; + SColor color; + }; + + struct S2DRectDebug + { + core::rectf rect; + bool outLine; + SColor color; + }; + + struct SBoxDebug + { + core::aabbox3df box; + SColor color; + }; + + struct STriDebug + { + core::triangle3df tri; + SColor color; + }; + + + protected: + SLineDebug m_lines[MAX_DEBUGGEOMETRY]; + SLineStripDebug m_linestrip[MAX_DEBUGGEOMETRY]; + + SBoxDebug m_boxs[MAX_DEBUGGEOMETRY]; + STriDebug m_tri[MAX_DEBUGGEOMETRY]; + + int m_nLine; + int m_nLineStrip; + int m_nBox; + int m_nTri; + + public: + + CSceneDebug(); + virtual ~CSceneDebug(); + + void addCircle(const core::vector3df& pos, float radius, const core::vector3df& normal, const SColor& color); + void addEclipse(const core::vector3df& pos, float radiusZ, float radiusX, const core::vector3df& normal, const SColor& color); + void addTri(const core::triangle3df& tri, const SColor& color); + void addLine(const core::line3df& line, const SColor& color); + void addLine(const core::vector3df& v1, const core::vector3df& v2, const SColor& color); + void addLinestrip(const core::array& point, const SColor& color); + void addLinestrip(const std::vector& point, const SColor& color); + void addBoudingBox(const core::aabbox3df& box, const SColor& color); + void addTransformBBox(const core::aabbox3df& box, const SColor& color, const core::matrix4& mat); + void addSphere(const core::vector3df& pos, float radius, const SColor& color); + void addTransform(const core::matrix4& mat, float vectorLength); + + void clear() + { + clearLines(); + clearLineStrip(); + clearBoxs(); + clearTri(); + } + + inline void clearLines() + { + m_nLine = 0; + } + + inline void clearLineStrip() + { + m_nLineStrip = 0; + } + + inline void clearBoxs() + { + m_nBox = 0; + } + + inline void clearTri() + { + m_nTri = 0; + } + + inline int getLinesCount() + { + return m_nLine; + } + + inline int getLineStripCount() + { + return m_nLineStrip; + } + + inline int getBoxCount() + { + return m_nBox; + } + + inline int getTriCount() + { + return m_nTri; + } + + inline const SLineDebug& getLine(int i) + { + return m_lines[i]; + } + + inline const SLineStripDebug& getLineStrip(int i) + { + return m_linestrip[i]; + } + + inline const SBoxDebug& getBox(int i) + { + return m_boxs[i]; + } + + inline const STriDebug& getTri(int i) + { + return m_tri[i]; + } + }; +} \ No newline at end of file diff --git a/Projects/Skylicht/Engine/Source/Entity/CEntityManager.cpp b/Projects/Skylicht/Engine/Source/Entity/CEntityManager.cpp index f43c1c871..977836a3a 100644 --- a/Projects/Skylicht/Engine/Source/Entity/CEntityManager.cpp +++ b/Projects/Skylicht/Engine/Source/Entity/CEntityManager.cpp @@ -37,6 +37,7 @@ This file is part of the "Skylicht Engine". #include "Culling/CCullingSystem.h" #include "Lighting/CLightCullingSystem.h" #include "ReflectionProbe/CReflectionProbeSystem.h" +#include "Debug/CDebugRenderer.h" namespace Skylicht { @@ -62,6 +63,7 @@ namespace Skylicht // systems run after culling addRenderSystem(); addRenderSystem(); + addRenderSystem(); } CEntityManager::~CEntityManager() diff --git a/Projects/Skylicht/Components/Source/LineDraw/CLineDrawData.cpp b/Projects/Skylicht/Engine/Source/LineDraw/CLineDrawData.cpp similarity index 100% rename from Projects/Skylicht/Components/Source/LineDraw/CLineDrawData.cpp rename to Projects/Skylicht/Engine/Source/LineDraw/CLineDrawData.cpp diff --git a/Projects/Skylicht/Components/Source/LineDraw/CLineDrawData.h b/Projects/Skylicht/Engine/Source/LineDraw/CLineDrawData.h similarity index 100% rename from Projects/Skylicht/Components/Source/LineDraw/CLineDrawData.h rename to Projects/Skylicht/Engine/Source/LineDraw/CLineDrawData.h diff --git a/Projects/Skylicht/Components/Source/PolygonDraw/CPolygonDrawData.cpp b/Projects/Skylicht/Engine/Source/PolygonDraw/CPolygonDrawData.cpp similarity index 100% rename from Projects/Skylicht/Components/Source/PolygonDraw/CPolygonDrawData.cpp rename to Projects/Skylicht/Engine/Source/PolygonDraw/CPolygonDrawData.cpp diff --git a/Projects/Skylicht/Components/Source/PolygonDraw/CPolygonDrawData.h b/Projects/Skylicht/Engine/Source/PolygonDraw/CPolygonDrawData.h similarity index 100% rename from Projects/Skylicht/Components/Source/PolygonDraw/CPolygonDrawData.h rename to Projects/Skylicht/Engine/Source/PolygonDraw/CPolygonDrawData.h diff --git a/Projects/Skylicht/Engine/Source/Skylicht.cpp b/Projects/Skylicht/Engine/Source/Skylicht.cpp index c66f0b8e1..fdd9f2d4a 100644 --- a/Projects/Skylicht/Engine/Source/Skylicht.cpp +++ b/Projects/Skylicht/Engine/Source/Skylicht.cpp @@ -43,6 +43,8 @@ This file is part of the "Skylicht Engine". #include "Animation/CAnimationManager.h" #include "TextureManager/CTextureManager.h" +#include "Debug/CSceneDebug.h" + // Tween #include "Tween/CTweenManager.h" @@ -91,6 +93,8 @@ namespace Skylicht CGlyphFreetype::createGetInstance(); #endif + CSceneDebug::createGetInstance(); + // alway use HW g_video->setMinHardwareBufferVertexCount(0); @@ -102,6 +106,8 @@ namespace Skylicht { os::Printer::log("Close skylicht core"); + CSceneDebug::releaseInstance(); + #if defined(USE_FREETYPE) CGlyphFreetype::releaseInstance(); #endif