Commit e16cbfd1 authored by chili's avatar chili
Browse files

cone added but it's weird :excitement:

parent d6e000ca
#include "App.h" #include "App.h"
#include "Box.h" #include "Box.h"
#include "Cylinder.h" #include "Cylinder.h"
#include "Pyramid.h"
#include <memory> #include <memory>
#include <algorithm> #include <algorithm>
#include "ChiliMath.h" #include "ChiliMath.h"
...@@ -40,6 +41,11 @@ App::App() ...@@ -40,6 +41,11 @@ App::App()
gfx,rng,adist,ddist,odist, gfx,rng,adist,ddist,odist,
rdist,bdist,tdist rdist,bdist,tdist
); );
case 2:
return std::make_unique<Pyramid>(
gfx,rng,adist,ddist,odist,
rdist,tdist
);
default: default:
assert( false && "impossible drawable option in factory" ); assert( false && "impossible drawable option in factory" );
return {}; return {};
...@@ -48,7 +54,7 @@ App::App() ...@@ -48,7 +54,7 @@ App::App()
private: private:
Graphics& gfx; Graphics& gfx;
std::mt19937 rng{ std::random_device{}() }; std::mt19937 rng{ std::random_device{}() };
std::uniform_int_distribution<int> sdist{ 0,1 }; std::uniform_int_distribution<int> sdist{ 0,2 };
std::uniform_real_distribution<float> adist{ 0.0f,PI * 2.0f }; 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> ddist{ 0.0f,PI * 0.5f };
std::uniform_real_distribution<float> odist{ 0.0f,PI * 0.08f }; std::uniform_real_distribution<float> odist{ 0.0f,PI * 0.08f };
......
cbuffer LightCBuf
{
float3 lightPos;
float3 ambient;
float3 diffuseColor;
float diffuseIntensity;
float attConst;
float attLin;
float attQuad;
};
cbuffer ObjectCBuf
{
float specularIntensity;
float specularPower;
float padding[2];
};
float4 main( float3 worldPos : Position,float3 n : Normal,float3 color : Color ) : 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) * color), 1.0f);
}
\ No newline at end of file
cbuffer CBuf
{
matrix modelView;
matrix modelViewProj;
};
struct VSOut
{
float3 worldPos : Position;
float3 normal : Normal;
float3 color : Color;
float4 pos : SV_Position;
};
VSOut main( float3 pos : Position,float3 n : Normal,float3 color : Color )
{
VSOut vso;
vso.worldPos = (float3)mul( float4(pos,1.0f),modelView );
vso.normal = mul( n,(float3x3)modelView );
vso.pos = mul( float4(pos,1.0f),modelViewProj );
vso.color = color;
return vso;
}
\ No newline at end of file
...@@ -56,6 +56,69 @@ public: ...@@ -56,6 +56,69 @@ public:
return { std::move( vertices ),std::move( indices ) }; return { std::move( vertices ),std::move( indices ) };
} }
template<class V> template<class V>
static IndexedTriangleList<V> MakeTesselatedIndependentFaces( int longDiv )
{
namespace dx = DirectX;
assert( longDiv >= 3 );
const auto base = dx::XMVectorSet( 1.0f,0.0f,-1.0f,0.0f );
const float longitudeAngle = 2.0f * PI / longDiv;
std::vector<V> vertices;
// cone vertices
const auto iCone = (unsigned short)vertices.size();
for( int iLong = 0; iLong < longDiv; iLong++ )
{
const float thetas[] = {
longitudeAngle * iLong,
longitudeAngle * (((iLong + 1) == longDiv) ? 0 : (iLong + 1))
};
vertices.emplace_back();
vertices.back().pos = { 0.0f,0.0f,1.0f };
for( auto theta : thetas )
{
vertices.emplace_back();
const auto v = dx::XMVector3Transform(
base,
dx::XMMatrixRotationZ( theta )
);
dx::XMStoreFloat3( &vertices.back().pos,v );
}
}
// base vertices
const auto iBaseCenter = (unsigned short)vertices.size();
vertices.emplace_back();
vertices.back().pos = { 0.0f,0.0f,-1.0f };
const auto iBaseEdge = (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 );
}
std::vector<unsigned short> indices;
// cone indices
for( unsigned short i = 0; i < longDiv * 3; i++ )
{
indices.push_back( i + iCone );
}
// base indices
for( unsigned short iLong = 0; iLong < longDiv; iLong++ )
{
indices.push_back( iBaseCenter );
indices.push_back( (iLong + 1) % longDiv + iBaseEdge );
indices.push_back( iLong + iBaseEdge );
}
return { std::move( vertices ),std::move( indices ) };
}
template<class V>
static IndexedTriangleList<V> Make() static IndexedTriangleList<V> Make()
{ {
return MakeTesselated<V>( 24 ); return MakeTesselated<V>( 24 );
......
...@@ -2,25 +2,17 @@ ...@@ -2,25 +2,17 @@
#include "BindableBase.h" #include "BindableBase.h"
#include "GraphicsThrowMacros.h" #include "GraphicsThrowMacros.h"
#include "Cone.h" #include "Cone.h"
#include <array>
Pyramid::Pyramid( Graphics& gfx, Pyramid::Pyramid( Graphics& gfx,std::mt19937& rng,
std::mt19937& rng,
std::uniform_real_distribution<float>& adist, std::uniform_real_distribution<float>& adist,
std::uniform_real_distribution<float>& ddist, std::uniform_real_distribution<float>& ddist,
std::uniform_real_distribution<float>& odist, std::uniform_real_distribution<float>& odist,
std::uniform_real_distribution<float>& rdist ) std::uniform_real_distribution<float>& rdist,
std::uniform_int_distribution<int>& tdist )
: :
r( rdist( rng ) ), TestObject( gfx,rng,adist,ddist,odist,rdist )
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; namespace dx = DirectX;
...@@ -29,39 +21,37 @@ Pyramid::Pyramid( Graphics& gfx, ...@@ -29,39 +21,37 @@ Pyramid::Pyramid( Graphics& gfx,
struct Vertex struct Vertex
{ {
dx::XMFLOAT3 pos; dx::XMFLOAT3 pos;
struct dx::XMFLOAT3 n;
{ std::array<char,4> color;
unsigned char r; char padding;
unsigned char g;
unsigned char b;
unsigned char a;
} color;
}; };
auto model = Cone::MakeTesselated<Vertex>( 4 ); auto model = Cone::MakeTesselatedIndependentFaces<Vertex>( tdist( rng ) );
// set vertex colors for mesh // set vertex colors for mesh
model.vertices[0].color = { 255,255,0 }; for( auto& v : model.vertices )
model.vertices[1].color = { 255,255,0 }; {
model.vertices[2].color = { 255,255,0 }; v.color = { (char)40,(char)40,(char)255 };
model.vertices[3].color = { 255,255,0 }; }
model.vertices[4].color = { 255,255,80 }; model.vertices.front().color = { (char)255,(char)20,(char)20 }; // very first vertex is the cone tip
model.vertices[5].color = { 255,10,0 }; // squash mesh a bit in the z direction
// deform mesh linearly
model.Transform( dx::XMMatrixScaling( 1.0f,1.0f,0.7f ) ); model.Transform( dx::XMMatrixScaling( 1.0f,1.0f,0.7f ) );
// add normals
model.SetNormalsIndependentFlat();
AddStaticBind( std::make_unique<VertexBuffer>( gfx,model.vertices ) ); AddStaticBind( std::make_unique<VertexBuffer>( gfx,model.vertices ) );
auto pvs = std::make_unique<VertexShader>( gfx,L"ColorBlendVS.cso" ); auto pvs = std::make_unique<VertexShader>( gfx,L"BlendedPhongVS.cso" );
auto pvsbc = pvs->GetBytecode(); auto pvsbc = pvs->GetBytecode();
AddStaticBind( std::move( pvs ) ); AddStaticBind( std::move( pvs ) );
AddStaticBind( std::make_unique<PixelShader>( gfx,L"ColorBlendPS.cso" ) ); AddStaticBind( std::make_unique<PixelShader>( gfx,L"BlendedPhongPS.cso" ) );
AddStaticIndexBuffer( std::make_unique<IndexBuffer>( gfx,model.indices ) ); AddStaticIndexBuffer( std::make_unique<IndexBuffer>( gfx,model.indices ) );
const std::vector<D3D11_INPUT_ELEMENT_DESC> ied = const std::vector<D3D11_INPUT_ELEMENT_DESC> ied =
{ {
{ "Position",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 }, { "Position",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
{ "Color",0,DXGI_FORMAT_R8G8B8A8_UNORM,0,12,D3D11_INPUT_PER_VERTEX_DATA,0 }, { "Normal",0,DXGI_FORMAT_R32G32B32_FLOAT,0,12,D3D11_INPUT_PER_VERTEX_DATA,0 },
{ "Color",0,DXGI_FORMAT_R8G8B8A8_UNORM,0,24,D3D11_INPUT_PER_VERTEX_DATA,0 },
}; };
AddStaticBind( std::make_unique<InputLayout>( gfx,ied,pvsbc ) ); AddStaticBind( std::make_unique<InputLayout>( gfx,ied,pvsbc ) );
...@@ -74,21 +64,3 @@ Pyramid::Pyramid( Graphics& gfx, ...@@ -74,21 +64,3 @@ Pyramid::Pyramid( Graphics& gfx,
AddBind( std::make_unique<TransformCbuf>( gfx,*this ) ); AddBind( std::make_unique<TransformCbuf>( gfx,*this ) );
} }
void Pyramid::Update( float dt ) noexcept
{
roll += droll * dt;
pitch += dpitch * dt;
yaw += dyaw * dt;
theta += dtheta * dt;
phi += dphi * dt;
chi += dchi * dt;
}
DirectX::XMMATRIX Pyramid::GetTransformXM() const noexcept
{
namespace dx = DirectX;
return dx::XMMatrixRotationRollPitchYaw( pitch,yaw,roll ) *
dx::XMMatrixTranslation( r,0.0f,0.0f ) *
dx::XMMatrixRotationRollPitchYaw( theta,phi,chi );
}
#pragma once #pragma once
#include "DrawableBase.h" #include "TestObject.h"
class Pyramid : public DrawableBase<Pyramid> class Pyramid : public TestObject<Pyramid>
{ {
public: public:
Pyramid( Graphics& gfx,std::mt19937& rng, Pyramid( Graphics& gfx,std::mt19937& rng,
std::uniform_real_distribution<float>& adist, std::uniform_real_distribution<float>& adist,
std::uniform_real_distribution<float>& ddist, std::uniform_real_distribution<float>& ddist,
std::uniform_real_distribution<float>& odist, std::uniform_real_distribution<float>& odist,
std::uniform_real_distribution<float>& rdist ); std::uniform_real_distribution<float>& rdist,
void Update( float dt ) noexcept override; std::uniform_int_distribution<int>& tdist );
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
...@@ -254,6 +254,34 @@ ...@@ -254,6 +254,34 @@
<None Include="DXTrace.inl" /> <None Include="DXTrace.inl" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<FxCompile Include="BlendedPhongPS.hlsl">
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Pixel</ShaderType>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">4.0</ShaderModel>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Pixel</ShaderType>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">4.0</ShaderModel>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">4.0</ShaderModel>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Pixel</ShaderType>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Release|x64'">4.0</ShaderModel>
</FxCompile>
<FxCompile Include="BlendedPhongVS.hlsl">
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Vertex</ShaderType>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">4.0</ShaderModel>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Vertex</ShaderType>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">4.0</ShaderModel>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Vertex</ShaderType>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">4.0</ShaderModel>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Vertex</ShaderType>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Release|x64'">4.0</ShaderModel>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
</FxCompile>
<FxCompile Include="ColorBlendPS.hlsl"> <FxCompile Include="ColorBlendPS.hlsl">
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Pixel</ShaderType> <ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Pixel</ShaderType>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">4.0</ShaderModel> <ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">4.0</ShaderModel>
......
...@@ -370,5 +370,11 @@ ...@@ -370,5 +370,11 @@
<FxCompile Include="IndexedPhongPS.hlsl"> <FxCompile Include="IndexedPhongPS.hlsl">
<Filter>Shader</Filter> <Filter>Shader</Filter>
</FxCompile> </FxCompile>
<FxCompile Include="BlendedPhongPS.hlsl">
<Filter>Shader</Filter>
</FxCompile>
<FxCompile Include="BlendedPhongVS.hlsl">
<Filter>Shader</Filter>
</FxCompile>
</ItemGroup> </ItemGroup>
</Project> </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