godot_voxel/meshers/dmc/mesh_builder.h

56 lines
1.0 KiB
C
Raw Normal View History

#ifndef MESH_BUILDER_H
#define MESH_BUILDER_H
#include "../../util/utility.h"
#include <core/map.h>
#include <scene/resources/mesh.h>
#include <vector>
namespace dmc {
// Faster than SurfaceTool, only does what is needed to build a smooth mesh
class MeshBuilder {
public:
2019-04-22 16:14:20 -07:00
MeshBuilder() :
_reused_vertices(0) {}
inline void add_vertex(Vector3 position, Vector3 normal) {
int i = 0;
Map<Vector3, int>::Element *e = _position_to_index.find(position);
if (e) {
i = e->get();
2019-04-22 16:14:20 -07:00
++_reused_vertices;
} else {
i = _positions.size();
_position_to_index.insert(position, i);
_positions.push_back(position);
_normals.push_back(normal);
}
_indices.push_back(i);
}
Array commit(bool wireframe);
void clear();
2019-04-22 16:14:20 -07:00
int get_reused_vertex_count() const { return _reused_vertices; }
private:
std::vector<Vector3> _positions;
std::vector<Vector3> _normals;
std::vector<int> _indices;
Map<Vector3, int> _position_to_index;
2019-04-22 16:14:20 -07:00
int _reused_vertices;
};
} // namespace dmc
#endif // MESH_BUILDER_H