godot_voxel/meshers/dmc/mesh_builder.cpp
Marc Gilleron 48081faf03 Use real_t in most utils, use 32-bit floats in meshers.
This is a first step to supporting double-precision floats in Godot.
Meshers don't need doubles because vertices, normals and UVs should
never span large enough areas while needing such amount of precision.
Besides, it costs a lot more memory and processing.
2022-02-14 00:11:41 +00:00

67 lines
1.5 KiB
C++

#include "mesh_builder.h"
#include "../../util/funcs.h"
#include "../../util/godot/funcs.h"
#include <scene/resources/mesh.h>
namespace zylann::voxel::dmc {
Array MeshBuilder::commit(bool wireframe) {
if (_positions.size() == 0) {
return Array();
}
ERR_FAIL_COND_V(_indices.size() % 3 != 0, Array());
if (wireframe) {
// Debug purpose, no effort to be fast here
std::vector<int> wireframe_indices;
for (unsigned int i = 0; i < _indices.size(); i += 3) {
wireframe_indices.push_back(_indices[i]);
wireframe_indices.push_back(_indices[i + 1]);
wireframe_indices.push_back(_indices[i + 1]);
wireframe_indices.push_back(_indices[i + 2]);
wireframe_indices.push_back(_indices[i + 2]);
wireframe_indices.push_back(_indices[i]);
}
_indices = wireframe_indices;
}
PackedVector3Array positions;
PackedVector3Array normals;
PackedInt32Array indices;
copy_to(positions, _positions);
copy_to(normals, _normals);
raw_copy_to(indices, _indices);
clear();
Array surface;
surface.resize(Mesh::ARRAY_MAX);
surface[Mesh::ARRAY_VERTEX] = positions;
surface[Mesh::ARRAY_NORMAL] = normals;
surface[Mesh::ARRAY_INDEX] = indices;
return surface;
}
void MeshBuilder::scale(float scale) {
for (auto it = _positions.begin(); it != _positions.end(); ++it) {
*it *= scale;
}
}
void MeshBuilder::clear() {
_positions.clear();
_normals.clear();
_indices.clear();
_position_to_index.clear();
_reused_vertices = 0;
}
} // namespace zylann::voxel::dmc