Commit 551cc463 authored by chili's avatar chili
Browse files

refactoring tentative

parent ffdeae8c
...@@ -160,7 +160,7 @@ void Graphics::BeginFrame( float red,float green,float blue ) noexcept ...@@ -160,7 +160,7 @@ void Graphics::BeginFrame( float red,float green,float blue ) noexcept
pContext->ClearDepthStencilView( pDSV.Get(),D3D11_CLEAR_DEPTH,1.0f,0u ); pContext->ClearDepthStencilView( pDSV.Get(),D3D11_CLEAR_DEPTH,1.0f,0u );
} }
void Graphics::DrawIndexed( UINT count ) noexcept(!IS_DEBUG) void Graphics::DrawIndexed( UINT count ) noxnd
{ {
GFX_THROW_INFO_ONLY( pContext->DrawIndexed( count,0u,0u ) ); GFX_THROW_INFO_ONLY( pContext->DrawIndexed( count,0u,0u ) );
} }
......
...@@ -9,10 +9,16 @@ ...@@ -9,10 +9,16 @@
#include <DirectXMath.h> #include <DirectXMath.h>
#include <memory> #include <memory>
#include <random> #include <random>
#include "ConditionalNoexcept.h"
namespace Bind
{
class Bindable;
}
class Graphics class Graphics
{ {
friend class Bindable; friend Bind::Bindable;
public: public:
class Exception : public ChiliException class Exception : public ChiliException
{ {
...@@ -57,7 +63,7 @@ public: ...@@ -57,7 +63,7 @@ public:
~Graphics(); ~Graphics();
void EndFrame(); void EndFrame();
void BeginFrame( float red,float green,float blue ) noexcept; void BeginFrame( float red,float green,float blue ) noexcept;
void DrawIndexed( UINT count ) noexcept(!IS_DEBUG); void DrawIndexed( UINT count ) noxnd;
void SetProjection( DirectX::FXMMATRIX proj ) noexcept; void SetProjection( DirectX::FXMMATRIX proj ) noexcept;
DirectX::XMMATRIX GetProjection() const noexcept; DirectX::XMMATRIX GetProjection() const noexcept;
void SetCamera( DirectX::FXMMATRIX cam ) noexcept; void SetCamera( DirectX::FXMMATRIX cam ) noexcept;
......
#include "IndexBuffer.h" #include "IndexBuffer.h"
#include "GraphicsThrowMacros.h" #include "GraphicsThrowMacros.h"
IndexBuffer::IndexBuffer( Graphics& gfx,const std::vector<unsigned short>& indices ) namespace Bind
:
count( (UINT)indices.size() )
{ {
INFOMAN( gfx ); IndexBuffer::IndexBuffer( Graphics& gfx,const std::vector<unsigned short>& indices )
:
count( (UINT)indices.size() )
{
INFOMAN( gfx );
D3D11_BUFFER_DESC ibd = {}; D3D11_BUFFER_DESC ibd = {};
ibd.BindFlags = D3D11_BIND_INDEX_BUFFER; ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
ibd.Usage = D3D11_USAGE_DEFAULT; ibd.Usage = D3D11_USAGE_DEFAULT;
ibd.CPUAccessFlags = 0u; ibd.CPUAccessFlags = 0u;
ibd.MiscFlags = 0u; ibd.MiscFlags = 0u;
ibd.ByteWidth = UINT( count * sizeof( unsigned short ) ); ibd.ByteWidth = UINT( count * sizeof( unsigned short ) );
ibd.StructureByteStride = sizeof( unsigned short ); ibd.StructureByteStride = sizeof( unsigned short );
D3D11_SUBRESOURCE_DATA isd = {}; D3D11_SUBRESOURCE_DATA isd = {};
isd.pSysMem = indices.data(); isd.pSysMem = indices.data();
GFX_THROW_INFO( GetDevice( gfx )->CreateBuffer( &ibd,&isd,&pIndexBuffer ) ); GFX_THROW_INFO( GetDevice( gfx )->CreateBuffer( &ibd,&isd,&pIndexBuffer ) );
} }
void IndexBuffer::Bind( Graphics& gfx ) noexcept void IndexBuffer::Bind( Graphics& gfx ) noexcept
{ {
GetContext( gfx )->IASetIndexBuffer( pIndexBuffer.Get(),DXGI_FORMAT_R16_UINT,0u ); GetContext( gfx )->IASetIndexBuffer( pIndexBuffer.Get(),DXGI_FORMAT_R16_UINT,0u );
} }
UINT IndexBuffer::GetCount() const noexcept UINT IndexBuffer::GetCount() const noexcept
{ {
return count; return count;
}
} }
#pragma once #pragma once
#include "Bindable.h" #include "Bindable.h"
class IndexBuffer : public Bindable namespace Bind
{ {
public: class IndexBuffer : public Bindable
IndexBuffer( Graphics& gfx,const std::vector<unsigned short>& indices ); {
void Bind( Graphics& gfx ) noexcept override; public:
UINT GetCount() const noexcept; IndexBuffer( Graphics& gfx,const std::vector<unsigned short>& indices );
protected: void Bind( Graphics& gfx ) noexcept override;
UINT count; UINT GetCount() const noexcept;
Microsoft::WRL::ComPtr<ID3D11Buffer> pIndexBuffer; protected:
}; UINT count;
\ No newline at end of file Microsoft::WRL::ComPtr<ID3D11Buffer> pIndexBuffer;
};
}
\ No newline at end of file
cbuffer LightCBuf
{
float3 lightPos;
float3 ambient;
float3 diffuseColor;
float diffuseIntensity;
float attConst;
float attLin;
float attQuad;
};
cbuffer ObjectCBuf
{
float3 materialColors[6];
float padding;
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 / 2) % 6]), 1.0f);
}
\ No newline at end of file
...@@ -27,7 +27,7 @@ public: ...@@ -27,7 +27,7 @@ public:
} }
} }
// asserts face-independent vertices w/ normals cleared to zero // asserts face-independent vertices w/ normals cleared to zero
void SetNormalsIndependentFlat() noexcept(!IS_DEBUG) void SetNormalsIndependentFlat() noxnd
{ {
using namespace DirectX; using namespace DirectX;
assert( indices.size() % 3 == 0 && indices.size() > 0 ); assert( indices.size() % 3 == 0 && indices.size() > 0 );
......
#include "InputLayout.h" #include "InputLayout.h"
#include "GraphicsThrowMacros.h" #include "GraphicsThrowMacros.h"
InputLayout::InputLayout( Graphics& gfx, namespace Bind
const std::vector<D3D11_INPUT_ELEMENT_DESC>& layout,
ID3DBlob* pVertexShaderBytecode )
{ {
INFOMAN( gfx ); InputLayout::InputLayout( Graphics& gfx,
const std::vector<D3D11_INPUT_ELEMENT_DESC>& layout,
ID3DBlob* pVertexShaderBytecode )
{
INFOMAN( gfx );
GFX_THROW_INFO( GetDevice( gfx )->CreateInputLayout( GFX_THROW_INFO( GetDevice( gfx )->CreateInputLayout(
layout.data(),(UINT)layout.size(), layout.data(),(UINT)layout.size(),
pVertexShaderBytecode->GetBufferPointer(), pVertexShaderBytecode->GetBufferPointer(),
pVertexShaderBytecode->GetBufferSize(), pVertexShaderBytecode->GetBufferSize(),
&pInputLayout &pInputLayout
) ); ) );
} }
void InputLayout::Bind( Graphics& gfx ) noexcept void InputLayout::Bind( Graphics& gfx ) noexcept
{ {
GetContext( gfx )->IASetInputLayout( pInputLayout.Get() ); GetContext( gfx )->IASetInputLayout( pInputLayout.Get() );
}
} }
#pragma once #pragma once
#include "Bindable.h" #include "Bindable.h"
class InputLayout : public Bindable namespace Bind
{ {
public: class InputLayout : public Bindable
InputLayout( Graphics& gfx, {
const std::vector<D3D11_INPUT_ELEMENT_DESC>& layout, public:
ID3DBlob* pVertexShaderBytecode ); InputLayout( Graphics& gfx,
void Bind( Graphics& gfx ) noexcept override; const std::vector<D3D11_INPUT_ELEMENT_DESC>& layout,
protected: ID3DBlob* pVertexShaderBytecode );
Microsoft::WRL::ComPtr<ID3D11InputLayout> pInputLayout; void Bind( Graphics& gfx ) noexcept override;
}; protected:
\ No newline at end of file Microsoft::WRL::ComPtr<ID3D11InputLayout> pInputLayout;
};
}
\ No newline at end of file
#include "Mesh.h"
// Mesh
Mesh::Mesh( Graphics& gfx,std::vector<std::unique_ptr<Bind::Bindable>> bindPtrs )
{
if( !IsStaticInitialized() )
{
AddStaticBind( std::make_unique<Bind::Topology>( gfx,D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ) );
}
for( auto& pb : bindPtrs )
{
if( auto pi = dynamic_cast<Bind::IndexBuffer*>(pb.get()) )
{
AddIndexBuffer( std::unique_ptr<Bind::IndexBuffer>{ pi } );
pb.release();
}
else
{
AddBind( std::move( pb ) );
}
}
AddBind( std::make_unique<Bind::TransformCbuf>( gfx,*this ) );
}
void Mesh::Draw( Graphics& gfx,DirectX::FXMMATRIX accumulatedTransform ) const noexcept(!IS_DEBUG)
{
DirectX::XMStoreFloat4x4( &transform,accumulatedTransform );
Drawable::Draw( gfx );
}
DirectX::XMMATRIX Mesh::GetTransformXM() const noexcept
{
return DirectX::XMLoadFloat4x4( &transform );
}
// Node
Node::Node( std::vector<Mesh*> meshPtrs,const DirectX::XMMATRIX& transform ) noxnd
:
meshPtrs( std::move( meshPtrs ) )
{
DirectX::XMStoreFloat4x4( &this->transform,transform );
}
void Node::Draw( Graphics& gfx,DirectX::FXMMATRIX accumulatedTransform ) const noxnd
{
const auto built = DirectX::XMLoadFloat4x4( &transform ) * accumulatedTransform;
for( const auto pm : meshPtrs )
{
pm->Draw( gfx,built );
}
for( const auto& pc : childPtrs )
{
pc->Draw( gfx,built );
}
}
void Node::AddChild( std::unique_ptr<Node> pChild ) noxnd
{
assert( pChild );
childPtrs.push_back( std::move( pChild ) );
}
// Model
Model::Model( Graphics& gfx,const std::string fileName )
{
Assimp::Importer imp;
const auto pScene = imp.ReadFile( fileName.c_str(),
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices
);
for( size_t i = 0; i < pScene->mNumMeshes; i++ )
{
meshPtrs.push_back( ParseMesh( gfx,*pScene->mMeshes[i] ) );
}
pRoot = ParseNode( *pScene->mRootNode );
}
void Model::Draw( Graphics& gfx,DirectX::FXMMATRIX transform ) const
{
pRoot->Draw( gfx,transform );
}
std::unique_ptr<Mesh> Model::ParseMesh( Graphics& gfx,const aiMesh& mesh )
{
namespace dx = DirectX;
using Dvtx::VertexLayout;
Dvtx::VertexBuffer vbuf( std::move(
VertexLayout{}
.Append( VertexLayout::Position3D )
.Append( VertexLayout::Normal )
) );
for( unsigned int i = 0; i < mesh.mNumVertices; i++ )
{
vbuf.EmplaceBack(
*reinterpret_cast<dx::XMFLOAT3*>(&mesh.mVertices[i]),
*reinterpret_cast<dx::XMFLOAT3*>(&mesh.mNormals[i])
);
}
std::vector<unsigned short> indices;
indices.reserve( mesh.mNumFaces * 3 );
for( unsigned int i = 0; i < mesh.mNumFaces; i++ )
{
const auto& face = mesh.mFaces[i];
assert( face.mNumIndices == 3 );
indices.push_back( face.mIndices[0] );
indices.push_back( face.mIndices[1] );
indices.push_back( face.mIndices[2] );
}
std::vector<std::unique_ptr<Bind::Bindable>> bindablePtrs;
bindablePtrs.push_back( std::make_unique<Bind::VertexBuffer>( gfx,vbuf ) );
bindablePtrs.push_back( std::make_unique<Bind::IndexBuffer>( gfx,indices ) );
auto pvs = std::make_unique<Bind::VertexShader>( gfx,L"PhongVS.cso" );
auto pvsbc = pvs->GetBytecode();
bindablePtrs.push_back( std::move( pvs ) );
bindablePtrs.push_back( std::make_unique<Bind::PixelShader>( gfx,L"PhongPS.cso" ) );
bindablePtrs.push_back( std::make_unique<Bind::InputLayout>( gfx,vbuf.GetLayout().GetD3DLayout(),pvsbc ) );
struct PSMaterialConstant
{
DirectX::XMFLOAT3 color = { 0.6f,0.6f,0.8f };
float specularIntensity = 0.6f;
float specularPower = 30.0f;
float padding[3];
} pmc;
bindablePtrs.push_back( std::make_unique<Bind::PixelConstantBuffer<PSMaterialConstant>>( gfx,pmc,1u ) );
return std::make_unique<Mesh>( gfx,std::move( bindablePtrs ) );
}
std::unique_ptr<Node> Model::ParseNode( const aiNode& node )
{
namespace dx = DirectX;
const auto transform = dx::XMMatrixTranspose( dx::XMLoadFloat4x4(
reinterpret_cast<const dx::XMFLOAT4X4*>(&node.mTransformation)
) );
std::vector<Mesh*> curMeshPtrs;
curMeshPtrs.reserve( node.mNumMeshes );
for( size_t i = 0; i < node.mNumMeshes; i++ )
{
const auto meshIdx = node.mMeshes[i];
curMeshPtrs.push_back( meshPtrs.at( meshIdx ).get() );
}
auto pNode = std::make_unique<Node>( std::move( curMeshPtrs ),transform );
for( size_t i = 0; i < node.mNumChildren; i++ )
{
pNode->AddChild( ParseNode( *node.mChildren[i] ) );
}
return pNode;
}
\ No newline at end of file
#pragma once
#include "DrawableBase.h"
#include "BindableCommon.h"
#include "Vertex.h"
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include "ConditionalNoexcept.h"
class Mesh : public DrawableBase<Mesh>
{
public:
Mesh( Graphics& gfx,std::vector<std::unique_ptr<Bind::Bindable>> bindPtrs );
void Draw( Graphics& gfx,DirectX::FXMMATRIX accumulatedTransform ) const noxnd;
DirectX::XMMATRIX GetTransformXM() const noexcept override;
private:
mutable DirectX::XMFLOAT4X4 transform;
};
class Node
{
friend class Model;
public:
Node( std::vector<Mesh*> meshPtrs,const DirectX::XMMATRIX& transform ) noxnd;
void Draw( Graphics& gfx,DirectX::FXMMATRIX accumulatedTransform ) const noxnd;
private:
void AddChild( std::unique_ptr<Node> pChild ) noxnd;
private:
std::vector<std::unique_ptr<Node>> childPtrs;
std::vector<Mesh*> meshPtrs;
DirectX::XMFLOAT4X4 transform;
};
class Model
{
public:
Model( Graphics& gfx,const std::string fileName );
void Draw( Graphics& gfx,DirectX::FXMMATRIX transform ) const;
private:
static std::unique_ptr<Mesh> ParseMesh( Graphics& gfx,const aiMesh& mesh );
std::unique_ptr<Node> ParseNode( const aiNode& node );
private:
std::unique_ptr<Node> pRoot;
std::vector<std::unique_ptr<Mesh>> meshPtrs;
};
\ No newline at end of file
#pragma once
#include "DrawableBase.h"
#include "BindableBase.h"
#include "Vertex.h"
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
class Mesh : public DrawableBase<Mesh>
{
public:
Mesh( Graphics& gfx,std::vector<std::unique_ptr<Bindable>> bindPtrs )
{
if( !IsStaticInitialized() )
{
AddStaticBind( std::make_unique<Topology>( gfx,D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ) );
}
for( auto& pb : bindPtrs )
{
if( auto pi = dynamic_cast<IndexBuffer*>(pb.get()) )
{
AddIndexBuffer( std::unique_ptr<IndexBuffer>{ pi } );
pb.release();
}
else
{
AddBind( std::move( pb ) );
}
}
AddBind( std::make_unique<TransformCbuf>( gfx,*this ) );
}
void Draw( Graphics& gfx,DirectX::FXMMATRIX accumulatedTransform ) const noexcept( !IS_DEBUG )
{
DirectX::XMStoreFloat4x4( &transform,accumulatedTransform );
Drawable::Draw( gfx );
}
DirectX::XMMATRIX GetTransformXM() const noexcept override
{
return DirectX::XMLoadFloat4x4( &transform );
}
private:
mutable DirectX::XMFLOAT4X4 transform;
};
class Node
{
friend class Model;
public:
Node( std::vector<Mesh*> meshPtrs,const DirectX::XMMATRIX& transform ) noexcept(!IS_DEBUG)
:
meshPtrs( std::move( meshPtrs ) )
{
DirectX::XMStoreFloat4x4( &this->transform,transform );
}
void Draw( Graphics& gfx,DirectX::FXMMATRIX accumulatedTransform ) const noexcept(!IS_DEBUG)
{
const auto built = DirectX::XMLoadFloat4x4( &transform ) * accumulatedTransform;
for( const auto pm : meshPtrs )
{
pm->Draw( gfx,built );
}
for( const auto& pc : childPtrs )
{
pc->Draw( gfx,built );
}
}
private:
void AddChild( std::unique_ptr<Node> pChild ) noexcept(!IS_DEBUG)
{
assert( pChild );
childPtrs.push_back( std::move( pChild ) );
}
private:
std::vector<std::unique_ptr<Node>> childPtrs;
std::vector<Mesh*> meshPtrs;
DirectX::XMFLOAT4X4 transform;
};
class Model
{
public:
Model( Graphics& gfx,const std::string fileName )
{
Assimp::Importer imp;
const auto pScene = imp.ReadFile( fileName.c_str(),
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices
);
for( size_t i = 0; i < pScene->mNumMeshes; i++ )
{
meshPtrs.push_back( ParseMesh( gfx,*pScene->mMeshes[i] ) );
}
pRoot = ParseNode( *pScene->mRootNode );
}
void Draw( Graphics& gfx,DirectX::FXMMATRIX transform ) const
{
pRoot->Draw( gfx,transform );
}
private:
static std::unique_ptr<Mesh> ParseMesh( Graphics& gfx,const aiMesh& mesh )
{
namespace dx = DirectX;
using hw3dexp::VertexLayout;
hw3dexp::VertexBuffer vbuf( std::move(
VertexLayout{}
.Append( VertexLayout::Position3D )
.Append( VertexLayout::Normal )
) );
for( unsigned int i = 0; i < mesh.mNumVertices; i++ )
{
vbuf.EmplaceBack(
*reinterpret_cast<dx::XMFLOAT3*>(&mesh.mVertices[i]),
*reinterpret_cast<dx::XMFLOAT3*>(&mesh.mNormals[i])
);
}
std::vector<unsigned short> indices;
indices.reserve( mesh.mNumFaces * 3 );
for( unsigned int i = 0; i < mesh.mNumFaces; i++ )
{
const auto& face = mesh.mFaces[i];
assert( face.mNumIndices == 3 );
indices.push_back( face.mIndices[0] );
indices.push_back( face.mIndices[1] );
indices.push_back( face.mIndices[2] );
}
std::vector<std::unique_ptr<Bindable>> bindablePtrs;
bindablePtrs.push_back( std::make_unique<VertexBuffer>( gfx,vbuf ) );
bindablePtrs.push_back( std::make_unique<IndexBuffer>( gfx,indices ) );
auto pvs = std::make_unique<VertexShader>( gfx,L"PhongVS.cso" );
auto pvsbc = pvs->GetBytecode();
bindablePtrs.push_back( std::move( pvs ) );
bindablePtrs.push_back( std::make_unique<PixelShader>( gfx,L"PhongPS.cso" ) );
bindablePtrs.push_back( std::make_unique<InputLayout>( gfx,vbuf.GetLayout().GetD3DLayout(),pvsbc ) );
struct PSMaterialConstant
{
DirectX::XMFLOAT3 color = { 0.6f,0.6f,0.8f };
float specularIntensity = 0.6f;
float specularPower = 30.0f;
float padding[3];
} pmc;
bindablePtrs.push_back( std::make_unique<PixelConstantBuffer<PSMaterialConstant>>( gfx,pmc,1u ) );
return std::make_unique<Mesh>( gfx,std::move( bindablePtrs ) );
}
std::unique_ptr<Node> ParseNode( const aiNode& node )
{
namespace dx = DirectX;
const auto transform = dx::XMMatrixTranspose( dx::XMLoadFloat4x4(
reinterpret_cast<const dx::XMFLOAT4X4*>(&node.mTransformation)
) );
std::vector<Mesh*> curMeshPtrs;
curMeshPtrs.reserve( node.mNumMeshes );
for( size_t i = 0; i < node.mNumMeshes; i++ )
{
const auto meshIdx = node.mMeshes[i];
curMeshPtrs.push_back( meshPtrs.at( meshIdx ).get() );
}
auto pNode = std::make_unique<Node>( std::move( curMeshPtrs ),transform );
for( size_t i = 0; i < node.mNumChildren; i++ )
{
pNode->AddChild( ParseNode( *node.mChildren[i] ) );
}
return pNode;
}
private:
std::unique_ptr<Node> pRoot;
std::vector<std::unique_ptr<Mesh>> meshPtrs;
};
\ No newline at end of file
#include "PixelShader.h" #include "PixelShader.h"
#include "GraphicsThrowMacros.h" #include "GraphicsThrowMacros.h"
PixelShader::PixelShader( Graphics& gfx,const std::wstring& path ) namespace Bind
{ {
INFOMAN( gfx ); PixelShader::PixelShader( Graphics& gfx,const std::wstring& path )
{
INFOMAN( gfx );
Microsoft::WRL::ComPtr<ID3DBlob> pBlob; Microsoft::WRL::ComPtr<ID3DBlob> pBlob;
GFX_THROW_INFO( D3DReadFileToBlob( path.c_str(),&pBlob ) ); GFX_THROW_INFO( D3DReadFileToBlob( path.c_str(),&pBlob ) );
GFX_THROW_INFO( GetDevice( gfx )->CreatePixelShader( pBlob->GetBufferPointer(),pBlob->GetBufferSize(),nullptr,&pPixelShader ) ); GFX_THROW_INFO( GetDevice( gfx )->CreatePixelShader( pBlob->GetBufferPointer(),pBlob->GetBufferSize(),nullptr,&pPixelShader ) );
} }
void PixelShader::Bind( Graphics& gfx ) noexcept void PixelShader::Bind( Graphics& gfx ) noexcept
{ {
GetContext( gfx )->PSSetShader( pPixelShader.Get(),nullptr,0u ); GetContext( gfx )->PSSetShader( pPixelShader.Get(),nullptr,0u );
}
} }
#pragma once #pragma once
#include "Bindable.h" #include "Bindable.h"
class PixelShader : public Bindable namespace Bind
{ {
public: class PixelShader : public Bindable
PixelShader( Graphics& gfx,const std::wstring& path ); {
void Bind( Graphics& gfx ) noexcept override; public:
protected: PixelShader( Graphics& gfx,const std::wstring& path );
Microsoft::WRL::ComPtr<ID3D11PixelShader> pPixelShader; void Bind( Graphics& gfx ) noexcept override;
}; protected:
\ No newline at end of file Microsoft::WRL::ComPtr<ID3D11PixelShader> pPixelShader;
};
}
\ No newline at end of file
...@@ -49,7 +49,7 @@ void PointLight::Reset() noexcept ...@@ -49,7 +49,7 @@ void PointLight::Reset() noexcept
}; };
} }
void PointLight::Draw( Graphics& gfx ) const noexcept(!IS_DEBUG) void PointLight::Draw( Graphics& gfx ) const noxnd
{ {
mesh.SetPos( cbData.pos ); mesh.SetPos( cbData.pos );
mesh.Draw( gfx ); mesh.Draw( gfx );
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include "Graphics.h" #include "Graphics.h"
#include "SolidSphere.h" #include "SolidSphere.h"
#include "ConstantBuffers.h" #include "ConstantBuffers.h"
#include "ConditionalNoexcept.h"
class PointLight class PointLight
{ {
...@@ -9,7 +10,7 @@ public: ...@@ -9,7 +10,7 @@ public:
PointLight( Graphics& gfx,float radius = 0.5f ); PointLight( Graphics& gfx,float radius = 0.5f );
void SpawnControlWindow() noexcept; void SpawnControlWindow() noexcept;
void Reset() noexcept; void Reset() noexcept;
void Draw( Graphics& gfx ) const noexcept(!IS_DEBUG); void Draw( Graphics& gfx ) const noxnd;
void Bind( Graphics& gfx,DirectX::FXMMATRIX view ) const noexcept; void Bind( Graphics& gfx,DirectX::FXMMATRIX view ) const noexcept;
private: private:
struct PointLightCBuf struct PointLightCBuf
...@@ -25,5 +26,5 @@ private: ...@@ -25,5 +26,5 @@ private:
private: private:
PointLightCBuf cbData; PointLightCBuf cbData;
mutable SolidSphere mesh; mutable SolidSphere mesh;
mutable PixelConstantBuffer<PointLightCBuf> cbuf; mutable Bind::PixelConstantBuffer<PointLightCBuf> cbuf;
}; };
\ No newline at end of file
#include "Pyramid.h"
#include "BindableBase.h"
#include "GraphicsThrowMacros.h"
#include "Cone.h"
#include <array>
Pyramid::Pyramid( 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_int_distribution<int>& tdist )
:
TestObject( gfx,rng,adist,ddist,odist,rdist )
{
namespace dx = DirectX;
if( !IsStaticInitialized() )
{
auto pvs = std::make_unique<VertexShader>( gfx,L"BlendedPhongVS.cso" );
auto pvsbc = pvs->GetBytecode();
AddStaticBind( std::move( pvs ) );
AddStaticBind( std::make_unique<PixelShader>( gfx,L"BlendedPhongPS.cso" ) );
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 },
{ "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<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 ) );
}
struct Vertex
{
dx::XMFLOAT3 pos;
dx::XMFLOAT3 n;
std::array<char,4> color;
char padding;
};
const auto tesselation = tdist( rng );
auto model = Cone::MakeTesselatedIndependentFaces<Vertex>( tesselation );
// set vertex colors for mesh (tip red blending to blue base)
for( auto& v : model.vertices )
{
v.color = { (char)10,(char)10,(char)255 };
}
for( int i = 0; i < tesselation; i++ )
{
model.vertices[i * 3].color = { (char)255,(char)10,(char)10 };
}
// squash mesh a bit in the z direction
model.Transform( dx::XMMatrixScaling( 1.0f,1.0f,0.7f ) );
// add normals
model.SetNormalsIndependentFlat();
AddBind( std::make_unique<VertexBuffer>( gfx,model.vertices ) );
AddIndexBuffer( std::make_unique<IndexBuffer>( gfx,model.indices ) );
AddBind( std::make_unique<TransformCbuf>( gfx,*this ) );
}
#pragma once
#include "TestObject.h"
class Pyramid : public TestObject<Pyramid>
{
public:
Pyramid( 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_int_distribution<int>& tdist );
};
\ No newline at end of file
#include "Sampler.h" #include "Sampler.h"
#include "GraphicsThrowMacros.h" #include "GraphicsThrowMacros.h"
Sampler::Sampler( Graphics& gfx ) namespace Bind
{ {
INFOMAN( gfx ); Sampler::Sampler( Graphics& gfx )
{
INFOMAN( gfx );
D3D11_SAMPLER_DESC samplerDesc = {}; D3D11_SAMPLER_DESC samplerDesc = {};
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
GFX_THROW_INFO( GetDevice( gfx )->CreateSamplerState( &samplerDesc,&pSampler ) ); GFX_THROW_INFO( GetDevice( gfx )->CreateSamplerState( &samplerDesc,&pSampler ) );
} }
void Sampler::Bind( Graphics& gfx ) noexcept void Sampler::Bind( Graphics& gfx ) noexcept
{ {
GetContext( gfx )->PSSetSamplers( 0,1,pSampler.GetAddressOf() ); GetContext( gfx )->PSSetSamplers( 0,1,pSampler.GetAddressOf() );
}
} }
\ No newline at end of file
#pragma once #pragma once
#include "Bindable.h" #include "Bindable.h"
class Sampler : public Bindable namespace Bind
{ {
public: class Sampler : public Bindable
Sampler( Graphics& gfx ); {
void Bind( Graphics& gfx ) noexcept override; public:
protected: Sampler( Graphics& gfx );
Microsoft::WRL::ComPtr<ID3D11SamplerState> pSampler; void Bind( Graphics& gfx ) noexcept override;
}; protected:
Microsoft::WRL::ComPtr<ID3D11SamplerState> pSampler;
};
}
#include "SkinnedBox.h"
#include "BindableBase.h"
#include "GraphicsThrowMacros.h"
#include "Cube.h"
#include "Surface.h"
#include "Texture.h"
#include "Sampler.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 )
:
TestObject( gfx,rng,adist,ddist,odist,rdist )
{
namespace dx = DirectX;
if( !IsStaticInitialized() )
{
struct Vertex
{
dx::XMFLOAT3 pos;
dx::XMFLOAT3 n;
dx::XMFLOAT2 tc;
};
auto model = Cube::MakeIndependentTextured<Vertex>();
model.SetNormalsIndependentFlat();
AddStaticBind( std::make_unique<VertexBuffer>( gfx,model.vertices ) );
AddStaticBind( std::make_unique<Texture>( gfx,Surface::FromFile( "Images\\kappa50.png" ) ) );
AddStaticBind( std::make_unique<Sampler>( gfx ) );
auto pvs = std::make_unique<VertexShader>( gfx,L"TexturedPhongVS.cso" );
auto pvsbc = pvs->GetBytecode();
AddStaticBind( std::move( pvs ) );
AddStaticBind( std::make_unique<PixelShader>( gfx,L"TexturedPhongPS.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 },
{ "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<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
{
SetIndexFromStatic();
}
AddBind( std::make_unique<TransformCbuf>( gfx,*this ) );
}
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