Commit 9cd80da7 authored by chili's avatar chili
Browse files

migrate solid sphere over to dvtx

parent c60c4cb1
#pragma once #pragma once
#include "Vertex.h"
#include <vector> #include <vector>
#include <DirectXMath.h> #include <DirectXMath.h>
template<class T>
class IndexedTriangleList class IndexedTriangleList
{ {
public: public:
IndexedTriangleList() = default; IndexedTriangleList() = default;
IndexedTriangleList( std::vector<T> verts_in,std::vector<unsigned short> indices_in ) IndexedTriangleList( Dvtx::VertexBuffer verts_in,std::vector<unsigned short> indices_in )
: :
vertices( std::move( verts_in ) ), vertices( std::move( verts_in ) ),
indices( std::move( indices_in ) ) indices( std::move( indices_in ) )
{ {
assert( vertices.size() > 2 ); assert( vertices.Size() > 2 );
assert( indices.size() % 3 == 0 ); assert( indices.size() % 3 == 0 );
} }
void Transform( DirectX::FXMMATRIX matrix ) void Transform( DirectX::FXMMATRIX matrix )
{ {
for( auto& v : vertices ) using Elements = Dvtx::VertexLayout::ElementType;
for( int i = 0; i < vertices.Size(); i++ )
{ {
const DirectX::XMVECTOR pos = DirectX::XMLoadFloat3( &v.pos ); auto& pos = vertices[i].Attr<Elements::Position3D>();
DirectX::XMStoreFloat3( DirectX::XMStoreFloat3(
&v.pos, &pos,
DirectX::XMVector3Transform( pos,matrix ) DirectX::XMVector3Transform( DirectX::XMLoadFloat3( &pos ),matrix )
); );
} }
} }
// asserts face-independent vertices w/ normals cleared to zero //// asserts face-independent vertices w/ normals cleared to zero
void SetNormalsIndependentFlat() noxnd //void SetNormalsIndependentFlat() noxnd
{ //{
using namespace DirectX; // using namespace DirectX;
assert( indices.size() % 3 == 0 && indices.size() > 0 ); // for( size_t i = 0; i < indices.size(); i += 3 )
for( size_t i = 0; i < indices.size(); i += 3 ) // {
{ // auto& v0 = vertices[indices[i]];
auto& v0 = vertices[indices[i]]; // auto& v1 = vertices[indices[i + 1]];
auto& v1 = vertices[indices[i + 1]]; // auto& v2 = vertices[indices[i + 2]];
auto& v2 = vertices[indices[i + 2]]; // const auto p0 = XMLoadFloat3( &v0.pos );
const auto p0 = XMLoadFloat3( &v0.pos ); // const auto p1 = XMLoadFloat3( &v1.pos );
const auto p1 = XMLoadFloat3( &v1.pos ); // const auto p2 = XMLoadFloat3( &v2.pos );
const auto p2 = XMLoadFloat3( &v2.pos );
const auto n = XMVector3Normalize( XMVector3Cross( (p1 - p0),(p2 - p0) ) ); // const auto n = XMVector3Normalize( XMVector3Cross( (p1 - p0),(p2 - p0) ) );
//
XMStoreFloat3( &v0.n,n ); // XMStoreFloat3( &v0.n,n );
XMStoreFloat3( &v1.n,n ); // XMStoreFloat3( &v1.n,n );
XMStoreFloat3( &v2.n,n ); // XMStoreFloat3( &v2.n,n );
} // }
} //}
public: public:
std::vector<T> vertices; Dvtx::VertexBuffer vertices;
std::vector<unsigned short> indices; std::vector<unsigned short> indices;
}; };
\ No newline at end of file
#include "SolidSphere.h" #include "SolidSphere.h"
#include "BindableCommon.h" #include "BindableCommon.h"
#include "GraphicsThrowMacros.h" #include "GraphicsThrowMacros.h"
#include "Vertex.h"
#include "Sphere.h" #include "Sphere.h"
...@@ -8,12 +9,8 @@ SolidSphere::SolidSphere( Graphics& gfx,float radius ) ...@@ -8,12 +9,8 @@ SolidSphere::SolidSphere( Graphics& gfx,float radius )
{ {
using namespace Bind; using namespace Bind;
namespace dx = DirectX; namespace dx = DirectX;
struct Vertex auto model = Sphere::Make();
{
dx::XMFLOAT3 pos;
};
auto model = Sphere::Make<Vertex>();
model.Transform( dx::XMMatrixScaling( radius,radius,radius ) ); model.Transform( dx::XMMatrixScaling( radius,radius,radius ) );
AddBind( std::make_shared<VertexBuffer>( gfx,model.vertices ) ); AddBind( std::make_shared<VertexBuffer>( gfx,model.vertices ) );
AddBind( std::make_shared<IndexBuffer>( gfx,model.indices ) ); AddBind( std::make_shared<IndexBuffer>( gfx,model.indices ) );
...@@ -31,11 +28,7 @@ SolidSphere::SolidSphere( Graphics& gfx,float radius ) ...@@ -31,11 +28,7 @@ SolidSphere::SolidSphere( Graphics& gfx,float radius )
} colorConst; } colorConst;
AddBind( std::make_shared<PixelConstantBuffer<PSColorConstant>>( gfx,colorConst ) ); AddBind( std::make_shared<PixelConstantBuffer<PSColorConstant>>( gfx,colorConst ) );
const std::vector<D3D11_INPUT_ELEMENT_DESC> ied = AddBind( std::make_shared<InputLayout>( gfx,model.vertices.GetLayout().GetD3DLayout(),pvsbc ) );
{
{ "Position",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
};
AddBind( std::make_shared<InputLayout>( gfx,ied,pvsbc ) );
AddBind( std::make_shared<Topology>( gfx,D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ) ); AddBind( std::make_shared<Topology>( gfx,D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ) );
......
#pragma once #pragma once
#include <optional>
#include "Vertex.h"
#include "IndexedTriangleList.h" #include "IndexedTriangleList.h"
#include <DirectXMath.h> #include <DirectXMath.h>
#include "ChiliMath.h" #include "ChiliMath.h"
...@@ -6,8 +8,7 @@ ...@@ -6,8 +8,7 @@
class Sphere class Sphere
{ {
public: public:
template<class V> static IndexedTriangleList MakeTesselated( Dvtx::VertexLayout layout,int latDiv,int longDiv )
static IndexedTriangleList<V> MakeTesselated( int latDiv,int longDiv )
{ {
namespace dx = DirectX; namespace dx = DirectX;
assert( latDiv >= 3 ); assert( latDiv >= 3 );
...@@ -18,7 +19,7 @@ public: ...@@ -18,7 +19,7 @@ public:
const float lattitudeAngle = PI / latDiv; const float lattitudeAngle = PI / latDiv;
const float longitudeAngle = 2.0f * PI / longDiv; const float longitudeAngle = 2.0f * PI / longDiv;
std::vector<V> vertices; Dvtx::VertexBuffer vb{ std::move( layout ) };
for( int iLat = 1; iLat < latDiv; iLat++ ) for( int iLat = 1; iLat < latDiv; iLat++ )
{ {
const auto latBase = dx::XMVector3Transform( const auto latBase = dx::XMVector3Transform(
...@@ -27,22 +28,29 @@ public: ...@@ -27,22 +28,29 @@ public:
); );
for( int iLong = 0; iLong < longDiv; iLong++ ) for( int iLong = 0; iLong < longDiv; iLong++ )
{ {
vertices.emplace_back(); dx::XMFLOAT3 calculatedPos;
auto v = dx::XMVector3Transform( auto v = dx::XMVector3Transform(
latBase, latBase,
dx::XMMatrixRotationZ( longitudeAngle * iLong ) dx::XMMatrixRotationZ( longitudeAngle * iLong )
); );
dx::XMStoreFloat3( &vertices.back().pos,v ); dx::XMStoreFloat3( &calculatedPos,v );
vb.EmplaceBack( calculatedPos );
} }
} }
// add the cap vertices // add the cap vertices
const auto iNorthPole = (unsigned short)vertices.size(); const auto iNorthPole = (unsigned short)vb.Size();
vertices.emplace_back(); {
dx::XMStoreFloat3( &vertices.back().pos,base ); dx::XMFLOAT3 northPos;
const auto iSouthPole = (unsigned short)vertices.size(); dx::XMStoreFloat3( &northPos,base );
vertices.emplace_back(); vb.EmplaceBack( northPos );
dx::XMStoreFloat3( &vertices.back().pos,dx::XMVectorNegate( base ) ); }
const auto iSouthPole = (unsigned short)vb.Size();
{
dx::XMFLOAT3 southPos;
dx::XMStoreFloat3( &southPos,dx::XMVectorNegate( base ) );
vb.EmplaceBack( southPos );
}
const auto calcIdx = [latDiv,longDiv]( unsigned short iLat,unsigned short iLong ) const auto calcIdx = [latDiv,longDiv]( unsigned short iLat,unsigned short iLong )
{ return iLat * longDiv + iLong; }; { return iLat * longDiv + iLong; };
...@@ -89,11 +97,15 @@ public: ...@@ -89,11 +97,15 @@ public:
indices.push_back( calcIdx( latDiv - 2,longDiv - 1 ) ); indices.push_back( calcIdx( latDiv - 2,longDiv - 1 ) );
indices.push_back( iSouthPole ); indices.push_back( iSouthPole );
return { std::move( vertices ),std::move( indices ) }; return { std::move( vb ),std::move( indices ) };
} }
template<class V> static IndexedTriangleList Make( std::optional<Dvtx::VertexLayout> layout = std::nullopt )
static IndexedTriangleList<V> Make()
{ {
return MakeTesselated<V>( 12,24 ); using Element = Dvtx::VertexLayout::ElementType;
if( !layout )
{
layout = Dvtx::VertexLayout{}.Append( Element::Position3D );
}
return MakeTesselated( std::move( *layout ),12,24 );
} }
}; };
\ No newline at end of file
...@@ -8,24 +8,6 @@ namespace Bind ...@@ -8,24 +8,6 @@ namespace Bind
class VertexBuffer : public Bindable class VertexBuffer : public Bindable
{ {
public: public:
template<class V>
VertexBuffer( Graphics& gfx,const std::vector<V>& vertices )
:
stride( sizeof( V ) )
{
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( sizeof( V ) * vertices.size() );
bd.StructureByteStride = sizeof( V );
D3D11_SUBRESOURCE_DATA sd = {};
sd.pSysMem = vertices.data();
GFX_THROW_INFO( GetDevice( gfx )->CreateBuffer( &bd,&sd,&pVertexBuffer ) );
}
VertexBuffer( Graphics& gfx,const Dvtx::VertexBuffer& vbuf ); VertexBuffer( Graphics& gfx,const Dvtx::VertexBuffer& vbuf );
void Bind( Graphics& gfx ) noexcept override; void Bind( Graphics& gfx ) noexcept override;
protected: protected:
......
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