Commit 45e4a393 authored by chili's avatar chili
Browse files

static bindables (crashes, but try 1 box only)

parent b8dbcc7c
......@@ -20,75 +20,78 @@ Box::Box( Graphics& gfx,
theta( adist( rng ) ),
phi( adist( rng ) )
{
struct Vertex
if( !IsStaticInitialized() )
{
struct
struct Vertex
{
float x;
float y;
float z;
} pos;
};
const std::vector<Vertex> vertices =
{
{ -1.0f,-1.0f,-1.0f },
{ 1.0f,-1.0f,-1.0f },
{ -1.0f,1.0f,-1.0f },
{ 1.0f,1.0f,-1.0f },
{ -1.0f,-1.0f,1.0f },
{ 1.0f,-1.0f,1.0f },
{ -1.0f,1.0f,1.0f },
{ 1.0f,1.0f,1.0f },
};
AddBind( std::make_unique<VertexBuffer>( gfx,vertices ) );
struct
{
float x;
float y;
float z;
} pos;
};
const std::vector<Vertex> vertices =
{
{ -1.0f,-1.0f,-1.0f },
{ 1.0f,-1.0f,-1.0f },
{ -1.0f,1.0f,-1.0f },
{ 1.0f,1.0f,-1.0f },
{ -1.0f,-1.0f,1.0f },
{ 1.0f,-1.0f,1.0f },
{ -1.0f,1.0f,1.0f },
{ 1.0f,1.0f,1.0f },
};
AddStaticBind( std::make_unique<VertexBuffer>( gfx,vertices ) );
auto pvs = std::make_unique<VertexShader>( gfx,L"VertexShader.cso" );
auto pvsbc = pvs->GetBytecode();
AddBind( std::move( pvs ) );
auto pvs = std::make_unique<VertexShader>( gfx,L"VertexShader.cso" );
auto pvsbc = pvs->GetBytecode();
AddStaticBind( std::move( pvs ) );
AddBind( std::make_unique<PixelShader>( gfx,L"PixelShader.cso" ) );
AddStaticBind( std::make_unique<PixelShader>( gfx,L"PixelShader.cso" ) );
const std::vector<unsigned short> indices =
{
0,2,1, 2,3,1,
1,3,5, 3,7,5,
2,6,3, 3,6,7,
4,5,7, 4,7,6,
0,4,2, 2,4,6,
0,1,4, 1,5,4
};
AddIndexBuffer( std::make_unique<IndexBuffer>( gfx,indices ) );
const std::vector<unsigned short> indices =
{
0,2,1, 2,3,1,
1,3,5, 3,7,5,
2,6,3, 3,6,7,
4,5,7, 4,7,6,
0,4,2, 2,4,6,
0,1,4, 1,5,4
};
AddStaticIndexBuffer( std::make_unique<IndexBuffer>( gfx,indices ) );
struct ConstantBuffer2
{
struct
struct ConstantBuffer2
{
float r;
float g;
float b;
float a;
} face_colors[6];
};
const ConstantBuffer2 cb2 =
{
struct
{
float r;
float g;
float b;
float a;
} face_colors[6];
};
const ConstantBuffer2 cb2 =
{
{ 1.0f,0.0f,1.0f },
{ 1.0f,0.0f,0.0f },
{ 0.0f,1.0f,0.0f },
{ 0.0f,0.0f,1.0f },
{ 1.0f,1.0f,0.0f },
{ 0.0f,1.0f,1.0f },
}
};
AddBind( std::make_unique<PixelConstantBuffer<ConstantBuffer2>>( gfx,cb2 ) );
{
{ 1.0f,0.0f,1.0f },
{ 1.0f,0.0f,0.0f },
{ 0.0f,1.0f,0.0f },
{ 0.0f,0.0f,1.0f },
{ 1.0f,1.0f,0.0f },
{ 0.0f,1.0f,1.0f },
}
};
AddStaticBind( std::make_unique<PixelConstantBuffer<ConstantBuffer2>>( gfx,cb2 ) );
const std::vector<D3D11_INPUT_ELEMENT_DESC> ied =
{
{ "Position",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
};
AddBind( std::make_unique<InputLayout>( gfx,ied,pvsbc ) );
const std::vector<D3D11_INPUT_ELEMENT_DESC> ied =
{
{ "Position",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
};
AddStaticBind( std::make_unique<InputLayout>( gfx,ied,pvsbc ) );
AddBind( std::make_unique<Topology>( gfx,D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ) );
AddStaticBind( std::make_unique<Topology>( gfx,D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ) );
}
AddBind( std::make_unique<TransformCbuf>( gfx,*this ) );
}
......
#pragma once
#include "Drawable.h"
#include "DrawableBase.h"
class Box : public Drawable
class Box : public DrawableBase<Box>
{
public:
Box( Graphics& gfx,std::mt19937& rng,
......
......@@ -10,6 +10,10 @@ void Drawable::Draw( Graphics& gfx ) const noexcept(!IS_DEBUG)
{
b->Bind( gfx );
}
for( auto& b : GetStaticBinds() )
{
b->Bind( gfx );
}
gfx.DrawIndexed( pIndexBuffer->GetCount() );
}
......@@ -19,9 +23,9 @@ void Drawable::AddBind( std::unique_ptr<Bindable> bind ) noexcept(!IS_DEBUG)
binds.push_back( std::move( bind ) );
}
void Drawable::AddIndexBuffer( std::unique_ptr<IndexBuffer> ibuf ) noexcept
void Drawable::AddIndexBuffer( std::unique_ptr<IndexBuffer> ibuf ) noexcept(!IS_DEBUG)
{
assert( "Attempting to add index buffer a second time" && pIndexBuffer == nullptr );
pIndexBuffer = ibuf.get();
binds.push_back( std::move( ibuf ) );
}
}
\ No newline at end of file
......@@ -6,6 +6,8 @@ class Bindable;
class Drawable
{
template<class T>
friend class DrawableBase;
public:
Drawable() = default;
Drawable( const Drawable& ) = delete;
......@@ -13,9 +15,11 @@ public:
void Draw( Graphics& gfx ) const noexcept(!IS_DEBUG);
virtual void Update( float dt ) noexcept = 0;
void AddBind( std::unique_ptr<Bindable> bind ) noexcept(!IS_DEBUG);
void AddIndexBuffer( std::unique_ptr<class IndexBuffer> ibuf ) noexcept;
void AddIndexBuffer( std::unique_ptr<class IndexBuffer> ibuf ) noexcept(!IS_DEBUG);
virtual ~Drawable() = default;
private:
const IndexBuffer* pIndexBuffer = nullptr;
virtual const std::vector<std::unique_ptr<Bindable>>& GetStaticBinds() const noexcept = 0;
private:
const class IndexBuffer* pIndexBuffer = nullptr;
std::vector<std::unique_ptr<Bindable>> binds;
};
\ No newline at end of file
#pragma once
#include "Drawable.h"
#include "IndexBuffer.h"
template<class T>
class DrawableBase : public Drawable
{
public:
bool IsStaticInitialized() const noexcept
{
return !staticBinds.empty();
}
void AddStaticBind( std::unique_ptr<Bindable> bind ) noexcept(!IS_DEBUG)
{
assert( "*Must* use AddIndexBuffer to bind index buffer" && typeid(*bind) != typeid(IndexBuffer) );
staticBinds.push_back( std::move( bind ) );
}
void AddStaticIndexBuffer( std::unique_ptr<IndexBuffer> ibuf ) noexcept(!IS_DEBUG)
{
assert( pIndexBuffer == nullptr );
pIndexBuffer = ibuf.get();
staticBinds.push_back( std::move( ibuf ) );
}
private:
const std::vector<std::unique_ptr<Bindable>>& GetStaticBinds() const noexcept override
{
return staticBinds;
}
private:
static std::vector<std::unique_ptr<Bindable>> staticBinds;
};
template<class T>
std::vector<std::unique_ptr<Bindable>> DrawableBase<T>::staticBinds;
\ No newline at end of file
......@@ -179,6 +179,7 @@
<ClInclude Include="ChiliWin.h" />
<ClInclude Include="ConstantBuffers.h" />
<ClInclude Include="Drawable.h" />
<ClInclude Include="DrawableBase.h" />
<ClInclude Include="dxerr.h" />
<ClInclude Include="DxgiInfoManager.h" />
<ClInclude Include="Graphics.h" />
......
......@@ -179,6 +179,9 @@
<ClInclude Include="Box.h">
<Filter>Header Files\Drawable</Filter>
</ClInclude>
<ClInclude Include="DrawableBase.h">
<Filter>Header Files\Drawable</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="hw3d.rc">
......
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