mirror of
https://github.com/luanti-org/luanti.git
synced 2025-09-15 18:57:08 +00:00
Split CIndexBuffer from CMeshBuffer
This commit is contained in:
parent
5d6e15bc49
commit
47e4c33a50
13 changed files with 197 additions and 104 deletions
89
irr/include/CIndexBuffer.h
Normal file
89
irr/include/CIndexBuffer.h
Normal file
|
@ -0,0 +1,89 @@
|
|||
// Copyright (C) 2002-2012 Nikolaus Gebhardt
|
||||
// This file is part of the "Irrlicht Engine".
|
||||
// For conditions of distribution and use, see copyright notice in irrlicht.h
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include "IIndexBuffer.h"
|
||||
|
||||
namespace irr
|
||||
{
|
||||
namespace scene
|
||||
{
|
||||
//! Template implementation of the IIndexBuffer interface
|
||||
template <class T>
|
||||
class CIndexBuffer : public IIndexBuffer
|
||||
{
|
||||
public:
|
||||
//! Default constructor for empty buffer
|
||||
CIndexBuffer()
|
||||
{
|
||||
#ifdef _DEBUG
|
||||
setDebugName("CIndexBuffer");
|
||||
#endif
|
||||
}
|
||||
|
||||
video::E_INDEX_TYPE getType() const
|
||||
{
|
||||
static_assert(sizeof(T) == 2 || sizeof(T) == 4, "invalid index type");
|
||||
return sizeof(T) == 2 ? video::EIT_16BIT : video::EIT_32BIT;
|
||||
}
|
||||
|
||||
const void *getData() const override
|
||||
{
|
||||
return Data.data();
|
||||
}
|
||||
|
||||
void *getData() override
|
||||
{
|
||||
return Data.data();
|
||||
}
|
||||
|
||||
u32 getCount() const override
|
||||
{
|
||||
return static_cast<u32>(Data.size());
|
||||
}
|
||||
|
||||
E_HARDWARE_MAPPING getHardwareMappingHint() const override
|
||||
{
|
||||
return MappingHint;
|
||||
}
|
||||
|
||||
void setHardwareMappingHint(E_HARDWARE_MAPPING NewMappingHint) override
|
||||
{
|
||||
MappingHint = NewMappingHint;
|
||||
}
|
||||
|
||||
void setDirty() override
|
||||
{
|
||||
++ChangedID;
|
||||
}
|
||||
|
||||
u32 getChangedID() const override { return ChangedID; }
|
||||
|
||||
void setHWBuffer(void *ptr) const override
|
||||
{
|
||||
HWBuffer = ptr;
|
||||
}
|
||||
|
||||
void *getHWBuffer() const override
|
||||
{
|
||||
return HWBuffer;
|
||||
}
|
||||
|
||||
u32 ChangedID = 1;
|
||||
|
||||
//! hardware mapping hint
|
||||
E_HARDWARE_MAPPING MappingHint = EHM_NEVER;
|
||||
mutable void *HWBuffer = nullptr;
|
||||
|
||||
//! Indices of this buffer
|
||||
std::vector<T> Data;
|
||||
};
|
||||
|
||||
//! Standard 16-bit buffer
|
||||
typedef CIndexBuffer<u16> SIndexBuffer;
|
||||
|
||||
} // end namespace scene
|
||||
} // end namespace irr
|
|
@ -7,6 +7,7 @@
|
|||
#include <vector>
|
||||
#include "IMeshBuffer.h"
|
||||
#include "CVertexBuffer.h"
|
||||
#include "CIndexBuffer.h"
|
||||
|
||||
namespace irr
|
||||
{
|
||||
|
@ -19,17 +20,19 @@ class CMeshBuffer : public IMeshBuffer
|
|||
public:
|
||||
//! Default constructor for empty meshbuffer
|
||||
CMeshBuffer() :
|
||||
ChangedID_Index(1), MappingHint_Index(EHM_NEVER), HWBuffer(NULL), PrimitiveType(EPT_TRIANGLES)
|
||||
HWBuffer(NULL), PrimitiveType(EPT_TRIANGLES)
|
||||
{
|
||||
#ifdef _DEBUG
|
||||
setDebugName("CMeshBuffer");
|
||||
#endif
|
||||
Vertices = new CVertexBuffer<T>();
|
||||
Indices = new SIndexBuffer();
|
||||
}
|
||||
|
||||
~CMeshBuffer()
|
||||
{
|
||||
Vertices->drop();
|
||||
Indices->drop();
|
||||
}
|
||||
|
||||
//! Get material of this meshbuffer
|
||||
|
@ -77,28 +80,34 @@ public:
|
|||
/** \return Index type of this buffer. */
|
||||
video::E_INDEX_TYPE getIndexType() const override
|
||||
{
|
||||
return video::EIT_16BIT;
|
||||
return Indices->getType();
|
||||
}
|
||||
|
||||
//! Get pointer to indices
|
||||
/** \return Pointer to indices. */
|
||||
const u16 *getIndices() const override
|
||||
{
|
||||
return Indices.data();
|
||||
return static_cast<const u16*>(Indices->getData());
|
||||
}
|
||||
|
||||
//! Get pointer to indices
|
||||
/** \return Pointer to indices. */
|
||||
u16 *getIndices() override
|
||||
{
|
||||
return Indices.data();
|
||||
return static_cast<u16*>(Indices->getData());
|
||||
}
|
||||
|
||||
//! Get number of indices
|
||||
/** \return Number of indices. */
|
||||
u32 getIndexCount() const override
|
||||
{
|
||||
return static_cast<u32>(Indices.size());
|
||||
return Indices->getCount();
|
||||
}
|
||||
|
||||
// TEMPORARY helper for direct buffer acess
|
||||
inline auto &IndexBuffer()
|
||||
{
|
||||
return Indices->Data;
|
||||
}
|
||||
|
||||
//! Get the axis aligned bounding box
|
||||
|
@ -186,10 +195,10 @@ public:
|
|||
for (u32 i = vertexCount; i < getVertexCount(); i++)
|
||||
BoundingBox.addInternalPoint(Vertices->getPosition(i));
|
||||
|
||||
Indices.insert(Indices.end(), indices, indices + numIndices);
|
||||
Indices->Data.insert(Indices->Data.end(), indices, indices + numIndices);
|
||||
if (vertexCount != 0) {
|
||||
for (u32 i = indexCount; i < getIndexCount(); i++)
|
||||
Indices[i] += vertexCount;
|
||||
Indices->Data[i] += vertexCount;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -202,7 +211,7 @@ public:
|
|||
//! get the current hardware mapping hint
|
||||
E_HARDWARE_MAPPING getHardwareMappingHint_Index() const override
|
||||
{
|
||||
return MappingHint_Index;
|
||||
return Indices->getHardwareMappingHint();
|
||||
}
|
||||
|
||||
//! set the hardware mapping hint, for driver
|
||||
|
@ -211,7 +220,7 @@ public:
|
|||
if (Buffer == EBT_VERTEX_AND_INDEX || Buffer == EBT_VERTEX)
|
||||
Vertices->setHardwareMappingHint(NewMappingHint);
|
||||
if (Buffer == EBT_VERTEX_AND_INDEX || Buffer == EBT_INDEX)
|
||||
MappingHint_Index = NewMappingHint;
|
||||
Indices->setHardwareMappingHint(NewMappingHint);
|
||||
}
|
||||
|
||||
//! Describe what kind of primitive geometry is used by the meshbuffer
|
||||
|
@ -232,7 +241,7 @@ public:
|
|||
if (Buffer == EBT_VERTEX_AND_INDEX || Buffer == EBT_VERTEX)
|
||||
Vertices->setDirty();
|
||||
if (Buffer == EBT_VERTEX_AND_INDEX || Buffer == EBT_INDEX)
|
||||
++ChangedID_Index;
|
||||
Indices->setDirty();
|
||||
}
|
||||
|
||||
//! Get the currently used ID for identification of changes.
|
||||
|
@ -241,7 +250,7 @@ public:
|
|||
|
||||
//! Get the currently used ID for identification of changes.
|
||||
/** This shouldn't be used for anything outside the VideoDriver. */
|
||||
u32 getChangedID_Index() const override { return ChangedID_Index; }
|
||||
u32 getChangedID_Index() const override { return Indices->getChangedID(); }
|
||||
|
||||
void setHWBuffer(void *ptr) const override
|
||||
{
|
||||
|
@ -253,17 +262,14 @@ public:
|
|||
return HWBuffer;
|
||||
}
|
||||
|
||||
u32 ChangedID_Index;
|
||||
|
||||
E_HARDWARE_MAPPING MappingHint_Index;
|
||||
mutable void *HWBuffer;
|
||||
|
||||
//! Material for this meshbuffer.
|
||||
video::SMaterial Material;
|
||||
//! Vertex buffer
|
||||
CVertexBuffer<T> *Vertices;
|
||||
//! Indices into the vertices of this buffer.
|
||||
std::vector<u16> Indices;
|
||||
//! Index buffer
|
||||
SIndexBuffer *Indices;
|
||||
//! Bounding box of this meshbuffer.
|
||||
core::aabbox3d<f32> BoundingBox;
|
||||
//! Primitive type used for rendering (triangles, lines, ...)
|
||||
|
|
|
@ -12,40 +12,33 @@
|
|||
namespace irr
|
||||
{
|
||||
|
||||
namespace video
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
namespace scene
|
||||
{
|
||||
|
||||
class IIndexBuffer : public virtual IReferenceCounted
|
||||
{
|
||||
public:
|
||||
//! Get type of index data which is stored in this meshbuffer.
|
||||
/** \return Index type of this buffer. */
|
||||
virtual video::E_INDEX_TYPE getType() const = 0;
|
||||
|
||||
//! Get access to indices.
|
||||
/** \return Pointer to indices array. */
|
||||
virtual const void *getData() const = 0;
|
||||
|
||||
//! Get access to indices.
|
||||
/** \return Pointer to indices array. */
|
||||
virtual void *getData() = 0;
|
||||
|
||||
virtual video::E_INDEX_TYPE getType() const = 0;
|
||||
virtual void setType(video::E_INDEX_TYPE IndexType) = 0;
|
||||
|
||||
virtual u32 stride() const = 0;
|
||||
|
||||
virtual u32 size() const = 0;
|
||||
virtual void push_back(const u32 &element) = 0;
|
||||
virtual u32 operator[](u32 index) const = 0;
|
||||
virtual u32 getLast() = 0;
|
||||
virtual void setValue(u32 index, u32 value) = 0;
|
||||
virtual void set_used(u32 usedNow) = 0;
|
||||
virtual void reallocate(u32 new_size) = 0;
|
||||
virtual u32 allocated_size() const = 0;
|
||||
|
||||
virtual void *pointer() = 0;
|
||||
//! Get amount of indices in this meshbuffer.
|
||||
/** \return Number of indices in this buffer. */
|
||||
virtual u32 getCount() const = 0;
|
||||
|
||||
//! get the current hardware mapping hint
|
||||
virtual E_HARDWARE_MAPPING getHardwareMappingHint() const = 0;
|
||||
|
||||
//! set the hardware mapping hint, for driver
|
||||
virtual void setHardwareMappingHint(E_HARDWARE_MAPPING NewMappingHint) = 0;
|
||||
virtual void setHardwareMappingHint(E_HARDWARE_MAPPING newMappingHint) = 0;
|
||||
|
||||
//! flags the meshbuffer as changed, reloads hardware buffers
|
||||
virtual void setDirty() = 0;
|
||||
|
@ -53,6 +46,10 @@ public:
|
|||
//! Get the currently used ID for identification of changes.
|
||||
/** This shouldn't be used for anything outside the VideoDriver. */
|
||||
virtual u32 getChangedID() const = 0;
|
||||
|
||||
//! Used by the VideoDriver to remember the buffer link.
|
||||
virtual void setHWBuffer(void *ptr) const = 0;
|
||||
virtual void *getHWBuffer() const = 0;
|
||||
};
|
||||
|
||||
} // end namespace scene
|
||||
|
|
|
@ -27,16 +27,17 @@ CBillboardSceneNode::CBillboardSceneNode(ISceneNode *parent, ISceneManager *mgr,
|
|||
setSize(size);
|
||||
|
||||
auto &Vertices = Buffer->Vertices->Data;
|
||||
auto &Indices = Buffer->Indices->Data;
|
||||
|
||||
Vertices.resize(4);
|
||||
Buffer->Indices.resize(6);
|
||||
Indices.resize(6);
|
||||
|
||||
Buffer->Indices[0] = 0;
|
||||
Buffer->Indices[1] = 2;
|
||||
Buffer->Indices[2] = 1;
|
||||
Buffer->Indices[3] = 0;
|
||||
Buffer->Indices[4] = 3;
|
||||
Buffer->Indices[5] = 2;
|
||||
Indices[0] = 0;
|
||||
Indices[1] = 2;
|
||||
Indices[2] = 1;
|
||||
Indices[3] = 0;
|
||||
Indices[4] = 3;
|
||||
Indices[5] = 2;
|
||||
|
||||
Vertices[0].TCoords.set(1.0f, 1.0f);
|
||||
Vertices[0].Color = colorBottom;
|
||||
|
|
|
@ -135,7 +135,7 @@ SMesh *CMeshManipulator::createMeshCopy(scene::IMesh *mesh) const
|
|||
auto *vt = static_cast<const video::S3DVertex*>(mb->getVertices());
|
||||
buffer->VertexBuffer().insert(buffer->VertexBuffer().end(), vt, vt + mb->getVertexCount());
|
||||
auto *indices = mb->getIndices();
|
||||
buffer->Indices.insert(buffer->Indices.end(), indices, indices + mb->getIndexCount());
|
||||
buffer->IndexBuffer().insert(buffer->IndexBuffer().end(), indices, indices + mb->getIndexCount());
|
||||
clone->addMeshBuffer(buffer);
|
||||
buffer->drop();
|
||||
} break;
|
||||
|
@ -145,7 +145,7 @@ SMesh *CMeshManipulator::createMeshCopy(scene::IMesh *mesh) const
|
|||
auto *vt = static_cast<const video::S3DVertex2TCoords*>(mb->getVertices());
|
||||
buffer->VertexBuffer().insert(buffer->VertexBuffer().end(), vt, vt + mb->getVertexCount());
|
||||
auto *indices = mb->getIndices();
|
||||
buffer->Indices.insert(buffer->Indices.end(), indices, indices + mb->getIndexCount());
|
||||
buffer->IndexBuffer().insert(buffer->IndexBuffer().end(), indices, indices + mb->getIndexCount());
|
||||
clone->addMeshBuffer(buffer);
|
||||
buffer->drop();
|
||||
} break;
|
||||
|
@ -155,7 +155,7 @@ SMesh *CMeshManipulator::createMeshCopy(scene::IMesh *mesh) const
|
|||
auto *vt = static_cast<const video::S3DVertexTangents*>(mb->getVertices());
|
||||
buffer->VertexBuffer().insert(buffer->VertexBuffer().end(), vt, vt + mb->getVertexCount());
|
||||
auto *indices = mb->getIndices();
|
||||
buffer->Indices.insert(buffer->Indices.end(), indices, indices + mb->getIndexCount());
|
||||
buffer->IndexBuffer().insert(buffer->IndexBuffer().end(), indices, indices + mb->getIndexCount());
|
||||
clone->addMeshBuffer(buffer);
|
||||
buffer->drop();
|
||||
} break;
|
||||
|
|
|
@ -247,15 +247,16 @@ IAnimatedMesh *COBJMeshFileLoader::createMesh(io::IReadFile *file)
|
|||
}
|
||||
|
||||
// triangulate the face
|
||||
auto &Indices = currMtl->Meshbuffer->IndexBuffer();
|
||||
const int c = faceCorners[0];
|
||||
for (u32 i = 1; i < faceCorners.size() - 1; ++i) {
|
||||
// Add a triangle
|
||||
const int a = faceCorners[i + 1];
|
||||
const int b = faceCorners[i];
|
||||
if (a != b && a != c && b != c) { // ignore degenerated faces. We can get them when we merge vertices above in the VertMap.
|
||||
currMtl->Meshbuffer->Indices.push_back(a);
|
||||
currMtl->Meshbuffer->Indices.push_back(b);
|
||||
currMtl->Meshbuffer->Indices.push_back(c);
|
||||
Indices.push_back(a);
|
||||
Indices.push_back(b);
|
||||
Indices.push_back(c);
|
||||
} else {
|
||||
++degeneratedFaces;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue