Split CVertexBuffer from CMeshBuffer

This commit is contained in:
sfan5 2024-08-27 16:50:41 +02:00
parent 538b8b9b34
commit 5d6e15bc49
10 changed files with 246 additions and 78 deletions

View File

@ -6,6 +6,7 @@
#include <vector>
#include "IMeshBuffer.h"
#include "CVertexBuffer.h"
namespace irr
{
@ -18,11 +19,17 @@ class CMeshBuffer : public IMeshBuffer
public:
//! Default constructor for empty meshbuffer
CMeshBuffer() :
ChangedID_Vertex(1), ChangedID_Index(1), MappingHint_Vertex(EHM_NEVER), MappingHint_Index(EHM_NEVER), HWBuffer(NULL), PrimitiveType(EPT_TRIANGLES)
ChangedID_Index(1), MappingHint_Index(EHM_NEVER), HWBuffer(NULL), PrimitiveType(EPT_TRIANGLES)
{
#ifdef _DEBUG
setDebugName("CMeshBuffer");
#endif
Vertices = new CVertexBuffer<T>();
}
~CMeshBuffer()
{
Vertices->drop();
}
//! Get material of this meshbuffer
@ -43,21 +50,27 @@ public:
/** \return Pointer to vertices. */
const void *getVertices() const override
{
return Vertices.data();
return Vertices->getData();
}
//! Get pointer to vertices
/** \return Pointer to vertices. */
void *getVertices() override
{
return Vertices.data();
return Vertices->getData();
}
//! Get number of vertices
/** \return Number of vertices. */
u32 getVertexCount() const override
{
return static_cast<u32>(Vertices.size());
return Vertices->getCount();
}
// TEMPORARY helper for direct buffer acess
inline auto &VertexBuffer()
{
return Vertices->Data;
}
//! Get type of index data which is stored in this meshbuffer.
@ -107,11 +120,11 @@ public:
/** should be called if the mesh changed. */
void recalculateBoundingBox() override
{
if (!Vertices.empty()) {
BoundingBox.reset(Vertices[0].Pos);
const irr::u32 vsize = Vertices.size();
if (Vertices->getCount()) {
BoundingBox.reset(Vertices->getPosition(0));
const irr::u32 vsize = Vertices->getCount();
for (u32 i = 1; i < vsize; ++i)
BoundingBox.addInternalPoint(Vertices[i].Pos);
BoundingBox.addInternalPoint(Vertices->getPosition(i));
} else
BoundingBox.reset(0, 0, 0);
}
@ -120,43 +133,43 @@ public:
/** \return Type of vertex data. */
video::E_VERTEX_TYPE getVertexType() const override
{
return T::getType();
return Vertices->getType();
}
//! returns position of vertex i
const core::vector3df &getPosition(u32 i) const override
{
return Vertices[i].Pos;
return Vertices->getPosition(i);
}
//! returns position of vertex i
core::vector3df &getPosition(u32 i) override
{
return Vertices[i].Pos;
return Vertices->getPosition(i);
}
//! returns normal of vertex i
const core::vector3df &getNormal(u32 i) const override
{
return Vertices[i].Normal;
return Vertices->getNormal(i);
}
//! returns normal of vertex i
core::vector3df &getNormal(u32 i) override
{
return Vertices[i].Normal;
return Vertices->getNormal(i);
}
//! returns texture coord of vertex i
const core::vector2df &getTCoords(u32 i) const override
{
return Vertices[i].TCoords;
return Vertices->getTCoords(i);
}
//! returns texture coord of vertex i
core::vector2df &getTCoords(u32 i) override
{
return Vertices[i].TCoords;
return Vertices->getTCoords(i);
}
//! Append the vertices and indices to the current buffer
@ -169,9 +182,9 @@ public:
const u32 indexCount = getIndexCount();
auto *vt = static_cast<const T *>(vertices);
Vertices.insert(Vertices.end(), vt, vt + numVertices);
Vertices->Data.insert(Vertices->Data.end(), vt, vt + numVertices);
for (u32 i = vertexCount; i < getVertexCount(); i++)
BoundingBox.addInternalPoint(Vertices[i].Pos);
BoundingBox.addInternalPoint(Vertices->getPosition(i));
Indices.insert(Indices.end(), indices, indices + numIndices);
if (vertexCount != 0) {
@ -183,7 +196,7 @@ public:
//! get the current hardware mapping hint
E_HARDWARE_MAPPING getHardwareMappingHint_Vertex() const override
{
return MappingHint_Vertex;
return Vertices->getHardwareMappingHint();
}
//! get the current hardware mapping hint
@ -196,7 +209,7 @@ public:
void setHardwareMappingHint(E_HARDWARE_MAPPING NewMappingHint, E_BUFFER_TYPE Buffer = EBT_VERTEX_AND_INDEX) override
{
if (Buffer == EBT_VERTEX_AND_INDEX || Buffer == EBT_VERTEX)
MappingHint_Vertex = NewMappingHint;
Vertices->setHardwareMappingHint(NewMappingHint);
if (Buffer == EBT_VERTEX_AND_INDEX || Buffer == EBT_INDEX)
MappingHint_Index = NewMappingHint;
}
@ -217,14 +230,14 @@ public:
void setDirty(E_BUFFER_TYPE Buffer = EBT_VERTEX_AND_INDEX) override
{
if (Buffer == EBT_VERTEX_AND_INDEX || Buffer == EBT_VERTEX)
++ChangedID_Vertex;
Vertices->setDirty();
if (Buffer == EBT_VERTEX_AND_INDEX || Buffer == EBT_INDEX)
++ChangedID_Index;
}
//! Get the currently used ID for identification of changes.
/** This shouldn't be used for anything outside the VideoDriver. */
u32 getChangedID_Vertex() const override { return ChangedID_Vertex; }
u32 getChangedID_Vertex() const override { return Vertices->getChangedID(); }
//! Get the currently used ID for identification of changes.
/** This shouldn't be used for anything outside the VideoDriver. */
@ -240,18 +253,15 @@ public:
return HWBuffer;
}
u32 ChangedID_Vertex;
u32 ChangedID_Index;
//! hardware mapping hint
E_HARDWARE_MAPPING MappingHint_Vertex;
E_HARDWARE_MAPPING MappingHint_Index;
mutable void *HWBuffer;
//! Material for this meshbuffer.
video::SMaterial Material;
//! Vertices of this buffer
std::vector<T> Vertices;
//! Vertex buffer
CVertexBuffer<T> *Vertices;
//! Indices into the vertices of this buffer.
std::vector<u16> Indices;
//! Bounding box of this meshbuffer.

122
irr/include/CVertexBuffer.h Normal file
View File

@ -0,0 +1,122 @@
// 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 "IVertexBuffer.h"
namespace irr
{
namespace scene
{
//! Template implementation of the IVertexBuffer interface
template <class T>
class CVertexBuffer : public IVertexBuffer
{
public:
//! Default constructor for empty buffer
CVertexBuffer()
{
#ifdef _DEBUG
setDebugName("CVertexBuffer");
#endif
}
const void *getData() const override
{
return Data.data();
}
void *getData() override
{
return Data.data();
}
u32 getCount() const override
{
return static_cast<u32>(Data.size());
}
video::E_VERTEX_TYPE getType() const override
{
return T::getType();
}
const core::vector3df &getPosition(u32 i) const override
{
return Data[i].Pos;
}
core::vector3df &getPosition(u32 i) override
{
return Data[i].Pos;
}
const core::vector3df &getNormal(u32 i) const override
{
return Data[i].Normal;
}
core::vector3df &getNormal(u32 i) override
{
return Data[i].Normal;
}
const core::vector2df &getTCoords(u32 i) const override
{
return Data[i].TCoords;
}
core::vector2df &getTCoords(u32 i) override
{
return Data[i].TCoords;
}
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;
//! Vertices of this buffer
std::vector<T> Data;
};
//! Standard buffer
typedef CVertexBuffer<video::S3DVertex> SVertexBuffer;
//! Buffer with two texture coords per vertex, e.g. for lightmaps
typedef CVertexBuffer<video::S3DVertex2TCoords> SVertexBufferLightMap;
//! Buffer with vertices having tangents stored, e.g. for normal mapping
typedef CVertexBuffer<video::S3DVertexTangents> SVertexBufferTangents;
} // end namespace scene
} // end namespace irr

View File

@ -17,24 +17,47 @@ namespace scene
class IVertexBuffer : public virtual IReferenceCounted
{
public:
virtual void *getData() = 0;
//! Get type of vertex data which is stored in this meshbuffer.
/** \return Vertex type of this buffer. */
virtual video::E_VERTEX_TYPE getType() const = 0;
virtual void setType(video::E_VERTEX_TYPE vertexType) = 0;
virtual u32 stride() const = 0;
virtual u32 size() const = 0;
virtual void push_back(const video::S3DVertex &element) = 0;
virtual video::S3DVertex &operator[](const u32 index) const = 0;
virtual video::S3DVertex &getLast() = 0;
virtual void set_used(u32 usedNow) = 0;
virtual void reallocate(u32 new_size) = 0;
virtual u32 allocated_size() const = 0;
virtual video::S3DVertex *pointer() = 0;
//! Get access to vertex data. The data is an array of vertices.
/** Which vertex type is used can be determined by getVertexType().
\return Pointer to array of vertices. */
virtual const void *getData() const = 0;
//! Get access to vertex data. The data is an array of vertices.
/** Which vertex type is used can be determined by getVertexType().
\return Pointer to array of vertices. */
virtual void *getData() = 0;
//! Get amount of vertices in meshbuffer.
/** \return Number of vertices in this buffer. */
virtual u32 getCount() const = 0;
//! returns position of vertex i
virtual const core::vector3df &getPosition(u32 i) const = 0;
//! returns position of vertex i
virtual core::vector3df &getPosition(u32 i) = 0;
//! returns normal of vertex i
virtual const core::vector3df &getNormal(u32 i) const = 0;
//! returns normal of vertex i
virtual core::vector3df &getNormal(u32 i) = 0;
//! returns texture coord of vertex i
virtual const core::vector2df &getTCoords(u32 i) const = 0;
//! returns texture coord of vertex i
virtual core::vector2df &getTCoords(u32 i) = 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;
@ -42,6 +65,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

View File

@ -26,7 +26,9 @@ CBillboardSceneNode::CBillboardSceneNode(ISceneNode *parent, ISceneManager *mgr,
setSize(size);
Buffer->Vertices.resize(4);
auto &Vertices = Buffer->Vertices->Data;
Vertices.resize(4);
Buffer->Indices.resize(6);
Buffer->Indices[0] = 0;
@ -36,17 +38,17 @@ CBillboardSceneNode::CBillboardSceneNode(ISceneNode *parent, ISceneManager *mgr,
Buffer->Indices[4] = 3;
Buffer->Indices[5] = 2;
Buffer->Vertices[0].TCoords.set(1.0f, 1.0f);
Buffer->Vertices[0].Color = colorBottom;
Vertices[0].TCoords.set(1.0f, 1.0f);
Vertices[0].Color = colorBottom;
Buffer->Vertices[1].TCoords.set(1.0f, 0.0f);
Buffer->Vertices[1].Color = colorTop;
Vertices[1].TCoords.set(1.0f, 0.0f);
Vertices[1].Color = colorTop;
Buffer->Vertices[2].TCoords.set(0.0f, 0.0f);
Buffer->Vertices[2].Color = colorTop;
Vertices[2].TCoords.set(0.0f, 0.0f);
Vertices[2].Color = colorTop;
Buffer->Vertices[3].TCoords.set(0.0f, 1.0f);
Buffer->Vertices[3].Color = colorBottom;
Vertices[3].TCoords.set(0.0f, 1.0f);
Vertices[3].Color = colorBottom;
}
CBillboardSceneNode::~CBillboardSceneNode()
@ -114,7 +116,7 @@ void CBillboardSceneNode::updateMesh(const irr::scene::ICameraSceneNode *camera)
view *= -1.0f;
auto *vertices = Buffer->Vertices.data();
auto &vertices = Buffer->Vertices->Data;
for (s32 i = 0; i < 4; ++i)
vertices[i].Normal = view;
@ -211,8 +213,9 @@ void CBillboardSceneNode::getSize(f32 &height, f32 &bottomEdgeWidth,
//! \param overallColor: the color to set
void CBillboardSceneNode::setColor(const video::SColor &overallColor)
{
auto &vertices = Buffer->Vertices->Data;
for (u32 vertex = 0; vertex < 4; ++vertex)
Buffer->Vertices[vertex].Color = overallColor;
vertices[vertex].Color = overallColor;
}
//! Set the color of the top and bottom vertices of the billboard
@ -221,10 +224,11 @@ void CBillboardSceneNode::setColor(const video::SColor &overallColor)
void CBillboardSceneNode::setColor(const video::SColor &topColor,
const video::SColor &bottomColor)
{
Buffer->Vertices[0].Color = bottomColor;
Buffer->Vertices[1].Color = topColor;
Buffer->Vertices[2].Color = topColor;
Buffer->Vertices[3].Color = bottomColor;
auto &vertices = Buffer->Vertices->Data;
vertices[0].Color = bottomColor;
vertices[1].Color = topColor;
vertices[2].Color = topColor;
vertices[3].Color = bottomColor;
}
//! Gets the color of the top and bottom vertices of the billboard
@ -233,8 +237,9 @@ void CBillboardSceneNode::setColor(const video::SColor &topColor,
void CBillboardSceneNode::getColor(video::SColor &topColor,
video::SColor &bottomColor) const
{
bottomColor = Buffer->Vertices[0].Color;
topColor = Buffer->Vertices[1].Color;
auto &vertices = Buffer->Vertices->Data;
bottomColor = vertices[0].Color;
topColor = vertices[1].Color;
}
//! Creates a clone of this scene node and its children.

View File

@ -133,7 +133,7 @@ SMesh *CMeshManipulator::createMeshCopy(scene::IMesh *mesh) const
SMeshBuffer *buffer = new SMeshBuffer();
buffer->Material = mb->getMaterial();
auto *vt = static_cast<const video::S3DVertex*>(mb->getVertices());
buffer->Vertices.insert(buffer->Vertices.end(), vt, vt + mb->getVertexCount());
buffer->VertexBuffer().insert(buffer->VertexBuffer().end(), vt, vt + mb->getVertexCount());
auto *indices = mb->getIndices();
buffer->Indices.insert(buffer->Indices.end(), indices, indices + mb->getIndexCount());
clone->addMeshBuffer(buffer);
@ -143,7 +143,7 @@ SMesh *CMeshManipulator::createMeshCopy(scene::IMesh *mesh) const
SMeshBufferLightMap *buffer = new SMeshBufferLightMap();
buffer->Material = mb->getMaterial();
auto *vt = static_cast<const video::S3DVertex2TCoords*>(mb->getVertices());
buffer->Vertices.insert(buffer->Vertices.end(), vt, vt + mb->getVertexCount());
buffer->VertexBuffer().insert(buffer->VertexBuffer().end(), vt, vt + mb->getVertexCount());
auto *indices = mb->getIndices();
buffer->Indices.insert(buffer->Indices.end(), indices, indices + mb->getIndexCount());
clone->addMeshBuffer(buffer);
@ -153,7 +153,7 @@ SMesh *CMeshManipulator::createMeshCopy(scene::IMesh *mesh) const
SMeshBufferTangents *buffer = new SMeshBufferTangents();
buffer->Material = mb->getMaterial();
auto *vt = static_cast<const video::S3DVertexTangents*>(mb->getVertices());
buffer->Vertices.insert(buffer->Vertices.end(), vt, vt + mb->getVertexCount());
buffer->VertexBuffer().insert(buffer->VertexBuffer().end(), vt, vt + mb->getVertexCount());
auto *indices = mb->getIndices();
buffer->Indices.insert(buffer->Indices.end(), indices, indices + mb->getIndexCount());
clone->addMeshBuffer(buffer);

View File

@ -228,8 +228,8 @@ IAnimatedMesh *COBJMeshFileLoader::createMesh(io::IReadFile *file)
if (n != currMtl->VertMap.end()) {
vertLocation = n->second;
} else {
currMtl->Meshbuffer->Vertices.push_back(v);
vertLocation = currMtl->Meshbuffer->Vertices.size() - 1;
currMtl->Meshbuffer->VertexBuffer().push_back(v);
vertLocation = currMtl->Meshbuffer->VertexBuffer().size() - 1;
currMtl->VertMap.emplace(v, vertLocation);
}

View File

@ -178,13 +178,14 @@ void Clouds::updateMesh()
auto *mb = m_meshbuffer.get();
auto &vertices = mb->Vertices->Data;
{
const u32 vertex_count = num_faces_to_draw * 16 * m_cloud_radius_i * m_cloud_radius_i;
const u32 quad_count = vertex_count / 4;
const u32 index_count = quad_count * 6;
// reserve memory
mb->Vertices.reserve(vertex_count);
vertices.reserve(vertex_count);
mb->Indices.reserve(index_count);
}
@ -192,7 +193,7 @@ void Clouds::updateMesh()
#define INAREA(x, z, radius) \
((x) >= -(radius) && (x) < (radius) && (z) >= -(radius) && (z) < (radius))
mb->Vertices.clear();
vertices.clear();
for (s16 zi0= -m_cloud_radius_i; zi0 < m_cloud_radius_i; zi0++)
for (s16 xi0= -m_cloud_radius_i; xi0 < m_cloud_radius_i; xi0++)
{
@ -312,7 +313,7 @@ void Clouds::updateMesh()
for (video::S3DVertex &vertex : v) {
vertex.Pos += pos;
mb->Vertices.push_back(vertex);
vertices.push_back(vertex);
}
}
}

View File

@ -134,16 +134,17 @@ Hud::Hud(Client *client, LocalPlayer *player,
// Prepare mesh for compass drawing
auto &b = m_rotation_mesh_buffer;
b.Vertices.resize(4);
auto &vertices = b.Vertices->Data;
vertices.resize(4);
b.Indices.resize(6);
video::SColor white(255, 255, 255, 255);
v3f normal(0.f, 0.f, 1.f);
b.Vertices[0] = video::S3DVertex(v3f(-1.f, -1.f, 0.f), normal, white, v2f(0.f, 1.f));
b.Vertices[1] = video::S3DVertex(v3f(-1.f, 1.f, 0.f), normal, white, v2f(0.f, 0.f));
b.Vertices[2] = video::S3DVertex(v3f( 1.f, 1.f, 0.f), normal, white, v2f(1.f, 0.f));
b.Vertices[3] = video::S3DVertex(v3f( 1.f, -1.f, 0.f), normal, white, v2f(1.f, 1.f));
vertices[0] = video::S3DVertex(v3f(-1.f, -1.f, 0.f), normal, white, v2f(0.f, 1.f));
vertices[1] = video::S3DVertex(v3f(-1.f, 1.f, 0.f), normal, white, v2f(0.f, 0.f));
vertices[2] = video::S3DVertex(v3f( 1.f, 1.f, 0.f), normal, white, v2f(1.f, 0.f));
vertices[3] = video::S3DVertex(v3f( 1.f, -1.f, 0.f), normal, white, v2f(1.f, 1.f));
b.Indices[0] = 0;
b.Indices[1] = 1;

View File

@ -555,14 +555,15 @@ v3f Minimap::getYawVec()
scene::SMeshBuffer *Minimap::getMinimapMeshBuffer()
{
scene::SMeshBuffer *buf = new scene::SMeshBuffer();
buf->Vertices.resize(4);
auto &vertices = buf->Vertices->Data;
vertices.resize(4);
buf->Indices.resize(6);
static const video::SColor c(255, 255, 255, 255);
buf->Vertices[0] = video::S3DVertex(-1, -1, 0, 0, 0, 1, c, 0, 1);
buf->Vertices[1] = video::S3DVertex(-1, 1, 0, 0, 0, 1, c, 0, 0);
buf->Vertices[2] = video::S3DVertex( 1, 1, 0, 0, 0, 1, c, 1, 0);
buf->Vertices[3] = video::S3DVertex( 1, -1, 0, 0, 0, 1, c, 1, 1);
vertices[0] = video::S3DVertex(-1, -1, 0, 0, 0, 1, c, 0, 1);
vertices[1] = video::S3DVertex(-1, 1, 0, 0, 0, 1, c, 0, 0);
vertices[2] = video::S3DVertex( 1, 1, 0, 0, 0, 1, c, 1, 0);
vertices[3] = video::S3DVertex( 1, -1, 0, 0, 0, 1, c, 1, 1);
buf->Indices[0] = 0;
buf->Indices[1] = 1;

View File

@ -830,7 +830,8 @@ void Sky::updateStars()
warningstream << "Requested " << m_star_params.count << " stars but " << 0x4000 << " is the max\n";
m_star_params.count = 0x4000;
}
m_stars->Vertices.reserve(4 * m_star_params.count);
auto &vertices = m_stars->Vertices->Data;
vertices.reserve(4 * m_star_params.count);
m_stars->Indices.reserve(6 * m_star_params.count);
video::SColor fallback_color = m_star_params.starcolor; // used on GLES 2 “without shaders”
@ -852,10 +853,10 @@ void Sky::updateStars()
a.rotateVect(p1);
a.rotateVect(p2);
a.rotateVect(p3);
m_stars->Vertices.push_back(video::S3DVertex(p, {}, fallback_color, {}));
m_stars->Vertices.push_back(video::S3DVertex(p1, {}, fallback_color, {}));
m_stars->Vertices.push_back(video::S3DVertex(p2, {}, fallback_color, {}));
m_stars->Vertices.push_back(video::S3DVertex(p3, {}, fallback_color, {}));
vertices.push_back(video::S3DVertex(p, {}, fallback_color, {}));
vertices.push_back(video::S3DVertex(p1, {}, fallback_color, {}));
vertices.push_back(video::S3DVertex(p2, {}, fallback_color, {}));
vertices.push_back(video::S3DVertex(p3, {}, fallback_color, {}));
}
for (u16 i = 0; i < m_star_params.count; i++) {
m_stars->Indices.push_back(i * 4 + 0);