Commit 97013ac9 authored by chili's avatar chili
Browse files

basic model node tree

parent 551cc463
...@@ -25,38 +25,27 @@ void App::DoFrame() ...@@ -25,38 +25,27 @@ void App::DoFrame()
wnd.Gfx().BeginFrame( 0.07f,0.0f,0.12f ); wnd.Gfx().BeginFrame( 0.07f,0.0f,0.12f );
wnd.Gfx().SetCamera( cam.GetMatrix() ); wnd.Gfx().SetCamera( cam.GetMatrix() );
light.Bind( wnd.Gfx(),cam.GetMatrix() ); light.Bind( wnd.Gfx(),cam.GetMatrix() );
const auto transform = dx::XMMatrixRotationRollPitchYaw( pos.roll,pos.pitch,pos.yaw ) * nano.Draw( wnd.Gfx() );
dx::XMMatrixTranslation( pos.x,pos.y,pos.z );
nano.Draw( wnd.Gfx(),transform );
light.Draw( wnd.Gfx() ); light.Draw( wnd.Gfx() );
// imgui windows // imgui windows
cam.SpawnControlWindow(); cam.SpawnControlWindow();
light.SpawnControlWindow(); light.SpawnControlWindow();
ShowModelWindow(); ShowImguiDemoWindow();
nano.ShowWindow();
// present // present
wnd.Gfx().EndFrame(); wnd.Gfx().EndFrame();
} }
void App::ShowModelWindow() void App::ShowImguiDemoWindow()
{ {
if( ImGui::Begin( "Model" ) ) static bool show_demo_window = true;
if( show_demo_window )
{ {
using namespace std::string_literals; ImGui::ShowDemoWindow( &show_demo_window );
ImGui::Text( "Orientation" );
ImGui::SliderAngle( "Roll",&pos.roll,-180.0f,180.0f );
ImGui::SliderAngle( "Pitch",&pos.pitch,-180.0f,180.0f );
ImGui::SliderAngle( "Yaw",&pos.yaw,-180.0f,180.0f );
ImGui::Text( "Position" );
ImGui::SliderFloat( "X",&pos.x,-20.0f,20.0f );
ImGui::SliderFloat( "Y",&pos.y,-20.0f,20.0f );
ImGui::SliderFloat( "Z",&pos.z,-20.0f,20.0f );
} }
ImGui::End();
} }
App::~App() App::~App()
......
...@@ -16,7 +16,7 @@ public: ...@@ -16,7 +16,7 @@ public:
~App(); ~App();
private: private:
void DoFrame(); void DoFrame();
void ShowModelWindow(); void ShowImguiDemoWindow();
private: private:
ImguiManager imgui; ImguiManager imgui;
Window wnd; Window wnd;
...@@ -25,13 +25,4 @@ private: ...@@ -25,13 +25,4 @@ private:
Camera cam; Camera cam;
PointLight light; PointLight light;
Model nano{ wnd.Gfx(),"Models\\nanosuit.obj" }; Model nano{ wnd.Gfx(),"Models\\nanosuit.obj" };
struct
{
float roll = 0.0f;
float pitch = 0.0f;
float yaw = 0.0f;
float x = 0.0f;
float y = 0.0f;
float z = 0.0f;
} pos;
}; };
\ No newline at end of file
#include "Mesh.h" #include "Mesh.h"
#include "imgui/imgui.h"
// Mesh // Mesh
Mesh::Mesh( Graphics& gfx,std::vector<std::unique_ptr<Bind::Bindable>> bindPtrs ) Mesh::Mesh( Graphics& gfx,std::vector<std::unique_ptr<Bind::Bindable>> bindPtrs )
...@@ -23,7 +24,7 @@ Mesh::Mesh( Graphics& gfx,std::vector<std::unique_ptr<Bind::Bindable>> bindPtrs ...@@ -23,7 +24,7 @@ Mesh::Mesh( Graphics& gfx,std::vector<std::unique_ptr<Bind::Bindable>> bindPtrs
AddBind( std::make_unique<Bind::TransformCbuf>( gfx,*this ) ); AddBind( std::make_unique<Bind::TransformCbuf>( gfx,*this ) );
} }
void Mesh::Draw( Graphics& gfx,DirectX::FXMMATRIX accumulatedTransform ) const noexcept(!IS_DEBUG) void Mesh::Draw( Graphics& gfx,DirectX::FXMMATRIX accumulatedTransform ) const noxnd
{ {
DirectX::XMStoreFloat4x4( &transform,accumulatedTransform ); DirectX::XMStoreFloat4x4( &transform,accumulatedTransform );
Drawable::Draw( gfx ); Drawable::Draw( gfx );
...@@ -35,12 +36,14 @@ DirectX::XMMATRIX Mesh::GetTransformXM() const noexcept ...@@ -35,12 +36,14 @@ DirectX::XMMATRIX Mesh::GetTransformXM() const noexcept
// Node // Node
Node::Node( std::vector<Mesh*> meshPtrs,const DirectX::XMMATRIX& transform ) noxnd Node::Node( const std::string& name,std::vector<Mesh*> meshPtrs,const DirectX::XMMATRIX& transform ) noxnd
: :
meshPtrs( std::move( meshPtrs ) ) meshPtrs( std::move( meshPtrs ) ),
name( name )
{ {
DirectX::XMStoreFloat4x4( &this->transform,transform ); DirectX::XMStoreFloat4x4( &this->transform,transform );
} }
void Node::Draw( Graphics& gfx,DirectX::FXMMATRIX accumulatedTransform ) const noxnd void Node::Draw( Graphics& gfx,DirectX::FXMMATRIX accumulatedTransform ) const noxnd
{ {
const auto built = DirectX::XMLoadFloat4x4( &transform ) * accumulatedTransform; const auto built = DirectX::XMLoadFloat4x4( &transform ) * accumulatedTransform;
...@@ -53,12 +56,26 @@ void Node::Draw( Graphics& gfx,DirectX::FXMMATRIX accumulatedTransform ) const n ...@@ -53,12 +56,26 @@ void Node::Draw( Graphics& gfx,DirectX::FXMMATRIX accumulatedTransform ) const n
pc->Draw( gfx,built ); pc->Draw( gfx,built );
} }
} }
void Node::AddChild( std::unique_ptr<Node> pChild ) noxnd void Node::AddChild( std::unique_ptr<Node> pChild ) noxnd
{ {
assert( pChild ); assert( pChild );
childPtrs.push_back( std::move( pChild ) ); childPtrs.push_back( std::move( pChild ) );
} }
void Node::RenderTree() const noexcept
{
// if tree node expanded, recursively render all children
if( ImGui::TreeNode( name.c_str() ) )
{
for( const auto& pChild : childPtrs )
{
pChild->RenderTree();
}
ImGui::TreePop();
}
}
// Model // Model
Model::Model( Graphics& gfx,const std::string fileName ) Model::Model( Graphics& gfx,const std::string fileName )
...@@ -76,10 +93,35 @@ Model::Model( Graphics& gfx,const std::string fileName ) ...@@ -76,10 +93,35 @@ Model::Model( Graphics& gfx,const std::string fileName )
pRoot = ParseNode( *pScene->mRootNode ); pRoot = ParseNode( *pScene->mRootNode );
} }
void Model::Draw( Graphics& gfx,DirectX::FXMMATRIX transform ) const
void Model::Draw( Graphics& gfx ) const noxnd
{ {
const auto transform = DirectX::XMMatrixRotationRollPitchYaw( pos.roll,pos.pitch,pos.yaw ) *
DirectX::XMMatrixTranslation( pos.x,pos.y,pos.z );
pRoot->Draw( gfx,transform ); pRoot->Draw( gfx,transform );
} }
void Model::ShowWindow( const char* windowName ) noexcept
{
windowName = windowName ? windowName : "Model";
if( ImGui::Begin( windowName ) )
{
ImGui::Columns( 2,nullptr,true );
pRoot->RenderTree();
ImGui::NextColumn();
ImGui::Text( "Orientation" );
ImGui::SliderAngle( "Roll",&pos.roll,-180.0f,180.0f );
ImGui::SliderAngle( "Pitch",&pos.pitch,-180.0f,180.0f );
ImGui::SliderAngle( "Yaw",&pos.yaw,-180.0f,180.0f );
ImGui::Text( "Position" );
ImGui::SliderFloat( "X",&pos.x,-20.0f,20.0f );
ImGui::SliderFloat( "Y",&pos.y,-20.0f,20.0f );
ImGui::SliderFloat( "Z",&pos.z,-20.0f,20.0f );
}
ImGui::End();
}
std::unique_ptr<Mesh> Model::ParseMesh( Graphics& gfx,const aiMesh& mesh ) std::unique_ptr<Mesh> Model::ParseMesh( Graphics& gfx,const aiMesh& mesh )
{ {
namespace dx = DirectX; namespace dx = DirectX;
...@@ -135,7 +177,7 @@ std::unique_ptr<Mesh> Model::ParseMesh( Graphics& gfx,const aiMesh& mesh ) ...@@ -135,7 +177,7 @@ std::unique_ptr<Mesh> Model::ParseMesh( Graphics& gfx,const aiMesh& mesh )
return std::make_unique<Mesh>( gfx,std::move( bindablePtrs ) ); return std::make_unique<Mesh>( gfx,std::move( bindablePtrs ) );
} }
std::unique_ptr<Node> Model::ParseNode( const aiNode& node ) std::unique_ptr<Node> Model::ParseNode( const aiNode& node ) noexcept
{ {
namespace dx = DirectX; namespace dx = DirectX;
const auto transform = dx::XMMatrixTranspose( dx::XMLoadFloat4x4( const auto transform = dx::XMMatrixTranspose( dx::XMLoadFloat4x4(
...@@ -150,11 +192,11 @@ std::unique_ptr<Node> Model::ParseNode( const aiNode& node ) ...@@ -150,11 +192,11 @@ std::unique_ptr<Node> Model::ParseNode( const aiNode& node )
curMeshPtrs.push_back( meshPtrs.at( meshIdx ).get() ); curMeshPtrs.push_back( meshPtrs.at( meshIdx ).get() );
} }
auto pNode = std::make_unique<Node>( std::move( curMeshPtrs ),transform ); auto pNode = std::make_unique<Node>( node.mName.C_Str(),std::move( curMeshPtrs ),transform );
for( size_t i = 0; i < node.mNumChildren; i++ ) for( size_t i = 0; i < node.mNumChildren; i++ )
{ {
pNode->AddChild( ParseNode( *node.mChildren[i] ) ); pNode->AddChild( ParseNode( *node.mChildren[i] ) );
} }
return pNode; return pNode;
} }
\ No newline at end of file
...@@ -21,11 +21,13 @@ class Node ...@@ -21,11 +21,13 @@ class Node
{ {
friend class Model; friend class Model;
public: public:
Node( std::vector<Mesh*> meshPtrs,const DirectX::XMMATRIX& transform ) noxnd; Node( const std::string& name,std::vector<Mesh*> meshPtrs,const DirectX::XMMATRIX& transform ) noxnd;
void Draw( Graphics& gfx,DirectX::FXMMATRIX accumulatedTransform ) const noxnd; void Draw( Graphics& gfx,DirectX::FXMMATRIX accumulatedTransform ) const noxnd;
void RenderTree() const noexcept;
private: private:
void AddChild( std::unique_ptr<Node> pChild ) noxnd; void AddChild( std::unique_ptr<Node> pChild ) noxnd;
private: private:
std::string name;
std::vector<std::unique_ptr<Node>> childPtrs; std::vector<std::unique_ptr<Node>> childPtrs;
std::vector<Mesh*> meshPtrs; std::vector<Mesh*> meshPtrs;
DirectX::XMFLOAT4X4 transform; DirectX::XMFLOAT4X4 transform;
...@@ -35,11 +37,21 @@ class Model ...@@ -35,11 +37,21 @@ class Model
{ {
public: public:
Model( Graphics& gfx,const std::string fileName ); Model( Graphics& gfx,const std::string fileName );
void Draw( Graphics& gfx,DirectX::FXMMATRIX transform ) const; void Draw( Graphics& gfx ) const noxnd;
void ShowWindow( const char* windowName = nullptr ) noexcept;
private: private:
static std::unique_ptr<Mesh> ParseMesh( Graphics& gfx,const aiMesh& mesh ); static std::unique_ptr<Mesh> ParseMesh( Graphics& gfx,const aiMesh& mesh );
std::unique_ptr<Node> ParseNode( const aiNode& node ); std::unique_ptr<Node> ParseNode( const aiNode& node ) noexcept;
private: private:
std::unique_ptr<Node> pRoot; std::unique_ptr<Node> pRoot;
std::vector<std::unique_ptr<Mesh>> meshPtrs; std::vector<std::unique_ptr<Mesh>> meshPtrs;
struct
{
float roll = 0.0f;
float pitch = 0.0f;
float yaw = 0.0f;
float x = 0.0f;
float y = 0.0f;
float z = 0.0f;
} pos;
}; };
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment