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

migrate solid sphere over to dvtx

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