Manipulating vertices on a sphere

Hi All,

I am trying to create a sphere and be able to manipulate the vertices. Maybe I am way off on how to do this but I started with a sphere primitive and copied used getMesh to make that into a proper mesh. Everything works fine except for when I come to move the mesh vertices. My code below…any help is super appreciated.

ofSpherePrimitive sphere;
ofMesh planet;

ofEasyCam cam;

//--------------------------------------------------------------
void ofApp::setup(){
    
    sphere.setRadius(200);
    sphere.setResolution(4);
    sphere.setPosition(ofGetWidth()/2, ofGetHeight()/2, 0);
    
    planet = sphere.getMesh();
    vector<ofVec3f> verts = planet.getVertices();
  
    for(int i = 0; i < verts.size(); i++){
        
        //IN HERE 
        verts[i].x += ofRandom(300);
        verts[i].y += ofRandom(300);
        verts[i].z += ofRandom(300);
        
        planet.addColor(ofFloatColor( float(i)/planet.getVertices().size(), 0, 1.0 - (float(i)/planet.getVertices().size()) ));
        
    }

}

//--------------------------------------------------------------
void ofApp::update(){

}

//--------------------------------------------------------------
void ofApp::draw(){
    
    ofBackground(180, 180, 180);
   
    cam.begin();
    planet.drawWireframe();
    cam.end();
 
//    sphere.drawWireframe();

}

Hi, does this do it?

vector<ofVec3f>& verts = planet.getVertices();

1 Like

do as suggested by mr. hahakid

ofSpherePrimitive sphere;
ofMesh planet;
ofEasyCam cam;

//--------------------------------------------------------------
void ofApp::setup(){

    sphere.setRadius(200);
    sphere.setResolution(4);
    sphere.setPosition(ofGetWidth()/2, ofGetHeight()/2, 0);

    planet = sphere.getMesh();
}

//--------------------------------------------------------------
void ofApp::update(){
        vector<ofVec3f>& verts = planet.getVertices();
        for(unsigned int i = 0; i < verts.size(); i++){
                verts[i].x += ofSignedNoise(verts[i].x*ofRandom(-1.,1), verts[i].y/ofRandom(-1.,1),verts[i].z/ofRandom(-1.,1), ofGetElapsedTimef());
                verts[i].y += ofSignedNoise(verts[i].z*ofRandom(-1.,1), verts[i].x/ofRandom(-1.,1),verts[i].y/ofRandom(-1.,1), ofGetElapsedTimef());
                verts[i].z += ofSignedNoise(verts[i].y*ofRandom(-1.,1), verts[i].z/ofRandom(-1.,1),verts[i].x/ofRandom(-1.,1), ofGetElapsedTimef());
        }
}

//--------------------------------------------------------------
void ofApp::draw(){

    ofBackground(180, 180, 180);

    cam.begin();
    planet.drawWireframe();
    cam.end();

//  sphere.drawWireframe();

}
1 Like

Thanks @hahakid that worked. What exactly is the & doing there?

@kashim your noise example works awesome The vertices start to break, which makes sense… is there a simple way to say move all connected vertices/indices?

Thanks guys. Feeling super noob today.

Hi, it declares the variable to be a reference to a vector< ofVec3f > instead of an actual vector< ofVec3f >

It’s the difference between grabbing a reference to the original vertices and grabbing a new copy of the original vertices.

http://www.cprogramming.com/tutorial/references.html

1 Like

Durrr… to answer my own question you need to set the primitive type…

planet.setMode(OF_PRIMITIVE_TRIANGLE_STRIP);