mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-26 18:21:04 +00:00
Split CVertexBuffer from CMeshBuffer
This commit is contained in:
parent
538b8b9b34
commit
5d6e15bc49
10 changed files with 246 additions and 78 deletions
|
@ -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
122
irr/include/CVertexBuffer.h
Normal 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
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue