Drawing ofVboMesh with OF_PRIMITIVE_LINES is much slower than OF_PRIMITIVE_POINTS

Hi everyone,
I’m trying to draw 200,000 lines/points with a real-time generated ofVboMesh.
However, the drawing is much slower (more than 50%) with OF_PRIMITIVE_LINES mode to OF_PRIMITIVE_POINTS mode.

Here is my code for reference

ofVboMesh mesh;

void ofApp::setup() {
    ofDisableDepthTest();
    genMesh();
}

void ofApp::genMesh()
{
    mesh.clear();
   
    mesh.setMode(OF_PRIMITIVE_LINES);

    ofVec3f pos;
    for(int i = 0; i < 200000; i++)
    {
        pos.set(ofRandom(-200, 200), ofRandom(-200, 200), ofRandom(-200, 200));
        mesh.addVertex(pos);
        mesh.addColor(ofColor(255, 50));
        
        pos.set(ofRandom(-200, 200), ofRandom(-200, 200), ofRandom(-200, 200));
        mesh.addVertex(pos);
        mesh.addColor(ofColor(255, 50));
        
        mesh.addIndex(i * 2);
        mesh.addIndex(i * 2 + 1);
    }
}

void ofApp::updateMesh()
{
    ofVec3f pos;
    for(int i = 0; i < mesh.getNumVertices(); i++)
    {
        pos.set(ofRandom(-200, 200), ofRandom(-200, 200), ofRandom(-200, 200));
        mesh.setVertex(i, pos);
    }
}

//--------------------------------------------------------------
void ofApp::update()
{
    updateMesh();
}

//--------------------------------------------------------------
void ofApp::draw()
{
    ofBackground(ofColor::black);
    ofTranslate(ofGetWidth() / 2, ofGetHeight() / 2);
    mesh.draw();
}

I had 60fps with mesh.setMode(OF_PRIMITIVE_POINTS);
but got only 30fps with mesh.setMode(OF_PRIMITIVE_LINES);

Those vertex positions in mesh should have some other real-time generated values, but that’s not relevant here, so I just put some random positions instead.

So, is there any way to improve the performance when dealing with OF_PRIMITIVE_LINES mode?
Thank you in advance!

something that might help when changing a vbo every frame is to set it’s usage to dynamic or even stream:

mesh.setUsage(GL_DYNAMIC_DRAW);

or

mesh.setUsage(GL_STREAM_DRAW);

but in general the more the graphics card needs to draw and calculate (interpolating the lines among every point as opposed to just drawing those points) the more it’ll take to draw

Thank you arturo!

I’ve tried GL_DYNAMIC_DRAW and GL_STREAM_DRAW. Unfortunately, none of them makes much difference to GL_STATIC_DRAW in this example.

I asume the time consuming job is some “actual” rendering stuff by the GPU, not the process of copying data from app’s memory into OpenGL’s memory.

There is an tutorial from Apple’s iOS Developer Library. In the “Map Buffers into Client Memory for Fast Updates” section, it mentions about

Use this function (glMapBufferRange) to retrieve a pointer to a region of OpenGL ES memory, which you can then use to write new data

so, according to the assumption earlier, even if the mapping process is possible in openFrameworks, it may not improve the performance either. Am I right?

yes if you are updating the whole buffer every time i don’t think it’ll make that much difference. something that might increase performance is to invalidate the buffer every time before uploading new content but i don’t think in this case it’ll make much difference since it’s the drawing not the uploading of the data that is the bottleneck. in any case in 0.9 (with the nightly builds by now) you can do:

ofVec3f * vertices = mesh.getVbo().getVertexBuffer().map<ofVec3f>(GL_WRITE_ONLY);
for(int i=0;i<mesh.getNumVertices();i++){
    vertices[i].set(...)
}
mesh.getVbo().getVertexBuffer().unmap();

if you want to invalidate the buffer before uploading new data you can do:

mesh.getVbo().setVertexData((ofVec3f*)nullptr, mesh.getNumVertices(), usage);

where usage is one of GL_DYNAMIC_DRAW, GL_STATIC_DRAW…

Hi arturo!

Unfortunately, this does not improve the performance in this case.
Since the botteneck is the drawing instead of the uploading of data as you mentioned, I might try to think in another way to my sketch.

Thank you for your time!

i would try using a later version of openGL, i’ve noticed lately that some applications run faster when using the programmable renderer

Hello arturo!
I’ve tried the sketch again, it seems that it is the blending brings down performance, as there are about 50% more fps if the alpha blending is disabled.
Found a thread the performance of blend operation on opengl.org about why blending is slow, and it suggest multi-pass procedure in shader (with textures).
However, for rendering of massive primitives like lines with alpha blending, is there any way to improve the performance?
Thank you!