1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00
luanti/irr/include/SSkinMeshBuffer.h

418 lines
10 KiB
C
Raw 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 "IMeshBuffer.h"
2024-08-28 17:59:53 +02:00
#include "CVertexBuffer.h"
#include "CIndexBuffer.h"
#include "IVertexBuffer.h"
2024-03-21 20:13:15 +01:00
#include "S3DVertex.h"
#include "irrTypes.h"
#include "vector3d.h"
#include <cassert>
#include <cstddef>
#include <cstring>
#include <vector>
2024-03-21 20:13:15 +01:00
namespace irr
{
namespace scene
{
struct MorphTargetDelta
{
std::vector<core::vector3df> positions;
std::vector<core::vector3df> normals;
std::vector<core::vector2df> texcoords;
void add(IVertexBuffer *buf, f32 weight)
{
size_t n = buf->getCount();
void *data = buf->getData();
switch (buf->getType()) {
case video::EVT_TANGENTS:
add<video::S3DVertexTangents>(data, n, weight);
case video::EVT_2TCOORDS:
add<video::S3DVertex2TCoords>(data, n, weight);
case video::EVT_STANDARD:
default:
add<video::S3DVertex>(data, n, weight);
}
buf->setDirty();
}
void set(IVertexBuffer *buf)
{
size_t n = buf->getCount();
void *data = buf->getData();
switch (buf->getType()) {
case video::EVT_TANGENTS:
set<video::S3DVertexTangents>(data, n);
case video::EVT_2TCOORDS:
set<video::S3DVertex2TCoords>(data, n);
case video::EVT_STANDARD:
default:
set<video::S3DVertex>(data, n);
}
buf->setDirty();
}
struct Flags {
bool positions = false;
bool normals = false;
bool texcoords = false;
};
void get(IVertexBuffer *buf, Flags flags)
{
size_t n = buf->getCount();
void *data = buf->getData();
switch (buf->getType()) {
case video::EVT_TANGENTS:
get<video::S3DVertexTangents>(data, n, flags);
case video::EVT_2TCOORDS:
get<video::S3DVertex2TCoords>(data, n, flags);
case video::EVT_STANDARD:
default:
get<video::S3DVertex>(data, n, flags);
}
}
private:
template<class VertexType>
void add(void *vertex_data, size_t n, f32 weight)
{
auto *vertices = static_cast<VertexType *>(vertex_data);
if (!positions.empty()) {
add<VertexType, core::vector3df>(&VertexType::Pos,
vertices, positions.data(), n, weight);
}
if (!normals.empty()) {
add<VertexType, core::vector3df>(&VertexType::Normal,
vertices, normals.data(), n, weight);
}
if (!texcoords.empty()) {
add<VertexType, core::vector2df>(&VertexType::TCoords,
vertices, texcoords.data(), n, weight);
}
}
template<class VertexType, class AttributeType>
static void add(AttributeType VertexType::* attribute, VertexType *vertex_data,
AttributeType *deltas, size_t n, f32 weight)
{
auto *vertices = static_cast<VertexType *>(vertex_data);
for (size_t i = 0; i < n; ++i) {
vertices[i].*attribute += weight * deltas[i];
}
}
template<class VertexType>
void set(void *vertex_data, size_t n)
{
auto *vertices = static_cast<VertexType *>(vertex_data);
if (!positions.empty()) {
set<VertexType, core::vector3df>(&VertexType::Pos,
vertices, positions.data(), n);
}
if (!normals.empty()) {
set<VertexType, core::vector3df>(&VertexType::Normal,
vertices, normals.data(), n);
}
if (!texcoords.empty()) {
set<VertexType, core::vector2df>(&VertexType::TCoords,
vertices, texcoords.data(), n);
}
}
template<class VertexType>
void get(void *vertex_data, size_t n, Flags flags)
{
auto *vertices = static_cast<VertexType *>(vertex_data);
if (flags.positions) {
positions.reserve(n);
get<VertexType, core::vector3df>(&VertexType::Pos,
vertices, positions, n);
}
if (flags.normals) {
normals.reserve(n);
get<VertexType, core::vector3df>(&VertexType::Normal,
vertices, normals, n);
}
if (flags.texcoords) {
texcoords.reserve(n);
get<VertexType, core::vector2df>(&VertexType::TCoords,
vertices, texcoords, n);
}
}
template<class VertexType, class AttributeType>
static void set(AttributeType VertexType::* attribute, VertexType *vertex_data,
AttributeType *deltas, size_t n)
{
auto *vertices = static_cast<VertexType *>(vertex_data);
for (size_t i = 0; i < n; ++i) {
vertices[i].*attribute = deltas[i];
}
}
template<class VertexType, class AttributeType>
static void get(AttributeType VertexType::* attribute, VertexType *vertex_data,
std::vector<AttributeType> &deltas, size_t n)
{
auto *vertices = static_cast<VertexType *>(vertex_data);
for (size_t i = 0; i < n; ++i) {
deltas.push_back(vertices[i].*attribute);
}
}
};
2024-03-21 20:13:15 +01:00
//! 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),
BoundingBoxNeedsRecalculated(true)
2024-08-28 17:59:53 +02:00
{
Vertices_Tangents = new SVertexBufferTangents();
Vertices_2TCoords = new SVertexBufferLightMap();
Vertices_Standard = new SVertexBuffer();
Indices = new SIndexBuffer();
}
//! 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;
}
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
}
}
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
}
}
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;
}
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: {
recalculateBoundingBox(Vertices_Standard);
2024-03-21 20:13:15 +01:00
break;
}
case video::EVT_2TCOORDS: {
recalculateBoundingBox(Vertices_2TCoords);
2024-03-21 20:13:15 +01:00
break;
}
case video::EVT_TANGENTS: {
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) {
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) {
video::S3DVertexTangents Vertex;
2024-08-28 17:59:53 +02:00
for (const auto &Vertex_Standard : Vertices_Standard->Data) {
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) {
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
void append(const void *const vertices, u32 numVertices, const u16 *const indices, u32 numIndices) override
{
assert(false);
}
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() { BoundingBoxNeedsRecalculated = true; }
void morph(const std::vector<f32> &weights)
{
assert(weights.size() == MorphTargets.size());
resetMorph();
for (size_t i = 0; i < weights.size(); ++i) {
MorphTargets[i].add(getVertexBuffer(), weights[i]);
if (!MorphTargets[i].positions.empty())
boundingBoxNeedsRecalculated();
}
}
void resetMorph()
{
MorphStaticPose.set(getVertexBuffer());
if (!MorphStaticPose.positions.empty())
boundingBoxNeedsRecalculated();
}
2024-03-21 20:13:15 +01:00
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;
// TODO consolidate with Static(Pos|Normal) in weights
MorphTargetDelta MorphStaticPose;
std::vector<MorphTargetDelta> MorphTargets;
2024-03-21 20:13:15 +01:00
video::SMaterial Material;
video::E_VERTEX_TYPE VertexType;
core::aabbox3d<f32> BoundingBox{{0, 0, 0}};
2024-03-21 20:13:15 +01:00
//! 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