Iterating vertexes ofMesh::sphere

I’ve been trying to displace the surface of a ofMesh::sphere instance by iterating over the vertexes and adding a noise function. Most things are working but somehow the iterating over the vertexes doesn’t seem normal. There’s a stitching error spiralling around the sphere, and the effect occurs symmetrical. Also one of the ends (the top i guess) has all vertexes connecting to the last vertex, but that last one doesn’t seem to move at all.
From the source code of the ofMesh I can’t figure out how the vertexes are generated, so I’ve been struggling to fix this.

I’ve included the displace function of the class that holds and construct geometries. geoRes is a variable that holds the resolution of the mesh (which is 12 as a default, 200 in my current code). originalVertexes[] is a vector that stores all the original positions.

Hope someone can help me out :slight_smile:

void GeoHandler::displace() {
    for (int y = 0; y<geoRes*2 + 1; y++){
             for (int x = 0; x<geoRes*2 + 1; x++){
                 glm::vec3 tempVertex = geoMesh.getVertex(x+(y*geoRes));
                 glm::vec3 tempNormal = geoMesh.getNormal(x+(y*geoRes));
                 float noiseSpeed = ofGetFrameNum() * 0.01;
                 float xNoise = x * 0.01;
                 float yNoise = y * 0.01;
                 float noiseVal = ofNoise(noiseSpeed + xNoise,noiseSpeed + yNoise);
                 float noiseMap = ofMap(noiseVal,0,1,0,displaceSize);
                 glm::vec3 newVertex = originalVertexes[x+(y*geoRes)] + (tempNormal * noiseMap);
                 geoMesh.setVertex(x+(y*geoRes), newVertex);

Do not do this in the mesh. it is super slow. Do it in the vertex shader.The technique is called vertex displacement. There is a nice tutorial here It is in threejs but it can be applied in OF. Have a look in the examples/shader folder if you are new to shaders.

Isn’t that a bit extreme? :slight_smile: You know what they say about premature optimization… I think it’s fine to try on the CPU, and if the frame rate is not high enough, then you can learn about shaders.

Probably the reason you see a symmetrical noise is that you are only using x and y in ofNoise, but not Z. So it’s like applying a deformation plane that crosses the shape. If you also use Z it should no longer be symmetrical.

A second issue is that you are not updating the normals. The normal of a deformed sphere no longer point outwards, but in different directions depending on the deformation. The forum contains posts describing how to calculate the normals for your mesh.

About performance, as @edapx mentions, shaders would make this much faster. But even on the CPU, it would be probably better to use ofMesh::getVertices() and ofMesh::getNormals() because they return references to the data, avoiding making copies of each vertex and normal. Then you could iterate over those references and update them directly. Another benefit is that you don’t need to calculate the index of the vertex or the normal, making sure you iterate through every single one.