Commit 3164667b authored by chili's avatar chili
Browse files

textured cube added

parent 85e7ba5b
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include "Box.h" #include "Box.h"
#include "Cylinder.h" #include "Cylinder.h"
#include "Pyramid.h" #include "Pyramid.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,odist, gfx,rng,adist,ddist,odist,
rdist,tdist rdist,tdist
); );
case 3:
return std::make_unique<SkinnedBox>(
gfx,rng,adist,ddist,
odist,rdist
);
default: default:
assert( false && "impossible drawable option in factory" ); assert( false && "impossible drawable option in factory" );
return {}; return {};
...@@ -54,7 +60,7 @@ App::App() ...@@ -54,7 +60,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,2 }; std::uniform_int_distribution<int> sdist{ 0,3 };
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 };
......
...@@ -125,4 +125,36 @@ public: ...@@ -125,4 +125,36 @@ public:
} }
}; };
} }
template<class V>
static IndexedTriangleList<V> MakeIndependentTextured()
{
auto itl = MakeIndependent<V>();
itl.vertices[0].tc = { 0.0f,0.0f };
itl.vertices[1].tc = { 1.0f,0.0f };
itl.vertices[2].tc = { 0.0f,1.0f };
itl.vertices[3].tc = { 1.0f,1.0f };
itl.vertices[4].tc = { 0.0f,0.0f };
itl.vertices[5].tc = { 1.0f,0.0f };
itl.vertices[6].tc = { 0.0f,1.0f };
itl.vertices[7].tc = { 1.0f,1.0f };
itl.vertices[8].tc = { 0.0f,0.0f };
itl.vertices[9].tc = { 1.0f,0.0f };
itl.vertices[10].tc = { 0.0f,1.0f };
itl.vertices[11].tc = { 1.0f,1.0f };
itl.vertices[12].tc = { 0.0f,0.0f };
itl.vertices[13].tc = { 1.0f,0.0f };
itl.vertices[14].tc = { 0.0f,1.0f };
itl.vertices[15].tc = { 1.0f,1.0f };
itl.vertices[16].tc = { 0.0f,0.0f };
itl.vertices[17].tc = { 1.0f,0.0f };
itl.vertices[18].tc = { 0.0f,1.0f };
itl.vertices[19].tc = { 1.0f,1.0f };
itl.vertices[20].tc = { 0.0f,0.0f };
itl.vertices[21].tc = { 1.0f,0.0f };
itl.vertices[22].tc = { 0.0f,1.0f };
itl.vertices[23].tc = { 1.0f,1.0f };
return itl;
}
}; };
\ No newline at end of file
...@@ -4,25 +4,16 @@ ...@@ -4,25 +4,16 @@
#include "Cube.h" #include "Cube.h"
#include "Surface.h" #include "Surface.h"
#include "Texture.h" #include "Texture.h"
#include "Sampler.h"
SkinnedBox::SkinnedBox( Graphics& gfx, SkinnedBox::SkinnedBox( 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 )
: :
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;
...@@ -31,34 +22,43 @@ SkinnedBox::SkinnedBox( Graphics& gfx, ...@@ -31,34 +22,43 @@ SkinnedBox::SkinnedBox( Graphics& gfx,
struct Vertex struct Vertex
{ {
dx::XMFLOAT3 pos; dx::XMFLOAT3 pos;
struct dx::XMFLOAT3 n;
{ dx::XMFLOAT2 tc;
float u;
float v;
} tex;
}; };
const auto model = Cube::MakeSkinned<Vertex>(); auto model = Cube::MakeIndependentTextured<Vertex>();
model.SetNormalsIndependentFlat();
AddStaticBind( std::make_unique<VertexBuffer>( gfx,model.vertices ) ); AddStaticBind( std::make_unique<VertexBuffer>( gfx,model.vertices ) );
AddStaticBind( std::make_unique<Texture>( gfx,Surface::FromFile( "Images\\cube.png" ) ) ); AddStaticBind( std::make_unique<Texture>( gfx,Surface::FromFile( "Images\\kappa50.png" ) ) );
auto pvs = std::make_unique<VertexShader>( gfx,L"TextureVS.cso" ); AddStaticBind( std::make_unique<Sampler>( gfx ) );
auto pvs = std::make_unique<VertexShader>( gfx,L"TexturedPhongVS.cso" );
auto pvsbc = pvs->GetBytecode(); auto pvsbc = pvs->GetBytecode();
AddStaticBind( std::move( pvs ) ); AddStaticBind( std::move( pvs ) );
AddStaticBind( std::make_unique<PixelShader>( gfx,L"TexturePS.cso" ) ); AddStaticBind( std::make_unique<PixelShader>( gfx,L"TexturedPhongPS.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 },
{ "TexCoord",0,DXGI_FORMAT_R32G32_FLOAT,0,12,D3D11_INPUT_PER_VERTEX_DATA,0 }, { "Normal",0,DXGI_FORMAT_R32G32B32_FLOAT,0,12,D3D11_INPUT_PER_VERTEX_DATA,0 },
{ "TexCoord",0,DXGI_FORMAT_R32G32_FLOAT,0,24,D3D11_INPUT_PER_VERTEX_DATA,0 },
}; };
AddStaticBind( std::make_unique<InputLayout>( gfx,ied,pvsbc ) ); AddStaticBind( std::make_unique<InputLayout>( gfx,ied,pvsbc ) );
AddStaticBind( std::make_unique<Topology>( gfx,D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ) ); AddStaticBind( std::make_unique<Topology>( gfx,D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ) );
struct PSMaterialConstant
{
float specularIntensity = 0.6f;
float specularPower = 30.0f;
float padding[2];
} colorConst;
AddStaticBind( std::make_unique<PixelConstantBuffer<PSMaterialConstant>>( gfx,colorConst,1u ) );
} }
else else
{ {
...@@ -67,21 +67,3 @@ SkinnedBox::SkinnedBox( Graphics& gfx, ...@@ -67,21 +67,3 @@ SkinnedBox::SkinnedBox( Graphics& gfx,
AddBind( std::make_unique<TransformCbuf>( gfx,*this ) ); 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 );
}
#pragma once #pragma once
#include "DrawableBase.h" #include "TestObject.h"
class SkinnedBox : public DrawableBase<SkinnedBox> class SkinnedBox : public TestObject<SkinnedBox>
{ {
public: public:
SkinnedBox( Graphics& gfx,std::mt19937& rng, SkinnedBox( Graphics& gfx,std::mt19937& rng,
...@@ -9,22 +9,4 @@ public: ...@@ -9,22 +9,4 @@ public:
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;
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
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];
};
Texture2D tex;
SamplerState splr;
float4 main( float3 worldPos : Position,float3 n : Normal,float2 tc : Texcoord ) : 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), 1.0f) * tex.Sample(splr, tc);
}
\ No newline at end of file
cbuffer CBuf
{
matrix modelView;
matrix modelViewProj;
};
struct VSOut
{
float3 worldPos : Position;
float3 normal : Normal;
float2 tc : Texcoord;
float4 pos : SV_Position;
};
VSOut main( float3 pos : Position,float3 n : Normal,float2 tc : Texcoord )
{
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.tc = tc;
return vso;
}
\ No newline at end of file
...@@ -372,6 +372,34 @@ ...@@ -372,6 +372,34 @@
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(ProjectDir)%(Filename).cso</ObjectFileOutput> <ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(ProjectDir)%(Filename).cso</ObjectFileOutput> <ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
</FxCompile> </FxCompile>
<FxCompile Include="TexturedPhongPS.hlsl">
<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>
<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="TexturedPhongVS.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="TexturePS.hlsl"> <FxCompile Include="TexturePS.hlsl">
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(ProjectDir)%(Filename).cso</ObjectFileOutput> <ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(ProjectDir)%(Filename).cso</ObjectFileOutput> <ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
......
...@@ -376,5 +376,11 @@ ...@@ -376,5 +376,11 @@
<FxCompile Include="BlendedPhongVS.hlsl"> <FxCompile Include="BlendedPhongVS.hlsl">
<Filter>Shader</Filter> <Filter>Shader</Filter>
</FxCompile> </FxCompile>
<FxCompile Include="TexturedPhongPS.hlsl">
<Filter>Shader</Filter>
</FxCompile>
<FxCompile Include="TexturedPhongVS.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