Gl_lines vs ofLine

Hello, can anybody explain me the differences between these?




It seems that the first is much faster in terms of cpu speed. Why is that?

thanks in advance


Are you using smoothing?
Smoothing is turned on by calling


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;  
	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
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();  
	// back to normal, if smoothness is on  
	if (bSmoothHinted) endSmoothing();  

Sorry because of the ignorance, but are Verteex array something as vfos which make calculation in the gpu?


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.

1 Like

@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:

for(vector<Particle>::iterator p = particles.begin() + i; p != particles.end(); ++p) {  
    // find ofVecs p1 and p2  
    // ...  
    float distance = p1.distance(p2);  
    if (distance > 0 && distance < 100) {  
        ofSetColor(100, 100, 100, 200 - (distance / 100) * 255);  
        // ofLine(p1.x, p1.y, p2.x, p2.y);  
        glVertex2f(p1.x, p1.y);  
        glVertex2f(p2.x, p2.y);  

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.

Can anybody please teach me how to rewrite the code in OpenGL ES 2.0 so I can use on iOS?

I can’t even use glBegin() and glEnd()… What should I use instead?

@ZackLee123 comment glBegin(GL_LINES), glEnd(); and use ofLine(p1.x, p1.y, p2.x, p2.y); instead.

1 Like

Thanks but ofLine seems like it’s pretty slow to use on iOS.

That’s why I was thinking to replace ofLine with Gl_LINES since people say it’s much faster.

But unfortunately, I can’t use the same code above since iOS only supports OpenGL ES 1.1 and 2.0.

I would really appreciate if anyone can give me a tip how to replace ofLine with GL_LINES on iOS.

Also you can try ofVboMesh . Use setMode(OF_PRIMITIVE_LINES)

1 Like

Thank you!!