Gl_lines vs ofLine

Hello, can anybody explain me the differences between these?

glBegin(GL_LINES);
glVertex3f(x1,y1,z1);
glVertex3f(x2,y2,z2);
glEnd();

y

ofLine(x1,y1,x2,y2);

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

thanks in advance

Matias.

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();  
}  
  

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

R.

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:

  
  
glEnable(GL_BLEND);  
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);  
glBegin(GL_LINES);  
  
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);  
    }  
}  
  
glEnd();  
  

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!!