Commit ce1e5732 authored by chili's avatar chili
Browse files

using dynamic vtx layout for suzanne asstest

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