Tweak the ofConePrimitives' vertices

I’m using the ofConePrimitive to create a pyramid. I want to tweak the vertices and in the documention ([ofConePrimitive]) the following is given to do that.

// get all the faces from the cpme, handy when you want to copy
// individual vertices or tweak them a little
vector triangles = cone.getMesh().getUniqueFaces();

So now they’re stored in a variable, but how do I get the vertices, tweak them and aply them? Or use them to draw lines between them?

The vector (actually, a vector<ofMeshFace>) that you get from cone.getMesh().getUniqueFaces() is a list of the triangles required to draw the cone. Each individual ofMeshFace in the vector is a triangle - a set of three vertices.

Each vertex (each represented as an ofVec3f) in an ofMeshFace (the triangle) can be manipulated as you like by adding or substracting from the individual x/y/z coordinates, or by adding/subtracting a complete vector (another ofVec3f)

Once you’re done manipulating the vertices, you put them all back into the cone’s mesh with cone.getMesh().setFromTriangles(triangles), and then you draw the cone.

sine-functions are usually fun, so for example you could do this:

cone.setMode(OF_PRIMITIVE_TRIANGLES);
vector<ofMeshFace> triangles = cone.getMesh().getUniqueFaces();

float displacement = sin(ofGetElapsedTimef() * 4) * 10;
for (size_t i = 0; i < triangles.size(); i++) {
    ofVec3f normal = triangles[i].getFaceNormal();
    for (int j = 0; j < 3; j++) {
        triangles[i].setVertex(j, triangles[i].getVertex(j) + normal * displacement);
    }
}
cone.getMesh().setFromTriangles(triangles);

cone.drawWireframe();

(I’ve used cone.drawWireframe() to make it more clear what is going on - you can also use cone.draw())

Here, we slide each triangle back-and-forth along it’s own normal: this gives the effect of the cone repeatedly exploding & imploding. The displacement depends on the sin of the total time the program is running, times two factors I picked because the result looked cool :slight_smile:

Hope this helps!

2 Likes