mesh patch 1

This commit is contained in:
h4570
2022-08-09 23:12:09 +02:00
parent 6b45629595
commit 2dad96d396
33 changed files with 252 additions and 661 deletions
@@ -15,21 +15,6 @@
namespace Tyra {
DynamicMesh::DynamicMesh(const MeshBuilderData& data) : Mesh(data) {
framesCount = data.framesCount;
TYRA_WARN(
"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 MeshBuilder2Data& data) : Mesh(data) {
framesCount = data.materials[0]->frames.size();
-16
View File
@@ -13,22 +13,6 @@
namespace Tyra {
Mesh::Mesh(const MeshBuilderData& data) {
init();
TYRA_ASSERT(data.materialsCount > 0,
"Materials count must be greater than 0");
materialsCount = data.materialsCount;
materials = new MeshMaterial*[materialsCount];
for (u32 i = 0; i < materialsCount; i++) {
materials[i] = new MeshMaterial(data, i);
}
_isMother = true;
}
Mesh::Mesh(const MeshBuilder2Data& data) {
init();
@@ -19,18 +19,6 @@
namespace Tyra {
MeshFrame::MeshFrame(const MeshBuilderData& data, const u32& index) {
TYRA_ASSERT(index < data.framesCount && index >= 0, "Provided index \"",
index, "\" is out of range");
id = rand() % 1000000;
bbox =
new BBox(data.frames[index]->vertices, data.frames[index]->verticesCount);
_isMother = true;
}
MeshFrame::MeshFrame(const MeshBuilder2Data& data, const u32& index) {
id = rand() % 1000000;
@@ -51,35 +51,6 @@ MeshMaterial::MeshMaterial(const MeshBuilder2Data& data,
_isMother = true;
}
MeshMaterial::MeshMaterial(const MeshBuilderData& data,
const u32& materialIndex)
: color(false) {
TYRA_ASSERT(materialIndex < data.materialsCount && materialIndex >= 0,
"Provided index \"", materialIndex, "\" is out of range");
id = rand() % 1000000;
if (data.manyColorsEnabled) {
singleColorFlag = false;
TYRA_ASSERT(data.frames[0]->colors != nullptr, "Colors faces are required");
} else {
singleColorFlag = true;
}
color.set(128.0F, 128.0F, 128.0F, 128.0F);
_name = data.materials[materialIndex]->name;
TYRA_ASSERT(_name.length() > 0, "MeshMaterial name cannot be empty");
framesCount = data.framesCount;
frames = new MeshMaterialFrame*[framesCount];
for (u32 i = 0; i < framesCount; i++) {
frames[i] = new MeshMaterialFrame(data, i, materialIndex);
}
_isMother = true;
}
MeshMaterial::MeshMaterial(const MeshMaterial& mesh) {
id = rand() % 1000000;
@@ -14,33 +14,10 @@
#include <iomanip>
#include <string>
#include "renderer/models/color.hpp"
#include "loaders/3d/builder/mesh_builder_data.hpp"
#include "renderer/3d/mesh/mesh_material_frame.hpp"
namespace Tyra {
MeshMaterialFrame::MeshMaterialFrame(const MeshBuilderData& data,
const u32& frameIndex,
const u32& materialIndex) {
TYRA_ASSERT(frameIndex < data.framesCount && frameIndex >= 0,
"Provided index \"", frameIndex, "\" is out of range");
TYRA_ASSERT(materialIndex < data.materialsCount && materialIndex >= 0,
"Provided index \"", materialIndex, "\" is out of range");
id = rand() % 1000000;
bbox = new BBox(data.frames[frameIndex]->vertices,
data.materials[materialIndex]->vertexFaces,
data.materials[materialIndex]->count);
allocateVertices(data, frameIndex, materialIndex);
allocateNormals(data, frameIndex, materialIndex);
allocateTextureCoords(data, frameIndex, materialIndex);
allocateColors(data, frameIndex, materialIndex);
_isMother = true;
}
MeshMaterialFrame::MeshMaterialFrame(const MeshBuilder2Data& data,
const u32& frameIndex,
const u32& materialIndex) {
@@ -67,21 +44,9 @@ MeshMaterialFrame::MeshMaterialFrame(const MeshBuilder2Data& data,
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;
normals = frame->normals;
textureCoords = frame->textureCoords;
colors = frame->colors;
_isMother = true;
}
@@ -111,74 +76,6 @@ 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) {
@@ -13,16 +13,6 @@
namespace Tyra {
StaticMesh::StaticMesh(const MeshBuilderData& data) : Mesh(data) {
TYRA_ASSERT(data.framesCount > 0, "Frames count must be greater than 0");
if (data.framesCount > 1)
TYRA_WARN("Static meshes should have only one frame, but ",
data.framesCount, " frames were found");
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 ",
@@ -30,6 +30,8 @@ void DynPipRenderer::allocateOnUse(const u32& t_packetSize) {
staticDataPacket = packet2_create(3, P2_TYPE_NORMAL, P2_MODE_CHAIN, true);
objectDataPacket = packet2_create(16, P2_TYPE_NORMAL, P2_MODE_CHAIN, true);
packetSize = t_packetSize;
for (u16 i = 0; i < 2; i++)
packets[i] =
packet2_create(t_packetSize, P2_TYPE_NORMAL, P2_MODE_CHAIN, true);
@@ -174,6 +176,10 @@ void DynPipRenderer::addBufferDataToPacket(DynPipVU1Program* program,
void DynPipRenderer::sendPacket() {
dma_channel_wait(DMA_CHANNEL_VIF1, 0);
dma_channel_send_packet2(currentPacket, DMA_CHANNEL_VIF1, true);
TYRA_ASSERT(packet2_get_qw_count(currentPacket) <= packetSize,
"Packet is too big. Internal error.");
// Switch packet, so we can proceed during DMA transfer
context = !context;
}
@@ -36,6 +36,7 @@ void StaPipClipper::clip(StaPipQBuffer* buffer) {
for (u32 i = 0; i < buffer->size / 3; i++) {
for (u8 j = 0; j < 3; j++) {
inputVerts[j] = *mvp * buffer->vertices[i * 3 + j];
inputTriangle[j] = {
&inputVerts[j],
buffer->bag->lighting ? &buffer->normals[i * 3 + j] : nullptr,
@@ -123,7 +123,7 @@ void StaPipCore::render(StaPipBag* bag, StaPipBagPackagesBBox* bbox) {
rendererCore->renderer3D.frustumPlanes.getAll(), *bag->info->model);
if (frustumCheck == OUTSIDE_FRUSTUM) {
if (frustumCull && !bbox) {
if (!bbox) {
delete renderBbox;
}
return;
@@ -206,6 +206,7 @@ void StaPipCore::render(StaPipBag* bag, StaPipBagPackagesBBox* bbox) {
if (frustumCull && !bbox) {
delete renderBbox;
}
if (texBuffers) delete texBuffers;
qbufferRenderer.flushBuffers();
@@ -162,49 +162,53 @@ void StaPipQBuffer::reallocateManually(const u16& t_size) {
}
void StaPipQBuffer::deallocateDynamicData() {
if (_isDynamicallyAllocated) {
delete[] vertices;
if (!_isDynamicallyAllocated) return;
if (_stAllocated) {
delete[] sts;
_stAllocated = false;
}
delete[] vertices;
if (_colorAllocated) {
delete[] colors;
_colorAllocated = false;
}
if (_normalAllocated) {
delete[] normals;
_normalAllocated = false;
}
_isDynamicallyAllocated = false;
if (_stAllocated) {
delete[] sts;
_stAllocated = false;
}
if (_colorAllocated) {
delete[] colors;
_colorAllocated = false;
}
if (_normalAllocated) {
delete[] normals;
_normalAllocated = false;
}
_isDynamicallyAllocated = false;
}
/** When we not receive maxVertCount vertices, we must align it by ourself.
* Too bad - not efficient. */
/**
* When we not receive maxVertCount vertices, we must align it by ourself.
* Too bad - not efficient.
*/
void StaPipQBuffer::allocateDynamicData(u16 size, StaPipBag* bag) {
TYRA_ASSERT(size <= maxVertCount, "Wrong size. Max buffer size in VU1 is ",
maxVertCount, ". Provided: ", size);
TYRA_ASSERT(!_isDynamicallyAllocated, "Buffer is already allocated");
vertices = new (std::align_val_t(sizeof(VECTOR))) Vec4[size];
if (size == 0) return;
vertices = new Vec4[size];
if (bag->texture != nullptr) {
sts = new (std::align_val_t(sizeof(VECTOR))) Vec4[size];
sts = new Vec4[size];
_stAllocated = true;
}
if (bag->color->many != nullptr) {
colors = new (std::align_val_t(sizeof(VECTOR))) Vec4[size];
colors = new Vec4[size];
_colorAllocated = true;
}
if (bag->lighting != nullptr) {
normals = new (std::align_val_t(sizeof(VECTOR))) Vec4[size];
normals = new Vec4[size];
_normalAllocated = true;
}
@@ -189,6 +189,7 @@ CoreBBoxFrustum CoreBBox::isInFrustum(const Plane* frustumPlanes,
const auto margin = margins == nullptr ? 0.0F : margins[i];
boxOut = 0;
boxIn = 0;
// for each corner of the box do ...
// get out of the cycle as soon as a box as corners
// both inside and out of the frustum
@@ -202,6 +203,7 @@ CoreBBoxFrustum CoreBBox::isInFrustum(const Plane* frustumPlanes,
else
boxIn++;
}
// if all corners are out
if (!boxIn)
return OUTSIDE_FRUSTUM;
@@ -46,16 +46,16 @@ CoreBBoxFrustum RenderBBox::clipIsInFrustum(const Plane* frustumPlanes,
// Oh no, it probably needs clipping
float margins[6]; // This probably needs more calibration
float guardBand[6]; // This probably needs more calibration
margins[0] = -15.0F; // Top
margins[1] = -10.0F; // BOTTOM
margins[2] = -25.0F; // LEFT
margins[3] = -25.0F; // RIGHT
margins[4] = -10.0F; // NEAR
margins[5] = -10.0F; // FAR
guardBand[0] = -15.0F; // Top
guardBand[1] = -10.0F; // BOTTOM
guardBand[2] = -25.0F; // LEFT
guardBand[3] = -25.0F; // RIGHT
guardBand[4] = -10.0F; // NEAR
guardBand[5] = -10.0F; // FAR
return isInFrustum(frustumPlanes, model, margins); // Let's check it again
}
return isInFrustum(frustumPlanes, model, guardBand); // Let's check it again
} // namespace Tyra
} // namespace Tyra