Update vertices' positions of a mesh

Hi, I just started with openFrameworks, and have a Processing sketch, which I would like to implement. I have mesh whose vertices I want to update, and I’m not sure how and where (update/draw) to do it. I want the amount of Perlin noise to change over time. Here’s my ofApp.cpp:

ofEasyCam easyCam;
ofMesh mesh;

float tSize = 60;
float noiseScale = 0;
float tDistance = 6;
float heightValue = 0;
float heightScale = 20;

int counter = 0;

void ofApp::setup(){

void ofApp::update(){

void ofApp::draw(){
    glTranslatef(-tSize, 0, -tSize*0.25*tDistance);
    glTranslatef(-(tSize/2)*tDistance, -(tSize/2)*tDistance, 0);
    glRotatef(90, 1, 0, 0);
    for (int x=0; x<tSize; x++) {
        for (int y=0; y<tSize; y++) {
            noiseScale = sin(counter*0.1);
            heightValue = sin(ofMap(x,0,tSize,0,2*pi))*heightScale
            + sin(ofMap(y,0,tSize,0,2*pi))*heightScale*0;
            heightValue += ofNoise(x*noiseScale,y*noiseScale)*20;
            ofVec3f pos(tDistance*x, tDistance*y, heightValue);
    for (int x=0; x<tSize-1; x++) {
        for (int y=0; y<tSize-1; y++) {
            mesh.addIndex(x+y*tSize);               // 0
            mesh.addIndex((x+1)+y*tSize);           // 1
            mesh.addIndex(x+(y+1)*tSize);           // 10
            mesh.addIndex((x+1)+y*tSize);           // 1
            mesh.addIndex((x+1)+(y+1)*tSize);       // 11
            mesh.addIndex(x+(y+1)*tSize);           // 10

Thanks for your help!


So as you are adding more vertices to the mesh rather than modifying an existing mesh you need to do mesh.clear() first, just above where you start your for for loop in draw. Eventually you will run out of memory and crash otherwise.

Then you are drawing the mesh before you add indices. And the way you are (correctly) calculating the indices they will always point to the first set of vertices you added, not the new ones you added on top which have changed.

So clearing the mesh before you add the fresh vertices and move the mesh.draw to after the loop where you added indices to the mesh should get you what you want.

If you are interested in using ofNoise I’ve got some example files here from a workshop I did a while back: https://github.com/andreasmuller/NoiseWorkshop

Perfect, thank you!!!