Commit 8e18159c authored by chili's avatar chili
Browse files

controlling box material attributes

parent be6a14ed
...@@ -73,6 +73,15 @@ App::App() ...@@ -73,6 +73,15 @@ App::App()
drawables.reserve( nDrawables ); drawables.reserve( nDrawables );
std::generate_n( std::back_inserter( drawables ),nDrawables,Factory{ wnd.Gfx() } ); std::generate_n( std::back_inserter( drawables ),nDrawables,Factory{ wnd.Gfx() } );
// init box pointers for editing instance parameters
for( auto& pd : drawables )
{
if( auto pb = dynamic_cast<Box*>(pd.get()) )
{
boxes.push_back( pb );
}
}
wnd.Gfx().SetProjection( dx::XMMatrixPerspectiveLH( 1.0f,3.0f / 4.0f,0.5f,40.0f ) ); wnd.Gfx().SetProjection( dx::XMMatrixPerspectiveLH( 1.0f,3.0f / 4.0f,0.5f,40.0f ) );
} }
...@@ -101,6 +110,8 @@ void App::DoFrame() ...@@ -101,6 +110,8 @@ void App::DoFrame()
// imgui windows to control camera and light // imgui windows to control camera and light
cam.SpawnControlWindow(); cam.SpawnControlWindow();
light.SpawnControlWindow(); light.SpawnControlWindow();
// imgui window to adjust box instance parameters
boxes.front()->SpawnControlWindow( 69,wnd.Gfx() );
// present // present
wnd.Gfx().EndFrame(); wnd.Gfx().EndFrame();
......
...@@ -19,6 +19,7 @@ private: ...@@ -19,6 +19,7 @@ private:
Window wnd; Window wnd;
ChiliTimer timer; ChiliTimer timer;
std::vector<std::unique_ptr<class Drawable>> drawables; std::vector<std::unique_ptr<class Drawable>> drawables;
std::vector<class Box*> boxes;
float speed_factor = 1.0f; float speed_factor = 1.0f;
Camera cam; Camera cam;
PointLight light; PointLight light;
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#include "BindableBase.h" #include "BindableBase.h"
#include "GraphicsThrowMacros.h" #include "GraphicsThrowMacros.h"
#include "Cube.h" #include "Cube.h"
#include "imgui/imgui.h"
Box::Box( Graphics& gfx, Box::Box( Graphics& gfx,
std::mt19937& rng, std::mt19937& rng,
...@@ -53,15 +53,8 @@ Box::Box( Graphics& gfx, ...@@ -53,15 +53,8 @@ Box::Box( Graphics& gfx,
AddBind( std::make_unique<TransformCbuf>( gfx,*this ) ); AddBind( std::make_unique<TransformCbuf>( gfx,*this ) );
struct PSMaterialConstant materialConstants.color = material;
{ AddBind( std::make_unique<MaterialCbuf>( gfx,materialConstants,1u ) );
dx::XMFLOAT3 color;
float specularIntensity = 0.6f;
float specularPower = 30.0f;
float padding[3];
} colorConst;
colorConst.color = material;
AddBind( std::make_unique<PixelConstantBuffer<PSMaterialConstant>>( gfx,colorConst,1u ) );
// model deformation transform (per instance, not stored as bind) // model deformation transform (per instance, not stored as bind)
dx::XMStoreFloat3x3( dx::XMStoreFloat3x3(
...@@ -75,3 +68,29 @@ DirectX::XMMATRIX Box::GetTransformXM() const noexcept ...@@ -75,3 +68,29 @@ DirectX::XMMATRIX Box::GetTransformXM() const noexcept
namespace dx = DirectX; namespace dx = DirectX;
return dx::XMLoadFloat3x3( &mt ) * TestObject::GetTransformXM(); return dx::XMLoadFloat3x3( &mt ) * TestObject::GetTransformXM();
} }
void Box::SpawnControlWindow( int id,Graphics& gfx ) noexcept
{
using namespace std::string_literals;
bool dirty = false;
if( ImGui::Begin( ("Box "s + std::to_string( id )).c_str() ) )
{
dirty = dirty || ImGui::ColorEdit3( "Material Color",&materialConstants.color.x );
dirty = dirty || ImGui::SliderFloat( "Specular Intensity",&materialConstants.specularIntensity,0.05f,4.0f,"%.2f",2 );
dirty = dirty || ImGui::SliderFloat( "Specular Power",&materialConstants.specularPower,1.0f,200.0f,"%.2f",2 );
}
ImGui::End();
if( dirty )
{
SyncMaterial( gfx );
}
}
void Box::SyncMaterial( Graphics& gfx ) noexcept(!IS_DEBUG)
{
auto pConstPS = QueryBindable<MaterialCbuf>();
assert( pConstPS != nullptr );
pConstPS->Update( gfx,materialConstants );
}
#pragma once #pragma once
#include "TestObject.h" #include "TestObject.h"
#include "ConstantBuffers.h"
class Box : public TestObject<Box> class Box : public TestObject<Box>
{ {
...@@ -12,6 +13,18 @@ public: ...@@ -12,6 +13,18 @@ public:
std::uniform_real_distribution<float>& bdist, std::uniform_real_distribution<float>& bdist,
DirectX::XMFLOAT3 material ); DirectX::XMFLOAT3 material );
DirectX::XMMATRIX GetTransformXM() const noexcept override; DirectX::XMMATRIX GetTransformXM() const noexcept override;
void SpawnControlWindow( int id,Graphics& gfx ) noexcept;
private:
void SyncMaterial( Graphics& gfx ) noexcept(!IS_DEBUG);
private:
struct PSMaterialConstant
{
DirectX::XMFLOAT3 color;
float specularIntensity = 0.6f;
float specularPower = 30.0f;
float padding[3];
} materialConstants;
using MaterialCbuf = PixelConstantBuffer<PSMaterialConstant>;
private: private:
// model transform // model transform
DirectX::XMFLOAT3X3 mt; DirectX::XMFLOAT3X3 mt;
......
...@@ -16,6 +16,18 @@ public: ...@@ -16,6 +16,18 @@ public:
virtual void Update( float dt ) noexcept = 0; virtual void Update( float dt ) noexcept = 0;
virtual ~Drawable() = default; virtual ~Drawable() = default;
protected: protected:
template<class T>
T* QueryBindable() noexcept
{
for( auto& pb : binds )
{
if( auto pt = dynamic_cast<T*>(pb.get()) )
{
return pt;
}
}
return nullptr;
}
void AddBind( std::unique_ptr<Bindable> bind ) noexcept(!IS_DEBUG); void AddBind( std::unique_ptr<Bindable> bind ) noexcept(!IS_DEBUG);
void AddIndexBuffer( std::unique_ptr<class IndexBuffer> ibuf ) noexcept(!IS_DEBUG); void AddIndexBuffer( std::unique_ptr<class IndexBuffer> ibuf ) noexcept(!IS_DEBUG);
private: private:
......
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