-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
269 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
#ifndef LEPUS_GFX_SCENEGRAPH | ||
#define LEPUS_GFX_SCENEGRAPH | ||
#include "SceneGraph/Renderable.h" | ||
|
||
namespace lepus | ||
{ | ||
namespace gfx | ||
{ | ||
template<class MeshType = lepus::engine::objects::Mesh, class RenderableType = lepus::gfx::Renderable<MeshType>> | ||
class SceneGraph | ||
{ | ||
public: | ||
|
||
class Node | ||
{ | ||
private: | ||
friend class SceneGraph; | ||
|
||
protected: | ||
Node* m_Sibling; | ||
Node* m_Child; | ||
Node* m_Parent; | ||
const RenderableType* m_Renderable; | ||
|
||
public: | ||
Node() | ||
{ | ||
m_Sibling = nullptr; | ||
m_Child = nullptr; | ||
m_Parent = nullptr; | ||
m_Renderable = nullptr; | ||
} | ||
|
||
~Node() | ||
{ | ||
|
||
} | ||
|
||
inline const RenderableType* GetRenderable() const { return m_Renderable; } | ||
inline const Node* NextSibling() const { return m_Sibling; } | ||
inline const Node* Parent() const { return m_Parent; } | ||
inline const Node* FirstChild() const { return m_Child; } | ||
|
||
inline bool IsRoot() const { return Parent() == nullptr; } | ||
|
||
inline const Node* AddChild(const RenderableType* renderable) | ||
{ | ||
Node* currentNode = m_Child; | ||
Node* lastChild = m_Child; | ||
while (currentNode) | ||
{ | ||
lastChild = currentNode; | ||
currentNode = currentNode->m_Sibling; | ||
} | ||
|
||
if (!currentNode) | ||
{ | ||
currentNode = new Node(); | ||
currentNode->m_Renderable = renderable; | ||
currentNode->m_Parent = this; | ||
} | ||
|
||
if (!m_Child) | ||
{ | ||
m_Child = currentNode; | ||
} | ||
else if (lastChild && !lastChild->m_Sibling) | ||
{ | ||
lastChild->m_Sibling = currentNode; | ||
} | ||
|
||
return currentNode; | ||
} | ||
}; | ||
|
||
protected: | ||
Node* m_Root; | ||
|
||
public: | ||
|
||
SceneGraph() | ||
{ | ||
m_Root = new Node(); | ||
} | ||
|
||
const Node* AddChild(const RenderableType* renderable) | ||
{ | ||
return m_Root->AddChild(renderable); | ||
} | ||
|
||
~SceneGraph() | ||
{ | ||
Node* currentNode = m_Root->m_Child; | ||
Node* parent = currentNode; | ||
|
||
// Delete all children | ||
while (currentNode) | ||
{ | ||
if (currentNode->m_Child) | ||
{ | ||
parent = currentNode; | ||
currentNode = currentNode->m_Child; | ||
} | ||
else | ||
{ | ||
parent->m_Child = currentNode->m_Sibling; | ||
delete currentNode; | ||
currentNode = parent; | ||
} | ||
} | ||
} | ||
|
||
const Node* Root() const | ||
{ | ||
return m_Root; | ||
} | ||
}; | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#ifndef LEPUS_GFX_SCENEGRAPH_RENDERABLE | ||
#define LEPUS_GFX_SCENEGRAPH_RENDERABLE | ||
|
||
#include <lepus/engine/Objects/Mesh.h> | ||
#include <lepus/utility/types/Transform.h> | ||
|
||
namespace lepus | ||
{ | ||
namespace gfx | ||
{ | ||
template<class MeshType = lepus::engine::objects::Mesh> | ||
class Renderable | ||
{ | ||
private: | ||
const MeshType* m_Mesh; | ||
const lepus::math::Transform* m_Transform; | ||
bool m_OwnsTransform; | ||
|
||
public: | ||
Renderable() = delete; | ||
|
||
Renderable(const MeshType* mesh, const lepus::math::Transform& transform) | ||
{ | ||
m_Mesh = mesh; | ||
m_Transform = &transform; | ||
m_OwnsTransform = false; | ||
} | ||
|
||
Renderable(const MeshType* mesh, lepus::math::Transform&& transform) | ||
{ | ||
m_Mesh = mesh; | ||
m_Transform = new lepus::math::Transform(transform); | ||
m_OwnsTransform = true; | ||
} | ||
|
||
~Renderable() | ||
{ | ||
m_Mesh = nullptr; | ||
|
||
if (m_OwnsTransform) | ||
{ | ||
delete m_Transform; | ||
m_Transform = nullptr; | ||
} | ||
} | ||
|
||
const MeshType* GetMesh() const | ||
{ | ||
return m_Mesh; | ||
} | ||
|
||
const lepus::math::Transform& GetTransform() const | ||
{ | ||
return *m_Transform; | ||
} | ||
}; | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters