Trying to move some ofMesh vertices

I am trying to get some vertices of an ofMesh to move around. It looks like the vertices do change (according to haveVertsChanged()), but the they are not being redrawn. I uploaded a minimal example to http://github.com/jedahan/vertices , which is copied below. I feel like I’ve got fundamental misunderstandings of how ofMesh is meant to be used (or what better options there are) so any help with that, or making these vertices move would be appreciated.

src/ofMain.h

#pragma once
#include "ofMain.h"
class ofApp : public ofBaseApp{
  public:
    void setup();
    void update();
    void draw();
    void mousePressed(int x, int y, int button);
    vector<ofMesh> fireworks;
};

src/ofMain.cpp

#include "ofApp.h"
void ofApp::setup(){ ofBackground(0); }

void ofApp::update() {
  for(ofMesh firework : fireworks) {
    for(int i=0; i<firework.getNumVertices(); i++){
      firework.setVertex(i, firework.getVertex(i) + ofVec3f(100*(0.5-ofRandomuf()),0,0));
    }
  }
}

void ofApp::draw() {
  for(ofMesh firework : fireworks){ firework.drawVertices(); }
}

void ofApp::mousePressed(int x, int y, int button){
  ofMesh f;
  for(int i=0; i<100; i++){
    f.addVertex(ofVec3f(x+(ofRandom(80)-40),(y+ofRandom(80)-40),0));
    f.addColor(ofColor(255,255,0));
  }
  fireworks.push_back(f);
}

OK so it seems the big misunderstanding is that I was changing the vertice for a copy of the firework, not accessing it directly.

A switch to

for(int i=0; i<fireworks.size(); i++){
  for(int j=0; j<fireworks[i].getNumVertices(); j++){
    fireworks[i].setVertex(j, fireworks[i].getVertex(j) + ofVec3f(100*(0.5-ofRandomuf()),0,0));
  }
}

fixed it.

I’m still interested in knowing if there are better alternatives to what I’m doing, but this had been solved for me and isn’t really specific to oF :slight_smile:

For this kind of stuff I use vector of pointers

vector<ofMesh*> fireworks;

Because we can’t (at least I don’t know how) store references in vectors.

  for(int i=0; i<fireworks.size(); i++){
   for(int j=0; j<fireworks[i]->getNumVertices(); j++){
    fireworks[i]->setVertex(j, fireworks[i]->getVertex(j) + ofVec3f(100*(0.5-ofRandomuf()),0,0));
   }
 }

I find manipulating pointers easier to handle and easier to read… I can get pointers from the vector and pass them to functions anytime I need it. The only drawback is that you must take care of deallocating memory when an object is no more used.

But putting the deallocation code inside the exit() function of ofApp or in the destructor of the container class seems to be a good way to cleanup the stuff easily.

Even more, by using pointers I can use polymorphism and store in vector pointers from derived type, BUT have the derived methods called, like

vector<MyBaseClass*> foo;
MyDerivedClass *whi = new MyDerivedClass();
MyOtherDerivedClass *plash = new MyOtherDerivedClass();
foo.push_back(whi);
foo.push_back(plash);

This is related with ‘virtual’ methods, MyBaseClass declares a method virtual and this method is redefined in a derived class this is the code from the derived class that will be called when

foo[i]->doIt();

That way you can store bananas and apples in the same vector.

May be there is a simpler solution but until now, this is working quite well for me…

1 Like

your original syntax was missing a & if not you are always making a copy:

for(ofMesh & firework : fireworks) {
    for(ofVec3f & vertex: firework.getVertices()){
      vertex += ofVec3f(100*(0.5-ofRandomuf()),0,0));
    }
}

or even:

for(auto & firework : fireworks) {
    for(auto & vertex: firework.getVertices()){
      vertex += ofVec3f(100*(0.5-ofRandomuf()),0,0));
    }
}

using this new c++11 syntax is not only simpler and less error prone (ie: in nested for loops you can’t swap an i by a j) but also faster than an incrementing counter.

pointers is a possibility but then you need to take care of deleting freed memory, know when to free it so no one else is using it… in general whenever you need to modify something returned by an object you need to get it as a reference with & or else you’ll be making a copy. it’s also preferable to use & instead of pointers when possible since it simplifies memory management a lot.

1 Like

I’m wondering what it means by
for(ofMesh & firework : fireworks)
and
for(ofMesh firework : fireworks)

what does “:” means in open frameworks or c++ ?
sorry for dumb question.

the for with : translates as something like, foreach firework in fireworks

the & in c++ always means reference in this case the ofMesh & firework is a reference to the original ofMesh inside the vector<ofMesh>. if you don’t add it and instead use ofMesh firework what you get is a copy of the original which is slower and any modification will get lost.

Thanks for informative post


namal