Hi all,
I’m trying to calculate the centroid of a mesh’s faces, add that point to the list of vertices, and then further subdivide the mesh (sort of like this). I’ve gotten as far as finding the face centroids, but I’m not sure I understand how to reindex them as vertices back into the proper order in my vertex array. Calling addVertex just tacks them on to the end, and then the edges don’t connect in the proper order.
The addVertex documentation vaguely alludes to this, and ofMesh page tries to explain, but it doesn’t really go through adding indices to an already existing mesh.
I thought setupIndicesAuto would do the trick, but it doesn’t seem to be doing the trick in this particular instance.
Here’s what I’ve got. I’m just setting up a cube, finding the centroids, and adding them back to the mesh. So how do I “reindex” the vertices so that the centroids connect properly with all of their face corners?
.h
ofVboMesh vboMesh;
int w,h;
vector <ofMeshFace> faces;
ofVec3f calculateCentroid(ofMeshFace *face);
.cpp
void testApp::setup(){
w = 1280;
h = 720;
ofSetWindowShape(w,h);
ofSetFrameRate(30);
ofSetVerticalSync(true);
ofBackground(0, 0, 0);
ofEnableDepthTest();
vboMesh = ofMesh::box(400, 400, 400);
faces = vboMesh.getUniqueFaces();
vboMesh.setupIndicesAuto();
}
void testApp::draw(){
ofTranslate(w/2,h/2, -10);
ofRotate(ofGetMouseY(), 1, 0, 0);
ofRotate(ofGetMouseX(), 0, 1, 0);
for(unsigned int i = 0; i< faces.size(); i++){
ofMeshFace face = faces[i];
ofVec3f center = calculateCentroid(&face);
vboMesh.addVertex(center);
}
ofSetColor(255);
vboMesh.drawWireframe();
ofSetColor(255,0,0);
glPointSize(4);
vboMesh.drawVertices();
}
ofVec3f testApp::calculateCentroid(ofMeshFace *face){
int lastPointIndex = 3;
ofVec3f result;
// loop trough vertices
for (int i = 0; i < lastPointIndex; i++){
result += face->getVertex(i);
}
result /= lastPointIndex;
return result;
}
Thanks in advance for the help!