Added cone primitive generation

Lacks normal/tangents/uv


Former-commit-id: 682bb7ea926361bfb10807f6addf98f8fd4e7d75
This commit is contained in:
Lynix
2013-12-09 14:15:34 +01:00
parent b05f813e70
commit a9af5523ee
8 changed files with 134 additions and 2 deletions

View File

@@ -659,6 +659,15 @@ unsigned int NzComputeCacheMissCount(NzIndexIterator indices, unsigned int index
return cache.GetMissCount();
}
void NzComputeConeIndexVertexCount(unsigned int subdivision, unsigned int* indexCount, unsigned int* vertexCount)
{
if (indexCount)
*indexCount = (subdivision-1)*6;
if (vertexCount)
*vertexCount = subdivision + 2;
}
void NzComputeCubicSphereIndexVertexCount(unsigned int subdivision, unsigned int* indexCount, unsigned int* vertexCount)
{
// Comme tous nos plans sont identiques, on peut optimiser un peu
@@ -768,7 +777,53 @@ void NzGenerateBox(const NzVector3f& lengths, const NzVector3ui& subdivision, co
if (aabb)
{
aabb->Set(-halfLengths, halfLengths);
aabb->Transform(matrix, 0.f);
aabb->Transform(matrix, false);
}
}
void NzGenerateCone(float length, float radius, unsigned int subdivision, const NzMatrix4f& matrix, const NzRectf& textureCoords, NzMeshVertex* vertices, NzIndexIterator indices, NzBoxf* aabb, unsigned int indexOffset)
{
const float round = 2.f*static_cast<float>(M_PI);
float delta = round/subdivision;
vertices->position = matrix.GetTranslation(); // matrix.Transform(NzVector3f(0.f));
vertices->normal = matrix.Transform(NzVector3f::Up(), 0.f);
vertices++;
for (unsigned int i = 0; i < subdivision; ++i)
{
float angle = delta*i;
vertices->position = matrix.Transform(NzVector3f(radius*std::sin(angle), -length, radius*std::cos(angle)));
vertices++;
*indices++ = indexOffset + 0;
*indices++ = indexOffset + i+1;
*indices++ = indexOffset + ((i != subdivision-1) ? i+2 : 1);
if (i != 0 && i != subdivision-1)
{
*indices++ = indexOffset + ((i != subdivision-1) ? i+2 : 1);
*indices++ = indexOffset + i+1;
*indices++ = indexOffset + 1;
}
}
if (aabb)
{
aabb->MakeZero();
// On calcule le reste des points
NzVector3f base(NzVector3f::Down()*length);
NzVector3f lExtend = NzVector3f::Left()*radius;
NzVector3f fExtend = NzVector3f::Forward()*radius;
// Et on ajoute ensuite les quatres extrémités de la pyramide
aabb->ExtendTo(base + lExtend + fExtend);
aabb->ExtendTo(base + lExtend - fExtend);
aabb->ExtendTo(base - lExtend + fExtend);
aabb->ExtendTo(base - lExtend - fExtend);
aabb->Transform(matrix, false);
}
}
@@ -788,8 +843,8 @@ void NzGenerateCubicSphere(float size, unsigned int subdivision, const NzMatrix4
for (unsigned int i = 0; i < vertexCount; ++i)
{
vertices->position = matrix.Transform(size * vertices->position.GetNormal());
vertices->normal = vertices->position.GetNormal();
vertices->position = matrix.Transform(size * vertices->normal);
//vertices->tangent = ???
vertices++;
}

View File

@@ -195,6 +195,25 @@ NzSubMesh* NzMesh::BuildSubMesh(const NzPrimitive& primitive, const NzMeshParams
break;
}
case nzPrimitiveType_Cone:
{
unsigned int indexCount;
unsigned int vertexCount;
NzComputeConeIndexVertexCount(primitive.cone.subdivision, &indexCount, &vertexCount);
indexBuffer.reset(new NzIndexBuffer(vertexCount > std::numeric_limits<nzUInt16>::max(), indexCount, params.storage, nzBufferUsage_Static));
indexBuffer->SetPersistent(false);
vertexBuffer.reset(new NzVertexBuffer(declaration, vertexCount, params.storage, nzBufferUsage_Static));
vertexBuffer->SetPersistent(false);
NzBufferMapper<NzVertexBuffer> vertexMapper(vertexBuffer.get(), nzBufferAccess_WriteOnly);
NzIndexMapper indexMapper(indexBuffer.get(), nzBufferAccess_WriteOnly);
NzGenerateCone(primitive.cone.length, primitive.cone.radius, primitive.cone.subdivision, matrix, primitive.textureCoords, static_cast<NzMeshVertex*>(vertexMapper.GetPointer()), indexMapper.begin(), &aabb);
break;
}
case nzPrimitiveType_Plane:
{
unsigned int indexCount;