Commit 44b02568 authored by chili's avatar chili
Browse files

Texture / Sampler / Sheet drawable

parent 2fd7699d
......@@ -2,6 +2,7 @@
#include "Melon.h"
#include "Pyramid.h"
#include "Box.h"
#include "Sheet.h"
#include <memory>
#include <algorithm>
#include "ChiliMath.h"
......@@ -40,6 +41,11 @@ App::App()
gfx,rng,adist,ddist,
odist,rdist,longdist,latdist
);
case 3:
return std::make_unique<Sheet>(
gfx,rng,adist,ddist,
odist,rdist
);
default:
assert( false && "bad drawable type in factory" );
return {};
......@@ -55,15 +61,13 @@ App::App()
std::uniform_real_distribution<float> bdist{ 0.4f,3.0f };
std::uniform_int_distribution<int> latdist{ 5,20 };
std::uniform_int_distribution<int> longdist{ 10,40 };
std::uniform_int_distribution<int> typedist{ 0,2 };
std::uniform_int_distribution<int> typedist{ 0,3 };
};
Factory f( wnd.Gfx() );
drawables.reserve( nDrawables );
std::generate_n( std::back_inserter( drawables ),nDrawables,f );
const auto s = Surface::FromFile( "Images\\kappa50.png" );
wnd.Gfx().SetProjection( DirectX::XMMatrixPerspectiveLH( 1.0f,3.0f / 4.0f,0.5f,40.0f ) );
}
......@@ -73,7 +77,7 @@ void App::DoFrame()
wnd.Gfx().ClearBuffer( 0.07f,0.0f,0.12f );
for( auto& d : drawables )
{
d->Update( dt );
d->Update( wnd.kbd.KeyIsPressed( VK_SPACE ) ? 0.0f : dt );
d->Draw( wnd.Gfx() );
}
wnd.Gfx().EndFrame();
......
#include "Sampler.h"
#include "GraphicsThrowMacros.h"
Sampler::Sampler( Graphics& gfx )
{
INFOMAN( gfx );
D3D11_SAMPLER_DESC samplerDesc = {};
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
GFX_THROW_INFO( GetDevice( gfx )->CreateSamplerState( &samplerDesc,&pSampler ) );
}
void Sampler::Bind( Graphics& gfx ) noexcept
{
GetContext( gfx )->PSSetSamplers( 0,1,pSampler.GetAddressOf() );
}
\ No newline at end of file
#pragma once
#include "Bindable.h"
class Sampler : public Bindable
{
public:
Sampler( Graphics& gfx );
void Bind( Graphics& gfx ) noexcept override;
protected:
Microsoft::WRL::ComPtr<ID3D11SamplerState> pSampler;
};
#include "Sheet.h"
#include "BindableBase.h"
#include "GraphicsThrowMacros.h"
#include "Plane.h"
#include "Surface.h"
#include "Texture.h"
#include "Sampler.h"
Sheet::Sheet( 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;
};
auto model = Plane::Make<Vertex>();
model.vertices[0].tex = { 0.0f,0.0f };
model.vertices[1].tex = { 1.0f,0.0f };
model.vertices[2].tex = { 0.0f,1.0f };
model.vertices[3].tex = { 1.0f,1.0f };
AddStaticBind( std::make_unique<Texture>( gfx,Surface::FromFile( "Images\\kappa50.png" ) ) );
AddStaticBind( std::make_unique<VertexBuffer>( gfx,model.vertices ) );
AddStaticBind( std::make_unique<Sampler>( gfx ) );
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 Sheet::Update( float dt ) noexcept
{
roll += droll * dt;
pitch += dpitch * dt;
yaw += dyaw * dt;
theta += dtheta * dt;
phi += dphi * dt;
chi += dchi * dt;
}
DirectX::XMMATRIX Sheet::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
#include "DrawableBase.h"
class Sheet : public DrawableBase<Sheet>
{
public:
Sheet( 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
......@@ -31,9 +31,9 @@ namespace Gdiplus
#pragma comment( lib,"gdiplus.lib" )
Surface::Surface( unsigned int width,unsigned int height,unsigned int pitch ) noexcept
Surface::Surface( unsigned int width,unsigned int height ) noexcept
:
pBuffer( std::make_unique<Color[]>( pitch * height ) ),
pBuffer( std::make_unique<Color[]>( width * height ) ),
width( width ),
height( height )
{}
......@@ -47,11 +47,6 @@ Surface& Surface::operator=( Surface&& donor ) noexcept
return *this;
}
Surface::Surface( unsigned int width,unsigned int height ) noexcept
:
Surface( width,height,width )
{}
Surface::Surface( Surface && source ) noexcept
:
pBuffer( std::move( source.pBuffer ) ),
......@@ -114,8 +109,7 @@ Surface Surface::FromFile( const std::string& name )
{
unsigned int width = 0;
unsigned int height = 0;
unsigned int pitch = 0;
std::unique_ptr<Color[]> pBuffer = nullptr;
std::unique_ptr<Color[]> pBuffer;
{
// convert filenam to wide string (for Gdiplus)
......@@ -130,6 +124,7 @@ Surface Surface::FromFile( const std::string& name )
throw Exception( __LINE__,__FILE__ ,ss.str() );
}
width = bitmap.GetWidth();
height = bitmap.GetHeight();
pBuffer = std::make_unique<Color[]>( width * height );
......@@ -139,7 +134,7 @@ Surface Surface::FromFile( const std::string& name )
{
Gdiplus::Color c;
bitmap.GetPixel( x,y,&c );
pBuffer[y * pitch + x] = c.GetValue();
pBuffer[y * width + x] = c.GetValue();
}
}
}
......@@ -208,7 +203,7 @@ void Surface::Save( const std::string& filename ) const
}
}
void Surface::Copy( const Surface & src ) noexcept(!IS_DEBUG)
void Surface::Copy( const Surface& src ) noexcept(!IS_DEBUG)
{
assert( width == src.width );
assert( height == src.height );
......
......@@ -114,7 +114,6 @@ public:
std::string note;
};
public:
Surface( unsigned int width,unsigned int height,unsigned int pitch ) noexcept;
Surface( unsigned int width,unsigned int height ) noexcept;
Surface( Surface&& source ) noexcept;
Surface( Surface& ) = delete;
......
#include "Texture.h"
#include "Surface.h"
#include "GraphicsThrowMacros.h"
namespace wrl = Microsoft::WRL;
Texture::Texture( Graphics& gfx,const Surface& s )
{
INFOMAN( gfx );
// create texture resource
D3D11_TEXTURE2D_DESC textureDesc = {};
textureDesc.Width = s.GetWidth();
textureDesc.Height = s.GetHeight();
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
textureDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
textureDesc.SampleDesc.Count = 1;
textureDesc.SampleDesc.Quality = 0;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
textureDesc.CPUAccessFlags = 0;
textureDesc.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA sd = {};
sd.pSysMem = s.GetBufferPtr();
sd.SysMemPitch = s.GetWidth() * sizeof( Surface::Color );
wrl::ComPtr<ID3D11Texture2D> pTexture;
GFX_THROW_INFO( GetDevice( gfx )->CreateTexture2D(
&textureDesc,&sd,&pTexture
) );
// create the resource view on the texture
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
srvDesc.Format = textureDesc.Format;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MostDetailedMip = 0;
srvDesc.Texture2D.MipLevels = 1;
GFX_THROW_INFO( GetDevice( gfx )->CreateShaderResourceView(
pTexture.Get(),&srvDesc,&pTextureView
) );
}
void Texture::Bind( Graphics& gfx ) noexcept
{
GetContext( gfx )->PSSetShaderResources( 0u,1u,pTextureView.GetAddressOf() );
}
#pragma once
#include "Bindable.h"
class Texture : public Bindable
{
public:
Texture( Graphics& gfx,const class Surface& s );
void Bind( Graphics& gfx ) noexcept override;
protected:
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> pTextureView;
};
Texture2D tex;
SamplerState splr;
float4 main( float2 tc : TexCoord ) : SV_Target
{
return tex.Sample( splr,tc );
}
\ No newline at end of file
cbuffer CBuf
{
matrix transform;
};
struct VSOut
{
float2 tex : TexCoord;
float4 pos : SV_Position;
};
VSOut main( float3 pos : Position,float2 tex : TexCoord )
{
VSOut vso;
vso.pos = mul( float4(pos,1.0f),transform );
vso.tex = tex;
return vso;
}
\ No newline at end of file
......@@ -164,7 +164,10 @@
<ClCompile Include="Mouse.cpp" />
<ClCompile Include="PixelShader.cpp" />
<ClCompile Include="Pyramid.cpp" />
<ClCompile Include="Sampler.cpp" />
<ClCompile Include="Sheet.cpp" />
<ClCompile Include="Surface.cpp" />
<ClCompile Include="Texture.cpp" />
<ClCompile Include="Topology.cpp" />
<ClCompile Include="TransformCbuf.cpp" />
<ClCompile Include="VertexBuffer.cpp" />
......@@ -203,8 +206,11 @@
<ClInclude Include="Prism.h" />
<ClInclude Include="Pyramid.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="Sampler.h" />
<ClInclude Include="Sheet.h" />
<ClInclude Include="Sphere.h" />
<ClInclude Include="Surface.h" />
<ClInclude Include="Texture.h" />
<ClInclude Include="Topology.h" />
<ClInclude Include="TransformCbuf.h" />
<ClInclude Include="VertexBuffer.h" />
......@@ -281,6 +287,34 @@
<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">
<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="TextureVS.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>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
......
......@@ -114,6 +114,15 @@
<ClCompile Include="GDIPlusManager.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Sheet.cpp">
<Filter>Source Files\Drawable</Filter>
</ClCompile>
<ClCompile Include="Texture.cpp">
<Filter>Source Files\Bindable</Filter>
</ClCompile>
<ClCompile Include="Sampler.cpp">
<Filter>Source Files\Bindable</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="WindowsMessageMap.h">
......@@ -230,6 +239,15 @@
<ClInclude Include="GDIPlusManager.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Texture.h">
<Filter>Header Files\Bindable</Filter>
</ClInclude>
<ClInclude Include="Sheet.h">
<Filter>Header Files\Drawable</Filter>
</ClInclude>
<ClInclude Include="Sampler.h">
<Filter>Header Files\Bindable</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="hw3d.rc">
......@@ -265,5 +283,11 @@
<FxCompile Include="ColorIndexVS.hlsl">
<Filter>Shader</Filter>
</FxCompile>
<FxCompile Include="TexturePS.hlsl">
<Filter>Shader</Filter>
</FxCompile>
<FxCompile Include="TextureVS.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