48081faf03
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.
55 lines
1.1 KiB
C++
55 lines
1.1 KiB
C++
#ifndef MESH_BUILDER_H
|
|
#define MESH_BUILDER_H
|
|
|
|
#include "../../util/math/vector3f.h"
|
|
#include <core/templates/map.h>
|
|
#include <core/variant/array.h>
|
|
#include <vector>
|
|
|
|
namespace zylann::voxel::dmc {
|
|
|
|
// Faster than SurfaceTool, only does what is needed to build a smooth mesh
|
|
class MeshBuilder {
|
|
public:
|
|
MeshBuilder() : _reused_vertices(0) {}
|
|
|
|
inline void add_vertex(Vector3f position, Vector3f normal) {
|
|
int i = 0;
|
|
|
|
Map<Vector3f, int>::Element *e = _position_to_index.find(position);
|
|
|
|
if (e) {
|
|
i = e->get();
|
|
++_reused_vertices;
|
|
|
|
} else {
|
|
i = _positions.size();
|
|
_position_to_index.insert(position, i);
|
|
|
|
_positions.push_back(position);
|
|
_normals.push_back(normal);
|
|
}
|
|
|
|
_indices.push_back(i);
|
|
}
|
|
|
|
void scale(float scale);
|
|
Array commit(bool wireframe);
|
|
void clear();
|
|
|
|
int get_reused_vertex_count() const {
|
|
return _reused_vertices;
|
|
}
|
|
|
|
private:
|
|
std::vector<Vector3f> _positions;
|
|
std::vector<Vector3f> _normals;
|
|
std::vector<int> _indices;
|
|
Map<Vector3f, int> _position_to_index;
|
|
int _reused_vertices;
|
|
};
|
|
|
|
} // namespace zylann::voxel::dmc
|
|
|
|
#endif // MESH_BUILDER_H
|