Optimize and unify mesh processing (#7851)

master
Vitaliy 2019-06-28 05:22:16 +03:00 committed by Paramat
parent 5e7004e7af
commit 93f7e10e0d
1 changed files with 63 additions and 130 deletions

View File

@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "debug.h" #include "debug.h"
#include "log.h" #include "log.h"
#include "irrMap.h" #include "irrMap.h"
#include <cmath>
#include <iostream> #include <iostream>
#include <IAnimatedMesh.h> #include <IAnimatedMesh.h>
#include <SAnimatedMesh.h> #include <SAnimatedMesh.h>
@ -202,6 +203,20 @@ void setMeshColor(scene::IMesh *mesh, const video::SColor &color)
setMeshBufferColor(mesh->getMeshBuffer(j), color); setMeshBufferColor(mesh->getMeshBuffer(j), color);
} }
template <typename F>
static void applyToMesh(scene::IMesh *mesh, const F &fn)
{
u16 mc = mesh->getMeshBufferCount();
for (u16 j = 0; j < mc; j++) {
scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
const u32 stride = getVertexPitchFromType(buf->getVertexType());
u32 vertex_count = buf->getVertexCount();
char *vertices = reinterpret_cast<char *>(buf->getVertices());
for (u32 i = 0; i < vertex_count; i++)
fn(reinterpret_cast<video::S3DVertex *>(vertices + i * stride));
}
}
void colorizeMeshBuffer(scene::IMeshBuffer *buf, const video::SColor *buffercolor) void colorizeMeshBuffer(scene::IMeshBuffer *buf, const video::SColor *buffercolor)
{ {
const u32 stride = getVertexPitchFromType(buf->getVertexType()); const u32 stride = getVertexPitchFromType(buf->getVertexType());
@ -222,28 +237,20 @@ void setMeshColorByNormalXYZ(scene::IMesh *mesh,
const video::SColor &colorY, const video::SColor &colorY,
const video::SColor &colorZ) const video::SColor &colorZ)
{ {
if (mesh == NULL) if (!mesh)
return; return;
auto colorizator = [=] (video::S3DVertex *vertex) {
u16 mc = mesh->getMeshBufferCount(); f32 x = fabs(vertex->Normal.X);
for (u16 j = 0; j < mc; j++) { f32 y = fabs(vertex->Normal.Y);
scene::IMeshBuffer *buf = mesh->getMeshBuffer(j); f32 z = fabs(vertex->Normal.Z);
const u32 stride = getVertexPitchFromType(buf->getVertexType()); if (x >= y && x >= z)
u32 vertex_count = buf->getVertexCount(); vertex->Color = colorX;
u8 *vertices = (u8 *)buf->getVertices(); else if (y >= z)
for (u32 i = 0; i < vertex_count; i++) { vertex->Color = colorY;
video::S3DVertex *vertex = (video::S3DVertex *)(vertices + i * stride); else
f32 x = fabs(vertex->Normal.X); vertex->Color = colorZ;
f32 y = fabs(vertex->Normal.Y); };
f32 z = fabs(vertex->Normal.Z); applyToMesh(mesh, colorizator);
if (x >= y && x >= z)
vertex->Color = colorX;
else if (y >= z)
vertex->Color = colorY;
else
vertex->Color = colorZ;
}
}
} }
void setMeshColorByNormal(scene::IMesh *mesh, const v3f &normal, void setMeshColorByNormal(scene::IMesh *mesh, const v3f &normal,
@ -251,132 +258,58 @@ void setMeshColorByNormal(scene::IMesh *mesh, const v3f &normal,
{ {
if (!mesh) if (!mesh)
return; return;
auto colorizator = [normal, color] (video::S3DVertex *vertex) {
if (vertex->Normal == normal)
vertex->Color = color;
};
applyToMesh(mesh, colorizator);
}
u16 mc = mesh->getMeshBufferCount(); template <float v3f::*U, float v3f::*V>
for (u16 j = 0; j < mc; j++) { static void rotateMesh(scene::IMesh *mesh, float degrees)
scene::IMeshBuffer *buf = mesh->getMeshBuffer(j); {
const u32 stride = getVertexPitchFromType(buf->getVertexType()); degrees *= M_PI / 180.0f;
u32 vertex_count = buf->getVertexCount(); float c = std::cos(degrees);
u8 *vertices = (u8 *)buf->getVertices(); float s = std::sin(degrees);
for (u32 i = 0; i < vertex_count; i++) { auto rotator = [c, s] (video::S3DVertex *vertex) {
video::S3DVertex *vertex = (video::S3DVertex *)(vertices + i * stride); float u = vertex->Pos.*U;
if (normal == vertex->Normal) { float v = vertex->Pos.*V;
vertex->Color = color; vertex->Pos.*U = c * u - s * v;
} vertex->Pos.*V = s * u + c * v;
} };
} applyToMesh(mesh, rotator);
} }
void rotateMeshXYby(scene::IMesh *mesh, f64 degrees) void rotateMeshXYby(scene::IMesh *mesh, f64 degrees)
{ {
u16 mc = mesh->getMeshBufferCount(); rotateMesh<&v3f::X, &v3f::Y>(mesh, degrees);
for (u16 j = 0; j < mc; j++) {
scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
const u32 stride = getVertexPitchFromType(buf->getVertexType());
u32 vertex_count = buf->getVertexCount();
u8 *vertices = (u8 *)buf->getVertices();
for (u32 i = 0; i < vertex_count; i++)
((video::S3DVertex *)(vertices + i * stride))->Pos.rotateXYBy(degrees);
}
} }
void rotateMeshXZby(scene::IMesh *mesh, f64 degrees) void rotateMeshXZby(scene::IMesh *mesh, f64 degrees)
{ {
u16 mc = mesh->getMeshBufferCount(); rotateMesh<&v3f::X, &v3f::Z>(mesh, degrees);
for (u16 j = 0; j < mc; j++) {
scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
const u32 stride = getVertexPitchFromType(buf->getVertexType());
u32 vertex_count = buf->getVertexCount();
u8 *vertices = (u8 *)buf->getVertices();
for (u32 i = 0; i < vertex_count; i++)
((video::S3DVertex *)(vertices + i * stride))->Pos.rotateXZBy(degrees);
}
} }
void rotateMeshYZby(scene::IMesh *mesh, f64 degrees) void rotateMeshYZby(scene::IMesh *mesh, f64 degrees)
{ {
u16 mc = mesh->getMeshBufferCount(); rotateMesh<&v3f::Y, &v3f::Z>(mesh, degrees);
for (u16 j = 0; j < mc; j++) {
scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
const u32 stride = getVertexPitchFromType(buf->getVertexType());
u32 vertex_count = buf->getVertexCount();
u8 *vertices = (u8 *)buf->getVertices();
for (u32 i = 0; i < vertex_count; i++)
((video::S3DVertex *)(vertices + i * stride))->Pos.rotateYZBy(degrees);
}
} }
void rotateMeshBy6dFacedir(scene::IMesh *mesh, int facedir) void rotateMeshBy6dFacedir(scene::IMesh *mesh, int facedir)
{ {
int axisdir = facedir >> 2; int axisdir = facedir >> 2;
facedir &= 0x03; facedir &= 0x03;
switch (facedir) {
u16 mc = mesh->getMeshBufferCount(); case 1: rotateMeshXZby(mesh, -90); break;
for (u16 j = 0; j < mc; j++) { case 2: rotateMeshXZby(mesh, 180); break;
scene::IMeshBuffer *buf = mesh->getMeshBuffer(j); case 3: rotateMeshXZby(mesh, 90); break;
const u32 stride = getVertexPitchFromType(buf->getVertexType()); }
u32 vertex_count = buf->getVertexCount(); switch (axisdir) {
u8 *vertices = (u8 *)buf->getVertices(); case 1: rotateMeshYZby(mesh, 90); break; // z+
for (u32 i = 0; i < vertex_count; i++) { case 2: rotateMeshYZby(mesh, -90); break; // z-
video::S3DVertex *vertex = (video::S3DVertex *)(vertices + i * stride); case 3: rotateMeshXYby(mesh, -90); break; // x+
switch (axisdir) { case 4: rotateMeshXYby(mesh, 90); break; // x-
case 0: case 5: rotateMeshXYby(mesh, -180); break;
if (facedir == 1)
vertex->Pos.rotateXZBy(-90);
else if (facedir == 2)
vertex->Pos.rotateXZBy(180);
else if (facedir == 3)
vertex->Pos.rotateXZBy(90);
break;
case 1: // z+
vertex->Pos.rotateYZBy(90);
if (facedir == 1)
vertex->Pos.rotateXYBy(90);
else if (facedir == 2)
vertex->Pos.rotateXYBy(180);
else if (facedir == 3)
vertex->Pos.rotateXYBy(-90);
break;
case 2: //z-
vertex->Pos.rotateYZBy(-90);
if (facedir == 1)
vertex->Pos.rotateXYBy(-90);
else if (facedir == 2)
vertex->Pos.rotateXYBy(180);
else if (facedir == 3)
vertex->Pos.rotateXYBy(90);
break;
case 3: //x+
vertex->Pos.rotateXYBy(-90);
if (facedir == 1)
vertex->Pos.rotateYZBy(90);
else if (facedir == 2)
vertex->Pos.rotateYZBy(180);
else if (facedir == 3)
vertex->Pos.rotateYZBy(-90);
break;
case 4: //x-
vertex->Pos.rotateXYBy(90);
if (facedir == 1)
vertex->Pos.rotateYZBy(-90);
else if (facedir == 2)
vertex->Pos.rotateYZBy(180);
else if (facedir == 3)
vertex->Pos.rotateYZBy(90);
break;
case 5:
vertex->Pos.rotateXYBy(-180);
if (facedir == 1)
vertex->Pos.rotateXZBy(90);
else if (facedir == 2)
vertex->Pos.rotateXZBy(180);
else if (facedir == 3)
vertex->Pos.rotateXZBy(-90);
break;
default:
break;
}
}
} }
} }
@ -410,8 +343,8 @@ scene::IMeshBuffer* cloneMeshBuffer(scene::IMeshBuffer *mesh_buffer)
video::S3DVertex2TCoords *v = video::S3DVertex2TCoords *v =
(video::S3DVertex2TCoords *) mesh_buffer->getVertices(); (video::S3DVertex2TCoords *) mesh_buffer->getVertices();
u16 *indices = mesh_buffer->getIndices(); u16 *indices = mesh_buffer->getIndices();
scene::SMeshBufferTangents *cloned_buffer = scene::SMeshBufferLightMap *cloned_buffer =
new scene::SMeshBufferTangents(); new scene::SMeshBufferLightMap();
cloned_buffer->append(v, mesh_buffer->getVertexCount(), indices, cloned_buffer->append(v, mesh_buffer->getVertexCount(), indices,
mesh_buffer->getIndexCount()); mesh_buffer->getIndexCount());
return cloned_buffer; return cloned_buffer;