Commit ce1e5732 authored by chili's avatar chili
Browse files

using dynamic vtx layout for suzanne asstest

parent 2b41752b
...@@ -13,46 +13,16 @@ ...@@ -13,46 +13,16 @@
#include <assimp/Importer.hpp> #include <assimp/Importer.hpp>
#include <assimp/scene.h> #include <assimp/scene.h>
#include <assimp/postprocess.h> #include <assimp/postprocess.h>
#include "Vertex.h"
namespace dx = DirectX; namespace dx = DirectX;
GDIPlusManager gdipm; GDIPlusManager gdipm;
void f()
{
VertexBuffer vb( std::move(
VertexLayout{}
.Append<VertexLayout::Position3D>()
.Append<VertexLayout::Normal>()
.Append<VertexLayout::Texture2D>()
) );
vb.EmplaceBack(
dx::XMFLOAT3{1.0f,1.0f,5.0f},
dx::XMFLOAT3{ 2.0f,1.0f,4.0f },
dx::XMFLOAT2{ 6.0f,9.0f }
);
vb.EmplaceBack(
dx::XMFLOAT3{ 6.0f,9.0f,6.0f },
dx::XMFLOAT3{ 9.0f,6.0f,9.0f },
dx::XMFLOAT2{ 4.2f,0.0f }
);
auto pos = vb[0].Attr<VertexLayout::Position3D>();
auto nor = vb[0].Attr<VertexLayout::Normal>();
auto tex = vb[1].Attr<VertexLayout::Texture2D>();
vb.Back().Attr<VertexLayout::Position3D>().z = 420.0f;
pos = vb.Back().Attr<VertexLayout::Position3D>();
const auto& cvb = vb;
pos = cvb[1].Attr<VertexLayout::Position3D>();
}
App::App() App::App()
: :
wnd( 800,600,"The Donkey Fart Box" ), wnd( 800,600,"The Donkey Fart Box" ),
light( wnd.Gfx() ) light( wnd.Gfx() )
{ {
f();
class Factory class Factory
{ {
public: public:
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include <assimp/Importer.hpp> #include <assimp/Importer.hpp>
#include <assimp/scene.h> #include <assimp/scene.h>
#include <assimp/postprocess.h> #include <assimp/postprocess.h>
#include "Vertex.h"
AssTest::AssTest( Graphics& gfx,std::mt19937& rng, AssTest::AssTest( Graphics& gfx,std::mt19937& rng,
std::uniform_real_distribution<float>& adist, std::uniform_real_distribution<float>& adist,
...@@ -19,11 +20,12 @@ AssTest::AssTest( Graphics& gfx,std::mt19937& rng, ...@@ -19,11 +20,12 @@ AssTest::AssTest( Graphics& gfx,std::mt19937& rng,
if( !IsStaticInitialized() ) if( !IsStaticInitialized() )
{ {
struct Vertex using hw3dexp::VertexLayout;
{ hw3dexp::VertexBuffer vbuf( std::move(
dx::XMFLOAT3 pos; VertexLayout{}
dx::XMFLOAT3 n; .Append<VertexLayout::Position3D>()
}; .Append<VertexLayout::Normal>()
));
Assimp::Importer imp; Assimp::Importer imp;
const auto pModel = imp.ReadFile( "models\\suzanne.obj", const auto pModel = imp.ReadFile( "models\\suzanne.obj",
...@@ -32,14 +34,12 @@ AssTest::AssTest( Graphics& gfx,std::mt19937& rng, ...@@ -32,14 +34,12 @@ AssTest::AssTest( Graphics& gfx,std::mt19937& rng,
); );
const auto pMesh = pModel->mMeshes[0]; const auto pMesh = pModel->mMeshes[0];
std::vector<Vertex> vertices;
vertices.reserve( pMesh->mNumVertices );
for( unsigned int i = 0; i < pMesh->mNumVertices; i++ ) for( unsigned int i = 0; i < pMesh->mNumVertices; i++ )
{ {
vertices.push_back( { vbuf.EmplaceBack(
{ pMesh->mVertices[i].x * scale,pMesh->mVertices[i].y * scale,pMesh->mVertices[i].z * scale }, dx::XMFLOAT3{ pMesh->mVertices[i].x * scale,pMesh->mVertices[i].y * scale,pMesh->mVertices[i].z * scale },
*reinterpret_cast<dx::XMFLOAT3*>(&pMesh->mNormals[i]) *reinterpret_cast<dx::XMFLOAT3*>(&pMesh->mNormals[i])
} ); );
} }
std::vector<unsigned short> indices; std::vector<unsigned short> indices;
...@@ -53,7 +53,7 @@ AssTest::AssTest( Graphics& gfx,std::mt19937& rng, ...@@ -53,7 +53,7 @@ AssTest::AssTest( Graphics& gfx,std::mt19937& rng,
indices.push_back( face.mIndices[2] ); indices.push_back( face.mIndices[2] );
} }
AddStaticBind( std::make_unique<VertexBuffer>( gfx,vertices ) ); AddStaticBind( std::make_unique<VertexBuffer>( gfx,vbuf ) );
AddStaticIndexBuffer( std::make_unique<IndexBuffer>( gfx,indices ) ); AddStaticIndexBuffer( std::make_unique<IndexBuffer>( gfx,indices ) );
......
...@@ -3,17 +3,19 @@ ...@@ -3,17 +3,19 @@
#include <DirectXMath.h> #include <DirectXMath.h>
#include <type_traits> #include <type_traits>
struct BGRAColor namespace hw3dexp
{ {
struct BGRAColor
{
unsigned char a; unsigned char a;
unsigned char r; unsigned char r;
unsigned char g; unsigned char g;
unsigned char b; unsigned char b;
}; };
class VertexLayout class VertexLayout
{ {
public: public:
enum ElementType enum ElementType
{ {
Position2D, Position2D,
...@@ -62,7 +64,7 @@ public: ...@@ -62,7 +64,7 @@ public:
case Float4Color: case Float4Color:
return sizeof( XMFLOAT3 ); return sizeof( XMFLOAT3 );
case BGRAColor: case BGRAColor:
return sizeof( ::BGRAColor ); return sizeof( hw3dexp::BGRAColor );
} }
assert( "Invalid element type" && false ); assert( "Invalid element type" && false );
return 0u; return 0u;
...@@ -75,7 +77,7 @@ public: ...@@ -75,7 +77,7 @@ public:
ElementType type; ElementType type;
size_t offset; size_t offset;
}; };
public: public:
template<ElementType Type> template<ElementType Type>
const Element& Resolve() const noexcept(!IS_DEBUG) const Element& Resolve() const noexcept(!IS_DEBUG)
{ {
...@@ -107,14 +109,14 @@ public: ...@@ -107,14 +109,14 @@ public:
{ {
return elements.size(); return elements.size();
} }
private: private:
std::vector<Element> elements; std::vector<Element> elements;
}; };
class Vertex class Vertex
{ {
friend class VertexBuffer; friend class VertexBuffer;
public: public:
template<VertexLayout::ElementType Type> template<VertexLayout::ElementType Type>
auto& Attr() noexcept(!IS_DEBUG) auto& Attr() noexcept(!IS_DEBUG)
{ {
...@@ -188,7 +190,7 @@ public: ...@@ -188,7 +190,7 @@ public:
assert( "Bad element type" && false ); assert( "Bad element type" && false );
} }
} }
protected: protected:
Vertex( char* pData,const VertexLayout& layout ) noexcept(!IS_DEBUG) Vertex( char* pData,const VertexLayout& layout ) noexcept(!IS_DEBUG)
: :
pData( pData ), pData( pData ),
...@@ -196,7 +198,7 @@ protected: ...@@ -196,7 +198,7 @@ protected:
{ {
assert( pData != nullptr ); assert( pData != nullptr );
} }
private: private:
template<typename First,typename ...Rest> template<typename First,typename ...Rest>
// enables parameter pack setting of multiple parameters by element index // enables parameter pack setting of multiple parameters by element index
void SetAttributeByIndex( size_t i,First&& first,Rest&&... rest ) noexcept(!IS_DEBUG) void SetAttributeByIndex( size_t i,First&& first,Rest&&... rest ) noexcept(!IS_DEBUG)
...@@ -217,14 +219,14 @@ private: ...@@ -217,14 +219,14 @@ private:
assert( "Parameter attribute type mismatch" && false ); assert( "Parameter attribute type mismatch" && false );
} }
} }
private: private:
char* pData = nullptr; char* pData = nullptr;
const VertexLayout& layout; const VertexLayout& layout;
}; };
class ConstVertex class ConstVertex
{ {
public: public:
ConstVertex( const Vertex& v ) noexcept(!IS_DEBUG) ConstVertex( const Vertex& v ) noexcept(!IS_DEBUG)
: :
vertex( v ) vertex( v )
...@@ -234,17 +236,21 @@ public: ...@@ -234,17 +236,21 @@ public:
{ {
return const_cast<Vertex&>(vertex).Attr<Type>(); return const_cast<Vertex&>(vertex).Attr<Type>();
} }
private: private:
Vertex vertex; Vertex vertex;
}; };
class VertexBuffer class VertexBuffer
{ {
public: public:
VertexBuffer( VertexLayout layout ) noexcept(!IS_DEBUG) VertexBuffer( VertexLayout layout ) noexcept(!IS_DEBUG)
: :
layout( std::move( layout ) ) layout( std::move( layout ) )
{} {}
const char* GetData() const noexcept(!IS_DEBUG)
{
return buffer.data();
}
const VertexLayout& GetLayout() const noexcept const VertexLayout& GetLayout() const noexcept
{ {
return layout; return layout;
...@@ -287,7 +293,8 @@ public: ...@@ -287,7 +293,8 @@ public:
{ {
return const_cast<VertexBuffer&>(*this)[i]; return const_cast<VertexBuffer&>(*this)[i];
} }
private: private:
std::vector<char> buffer; std::vector<char> buffer;
VertexLayout layout; VertexLayout layout;
}; };
\ No newline at end of file }
\ No newline at end of file
#pragma once #pragma once
#include "Bindable.h" #include "Bindable.h"
#include "GraphicsThrowMacros.h" #include "GraphicsThrowMacros.h"
#include "Vertex.h"
class VertexBuffer : public Bindable class VertexBuffer : public Bindable
{ {
...@@ -23,6 +24,23 @@ public: ...@@ -23,6 +24,23 @@ public:
sd.pSysMem = vertices.data(); sd.pSysMem = vertices.data();
GFX_THROW_INFO( GetDevice( gfx )->CreateBuffer( &bd,&sd,&pVertexBuffer ) ); GFX_THROW_INFO( GetDevice( gfx )->CreateBuffer( &bd,&sd,&pVertexBuffer ) );
} }
VertexBuffer( Graphics& gfx,const hw3dexp::VertexBuffer& vbuf )
:
stride( (UINT)vbuf.GetLayout().Size() )
{
INFOMAN( gfx );
D3D11_BUFFER_DESC bd = {};
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.Usage = D3D11_USAGE_DEFAULT;
bd.CPUAccessFlags = 0u;
bd.MiscFlags = 0u;
bd.ByteWidth = UINT( vbuf.Size() );
bd.StructureByteStride = stride;
D3D11_SUBRESOURCE_DATA sd = {};
sd.pSysMem = vbuf.GetData();
GFX_THROW_INFO( GetDevice( gfx )->CreateBuffer( &bd,&sd,&pVertexBuffer ) );
}
void Bind( Graphics& gfx ) noexcept override; void Bind( Graphics& gfx ) noexcept override;
protected: protected:
UINT stride; UINT stride;
......
...@@ -182,8 +182,8 @@ ...@@ -182,8 +182,8 @@
<ClInclude Include="Texture.h" /> <ClInclude Include="Texture.h" />
<ClInclude Include="Topology.h" /> <ClInclude Include="Topology.h" />
<ClInclude Include="TransformCbuf.h" /> <ClInclude Include="TransformCbuf.h" />
<ClInclude Include="Vertex.h" />
<ClInclude Include="VertexBuffer.h" /> <ClInclude Include="VertexBuffer.h" />
<ClInclude Include="VertexLayout.h" />
<ClInclude Include="VertexShader.h" /> <ClInclude Include="VertexShader.h" />
<ClInclude Include="Window.h" /> <ClInclude Include="Window.h" />
<ClInclude Include="WindowsMessageMap.h" /> <ClInclude Include="WindowsMessageMap.h" />
......
...@@ -326,7 +326,7 @@ ...@@ -326,7 +326,7 @@
<ClInclude Include="AssTest.h"> <ClInclude Include="AssTest.h">
<Filter>Header Files\Drawable</Filter> <Filter>Header Files\Drawable</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="VertexLayout.h"> <ClInclude Include="Vertex.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>
......
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