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 "IMeshBuffer.h"
|
2024-08-28 17:59:53 +02:00
|
|
|
#include "CVertexBuffer.h"
|
|
|
|
#include "CIndexBuffer.h"
|
2024-03-21 20:13:15 +01:00
|
|
|
#include "S3DVertex.h"
|
2024-09-02 14:50:30 +02:00
|
|
|
#include "irrArray.h"
|
2024-03-21 20:13:15 +01:00
|
|
|
|
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
namespace scene
|
|
|
|
{
|
|
|
|
|
|
|
|
//! A mesh buffer able to choose between S3DVertex2TCoords, S3DVertex and S3DVertexTangents at runtime
|
2024-09-01 15:17:54 +02:00
|
|
|
struct SSkinMeshBuffer final : public IMeshBuffer
|
2024-03-21 20:13:15 +01:00
|
|
|
{
|
|
|
|
//! Default constructor
|
|
|
|
SSkinMeshBuffer(video::E_VERTEX_TYPE vt = video::EVT_STANDARD) :
|
2024-08-28 17:59:53 +02:00
|
|
|
VertexType(vt), PrimitiveType(EPT_TRIANGLES),
|
2024-08-28 21:46:52 +02:00
|
|
|
BoundingBoxNeedsRecalculated(true)
|
2024-08-28 17:59:53 +02:00
|
|
|
{
|
|
|
|
#ifdef _DEBUG
|
|
|
|
setDebugName("SSkinMeshBuffer");
|
|
|
|
#endif
|
|
|
|
Vertices_Tangents = new SVertexBufferTangents();
|
|
|
|
Vertices_2TCoords = new SVertexBufferLightMap();
|
|
|
|
Vertices_Standard = new SVertexBuffer();
|
|
|
|
Indices = new SIndexBuffer();
|
|
|
|
}
|
2024-09-02 14:50:30 +02:00
|
|
|
|
|
|
|
//! Constructor for standard vertices
|
|
|
|
SSkinMeshBuffer(std::vector<video::S3DVertex> &&vertices, std::vector<u16> &&indices) :
|
|
|
|
SSkinMeshBuffer()
|
2024-03-21 20:13:15 +01:00
|
|
|
{
|
2024-08-28 17:59:53 +02:00
|
|
|
Vertices_Standard->Data = std::move(vertices);
|
|
|
|
Indices->Data = std::move(indices);
|
|
|
|
}
|
|
|
|
|
|
|
|
~SSkinMeshBuffer()
|
|
|
|
{
|
|
|
|
Vertices_Tangents->drop();
|
|
|
|
Vertices_2TCoords->drop();
|
|
|
|
Vertices_Standard->drop();
|
|
|
|
Indices->drop();
|
2024-03-21 20:13:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Get Material of this buffer.
|
|
|
|
const video::SMaterial &getMaterial() const override
|
|
|
|
{
|
|
|
|
return Material;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Get Material of this buffer.
|
|
|
|
video::SMaterial &getMaterial() override
|
|
|
|
{
|
|
|
|
return Material;
|
|
|
|
}
|
|
|
|
|
2024-08-28 18:08:59 +02:00
|
|
|
const scene::IVertexBuffer *getVertexBuffer() const override
|
2024-03-21 20:13:15 +01:00
|
|
|
{
|
|
|
|
switch (VertexType) {
|
|
|
|
case video::EVT_2TCOORDS:
|
2024-08-28 17:59:53 +02:00
|
|
|
return Vertices_2TCoords;
|
2024-03-21 20:13:15 +01:00
|
|
|
case video::EVT_TANGENTS:
|
2024-08-28 17:59:53 +02:00
|
|
|
return Vertices_Tangents;
|
2024-03-21 20:13:15 +01:00
|
|
|
default:
|
2024-08-28 17:59:53 +02:00
|
|
|
return Vertices_Standard;
|
2024-03-21 20:13:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-28 21:46:52 +02:00
|
|
|
scene::IVertexBuffer *getVertexBuffer() override
|
2024-03-21 20:13:15 +01:00
|
|
|
{
|
|
|
|
switch (VertexType) {
|
|
|
|
case video::EVT_2TCOORDS:
|
2024-08-28 17:59:53 +02:00
|
|
|
return Vertices_2TCoords;
|
2024-03-21 20:13:15 +01:00
|
|
|
case video::EVT_TANGENTS:
|
2024-08-28 17:59:53 +02:00
|
|
|
return Vertices_Tangents;
|
2024-03-21 20:13:15 +01:00
|
|
|
default:
|
2024-08-28 17:59:53 +02:00
|
|
|
return Vertices_Standard;
|
2024-03-21 20:13:15 +01:00
|
|
|
}
|
|
|
|
}
|
2024-08-28 18:08:59 +02:00
|
|
|
|
|
|
|
const scene::IIndexBuffer *getIndexBuffer() const override
|
|
|
|
{
|
|
|
|
return Indices;
|
|
|
|
}
|
|
|
|
|
|
|
|
scene::IIndexBuffer *getIndexBuffer() override
|
|
|
|
{
|
|
|
|
return Indices;
|
|
|
|
}
|
2024-03-21 20:13:15 +01:00
|
|
|
|
2024-08-28 17:59:53 +02:00
|
|
|
//! Get standard vertex at given index
|
|
|
|
virtual video::S3DVertex *getVertex(u32 index)
|
2024-03-21 20:13:15 +01:00
|
|
|
{
|
|
|
|
switch (VertexType) {
|
|
|
|
case video::EVT_2TCOORDS:
|
2024-08-28 17:59:53 +02:00
|
|
|
return &Vertices_2TCoords->Data[index];
|
2024-03-21 20:13:15 +01:00
|
|
|
case video::EVT_TANGENTS:
|
2024-08-28 17:59:53 +02:00
|
|
|
return &Vertices_Tangents->Data[index];
|
2024-03-21 20:13:15 +01:00
|
|
|
default:
|
2024-08-28 17:59:53 +02:00
|
|
|
return &Vertices_Standard->Data[index];
|
2024-03-21 20:13:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Get bounding box
|
|
|
|
const core::aabbox3d<f32> &getBoundingBox() const override
|
|
|
|
{
|
|
|
|
return BoundingBox;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Set bounding box
|
|
|
|
void setBoundingBox(const core::aabbox3df &box) override
|
|
|
|
{
|
|
|
|
BoundingBox = box;
|
|
|
|
}
|
|
|
|
|
2024-08-28 21:46:52 +02:00
|
|
|
private:
|
|
|
|
template <typename T> void recalculateBoundingBox(const CVertexBuffer<T> *buf)
|
|
|
|
{
|
|
|
|
if (!buf->getCount()) {
|
|
|
|
BoundingBox.reset(0, 0, 0);
|
|
|
|
} else {
|
|
|
|
auto &vertices = buf->Data;
|
|
|
|
BoundingBox.reset(vertices[0].Pos);
|
|
|
|
for (size_t i = 1; i < vertices.size(); ++i)
|
|
|
|
BoundingBox.addInternalPoint(vertices[i].Pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T1, typename T2> static void copyVertex(const T1 &src, T2 &dst)
|
|
|
|
{
|
|
|
|
dst.Pos = src.Pos;
|
|
|
|
dst.Normal = src.Normal;
|
|
|
|
dst.Color = src.Color;
|
|
|
|
dst.TCoords = src.TCoords;
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
|
2024-03-21 20:13:15 +01:00
|
|
|
//! Recalculate bounding box
|
|
|
|
void recalculateBoundingBox() override
|
|
|
|
{
|
|
|
|
if (!BoundingBoxNeedsRecalculated)
|
|
|
|
return;
|
|
|
|
|
|
|
|
BoundingBoxNeedsRecalculated = false;
|
|
|
|
|
|
|
|
switch (VertexType) {
|
|
|
|
case video::EVT_STANDARD: {
|
2024-08-28 21:46:52 +02:00
|
|
|
recalculateBoundingBox(Vertices_Standard);
|
2024-03-21 20:13:15 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case video::EVT_2TCOORDS: {
|
2024-08-28 21:46:52 +02:00
|
|
|
recalculateBoundingBox(Vertices_2TCoords);
|
2024-03-21 20:13:15 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case video::EVT_TANGENTS: {
|
2024-08-28 21:46:52 +02:00
|
|
|
recalculateBoundingBox(Vertices_Tangents);
|
2024-03-21 20:13:15 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Convert to 2tcoords vertex type
|
|
|
|
void convertTo2TCoords()
|
|
|
|
{
|
|
|
|
if (VertexType == video::EVT_STANDARD) {
|
2024-08-28 17:59:53 +02:00
|
|
|
video::S3DVertex2TCoords Vertex;
|
|
|
|
for (const auto &Vertex_Standard : Vertices_Standard->Data) {
|
2024-08-28 21:46:52 +02:00
|
|
|
copyVertex(Vertex_Standard, Vertex);
|
2024-08-28 17:59:53 +02:00
|
|
|
Vertices_2TCoords->Data.push_back(Vertex);
|
2024-03-21 20:13:15 +01:00
|
|
|
}
|
2024-08-28 17:59:53 +02:00
|
|
|
Vertices_Standard->Data.clear();
|
2024-03-21 20:13:15 +01:00
|
|
|
VertexType = video::EVT_2TCOORDS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Convert to tangents vertex type
|
|
|
|
void convertToTangents()
|
|
|
|
{
|
|
|
|
if (VertexType == video::EVT_STANDARD) {
|
2024-08-28 21:46:52 +02:00
|
|
|
video::S3DVertexTangents Vertex;
|
2024-08-28 17:59:53 +02:00
|
|
|
for (const auto &Vertex_Standard : Vertices_Standard->Data) {
|
2024-08-28 21:46:52 +02:00
|
|
|
copyVertex(Vertex_Standard, Vertex);
|
2024-08-28 17:59:53 +02:00
|
|
|
Vertices_Tangents->Data.push_back(Vertex);
|
2024-03-21 20:13:15 +01:00
|
|
|
}
|
2024-08-28 17:59:53 +02:00
|
|
|
Vertices_Standard->Data.clear();
|
2024-03-21 20:13:15 +01:00
|
|
|
VertexType = video::EVT_TANGENTS;
|
|
|
|
} else if (VertexType == video::EVT_2TCOORDS) {
|
2024-08-28 17:59:53 +02:00
|
|
|
video::S3DVertexTangents Vertex;
|
|
|
|
for (const auto &Vertex_2TCoords : Vertices_2TCoords->Data) {
|
2024-08-28 21:46:52 +02:00
|
|
|
copyVertex(Vertex_2TCoords, Vertex);
|
2024-08-28 17:59:53 +02:00
|
|
|
Vertices_Tangents->Data.push_back(Vertex);
|
2024-03-21 20:13:15 +01:00
|
|
|
}
|
2024-08-28 17:59:53 +02:00
|
|
|
Vertices_2TCoords->Data.clear();
|
2024-03-21 20:13:15 +01:00
|
|
|
VertexType = video::EVT_TANGENTS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! append the vertices and indices to the current buffer
|
2024-08-17 19:49:11 +02:00
|
|
|
void append(const void *const vertices, u32 numVertices, const u16 *const indices, u32 numIndices) override
|
|
|
|
{
|
|
|
|
_IRR_DEBUG_BREAK_IF(true);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Call this after changing the positions of any vertex.
|
|
|
|
void boundingBoxNeedsRecalculated(void) { BoundingBoxNeedsRecalculated = true; }
|
|
|
|
|
2024-08-28 17:59:53 +02:00
|
|
|
SVertexBufferTangents *Vertices_Tangents;
|
|
|
|
SVertexBufferLightMap *Vertices_2TCoords;
|
|
|
|
SVertexBuffer *Vertices_Standard;
|
|
|
|
SIndexBuffer *Indices;
|
2024-03-21 20:13:15 +01:00
|
|
|
|
|
|
|
core::matrix4 Transformation;
|
|
|
|
|
|
|
|
video::SMaterial Material;
|
|
|
|
video::E_VERTEX_TYPE VertexType;
|
|
|
|
|
|
|
|
core::aabbox3d<f32> BoundingBox;
|
|
|
|
|
|
|
|
//! Primitive type used for rendering (triangles, lines, ...)
|
|
|
|
E_PRIMITIVE_TYPE PrimitiveType;
|
|
|
|
|
2024-08-28 17:59:53 +02:00
|
|
|
bool BoundingBoxNeedsRecalculated;
|
2024-03-21 20:13:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace scene
|
|
|
|
} // end namespace irr
|