Intermittent Rendering using multiple ofVboMesh

I am using two seperate ofVboMesh to render and rotate two spheres. However, when I run the code, the rendering results are intemitantly incorrect.

It should render a sphere rotating inside another sphere. However, sometimes it will render nothing, sometimes the inner sphere, sometimes the outer sphere, and sometimes both (see included images).

Any ideas of what might be going on, and why results are different each time? I have include the main code below. Ive commented out the syphon code, and tried to just draw one mesh, but am still getting intermittent results.

#include "ofApp.h"
#include "MeshUtils.h"
#include "ImageLoader.h"
#include "ofxSyphonClient.h"

MeshUtils utils;

float const BOUNDS_PADDING = 0.0;
float const POINT_COUNT = 10000;
int const ALPHA = 1.0 * 255;
int const RADIUS = 100;

ofRectangle bounds;
vector<ofVec3f>points;
vector<ofVec3f>outsidePoints;

ofVboMesh mesh;
ofVboMesh outsideMesh;

ImageLoader image;

ofVec3f center;

ofxSyphonServer syphon;

ofEasyCam cam;

//--------------------------------------------------------------
void ofApp::setup(){
    utils.enableScreenShot("Network_5");
    syphon.setName("Network_5");
    
    cam.setDistance(600.0);
    
    center.set(ofGetWidth() / 2, ofGetHeight() / 2, 0.0);
    
    points = MeshUtils::getRandomPointsOnSphere(center, RADIUS, POINT_COUNT);
    outsidePoints = MeshUtils::getRandomPointsOnSphere(center, RADIUS * 2, POINT_COUNT);
    
    bool imageLoaded = image.load("../../../images/gradient_3.jpg");
    

    if(!imageLoaded) {
        cout << "Error: Could not load image. Exiting app." << endl;
        ofExit();
    }
    
    image.setAlpha(ALPHA);
    image.resize(ofGetWidth() - BOUNDS_PADDING * 2, ofGetHeight() - BOUNDS_PADDING * 2);
    
    mesh.setMode(OF_PRIMITIVE_LINES);
    mesh.enableColors();
    
    outsideMesh.setMode(OF_PRIMITIVE_LINES);
    outsideMesh.enableColors();
    
    ofSetBackgroundAuto(true);
    ofSetBackgroundColor(ofColor::white);
}

void ofApp::createSphere(vector<ofVec3f> & _points, ofVboMesh & _mesh) {
    
    for(int i = 0 ; i < POINT_COUNT; i++) {
        ofVec3f p = _points[i]; //is this copying or passing by reference?
        
        //value so we can figure out if the vector has been initialized
        int initValue = -10000000;
        ofVec3f closest(initValue, initValue, initValue);
        
        for(int k = 0; k < POINT_COUNT; k++) {
            
            if(i == k) {
                continue;
            }
            
            ofVec3f p2 = _points[k];
            
            if(closest.x == initValue) {
                closest = p2;
                continue;
            }
            
            if(p.distance(p2) < p.distance(closest)) {
                closest = p2;
            }
        }
        
        _mesh.addColor(image.getColor(p));
        _mesh.addVertex(p);
        
        _mesh.addColor(image.getColor(closest));
        _mesh.addVertex(closest);
    }
}


//--------------------------------------------------------------
void ofApp::update(){
    
    if(mesh.getNumVertices() > 0) {
        return;
    }
    
    mesh.clear();
    outsideMesh.clear();
    
    createSphere(points, mesh);
    createSphere(outsidePoints, outsideMesh);

}

//--------------------------------------------------------------
void ofApp::draw(){
    
    cam.begin();
    
    ofPushMatrix();
        ofRotate(ofGetFrameNum() * .75, .75, 0, .75);
    
        ofPushMatrix();
            ofTranslate(-ofGetWidth()/2,-ofGetHeight()/2);
            mesh.draw();
    
        ofPopMatrix();

    ofPopMatrix();
    
    
    ofPushMatrix();
        ofRotate(ofGetFrameNum() * -.75, .75, 0, .75);
    
        ofPushMatrix();
            ofTranslate(-ofGetWidth()/2,-ofGetHeight()/2);
            outsideMesh.draw();
    
        ofPopMatrix();
    
    ofPopMatrix();
    
    cam.end();
    
    syphon.publishScreen();
}



mike

not sure without seeing the utils classes but this seems to do what I think you are trying. Maybe it helps to compare

#pragma once

#include "ofMain.h"

class ofApp : public ofBaseApp{

public:
    vector<ofVec3f>points;
    vector<ofVec3f>outsidePoints;
    
    ofVboMesh mesh;
    ofVboMesh outsideMesh;
    
    ofEasyCam cam;
    ofSpherePrimitive sphere;
    ofSpherePrimitive outsideSphere;
//--------------------------------------------------------------
void setup()
{
   
    
    mesh.setMode(OF_PRIMITIVE_POINTS);
    mesh.enableColors();
    outsideMesh.setMode(OF_PRIMITIVE_POINTS);
    outsideMesh.enableColors();
    
    cam.setDistance(600.0);
    

    sphere.set(100, 50, OF_PRIMITIVE_POINTS);
    mesh = ofVboMesh(sphere.getMesh());
    
    outsideSphere.set(200, 50, OF_PRIMITIVE_POINTS);
    outsideMesh = ofVboMesh(outsideSphere.getMesh());
    
    
    points = mesh.getVertices();
    outsidePoints = outsideMesh.getVertices();
    
    for(size_t i=0; i<points.size(); i++)
    {
        mesh.addColor(ofColor(ofRandom(0, 255),
                              ofRandom(0, 255),
                              ofRandom(0, 255),
                              255));
    }
}
//--------------------------------------------------------------
void draw()
{
        
       cam.begin();
            mesh.drawVertices();
            outsideMesh.drawVertices();
        cam.end();
}

		
};

to answer this - it is a copy. to avoid:

ofVec3f& p = _points[i];

@jcleave

Sorry. I dont think the issue is the algorithm to draw and display the sphere. The issue is that when I run it, it sometimes it renders everything (both spheres), sometimes renders the smaller one, and sometimes renders the larger. It is almost as if im running into some memory limit, and sometimes go over it. However, I tested on another, much more beefy machine, and still have the same intermittent rendering.

Im using two separate mesh vbos, because I want to rotate each sphere in the opposite direction.

mike