godot_voxel/meshers/dmc/mesh_builder.cpp
Marc Gilleron 922f361cb0 Made it compile in Godot 4
- Made a bunch of changes to comply with Godot 4 API
- Use Godot's Vector3i and add the missing stuff with helper functions
- Transvoxel uses custom attributes API, the old way would not work
- Wrap MeshOptimizer in a unique namespace (see build script why)
- Added clang-format file for the module as some rules now differ
- Prevent thirdparty code and lookup tables from being clang-formatted
- Very likely full of runtime bugs that need fixing
2021-12-13 21:38:10 +00:00

66 lines
1.4 KiB
C++

#include "mesh_builder.h"
#include "../../util/funcs.h"
#include <scene/resources/mesh.h>
namespace 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;
raw_copy_to(positions, _positions);
raw_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 dmc