Update vertex position in mesh

hello,

I can’t find the way to update vertex position inside an ofMesh.

here is the mesh creation in setup() :

     for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
        
        ofColor c = scaledImage.getColor(x, y);
        float brightness = c.getBrightness();
        ofMap(brightness, 0, 1, -displacement, displacement);
        ofPoint p(x * MAP_SCALE, y * MAP_SCALE, brightness);
        mesh.addVertex(p);
        mesh.addColor(image.getColor(x * MAP_SCALE, y * MAP_SCALE));
    }
}

//add indices
for (int y = 0; y< height - 1; y++){
    for (int x=0; x < width - 1; x++){
        mesh.addIndex(x+y*width);               
        mesh.addIndex((x+1)+y*width);         
        mesh.addIndex(x+(y+1)*width);         
        
        mesh.addIndex((x+1)+y*width);          
        mesh.addIndex((x+1)+(y+1)*width);     
        mesh.addIndex(x+(y+1)*width);           
    }
}

i also add an ofParameter listener to update the mesh when a slider is modified :

  displacement.addListener(this, &ofApp::onDisplacementChanged);

then in the listener function i update the mesh :

void ofApp::onDisplacementChanged(float &val){
    for (int i = 0 ; i < mesh.getNumIndices(); i++) {
        float brightness = mesh.getColor(i).getBrightness();
        ofMap(brightness, 0, 1, -val, val);
        ofPoint oldPoint = mesh.getVertex(i);
        ofPoint newPoint(oldPoint.x, oldPoint.y, brightness);
        mesh.setVertex(i, newPoint);
    }
}

this is supposed to read and modify the z value of the vertices position according to the brightness of some pixels.
It doesn’t work…

well something happens : the mesh is flattened. Every vertex seems to go back to the (x, y, 0) position.

thanks for your help

ofMap returns a float, but you aren’t setting it to anything. Seems like you want

brightness = ofMap(brightness, 0, 1, -val, val);

The way you have it now, that line does nothing, and brightness stays at its original value between 0 and 1. Same during the setup function:

brightness = ofMap(brightness, 0, 1, -displacement, displacement);

oops ! a bit too obvious :wink:
I didn’t even noticed it as it was working as expected !
thanks a lot.

Therefore, i get an EXE_BAD_ACCESS when moving the slider
probably something dealing with index out of bounds or something like this but i can’t figure it out…

setup() :

for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
        
        //ofColor c = grayMap.getPixels().getColor(x, y);
        ofColor c = scaledImage.getColor(x, y);
        float brightness = ofMap(c.getBrightness(), 0, 1, -displacement, displacement);
        ofPoint p(x * MAP_SCALE, y * MAP_SCALE, brightness);
        mesh.addVertex(p);
        mesh.addColor(image.getColor(x * MAP_SCALE, y * MAP_SCALE));
    }
}

//add indices
for (int y = 0; y< height - 1; y++){
    for (int x=0; x < width - 1; x++){
        mesh.addIndex(x+y*width);               // 0
        mesh.addIndex((x+1)+y*width);           // 1
        mesh.addIndex(x+(y+1)*width);           // 10
        
        mesh.addIndex((x+1)+y*width);           // 1
        mesh.addIndex((x+1)+(y+1)*width);       // 11
        mesh.addIndex(x+(y+1)*width);           // 10
    }
}

Listener :

void ofApp::onDisplacementChanged(float &val){
    for (int i = 0 ; i < mesh.getNumIndices() ; i++) {
        float b = mesh.getColor(i).getBrightness();
        float brightness = ofMap(b, 0, 1, -val, val);
        ofPoint oldPoint = mesh.getVertex(i);
        ofPoint newPoint(oldPoint.x, oldPoint.y, brightness);
        mesh.setVertex(i, newPoint);
        //mesh.getVertex(i).set(newPoint);
    }
}

hi. that EXE_BAD_ACCESS is because the for loop inside void ofApp::onDisplacementChanged(float &val){ is using the number of indices as limit which is different of the number of vertices, actually it is equal or greater than. just change for (int i = 0 ; i < mesh.getNumIndices() ; i++) { to for (int i = 0 ; i < mesh.getNumVertices() ; i++) {

best

yes ! perfect. Thanks @roymacdonald
I don’t get why number of indices is not equal to number of vertices though. Unless i am wrong, i have created an index for each vertex ?

I really need to understand openGL a bit more, some things are a bit odd to me.

The indices are used for calculating triangles. So each one is the index number of a vertex, and 3 of them make a triangle. In most cases, a vertex is part of multiple triangles, so you get a lot of repeat indices. Here’s a really shittily drawn example:

So vertices are 1, 2, 3 and 4, but indices will be 1, 2, 3, 1, 3, 4. Or something similar.

oh ! yes of course that makes sense !
thanks a lot for all those useful information !
I keep digging into openGL world :wink:

thanks again