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