minetest/irr/include/CMeshBuffer.h

153 lines
3.8 KiB
C
Raw Permalink Normal View History

2024-03-21 20:13:15 +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
#pragma once
#include <vector>
2024-03-21 20:13:15 +01:00
#include "IMeshBuffer.h"
2024-08-27 16:50:41 +02:00
#include "CVertexBuffer.h"
2024-08-27 17:40:29 +02:00
#include "CIndexBuffer.h"
2024-03-21 20:13:15 +01:00
namespace irr
{
namespace scene
{
//! Template implementation of the IMeshBuffer interface
template <class T>
2024-09-01 15:17:54 +02:00
class CMeshBuffer final : public IMeshBuffer
2024-03-21 20:13:15 +01:00
{
public:
//! Default constructor for empty meshbuffer
CMeshBuffer() :
PrimitiveType(EPT_TRIANGLES)
2024-03-21 20:13:15 +01:00
{
#ifdef _DEBUG
setDebugName("CMeshBuffer");
#endif
2024-08-27 16:50:41 +02:00
Vertices = new CVertexBuffer<T>();
2024-08-27 17:40:29 +02:00
Indices = new SIndexBuffer();
2024-08-27 16:50:41 +02:00
}
~CMeshBuffer()
{
Vertices->drop();
2024-08-27 17:40:29 +02:00
Indices->drop();
2024-03-21 20:13:15 +01:00
}
//! Get material of this meshbuffer
/** \return Material of this buffer */
const video::SMaterial &getMaterial() const override
{
return Material;
}
//! Get material of this meshbuffer
/** \return Material of this buffer */
video::SMaterial &getMaterial() override
{
return Material;
}
const scene::IVertexBuffer *getVertexBuffer() const override
2024-03-21 20:13:15 +01:00
{
return Vertices;
2024-03-21 20:13:15 +01:00
}
scene::IVertexBuffer *getVertexBuffer() override
2024-03-21 20:13:15 +01:00
{
return Vertices;
2024-03-21 20:13:15 +01:00
}
const scene::IIndexBuffer *getIndexBuffer() const override
2024-03-21 20:13:15 +01:00
{
return Indices;
2024-08-27 16:50:41 +02:00
}
scene::IIndexBuffer *getIndexBuffer() override
2024-08-27 16:50:41 +02:00
{
return Indices;
2024-03-21 20:13:15 +01:00
}
//! Get the axis aligned bounding box
/** \return Axis aligned bounding box of this buffer. */
const core::aabbox3d<f32> &getBoundingBox() const override
{
return BoundingBox;
}
//! Set the axis aligned bounding box
/** \param box New axis aligned bounding box for this buffer. */
//! set user axis aligned bounding box
void setBoundingBox(const core::aabbox3df &box) override
{
BoundingBox = box;
}
//! Recalculate the bounding box.
/** should be called if the mesh changed. */
void recalculateBoundingBox() override
{
2024-08-27 16:50:41 +02:00
if (Vertices->getCount()) {
BoundingBox.reset(Vertices->getPosition(0));
const irr::u32 vsize = Vertices->getCount();
2024-03-21 20:13:15 +01:00
for (u32 i = 1; i < vsize; ++i)
2024-08-27 16:50:41 +02:00
BoundingBox.addInternalPoint(Vertices->getPosition(i));
2024-03-21 20:13:15 +01:00
} else
BoundingBox.reset(0, 0, 0);
}
//! Append the vertices and indices to the current buffer
void append(const void *const vertices, u32 numVertices, const u16 *const indices, u32 numIndices) override
{
if (vertices == getVertices())
return;
const u32 vertexCount = getVertexCount();
const u32 indexCount = getIndexCount();
2024-03-21 20:13:15 +01:00
auto *vt = static_cast<const T *>(vertices);
2024-08-27 16:50:41 +02:00
Vertices->Data.insert(Vertices->Data.end(), vt, vt + numVertices);
for (u32 i = vertexCount; i < getVertexCount(); i++)
2024-08-27 16:50:41 +02:00
BoundingBox.addInternalPoint(Vertices->getPosition(i));
2024-03-21 20:13:15 +01:00
2024-08-27 17:40:29 +02:00
Indices->Data.insert(Indices->Data.end(), indices, indices + numIndices);
if (vertexCount != 0) {
for (u32 i = indexCount; i < getIndexCount(); i++)
2024-08-27 17:40:29 +02:00
Indices->Data[i] += vertexCount;
2024-03-21 20:13:15 +01:00
}
}
//! Describe what kind of primitive geometry is used by the meshbuffer
void setPrimitiveType(E_PRIMITIVE_TYPE type) override
{
PrimitiveType = type;
}
//! Get the kind of primitive geometry which is used by the meshbuffer
E_PRIMITIVE_TYPE getPrimitiveType() const override
{
return PrimitiveType;
}
//! Material for this meshbuffer.
video::SMaterial Material;
2024-08-27 16:50:41 +02:00
//! Vertex buffer
CVertexBuffer<T> *Vertices;
2024-08-27 17:40:29 +02:00
//! Index buffer
SIndexBuffer *Indices;
2024-03-21 20:13:15 +01:00
//! 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;
} // end namespace scene
} // end namespace irr