Commit 38ba0d3d authored by chili's avatar chili
Browse files

skinned cube

parent 44b02568
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "Pyramid.h" #include "Pyramid.h"
#include "Box.h" #include "Box.h"
#include "Sheet.h" #include "Sheet.h"
#include "SkinnedBox.h"
#include <memory> #include <memory>
#include <algorithm> #include <algorithm>
#include "ChiliMath.h" #include "ChiliMath.h"
...@@ -46,6 +47,11 @@ App::App() ...@@ -46,6 +47,11 @@ App::App()
gfx,rng,adist,ddist, gfx,rng,adist,ddist,
odist,rdist odist,rdist
); );
case 4:
return std::make_unique<SkinnedBox>(
gfx,rng,adist,ddist,
odist,rdist
);
default: default:
assert( false && "bad drawable type in factory" ); assert( false && "bad drawable type in factory" );
return {}; return {};
...@@ -61,12 +67,11 @@ App::App() ...@@ -61,12 +67,11 @@ App::App()
std::uniform_real_distribution<float> bdist{ 0.4f,3.0f }; std::uniform_real_distribution<float> bdist{ 0.4f,3.0f };
std::uniform_int_distribution<int> latdist{ 5,20 }; std::uniform_int_distribution<int> latdist{ 5,20 };
std::uniform_int_distribution<int> longdist{ 10,40 }; std::uniform_int_distribution<int> longdist{ 10,40 };
std::uniform_int_distribution<int> typedist{ 0,3 }; std::uniform_int_distribution<int> typedist{ 0,4 };
}; };
Factory f( wnd.Gfx() );
drawables.reserve( nDrawables ); drawables.reserve( nDrawables );
std::generate_n( std::back_inserter( drawables ),nDrawables,f ); std::generate_n( std::back_inserter( drawables ),nDrawables,Factory{ wnd.Gfx() } );
wnd.Gfx().SetProjection( DirectX::XMMatrixPerspectiveLH( 1.0f,3.0f / 4.0f,0.5f,40.0f ) ); wnd.Gfx().SetProjection( DirectX::XMMatrixPerspectiveLH( 1.0f,3.0f / 4.0f,0.5f,40.0f ) );
} }
......
#pragma once #pragma once
#include "IndexedTriangleList.h" #include "IndexedTriangleList.h"
#include <DirectXMath.h> #include <DirectXMath.h>
#include <initializer_list>
class Cube class Cube
{ {
...@@ -12,23 +13,18 @@ public: ...@@ -12,23 +13,18 @@ public:
constexpr float side = 1.0f / 2.0f; constexpr float side = 1.0f / 2.0f;
std::vector<dx::XMFLOAT3> vertices; std::vector<V> vertices( 8 );
vertices.emplace_back( -side,-side,-side ); // 0 vertices[0].pos = { -side,-side,-side };
vertices.emplace_back( side,-side,-side ); // 1 vertices[1].pos = { side,-side,-side };
vertices.emplace_back( -side,side,-side ); // 2 vertices[2].pos = { -side,side,-side };
vertices.emplace_back( side,side,-side ); // 3 vertices[3].pos = { side,side,-side };
vertices.emplace_back( -side,-side,side ); // 4 vertices[4].pos = { -side,-side,side };
vertices.emplace_back( side,-side,side ); // 5 vertices[5].pos = { side,-side,side };
vertices.emplace_back( -side,side,side ); // 6 vertices[6].pos = { -side,side,side };
vertices.emplace_back( side,side,side ); // 7 vertices[7].pos = { side,side,side };
std::vector<V> verts( vertices.size() );
for( size_t i = 0; i < vertices.size(); i++ )
{
verts[i].pos = vertices[i];
}
return{ return{
std::move( verts ),{ std::move( vertices ),{
0,2,1, 2,3,1, 0,2,1, 2,3,1,
1,3,5, 3,7,5, 1,3,5, 3,7,5,
2,6,3, 3,6,7, 2,6,3, 3,6,7,
...@@ -38,4 +34,53 @@ public: ...@@ -38,4 +34,53 @@ public:
} }
}; };
} }
template<class V>
static IndexedTriangleList<V> MakeSkinned()
{
namespace dx = DirectX;
constexpr float side = 1.0f / 2.0f;
std::vector<V> vertices( 14 );
vertices[0].pos = { -side,-side,-side };
vertices[0].tex = { 2.0f / 3.0f,0.0f / 4.0f };
vertices[1].pos = { side,-side,-side };
vertices[1].tex = { 1.0f / 3.0f,0.0f / 4.0f };
vertices[2].pos = { -side,side,-side };
vertices[2].tex = { 2.0f / 3.0f,1.0f / 4.0f };
vertices[3].pos = { side,side,-side };
vertices[3].tex = { 1.0f / 3.0f,1.0f / 4.0f };
vertices[4].pos = { -side,-side,side };
vertices[4].tex = { 2.0f / 3.0f,3.0f / 4.0f };
vertices[5].pos = { side,-side,side };
vertices[5].tex = { 1.0f / 3.0f,3.0f / 4.0f };
vertices[6].pos = { -side,side,side };
vertices[6].tex = { 2.0f / 3.0f,2.0f / 4.0f };
vertices[7].pos = { side,side,side };
vertices[7].tex = { 1.0f / 3.0f,2.0f / 4.0f };
vertices[8].pos = { -side,-side,-side };
vertices[8].tex = { 2.0f / 3.0f,4.0f / 4.0f };
vertices[9].pos = { side,-side,-side };
vertices[9].tex = { 1.0f / 3.0f,4.0f / 4.0f };
vertices[10].pos = { -side,-side,-side };
vertices[10].tex = { 3.0f / 3.0f,1.0f / 4.0f };
vertices[11].pos = { -side,-side,side };
vertices[11].tex = { 3.0f / 3.0f,2.0f / 4.0f };
vertices[12].pos = { side,-side,-side };
vertices[12].tex = { 0.0f / 3.0f,1.0f / 4.0f };
vertices[13].pos = { side,-side,side };
vertices[13].tex = { 0.0f / 3.0f,2.0f / 4.0f };
return{
std::move( vertices ),{
0,2,1, 2,3,1,
4,8,5, 5,8,9,
2,6,3, 3,6,7,
4,5,7, 4,7,6,
2,10,11, 2,11,6,
12,3,7, 12,7,13
}
};
}
}; };
\ No newline at end of file
#include "SkinnedBox.h"
#include "BindableBase.h"
#include "GraphicsThrowMacros.h"
#include "Cube.h"
#include "Surface.h"
#include "Texture.h"
SkinnedBox::SkinnedBox( Graphics& gfx,
std::mt19937& rng,
std::uniform_real_distribution<float>& adist,
std::uniform_real_distribution<float>& ddist,
std::uniform_real_distribution<float>& odist,
std::uniform_real_distribution<float>& rdist )
:
r( rdist( rng ) ),
droll( ddist( rng ) ),
dpitch( ddist( rng ) ),
dyaw( ddist( rng ) ),
dphi( odist( rng ) ),
dtheta( odist( rng ) ),
dchi( odist( rng ) ),
chi( adist( rng ) ),
theta( adist( rng ) ),
phi( adist( rng ) )
{
namespace dx = DirectX;
if( !IsStaticInitialized() )
{
struct Vertex
{
dx::XMFLOAT3 pos;
struct
{
float u;
float v;
} tex;
};
const auto model = Cube::MakeSkinned<Vertex>();
AddStaticBind( std::make_unique<VertexBuffer>( gfx,model.vertices ) );
AddStaticBind( std::make_unique<Texture>( gfx,Surface::FromFile( "Images\\cube.png" ) ) );
auto pvs = std::make_unique<VertexShader>( gfx,L"TextureVS.cso" );
auto pvsbc = pvs->GetBytecode();
AddStaticBind( std::move( pvs ) );
AddStaticBind( std::make_unique<PixelShader>( gfx,L"TexturePS.cso" ) );
AddStaticIndexBuffer( std::make_unique<IndexBuffer>( gfx,model.indices ) );
const std::vector<D3D11_INPUT_ELEMENT_DESC> ied =
{
{ "Position",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
{ "TexCoord",0,DXGI_FORMAT_R32G32_FLOAT,0,12,D3D11_INPUT_PER_VERTEX_DATA,0 },
};
AddStaticBind( std::make_unique<InputLayout>( gfx,ied,pvsbc ) );
AddStaticBind( std::make_unique<Topology>( gfx,D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ) );
}
else
{
SetIndexFromStatic();
}
AddBind( std::make_unique<TransformCbuf>( gfx,*this ) );
}
void SkinnedBox::Update( float dt ) noexcept
{
roll += droll * dt;
pitch += dpitch * dt;
yaw += dyaw * dt;
theta += dtheta * dt;
phi += dphi * dt;
chi += dchi * dt;
}
DirectX::XMMATRIX SkinnedBox::GetTransformXM() const noexcept
{
namespace dx = DirectX;
return dx::XMMatrixRotationRollPitchYaw( pitch,yaw,roll ) *
dx::XMMatrixTranslation( r,0.0f,0.0f ) *
dx::XMMatrixRotationRollPitchYaw( theta,phi,chi ) *
dx::XMMatrixTranslation( 0.0f,0.0f,20.0f );
}
#pragma once
#pragma once
#include "DrawableBase.h"
class SkinnedBox : public DrawableBase<SkinnedBox>
{
public:
SkinnedBox( Graphics& gfx,std::mt19937& rng,
std::uniform_real_distribution<float>& adist,
std::uniform_real_distribution<float>& ddist,
std::uniform_real_distribution<float>& odist,
std::uniform_real_distribution<float>& rdist );
void Update( float dt ) noexcept override;
DirectX::XMMATRIX GetTransformXM() const noexcept override;
private:
// positional
float r;
float roll = 0.0f;
float pitch = 0.0f;
float yaw = 0.0f;
float theta;
float phi;
float chi;
// speed (delta/s)
float droll;
float dpitch;
float dyaw;
float dtheta;
float dphi;
float dchi;
};
\ No newline at end of file
...@@ -166,6 +166,7 @@ ...@@ -166,6 +166,7 @@
<ClCompile Include="Pyramid.cpp" /> <ClCompile Include="Pyramid.cpp" />
<ClCompile Include="Sampler.cpp" /> <ClCompile Include="Sampler.cpp" />
<ClCompile Include="Sheet.cpp" /> <ClCompile Include="Sheet.cpp" />
<ClCompile Include="SkinnedBox.cpp" />
<ClCompile Include="Surface.cpp" /> <ClCompile Include="Surface.cpp" />
<ClCompile Include="Texture.cpp" /> <ClCompile Include="Texture.cpp" />
<ClCompile Include="Topology.cpp" /> <ClCompile Include="Topology.cpp" />
...@@ -208,6 +209,7 @@ ...@@ -208,6 +209,7 @@
<ClInclude Include="resource.h" /> <ClInclude Include="resource.h" />
<ClInclude Include="Sampler.h" /> <ClInclude Include="Sampler.h" />
<ClInclude Include="Sheet.h" /> <ClInclude Include="Sheet.h" />
<ClInclude Include="SkinnedBox.h" />
<ClInclude Include="Sphere.h" /> <ClInclude Include="Sphere.h" />
<ClInclude Include="Surface.h" /> <ClInclude Include="Surface.h" />
<ClInclude Include="Texture.h" /> <ClInclude Include="Texture.h" />
......
...@@ -123,6 +123,9 @@ ...@@ -123,6 +123,9 @@
<ClCompile Include="Sampler.cpp"> <ClCompile Include="Sampler.cpp">
<Filter>Source Files\Bindable</Filter> <Filter>Source Files\Bindable</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="SkinnedBox.cpp">
<Filter>Source Files\Drawable</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="WindowsMessageMap.h"> <ClInclude Include="WindowsMessageMap.h">
...@@ -248,6 +251,9 @@ ...@@ -248,6 +251,9 @@
<ClInclude Include="Sampler.h"> <ClInclude Include="Sampler.h">
<Filter>Header Files\Bindable</Filter> <Filter>Header Files\Bindable</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="SkinnedBox.h">
<Filter>Header Files\Drawable</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="hw3d.rc"> <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