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

controlling box material attributes

parent be6a14ed
......@@ -73,6 +73,15 @@ App::App()
drawables.reserve( nDrawables );
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 ) );
}
......@@ -101,6 +110,8 @@ void App::DoFrame()
// imgui windows to control camera and light
cam.SpawnControlWindow();
light.SpawnControlWindow();
// imgui window to adjust box instance parameters
boxes.front()->SpawnControlWindow( 69,wnd.Gfx() );
// present
wnd.Gfx().EndFrame();
......
......@@ -19,6 +19,7 @@ private:
Window wnd;
ChiliTimer timer;
std::vector<std::unique_ptr<class Drawable>> drawables;
std::vector<class Box*> boxes;
float speed_factor = 1.0f;
Camera cam;
PointLight light;
......
......@@ -2,7 +2,7 @@
#include "BindableBase.h"
#include "GraphicsThrowMacros.h"
#include "Cube.h"
#include "imgui/imgui.h"
Box::Box( Graphics& gfx,
std::mt19937& rng,
......@@ -53,15 +53,8 @@ Box::Box( Graphics& gfx,
AddBind( std::make_unique<TransformCbuf>( gfx,*this ) );
struct PSMaterialConstant
{
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 ) );
materialConstants.color = material;
AddBind( std::make_unique<MaterialCbuf>( gfx,materialConstants,1u ) );
// model deformation transform (per instance, not stored as bind)
dx::XMStoreFloat3x3(
......@@ -75,3 +68,29 @@ DirectX::XMMATRIX Box::GetTransformXM() const noexcept
namespace dx = DirectX;
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
#include "TestObject.h"
#include "ConstantBuffers.h"
class Box : public TestObject<Box>
{
......@@ -12,6 +13,18 @@ public:
std::uniform_real_distribution<float>& bdist,
DirectX::XMFLOAT3 material );
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:
// model transform
DirectX::XMFLOAT3X3 mt;
......
......@@ -16,6 +16,18 @@ public:
virtual void Update( float dt ) noexcept = 0;
virtual ~Drawable() = default;
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 AddIndexBuffer( std::unique_ptr<class IndexBuffer> ibuf ) noexcept(!IS_DEBUG);
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