Are you using smoothing?
Smoothing is turned on by calling
ofEnableSmoothing()
This would slow things down because ofLine takes into account smoothing.
If you haven’t turned smoothing on, it’s quite possible that the direct openGL code you are using is faster.
The code for ofLine is the following:
void ofLine(float x1,float y1,float x2,float y2){
// use smoothness, if requested:
if (bSmoothHinted) startSmoothing();
linePoints[0] = x1;
linePoints[1] = y1;
linePoints[2] = x2;
linePoints[3] = y2;
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, &linePoints[0]);
glDrawArrays(GL_LINES, 0, 2);
// back to normal, if smoothness is on
if (bSmoothHinted) endSmoothing();
}
ofLine uses vertex arrays (see http://www.songho.ca/opengl/gl-vertexarray.html)
which normally is faster when drawing geometry. However for a simple primitive like a line, this may be overkill. Vertex arrays are faster when there are shared vertices and more functions calls when using Immediate mode (the code you are using). However in your code there are only 2 calls to glVertex3f, which is very minimal. You also don’t have the overhead of the ofLine function call, as you are calling the code directly.
Try creating the following function and see if it is still faster:
void ofLine2(float x1,float y1,float x2,float y2)
{
// use smoothness, if requested:
if (bSmoothHinted) startSmoothing();
glBegin(GL_LINES);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glEnd();
// back to normal, if smoothness is on
if (bSmoothHinted) endSmoothing();
}
No, there is no calculation in the gpu, vertex arrays just reduces the number of vertices sent to the gpu, and reduces the number of opengl function calls.
@Pierre: thanks for the explanation, it helped me a lot. I believe your code was targeting an old version of openframeworks, so here’s what I did on 0072:
Changing from ofLine to glVertex2f already made it faster. Moving glBegin and glEnd out of the loop made it much faster. The lines about GL_BLEND and GL_SRC_ALPHA were taken from ofGLRenderer::startSmoothing() and they worked well for what I needed. You might need to use other GL settings.