mesh builder v2

This commit is contained in:
h4570
2022-08-03 20:31:19 +02:00
parent 689c8132d1
commit 153ebb52a5
19 changed files with 384 additions and 76 deletions
@@ -0,0 +1,24 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#include "loaders/3d/builder2/mesh_builder2_data.hpp"
namespace Tyra {
MeshBuilder2Data::MeshBuilder2Data() {
textureCoordsEnabled = false;
normalsEnabled = false;
lightMapEnabled = false;
}
MeshBuilder2Data::~MeshBuilder2Data() {}
} // namespace Tyra
@@ -0,0 +1,19 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#include "loaders/3d/builder2/mesh_builder2_material_data.hpp"
namespace Tyra {
MeshBuilder2MaterialData::MeshBuilder2MaterialData() {}
MeshBuilder2MaterialData::~MeshBuilder2MaterialData() {}
} // namespace Tyra
@@ -0,0 +1,24 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#include "loaders/3d/builder2/mesh_builder2_material_frame_data.hpp"
namespace Tyra {
MeshBuilder2MaterialFrameData::MeshBuilder2MaterialFrameData() {
vertices = nullptr;
textureCoords = nullptr;
normals = nullptr;
colors = nullptr;
}
MeshBuilder2MaterialFrameData::~MeshBuilder2MaterialFrameData() {}
} // namespace Tyra
@@ -30,6 +30,21 @@ DynamicMesh::DynamicMesh(const MeshBuilderData& data) : Mesh(data) {
initAnimation();
}
DynamicMesh::DynamicMesh(const MeshBuilder2Data& data) : Mesh(data) {
framesCount = data.materials[0]->frames.size();
TYRA_ERROR(framesCount > 1,
"Frames count should be greater than 1 for DynamicMesh! Maybe you "
"should use StaticMesh?");
frames = new MeshFrame*[framesCount];
for (u32 i = 0; i < framesCount; i++) {
frames[i] = new MeshFrame(data, i);
}
initAnimation();
}
DynamicMesh::DynamicMesh(const DynamicMesh& mesh) : Mesh(mesh) {
framesCount = mesh.framesCount;
+16
View File
@@ -29,6 +29,22 @@ Mesh::Mesh(const MeshBuilderData& data) {
_isMother = true;
}
Mesh::Mesh(const MeshBuilder2Data& data) {
init();
TYRA_ASSERT(data.materials.size() > 0,
"Materials count must be greater than 0");
materialsCount = data.materials.size();
materials = new MeshMaterial*[materialsCount];
for (u32 i = 0; i < materialsCount; i++) {
materials[i] = new MeshMaterial(data, i);
}
_isMother = true;
}
Mesh::Mesh(const Mesh& mesh) {
init();
@@ -31,6 +31,23 @@ MeshFrame::MeshFrame(const MeshBuilderData& data, const u32& index) {
_isMother = true;
}
MeshFrame::MeshFrame(const MeshBuilder2Data& data, const u32& index) {
id = rand() % 1000000;
auto bboxes = new CoreBBox*[data.materials.size()];
for (u32 i = 0; i < data.materials.size(); i++) {
bboxes[i] = new CoreBBox(data.materials[i]->frames[index]->vertices,
data.materials[i]->frames[index]->count);
}
bbox = new BBox(bboxes, data.materials.size());
for (u32 i = 0; i < data.materials.size(); i++) delete bboxes[i];
delete[] bboxes;
_isMother = true;
}
MeshFrame::MeshFrame(const MeshFrame& frame) {
id = rand() % 1000000;
@@ -17,6 +17,40 @@
namespace Tyra {
MeshMaterial::MeshMaterial(const MeshBuilder2Data& data,
const u32& materialIndex) {
TYRA_ASSERT(materialIndex < data.materials.size(), "Provided index \"",
materialIndex, "\" is out of range");
auto* material = data.materials[materialIndex];
TYRA_ASSERT(material->frames.size() > 0,
"Material must have at least one frame");
id = rand() % 1000000;
if (data.lightMapEnabled) {
singleColorFlag = false;
TYRA_ASSERT(material->frames[0]->colors != nullptr,
"Colors faces are required");
} else {
singleColorFlag = true;
}
color.set(128.0F, 128.0F, 128.0F, 128.0F);
_name = material->name;
TYRA_ASSERT(_name.length() > 0, "MeshMaterial name cannot be empty");
framesCount = material->frames.size();
frames = new MeshMaterialFrame*[framesCount];
for (u32 i = 0; i < framesCount; i++) {
frames[i] = new MeshMaterialFrame(data, i, materialIndex);
}
_isMother = true;
}
MeshMaterial::MeshMaterial(const MeshBuilderData& data,
const u32& materialIndex)
: color(false) {
@@ -41,10 +41,53 @@ MeshMaterialFrame::MeshMaterialFrame(const MeshBuilderData& data,
_isMother = true;
}
MeshMaterialFrame::MeshMaterialFrame(const MeshBuilder2Data& data,
const u32& frameIndex,
const u32& materialIndex) {
TYRA_ASSERT(materialIndex < data.materials.size(), "Provided index \"",
materialIndex, "\" is out of range");
TYRA_ASSERT(frameIndex < data.materials[materialIndex]->frames.size(),
"Provided index \"", frameIndex, "\" is out of range");
id = rand() % 1000000;
auto* frame = data.materials[materialIndex]->frames[frameIndex];
TYRA_ASSERT(frame->count > 0, "Vertex count must be greater than 0");
TYRA_ASSERT(frame->vertices != nullptr, "Vertex array can't be null");
TYRA_ASSERT(frame->normals != nullptr, "Normal array can't be null");
TYRA_ASSERT(frame->textureCoords != nullptr,
"Texture coordinate array can't be null");
TYRA_ASSERT(frame->colors != nullptr, "Color array can't be null");
bbox = new BBox(frame->vertices, frame->count);
count = frame->count;
vertices = frame->vertices;
if (data.normalsEnabled)
normals = frame->normals;
else
normals = nullptr;
if (data.textureCoordsEnabled)
textureCoords = frame->textureCoords;
else
textureCoords = nullptr;
if (data.lightMapEnabled)
colors = frame->colors;
else
colors = nullptr;
_isMother = true;
}
MeshMaterialFrame::MeshMaterialFrame(const MeshMaterialFrame& frame) {
id = rand() % 1000000;
vertexCount = frame.vertexCount;
count = frame.count;
vertices = frame.vertices;
normals = frame.normals;
@@ -66,6 +109,74 @@ MeshMaterialFrame::~MeshMaterialFrame() {
}
}
void MeshMaterialFrame::allocateVertices(const MeshBuilderData& data,
const u32& frameIndex,
const u32& materialIndex) {
count = data.materials[materialIndex]->count; // faces count
vertices = new Vec4[count];
TYRA_ASSERT(count > 0, "Vertex count must be greater than 0");
auto* rolled = data.frames[frameIndex]->vertices;
auto* faces = data.materials[materialIndex]->vertexFaces;
TYRA_ASSERT(rolled != nullptr, "Vertices are required");
TYRA_ASSERT(faces != nullptr, "Vertex faces are required");
for (u32 i = 0; i < count; i++) vertices[i] = rolled[faces[i]];
}
void MeshMaterialFrame::allocateTextureCoords(const MeshBuilderData& data,
const u32& frameIndex,
const u32& materialIndex) {
textureCoords = nullptr;
if (!data.textureCoordsEnabled) return;
textureCoords = new Vec4[count];
auto* rolled = data.frames[frameIndex]->textureCoords;
auto* faces = data.materials[materialIndex]->textureCoordFaces;
TYRA_ASSERT(rolled != nullptr, "Texture coordinates are required");
TYRA_ASSERT(faces != nullptr, "Texture coordinate faces are required");
for (u32 i = 0; i < count; i++) textureCoords[i] = rolled[faces[i]];
}
void MeshMaterialFrame::allocateNormals(const MeshBuilderData& data,
const u32& frameIndex,
const u32& materialIndex) {
normals = nullptr;
if (!data.normalsEnabled) return;
normals = new Vec4[count];
auto* rolled = data.frames[frameIndex]->normals;
auto* faces = data.materials[materialIndex]->normalFaces;
TYRA_ASSERT(rolled != nullptr, "Normals are required");
TYRA_ASSERT(faces != nullptr, "Normal faces are required");
for (u32 i = 0; i < count; i++) normals[i] = rolled[faces[i]];
}
void MeshMaterialFrame::allocateColors(const MeshBuilderData& data,
const u32& frameIndex,
const u32& materialIndex) {
colors = nullptr;
if (!data.manyColorsEnabled) return;
colors = new Color[count];
auto* rolled = data.frames[frameIndex]->colors;
auto* faces = data.materials[materialIndex]->colorFaces;
TYRA_ASSERT(rolled != nullptr, "Colors are required");
TYRA_ASSERT(faces != nullptr, "Color faces are required");
for (u32 i = 0; i < count; i++) colors[i] = rolled[faces[i]];
}
std::string MeshMaterialFrame::getPrint(const char* name) const {
std::stringstream res;
if (name) {
@@ -76,31 +187,31 @@ std::string MeshMaterialFrame::getPrint(const char* name) const {
res << std::fixed << std::setprecision(2);
res << "Id: " << id << ", " << std::endl;
res << "Vertex count: " << vertexCount << ", " << std::endl;
res << "Vertex count: " << count << ", " << std::endl;
res << "BBox: " << bbox->getPrint() << ", " << std::endl;
res << "Vertices: ";
for (u32 i = 0; i < vertexCount; i++) {
for (u32 i = 0; i < count; i++) {
res << vertices[i].getPrint() << ", " << std::endl;
}
if (normals) {
res << "Normals: ";
for (u32 i = 0; i < vertexCount; i++) {
for (u32 i = 0; i < count; i++) {
res << normals[i].getPrint() << ", " << std::endl;
}
}
if (textureCoords) {
res << "TextureCoords: ";
for (u32 i = 0; i < vertexCount; i++) {
for (u32 i = 0; i < count; i++) {
res << textureCoords[i].getPrint() << ", " << std::endl;
}
}
if (colors) {
res << "Colors: ";
for (u32 i = 0; i < vertexCount; i++) {
for (u32 i = 0; i < count; i++) {
res << colors[i].getPrint() << ", " << std::endl;
}
}
@@ -110,72 +221,4 @@ std::string MeshMaterialFrame::getPrint(const char* name) const {
return res.str();
}
void MeshMaterialFrame::allocateVertices(const MeshBuilderData& data,
const u32& frameIndex,
const u32& materialIndex) {
vertexCount = data.materials[materialIndex]->count; // faces count
vertices = new Vec4[vertexCount];
TYRA_ASSERT(vertexCount > 0, "Vertex count must be greater than 0");
auto* rolled = data.frames[frameIndex]->vertices;
auto* faces = data.materials[materialIndex]->vertexFaces;
TYRA_ASSERT(rolled != nullptr, "Vertices are required");
TYRA_ASSERT(faces != nullptr, "Vertex faces are required");
for (u32 i = 0; i < vertexCount; i++) vertices[i] = rolled[faces[i]];
}
void MeshMaterialFrame::allocateTextureCoords(const MeshBuilderData& data,
const u32& frameIndex,
const u32& materialIndex) {
textureCoords = nullptr;
if (!data.textureCoordsEnabled) return;
textureCoords = new Vec4[vertexCount];
auto* rolled = data.frames[frameIndex]->textureCoords;
auto* faces = data.materials[materialIndex]->textureCoordFaces;
TYRA_ASSERT(rolled != nullptr, "Texture coordinates are required");
TYRA_ASSERT(faces != nullptr, "Texture coordinate faces are required");
for (u32 i = 0; i < vertexCount; i++) textureCoords[i] = rolled[faces[i]];
}
void MeshMaterialFrame::allocateNormals(const MeshBuilderData& data,
const u32& frameIndex,
const u32& materialIndex) {
normals = nullptr;
if (!data.normalsEnabled) return;
normals = new Vec4[vertexCount];
auto* rolled = data.frames[frameIndex]->normals;
auto* faces = data.materials[materialIndex]->normalFaces;
TYRA_ASSERT(rolled != nullptr, "Normals are required");
TYRA_ASSERT(faces != nullptr, "Normal faces are required");
for (u32 i = 0; i < vertexCount; i++) normals[i] = rolled[faces[i]];
}
void MeshMaterialFrame::allocateColors(const MeshBuilderData& data,
const u32& frameIndex,
const u32& materialIndex) {
colors = nullptr;
if (!data.manyColorsEnabled) return;
colors = new Color[vertexCount];
auto* rolled = data.frames[frameIndex]->colors;
auto* faces = data.materials[materialIndex]->colorFaces;
TYRA_ASSERT(rolled != nullptr, "Colors are required");
TYRA_ASSERT(faces != nullptr, "Color faces are required");
for (u32 i = 0; i < vertexCount; i++) colors[i] = rolled[faces[i]];
}
} // namespace Tyra
@@ -23,6 +23,14 @@ StaticMesh::StaticMesh(const MeshBuilderData& data) : Mesh(data) {
frame = new MeshFrame(data, 0);
}
StaticMesh::StaticMesh(const MeshBuilder2Data& data) : Mesh(data) {
if (data.materials[0]->frames.size() > 1)
TYRA_WARN("Static meshes should have only one frame, but ",
data.materials[0]->frames.size(), " frames were found");
frame = new MeshFrame(data, 0);
}
StaticMesh::StaticMesh(const StaticMesh& mesh) : Mesh(mesh) {
frame = new MeshFrame(*mesh.frame);
}