At first I looked at an example https://github.com/kripken/bullet/blob/ ... o.cpp#L169, but it does not suit me as there they use WavefrontObj for getting data, and I receive other structures. For example, for simple cube the array of indexes will have 36 elements, vertices 108 (x, y, z), but HACD structure will require 8 unique points and 12 triangles consisting of points' indexes. For setting the data, I added the addVertex function which saves unique positions of vertices
Code: Select all
int addVertex(btVector3 v)
{
int index = unique_vertices.size();
auto it = std::find_if(unique_vertices.begin(), unique_vertices.end(), [v](btVector3 vertex) {return vertex == v; });
if (it != unique_vertices.end())
index = it - unique_vertices.begin();
else
unique_vertices.push_back(v);
return index;
}
Code: Select all
for (int i = 0; i < this->indices.size() / 3; i++)
{
int index0 = this->indices[i * 3];
int index1 = this->indices[i * 3 + 1];
int index2 = this->indices[i * 3 + 2];
btVector3 vertex0(this->vertices[index0 * 3], this->vertices[index0 * 3 + 1], this->vertices[index0 * 3 + 2]);
btVector3 vertex1(this->vertices[index1 * 3], this->vertices[index1 * 3 + 1], this->vertices[index1 * 3 + 2]);
btVector3 vertex2(this->vertices[index2 * 3], this->vertices[index2 * 3 + 1], this->vertices[index2 * 3 + 2]);
int v0 = addVertex(vertex0);
int v1 = addVertex(vertex1);
int v2 = addVertex(vertex2);
//clockwise order ?
triangle_indices.push_back(v2);
triangle_indices.push_back(v1);
triangle_indices.push_back(v0);
}
for (const btVector3& v : this->unique_vertices)
{
HACD::Vec3<HACD::Real> vertex(v.x(), v.y(), v.z());
points.push_back(vertex);
}
for (int i = 0; i < this->triangle_indices.size() / 3; i++)
{
HACD::Vec3<long> triangle(this->triangle_indices[i * 3], this->triangle_indices[i * 3 + 1], this->triangle_indices[i * 3 + 2]);
triangles.push_back(triangle);
}