Commit 0e8bebbf authored by chili's avatar chili
Browse files

cylindrical problems

parent f61cdfde
#include "App.h"
#include "Box.h"
#include "Cylinder.h"
#include <memory>
#include <algorithm>
#include "ChiliMath.h"
......@@ -26,20 +27,35 @@ App::App()
std::unique_ptr<Drawable> operator()()
{
const DirectX::XMFLOAT3 mat = { cdist( rng ),cdist( rng ),cdist( rng ) };
return std::make_unique<Box>(
gfx,rng,adist,ddist,
odist,rdist,bdist,mat
);
switch( sdist( rng ) )
{
case 0:
return std::make_unique<Box>(
gfx,rng,adist,ddist,
odist,rdist,bdist,mat
);
case 1:
return std::make_unique<Cylinder>(
gfx,rng,adist,ddist,odist,
rdist,bdist,tdist
);
default:
assert( false && "impossible drawable option in factory" );
return {};
}
}
private:
Graphics& gfx;
std::mt19937 rng{ std::random_device{}() };
std::uniform_int_distribution<int> sdist{ 0,1 };
std::uniform_real_distribution<float> adist{ 0.0f,PI * 2.0f };
std::uniform_real_distribution<float> ddist{ 0.0f,PI * 0.5f };
std::uniform_real_distribution<float> odist{ 0.0f,PI * 0.08f };
std::uniform_real_distribution<float> rdist{ 6.0f,20.0f };
std::uniform_real_distribution<float> bdist{ 0.4f,3.0f };
std::uniform_real_distribution<float> cdist{ 0.0f,1.0f };
std::uniform_int_distribution<int> tdist{ 3,30 };
};
drawables.reserve( nDrawables );
......
#include "Cylinder.h"
#include "Prism.h"
#include "BindableBase.h"
Cylinder::Cylinder( 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,
std::uniform_real_distribution<float>& bdist,
std::uniform_int_distribution<int>& tdist )
:
TestObject( gfx,rng,adist,ddist,odist,rdist )
{
namespace dx = DirectX;
if( !IsStaticInitialized() )
{
struct Vertex
{
dx::XMFLOAT3 pos;
dx::XMFLOAT3 n;
};
auto model = Prism::MakeTesselatedIndependentCapNormals<Vertex>( tdist( rng ) );
AddStaticBind( std::make_unique<VertexBuffer>( gfx,model.vertices ) );
auto pvs = std::make_unique<VertexShader>( gfx,L"PhongVS.cso" );
auto pvsbc = pvs->GetBytecode();
AddStaticBind( std::move( pvs ) );
AddStaticBind( std::make_unique<PixelShader>( gfx,L"IndexedPhongPS.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 },
{ "Normal",0,DXGI_FORMAT_R32G32B32_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 ) );
struct PSMaterialConstant
{
alignas(16) dx::XMFLOAT3A colors[6] = {
{1.0f,0.0f,0.0f},
{0.0f,1.0f,0.0f},
{0.0f,0.0f,1.0f},
{1.0f,1.0f,0.0f},
{1.0f,0.0f,1.0f},
{0.0f,1.0f,1.0f},
};
float specularIntensity = 0.6f;
float specularPower = 30.0f;
} matConst;
AddStaticBind( std::make_unique<PixelConstantBuffer<PSMaterialConstant>>( gfx,matConst,1u ) );
}
else
{
SetIndexFromStatic();
}
AddBind( std::make_unique<TransformCbuf>( gfx,*this ) );
}
#pragma once
#include "TestObject.h"
class Cylinder : public TestObject<Cylinder>
{
public:
Cylinder( 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,
std::uniform_real_distribution<float>& bdist,
std::uniform_int_distribution<int>& tdist );
};
\ No newline at end of file
......@@ -2,7 +2,6 @@
#include "GraphicsThrowMacros.h"
#include "IndexBuffer.h"
#include <cassert>
#include <typeinfo>
void Drawable::Draw( Graphics& gfx ) const noexcept(!IS_DEBUG)
{
......
cbuffer LightCBuf
{
float3 lightPos;
float3 ambient;
float3 diffuseColor;
float diffuseIntensity;
float attConst;
float attLin;
float attQuad;
};
cbuffer ObjectCBuf
{
float3 materialColors[6];
float specularIntensity;
float specularPower;
};
float4 main( float3 worldPos : Position,float3 n : Normal,uint tid : SV_PrimitiveID ) : SV_Target
{
// fragment to light vector data
const float3 vToL = lightPos - worldPos;
const float distToL = length( vToL );
const float3 dirToL = vToL / distToL;
// attenuation
const float att = 1.0f / (attConst + attLin * distToL + attQuad * (distToL * distToL));
// diffuse intensity
const float3 diffuse = diffuseColor * diffuseIntensity * att * max( 0.0f,dot( dirToL,n ) );
// reflected light vector
const float3 w = n * dot( vToL,n );
const float3 r = w * 2.0f - vToL;
// calculate specular intensity based on angle between viewing vector and reflection vector, narrow with power function
const float3 specular = att * (diffuseColor * diffuseIntensity) * specularIntensity * pow( max( 0.0f,dot( normalize( -r ),normalize( worldPos ) ) ),specularPower );
// final color
return float4(saturate( (diffuse + ambient + specular) * materialColors[tid % 6] ),1.0f);
}
\ No newline at end of file
......@@ -80,6 +80,117 @@ public:
return { std::move( vertices ),std::move( indices ) };
}
template<class V>
static IndexedTriangleList<V> MakeTesselatedIndependentCapNormals( int longDiv )
{
namespace dx = DirectX;
assert( longDiv >= 3 );
const auto base = dx::XMVectorSet( 1.0f,0.0f,-1.0f,0.0f );
const auto offset = dx::XMVectorSet( 0.0f,0.0f,2.0f,0.0f );
const float longitudeAngle = 2.0f * PI / longDiv;
std::vector<V> vertices;
// near center
const auto iCenterNear = (unsigned short)vertices.size();
vertices.emplace_back();
vertices.back().pos = { 0.0f,0.0f,-1.0f };
vertices.back().n = { 0.0f,0.0f,-1.0f };
// near base vertices
const auto iBaseNear = (unsigned short)vertices.size();
for( int iLong = 0; iLong < longDiv; iLong++ )
{
vertices.emplace_back();
auto v = dx::XMVector3Transform(
base,
dx::XMMatrixRotationZ( longitudeAngle * iLong )
);
dx::XMStoreFloat3( &vertices.back().pos,v );
vertices.back().n = { 0.0f,0.0f,-1.0f };
}
// far center
const auto iCenterFar = (unsigned short)vertices.size();
vertices.emplace_back();
vertices.back().pos = { 0.0f,0.0f,1.0f };
vertices.back().n = { 0.0f,0.0f,1.0f };
// far base vertices
const auto iBaseFar = (unsigned short)vertices.size();
for( int iLong = 0; iLong < longDiv; iLong++ )
{
vertices.emplace_back();
auto v = dx::XMVector3Transform(
base,
dx::XMMatrixRotationZ( longitudeAngle * iLong )
);
v = dx::XMVectorAdd( v,offset );
dx::XMStoreFloat3( &vertices.back().pos,v );
vertices.back().n = { 0.0f,0.0f,1.0f };
}
// fusilage vertices
const auto iFusilage = (unsigned short)vertices.size();
for( int iLong = 0; iLong < longDiv; iLong++ )
{
// near base
{
vertices.emplace_back();
auto v = dx::XMVector3Transform(
base,
dx::XMMatrixRotationZ( longitudeAngle * iLong )
);
dx::XMStoreFloat3( &vertices.back().pos,v );
vertices.back().n = { vertices.back().pos.x,vertices.back().pos.y,0.0f };
}
// far base
{
vertices.emplace_back();
auto v = dx::XMVector3Transform(
base,
dx::XMMatrixRotationZ( longitudeAngle * iLong )
);
v = dx::XMVectorAdd( v,offset );
dx::XMStoreFloat3( &vertices.back().pos,v );
vertices.back().n = { vertices.back().pos.x,vertices.back().pos.y,0.0f };
}
}
std::vector<unsigned short> indices;
// near base indices
for( unsigned short iLong = 0; iLong < longDiv; iLong++ )
{
const auto i = iLong * 2;
const auto mod = longDiv * 2;
// near
indices.push_back( i + iBaseNear );
indices.push_back( iCenterNear );
indices.push_back( (i + 2) % mod + iBaseNear );
}
// far base indices
for( unsigned short iLong = 0; iLong < longDiv; iLong++ )
{
const auto i = iLong * 2;
const auto mod = longDiv * 2;
// far
indices.push_back( iCenterFar );
indices.push_back( i + 1 + iBaseFar );
indices.push_back( (i + 3) % mod + iBaseFar );
}
// fusilage indices
for( unsigned short iLong = 0; iLong < longDiv; iLong++ )
{
const auto i = iLong * 2;
const auto mod = longDiv * 2;
indices.push_back( i + iFusilage );
indices.push_back( (i + 2) % mod + iFusilage );
indices.push_back( i + 1 + iFusilage );
indices.push_back( (i + 2) % mod + iFusilage );
indices.push_back( (i + 3) % mod + iFusilage );
indices.push_back( i + 1 + iFusilage );
}
return { std::move( vertices ),std::move( indices ) };
}
template<class V>
static IndexedTriangleList<V> Make()
{
return MakeTesselated<V>( 24 );
......
......@@ -153,6 +153,7 @@
<ClCompile Include="Camera.cpp" />
<ClCompile Include="ChiliException.cpp" />
<ClCompile Include="ChiliTimer.cpp" />
<ClCompile Include="Cylinder.cpp" />
<ClCompile Include="Drawable.cpp" />
<ClCompile Include="dxerr.cpp" />
<ClCompile Include="DxgiInfoManager.cpp" />
......@@ -199,6 +200,7 @@
<ClInclude Include="Cone.h" />
<ClInclude Include="ConstantBuffers.h" />
<ClInclude Include="Cube.h" />
<ClInclude Include="Cylinder.h" />
<ClInclude Include="Drawable.h" />
<ClInclude Include="DrawableBase.h" />
<ClInclude Include="dxerr.h" />
......@@ -280,6 +282,14 @@
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
</FxCompile>
<FxCompile Include="IndexedPhongPS.hlsl">
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Pixel</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">4.0</ShaderModel>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">4.0</ShaderModel>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
</FxCompile>
<FxCompile Include="SolidPS.hlsl">
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Pixel</ShaderType>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">4.0</ShaderModel>
......
......@@ -156,6 +156,9 @@
<ClCompile Include="TestObject.h">
<Filter>Header Files\Drawable</Filter>
</ClCompile>
<ClCompile Include="Cylinder.cpp">
<Filter>Source Files\Drawable</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="WindowsMessageMap.h">
......@@ -314,6 +317,9 @@
<ClInclude Include="Pyramid.h">
<Filter>Header Files\Drawable</Filter>
</ClInclude>
<ClInclude Include="Cylinder.h">
<Filter>Header Files\Drawable</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="hw3d.rc">
......@@ -361,5 +367,8 @@
<FxCompile Include="SolidVS.hlsl">
<Filter>Shader</Filter>
</FxCompile>
<FxCompile Include="IndexedPhongPS.hlsl">
<Filter>Shader</Filter>
</FxCompile>
</ItemGroup>
</Project>
\ 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