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

basic model node tree

parent 551cc463
......@@ -26,37 +26,26 @@ void App::DoFrame()
wnd.Gfx().SetCamera( cam.GetMatrix() );
light.Bind( wnd.Gfx(),cam.GetMatrix() );
const auto transform = dx::XMMatrixRotationRollPitchYaw( pos.roll,pos.pitch,pos.yaw ) *
dx::XMMatrixTranslation( pos.x,pos.y,pos.z );
nano.Draw( wnd.Gfx(),transform );
nano.Draw( wnd.Gfx() );
light.Draw( wnd.Gfx() );
// imgui windows
cam.SpawnControlWindow();
light.SpawnControlWindow();
ShowModelWindow();
ShowImguiDemoWindow();
nano.ShowWindow();
// present
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::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::ShowDemoWindow( &show_demo_window );
}
ImGui::End();
}
App::~App()
......
......@@ -16,7 +16,7 @@ public:
~App();
private:
void DoFrame();
void ShowModelWindow();
void ShowImguiDemoWindow();
private:
ImguiManager imgui;
Window wnd;
......@@ -25,13 +25,4 @@ private:
Camera cam;
PointLight light;
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 "imgui/imgui.h"
// Mesh
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 ) );
}
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 );
Drawable::Draw( gfx );
......@@ -35,12 +36,14 @@ DirectX::XMMATRIX Mesh::GetTransformXM() const noexcept
// 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 );
}
void Node::Draw( Graphics& gfx,DirectX::FXMMATRIX accumulatedTransform ) const noxnd
{
const auto built = DirectX::XMLoadFloat4x4( &transform ) * accumulatedTransform;
......@@ -53,12 +56,26 @@ void Node::Draw( Graphics& gfx,DirectX::FXMMATRIX accumulatedTransform ) const n
pc->Draw( gfx,built );
}
}
void Node::AddChild( std::unique_ptr<Node> pChild ) noxnd
{
assert( 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( Graphics& gfx,const std::string fileName )
......@@ -76,10 +93,35 @@ Model::Model( Graphics& gfx,const std::string fileName )
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 );
}
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 )
{
namespace dx = DirectX;
......@@ -135,7 +177,7 @@ std::unique_ptr<Mesh> Model::ParseMesh( Graphics& gfx,const aiMesh& mesh )
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;
const auto transform = dx::XMMatrixTranspose( dx::XMLoadFloat4x4(
......@@ -150,7 +192,7 @@ std::unique_ptr<Node> Model::ParseNode( const aiNode& node )
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++ )
{
pNode->AddChild( ParseNode( *node.mChildren[i] ) );
......
......@@ -21,11 +21,13 @@ class Node
{
friend class Model;
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 RenderTree() const noexcept;
private:
void AddChild( std::unique_ptr<Node> pChild ) noxnd;
private:
std::string name;
std::vector<std::unique_ptr<Node>> childPtrs;
std::vector<Mesh*> meshPtrs;
DirectX::XMFLOAT4X4 transform;
......@@ -35,11 +37,21 @@ class Model
{
public:
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:
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:
std::unique_ptr<Node> pRoot;
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