Hey,
When writing addVertex, the vertex gets added, but the one which was at the same (x,y) position doesn’t get removed, so your mesh just adds more and more vertices.
Yes my answer wasn’t very clear sorry, it’s because I didn’t test the code…
I have redone it using ofVbo since it’s a more elegant way of doing it( coding wise).
I think you should setup your ofVbomesh in setup, with colors, vertices and indices, then change the vertices z positions during update.
See the example below:
ofEasyCam cam;
int maxW = 400;
int maxH = 400;
ofVboMesh vbo;
int res = 5;
int amplitude = 160;
float perlinIndexX = 0.0;
float perlinIndexY = 0.0;
float incPerlinX = 0.01;
float incPerlinY = 0.05;
int indexVertex = 0;
vbo.setMode(OF_PRIMITIVE_TRIANGLES);
for( int i = -maxW; i <= maxW; i += res){
perlinIndexY = 0.0;
for( int j = -maxH; j <= maxH; j += res){
float z = ofNoise(perlinIndexX, perlinIndexY) * amplitude;
ofColor randomSurface = ofColor((float)(i* 255 /(maxW * 2)),
(float)(j* 255 /(maxH * 2)),
(float)(255 - (i * 255 + j * 255 ) /(maxH * 2 + maxW * 2)));
//init triangle
ofVec3f vert = {(float)i, (float)j, z};
vbo.addVertex(vert);
vbo.addColor(randomSurface);
vbo.addIndex(indexVertex);
indexVertex +=1;
z = ofNoise(perlinIndexX, perlinIndexY + incPerlinY) * amplitude;
vert = {(float)(i), (float)(j+res), z};
vbo.addVertex(vert);
vbo.addColor(randomSurface);
vbo.addIndex(indexVertex);
indexVertex +=1;
z = ofNoise(perlinIndexX + incPerlinX, perlinIndexY) * amplitude;
vert = {(float)(i+res), (float)j, z};
vbo.addVertex(vert);
vbo.addColor(randomSurface);
vbo.addIndex(indexVertex);
indexVertex +=1;
//bottom triangle
z = ofNoise(perlinIndexX, perlinIndexY + incPerlinY) * amplitude;
vert = {(float)(i), (float)(j+res), z};
vbo.addVertex(vert);
vbo.addColor(randomSurface);
vbo.addIndex(indexVertex);
indexVertex +=1;
z = ofNoise(perlinIndexX + incPerlinX, perlinIndexY + incPerlinY) * amplitude;
vert = {(float)(i + res), (float)(j+res), z};
vbo.addVertex(vert);
vbo.addColor(randomSurface);
vbo.addIndex(indexVertex);
indexVertex +=1;
z = ofNoise(perlinIndexX + incPerlinX, perlinIndexY) * amplitude;
vert = {(float)(i+res), (float)j, z};
vbo.addVertex(vert);
vbo.addColor(randomSurface);
vbo.addIndex(indexVertex);
indexVertex +=1;
perlinIndexY += incPerlinY;
}
perlinIndexX += incPerlinX;
}
in ofApp::draw() :
cam.begin();
vbo.draw();
cam.end();
should look like this :
In the update function, you can reuse the same function as to initialise the vbo, change addVertex
to setVertex
, not change the colors or indices, and increment the perlinIndexY
and perlinIndexX
over time to get the sliding effect.
I hope it helps.
Also this is only one way of doing it, there is probably a more elegant way of doing it i guess…