2020-01-03 20:05:16 +01:00
|
|
|
// 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
|
|
|
|
|
2021-08-27 21:20:42 +02:00
|
|
|
#ifndef IRR_T_MESH_BUFFER_H_INCLUDED
|
|
|
|
#define IRR_T_MESH_BUFFER_H_INCLUDED
|
2020-01-03 20:05:16 +01:00
|
|
|
|
|
|
|
#include "irrArray.h"
|
|
|
|
#include "IMeshBuffer.h"
|
|
|
|
|
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
namespace scene
|
|
|
|
{
|
2022-04-15 20:51:09 +02:00
|
|
|
//! Template implementation of the IMeshBuffer interface for 16-bit buffers
|
2020-01-03 20:05:16 +01:00
|
|
|
template <class T>
|
|
|
|
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)
|
|
|
|
, PrimitiveType(EPT_TRIANGLES)
|
|
|
|
{
|
|
|
|
#ifdef _DEBUG
|
|
|
|
setDebugName("CMeshBuffer");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Get material of this meshbuffer
|
|
|
|
/** \return Material of this buffer */
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual const video::SMaterial& getMaterial() const IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
return Material;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Get material of this meshbuffer
|
|
|
|
/** \return Material of this buffer */
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual video::SMaterial& getMaterial() IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
return Material;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Get pointer to vertices
|
|
|
|
/** \return Pointer to vertices. */
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual const void* getVertices() const IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
return Vertices.const_pointer();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Get pointer to vertices
|
|
|
|
/** \return Pointer to vertices. */
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual void* getVertices() IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
return Vertices.pointer();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Get number of vertices
|
|
|
|
/** \return Number of vertices. */
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual u32 getVertexCount() const IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
return Vertices.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Get type of index data which is stored in this meshbuffer.
|
|
|
|
/** \return Index type of this buffer. */
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual video::E_INDEX_TYPE getIndexType() const IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
return video::EIT_16BIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Get pointer to indices
|
|
|
|
/** \return Pointer to indices. */
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual const u16* getIndices() const IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
return Indices.const_pointer();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Get pointer to indices
|
|
|
|
/** \return Pointer to indices. */
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual u16* getIndices() IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
return Indices.pointer();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Get number of indices
|
|
|
|
/** \return Number of indices. */
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual u32 getIndexCount() const IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
return Indices.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Get the axis aligned bounding box
|
|
|
|
/** \return Axis aligned bounding box of this buffer. */
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual const core::aabbox3d<f32>& getBoundingBox() const IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
return BoundingBox;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Set the axis aligned bounding box
|
|
|
|
/** \param box New axis aligned bounding box for this buffer. */
|
|
|
|
//! set user axis aligned bounding box
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual void setBoundingBox(const core::aabbox3df& box) IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
BoundingBox = box;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Recalculate the bounding box.
|
|
|
|
/** should be called if the mesh changed. */
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual void recalculateBoundingBox() IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
if (!Vertices.empty())
|
|
|
|
{
|
|
|
|
BoundingBox.reset(Vertices[0].Pos);
|
|
|
|
const irr::u32 vsize = Vertices.size();
|
|
|
|
for (u32 i=1; i<vsize; ++i)
|
|
|
|
BoundingBox.addInternalPoint(Vertices[i].Pos);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
BoundingBox.reset(0,0,0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Get type of vertex data stored in this buffer.
|
|
|
|
/** \return Type of vertex data. */
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual video::E_VERTEX_TYPE getVertexType() const IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
return T::getType();
|
|
|
|
}
|
|
|
|
|
|
|
|
//! returns position of vertex i
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual const core::vector3df& getPosition(u32 i) const IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
return Vertices[i].Pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! returns position of vertex i
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual core::vector3df& getPosition(u32 i) IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
return Vertices[i].Pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! returns normal of vertex i
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual const core::vector3df& getNormal(u32 i) const IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
return Vertices[i].Normal;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! returns normal of vertex i
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual core::vector3df& getNormal(u32 i) IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
return Vertices[i].Normal;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! returns texture coord of vertex i
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual const core::vector2df& getTCoords(u32 i) const IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
return Vertices[i].TCoords;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! returns texture coord of vertex i
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual core::vector2df& getTCoords(u32 i) IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
return Vertices[i].TCoords;
|
|
|
|
}
|
|
|
|
|
2022-09-22 23:55:03 +02:00
|
|
|
//! returns color of vertex i
|
|
|
|
virtual video::SColor& getColor(u32 i) IRR_OVERRIDE
|
|
|
|
{
|
|
|
|
return Vertices[i].Color;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! returns color of vertex i
|
|
|
|
virtual const video::SColor& getColor(u32 i) const IRR_OVERRIDE
|
|
|
|
{
|
|
|
|
return Vertices[i].Color;
|
|
|
|
}
|
2020-01-03 20:05:16 +01:00
|
|
|
|
|
|
|
//! Append the vertices and indices to the current buffer
|
|
|
|
/** Only works for compatible types, i.e. either the same type
|
|
|
|
or the main buffer is of standard type. Otherwise, behavior is
|
2023-08-03 21:12:11 +02:00
|
|
|
undefined. Also can't append it's own vertices/indices to itself.
|
2020-01-03 20:05:16 +01:00
|
|
|
*/
|
2023-08-03 21:12:11 +02:00
|
|
|
virtual void append(const void* const vertices, u32 numVertices, const u16* const indices, u32 numIndices, bool updateBoundingBox=true) IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2023-08-03 21:12:11 +02:00
|
|
|
if (vertices == getVertices() || indices == getIndices()) // can't do that because we're doing reallocations on those blocks
|
2020-01-03 20:05:16 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
const u32 vertexCount = getVertexCount();
|
|
|
|
u32 i;
|
|
|
|
|
2023-08-03 21:12:11 +02:00
|
|
|
Vertices.reallocate(vertexCount+numVertices, false);
|
2020-01-03 20:05:16 +01:00
|
|
|
for (i=0; i<numVertices; ++i)
|
|
|
|
{
|
2022-04-15 20:51:09 +02:00
|
|
|
Vertices.push_back(static_cast<const T*>(vertices)[i]);
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
2023-08-03 21:12:11 +02:00
|
|
|
if ( updateBoundingBox && numVertices > 0)
|
|
|
|
{
|
|
|
|
if ( vertexCount == 0 )
|
|
|
|
BoundingBox.reset(static_cast<const T*>(vertices)[0].Pos);
|
|
|
|
|
|
|
|
for (i=0; i<numVertices; ++i)
|
|
|
|
BoundingBox.addInternalPoint(static_cast<const T*>(vertices)[i].Pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
Indices.reallocate(getIndexCount()+numIndices, false);
|
2020-01-03 20:05:16 +01:00
|
|
|
for (i=0; i<numIndices; ++i)
|
|
|
|
{
|
|
|
|
Indices.push_back(indices[i]+vertexCount);
|
|
|
|
}
|
2023-08-03 21:12:11 +02:00
|
|
|
|
|
|
|
setDirty();
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Append the meshbuffer to the current buffer
|
2023-08-03 21:12:11 +02:00
|
|
|
virtual void append(const IMeshBuffer* const other, bool updateBoundingBox=true) IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
2023-08-03 21:12:11 +02:00
|
|
|
if ( getVertexType() != other->getVertexType() )
|
2020-01-03 20:05:16 +01:00
|
|
|
return;
|
|
|
|
|
2023-08-03 21:12:11 +02:00
|
|
|
append(other->getVertices(), other->getVertexCount(), other->getIndices(), other->getIndexCount(), updateBoundingBox);
|
2020-01-03 20:05:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! get the current hardware mapping hint
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual E_HARDWARE_MAPPING getHardwareMappingHint_Vertex() const IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
return MappingHint_Vertex;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! get the current hardware mapping hint
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual E_HARDWARE_MAPPING getHardwareMappingHint_Index() const IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
return MappingHint_Index;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! set the hardware mapping hint, for driver
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual void setHardwareMappingHint( E_HARDWARE_MAPPING NewMappingHint, E_BUFFER_TYPE Buffer=EBT_VERTEX_AND_INDEX ) IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
if (Buffer==EBT_VERTEX_AND_INDEX || Buffer==EBT_VERTEX)
|
|
|
|
MappingHint_Vertex=NewMappingHint;
|
|
|
|
if (Buffer==EBT_VERTEX_AND_INDEX || Buffer==EBT_INDEX)
|
|
|
|
MappingHint_Index=NewMappingHint;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Describe what kind of primitive geometry is used by the meshbuffer
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual void setPrimitiveType(E_PRIMITIVE_TYPE type) IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
PrimitiveType = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Get the kind of primitive geometry which is used by the meshbuffer
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual E_PRIMITIVE_TYPE getPrimitiveType() const IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
return PrimitiveType;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! flags the mesh as changed, reloads hardware buffers
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual void setDirty(E_BUFFER_TYPE Buffer=EBT_VERTEX_AND_INDEX) IRR_OVERRIDE
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
if (Buffer==EBT_VERTEX_AND_INDEX ||Buffer==EBT_VERTEX)
|
|
|
|
++ChangedID_Vertex;
|
|
|
|
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. */
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual u32 getChangedID_Vertex() const IRR_OVERRIDE {return ChangedID_Vertex;}
|
2020-01-03 20:05:16 +01:00
|
|
|
|
|
|
|
//! Get the currently used ID for identification of changes.
|
|
|
|
/** This shouldn't be used for anything outside the VideoDriver. */
|
2021-08-27 21:20:42 +02:00
|
|
|
virtual u32 getChangedID_Index() const IRR_OVERRIDE {return ChangedID_Index;}
|
2020-01-03 20:05:16 +01:00
|
|
|
|
2022-04-15 20:51:09 +02:00
|
|
|
//! Returns type of the class implementing the IMeshBuffer
|
|
|
|
virtual EMESH_BUFFER_TYPE getType() const IRR_OVERRIDE
|
|
|
|
{
|
|
|
|
return getTypeT();
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Create copy of the meshbuffer
|
|
|
|
virtual IMeshBuffer* createClone(int cloneFlags) const IRR_OVERRIDE
|
|
|
|
{
|
|
|
|
CMeshBuffer<T> * clone = new CMeshBuffer<T>();
|
|
|
|
|
|
|
|
if (cloneFlags & ECF_VERTICES)
|
|
|
|
{
|
|
|
|
clone->Vertices = Vertices;
|
|
|
|
clone->BoundingBox = BoundingBox;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cloneFlags & ECF_INDICES)
|
|
|
|
{
|
|
|
|
clone->Indices = Indices;
|
|
|
|
}
|
|
|
|
|
|
|
|
clone->PrimitiveType = PrimitiveType;
|
|
|
|
clone->Material = getMaterial();
|
|
|
|
clone->MappingHint_Vertex = MappingHint_Vertex;
|
|
|
|
clone->MappingHint_Index = MappingHint_Index;
|
|
|
|
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Returns type of the class implementing the IMeshBuffer for template specialization
|
|
|
|
// Minor note: Some compilers (VS) allow directly specializing the virtual function,
|
|
|
|
// but this will fail on other compilers (GCC). So using a helper function.
|
|
|
|
EMESH_BUFFER_TYPE getTypeT() const;
|
|
|
|
|
2020-01-03 20:05:16 +01:00
|
|
|
u32 ChangedID_Vertex;
|
|
|
|
u32 ChangedID_Index;
|
|
|
|
|
|
|
|
//! hardware mapping hint
|
|
|
|
E_HARDWARE_MAPPING MappingHint_Vertex;
|
|
|
|
E_HARDWARE_MAPPING MappingHint_Index;
|
|
|
|
|
|
|
|
//! Material for this meshbuffer.
|
|
|
|
video::SMaterial Material;
|
|
|
|
//! Vertices of this buffer
|
|
|
|
core::array<T> Vertices;
|
|
|
|
//! Indices into the vertices of this buffer.
|
|
|
|
core::array<u16> Indices;
|
|
|
|
//! Bounding box of this meshbuffer.
|
|
|
|
core::aabbox3d<f32> BoundingBox;
|
|
|
|
//! Primitive type used for rendering (triangles, lines, ...)
|
|
|
|
E_PRIMITIVE_TYPE PrimitiveType;
|
|
|
|
};
|
|
|
|
|
|
|
|
//! Standard meshbuffer
|
|
|
|
typedef CMeshBuffer<video::S3DVertex> SMeshBuffer;
|
|
|
|
//! Meshbuffer with two texture coords per vertex, e.g. for lightmaps
|
|
|
|
typedef CMeshBuffer<video::S3DVertex2TCoords> SMeshBufferLightMap;
|
|
|
|
//! Meshbuffer with vertices having tangents stored, e.g. for normal mapping
|
|
|
|
typedef CMeshBuffer<video::S3DVertexTangents> SMeshBufferTangents;
|
2022-04-15 20:51:09 +02:00
|
|
|
|
|
|
|
//! partial specialization to return types
|
|
|
|
template <>
|
|
|
|
inline EMESH_BUFFER_TYPE CMeshBuffer<video::S3DVertex>::getTypeT() const
|
|
|
|
{
|
|
|
|
return EMBT_STANDARD;
|
|
|
|
}
|
|
|
|
template <>
|
|
|
|
inline EMESH_BUFFER_TYPE CMeshBuffer<video::S3DVertex2TCoords>::getTypeT() const
|
|
|
|
{
|
|
|
|
return EMBT_LIGHTMAP;
|
|
|
|
}
|
|
|
|
template <>
|
|
|
|
inline EMESH_BUFFER_TYPE CMeshBuffer<video::S3DVertexTangents>::getTypeT() const
|
|
|
|
{
|
|
|
|
return EMBT_TANGENTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-03 20:05:16 +01:00
|
|
|
} // end namespace scene
|
|
|
|
} // end namespace irr
|
|
|
|
|
|
|
|
#endif
|