Split into functions a bit

This commit is contained in:
Marc Gilleron 2022-08-03 19:31:24 +01:00
parent adbe393440
commit 9973ac5e3a

View File

@ -55,6 +55,90 @@ static void dilate_normalmap(Span<Vector3f> normals, Vector2i size) {
}
}
NormalMapData::Tile compute_tile_info(
const transvoxel::CellInfo cell_info, const transvoxel::MeshArrays &mesh, unsigned int first_index) {
Vector3f normal_sum;
unsigned int ii = first_index;
for (unsigned int triangle_index = 0; triangle_index < cell_info.triangle_count; ++triangle_index) {
const unsigned vi0 = mesh.indices[ii];
const unsigned vi1 = mesh.indices[ii + 1];
const unsigned vi2 = mesh.indices[ii + 2];
ii += 3;
const Vector3f normal0 = mesh.normals[vi0];
const Vector3f normal1 = mesh.normals[vi1];
const Vector3f normal2 = mesh.normals[vi2];
normal_sum += normal0;
normal_sum += normal1;
normal_sum += normal2;
}
#ifdef DEBUG_ENABLED
ZN_ASSERT(cell_info.position.x >= 0);
ZN_ASSERT(cell_info.position.y >= 0);
ZN_ASSERT(cell_info.position.z >= 0);
ZN_ASSERT(cell_info.position.x < 256);
ZN_ASSERT(cell_info.position.y < 256);
ZN_ASSERT(cell_info.position.z < 256);
#endif
const NormalMapData::Tile tile{ //
uint8_t(cell_info.position.x), //
uint8_t(cell_info.position.y), //
uint8_t(cell_info.position.z), //
uint8_t(math::get_longest_axis(normal_sum))
};
return tile;
}
void get_axis_indices(Vector3f::Axis axis, unsigned int &ax, unsigned int &ay, unsigned int &az) {
switch (axis) {
case Vector3f::AXIS_X:
ax = Vector3f::AXIS_Z;
ay = Vector3f::AXIS_Y;
az = Vector3f::AXIS_X;
break;
case Vector3f::AXIS_Y:
ax = Vector3f::AXIS_X;
ay = Vector3f::AXIS_Z;
az = Vector3f::AXIS_Y;
break;
case Vector3f::AXIS_Z:
ax = Vector3f::AXIS_X;
ay = Vector3f::AXIS_Y;
az = Vector3f::AXIS_Z;
break;
default:
ZN_CRASH();
}
}
typedef FixedArray<math::BakedIntersectionTriangleForFixedDirection, transvoxel::MAX_TRIANGLES_PER_CELL> CellTriangles;
unsigned int prepare_triangles(unsigned int first_index, const transvoxel::CellInfo cell_info, const Vector3f direction,
CellTriangles baked_triangles, const transvoxel::MeshArrays &mesh) {
unsigned int triangle_count = 0;
unsigned int ii = first_index;
for (unsigned int ti = 0; ti < cell_info.triangle_count; ++ti) {
#ifdef DEBUG_ENABLED
ZN_ASSERT(ii + 2 < mesh.indices.size());
#endif
const unsigned vi0 = mesh.indices[ii];
const unsigned vi1 = mesh.indices[ii + 1];
const unsigned vi2 = mesh.indices[ii + 2];
ii += 3;
const Vector3f a = mesh.vertices[vi0];
const Vector3f b = mesh.vertices[vi1];
const Vector3f c = mesh.vertices[vi2];
math::BakedIntersectionTriangleForFixedDirection baked_triangle;
// The triangle can be parallel to the direction
if (baked_triangle.bake(a, b, c, direction)) {
baked_triangles[triangle_count] = baked_triangle;
++triangle_count;
}
}
return triangle_count;
}
// For each non-empty cell of the mesh, choose an axis-aligned projection based on triangle normals in the cell.
// Sample voxels inside the cell to compute a tile of world space normals from the SDF.
void compute_normalmap(Span<const transvoxel::CellInfo> cell_infos, const transvoxel::MeshArrays &mesh,
@ -69,40 +153,12 @@ void compute_normalmap(Span<const transvoxel::CellInfo> cell_infos, const transv
normal_map_data.tiles.reserve(cell_infos.size());
unsigned int ii = 0;
unsigned int first_index = 0;
for (unsigned int cell_index = 0; cell_index < cell_infos.size(); ++cell_index) {
const transvoxel::CellInfo cell_info = cell_infos[cell_index];
Vector3f normal_sum;
const unsigned int ii0 = ii;
for (unsigned int triangle_index = 0; triangle_index < cell_info.triangle_count; ++triangle_index) {
const unsigned vi0 = mesh.indices[ii];
const unsigned vi1 = mesh.indices[ii + 1];
const unsigned vi2 = mesh.indices[ii + 2];
ii += 3;
const Vector3f normal0 = mesh.normals[vi0];
const Vector3f normal1 = mesh.normals[vi1];
const Vector3f normal2 = mesh.normals[vi2];
normal_sum += normal0;
normal_sum += normal1;
normal_sum += normal2;
}
#ifdef DEBUG_ENABLED
ZN_ASSERT(cell_info.position.x >= 0);
ZN_ASSERT(cell_info.position.y >= 0);
ZN_ASSERT(cell_info.position.z >= 0);
ZN_ASSERT_CONTINUE(cell_info.position.x < 256);
ZN_ASSERT_CONTINUE(cell_info.position.y < 256);
ZN_ASSERT_CONTINUE(cell_info.position.z < 256);
#endif
const NormalMapData::Tile tile{ //
uint8_t(cell_info.position.x), //
uint8_t(cell_info.position.y), //
uint8_t(cell_info.position.z), //
uint8_t(math::get_longest_axis(normal_sum))
};
const NormalMapData::Tile tile = compute_tile_info(cell_info, mesh, first_index);
normal_map_data.tiles.push_back(tile);
const Vector3f cell_origin_world = to_vec3f(origin_in_voxels + cell_info.position * cell_size);
@ -110,25 +166,7 @@ void compute_normalmap(Span<const transvoxel::CellInfo> cell_infos, const transv
unsigned int ax;
unsigned int ay;
unsigned int az;
switch (tile.axis) {
case Vector3f::AXIS_X:
ax = Vector3f::AXIS_Z;
ay = Vector3f::AXIS_Y;
az = Vector3f::AXIS_X;
break;
case Vector3f::AXIS_Y:
ax = Vector3f::AXIS_X;
ay = Vector3f::AXIS_Z;
az = Vector3f::AXIS_Y;
break;
case Vector3f::AXIS_Z:
ax = Vector3f::AXIS_X;
ay = Vector3f::AXIS_Y;
az = Vector3f::AXIS_Z;
break;
default:
ZN_CRASH();
}
get_axis_indices(Vector3f::Axis(tile.axis), ax, ay, az);
Vector3f quad_origin_world = cell_origin_world;
quad_origin_world[az] += cell_size * 0.5f;
@ -161,30 +199,8 @@ void compute_normalmap(Span<const transvoxel::CellInfo> cell_infos, const transv
tls_z_buffer.reserve(max_buffer_size);
// Optimize triangles
FixedArray<math::BakedIntersectionTriangleForFixedDirection, transvoxel::MAX_TRIANGLES_PER_CELL>
baked_triangles;
unsigned int triangle_count = 0;
{
unsigned int ii2 = ii0;
for (unsigned int ti = 0; ti < cell_info.triangle_count; ++ti) {
#ifdef DEBUG_ENABLED
ZN_ASSERT(ii2 + 2 < mesh.indices.size());
#endif
const unsigned vi0 = mesh.indices[ii2];
const unsigned vi1 = mesh.indices[ii2 + 1];
const unsigned vi2 = mesh.indices[ii2 + 2];
ii2 += 3;
const Vector3f a = mesh.vertices[vi0];
const Vector3f b = mesh.vertices[vi1];
const Vector3f c = mesh.vertices[vi2];
math::BakedIntersectionTriangleForFixedDirection baked_triangle;
// The triangle can be parallel to the direction
if (baked_triangle.bake(a, b, c, direction)) {
baked_triangles[triangle_count] = baked_triangle;
++triangle_count;
}
}
}
CellTriangles baked_triangles;
unsigned int triangle_count = prepare_triangles(first_index, cell_info, direction, baked_triangles, mesh);
// Fill query buffers
{
@ -289,6 +305,8 @@ void compute_normalmap(Span<const transvoxel::CellInfo> cell_infos, const transv
to_span_from_position_and_size(normal_map_data.normals, tile_begin, math::squared(tile_resolution)),
Vector2i(tile_resolution, tile_resolution));
}
first_index += 3 * cell_info.triangle_count;
}
}