Fastest way to render line mesh over 3D triangular mesh?

Started by
4 comments, last by DracoLacertae 12 years ago
I am rendering lines over 3D triangular mesh in order to visualize the mesh triangles. Well-known feature of every 3D modeling software right?
but I am drawing the line with glVertex3f, How can I draw with vertex buffer or vertex array.
and how can i optimize this code.. because with this code, i am getting very low FPS... just 15..
any idea ?



void RenderModelMeshAsLines(GLpoint *P, GLFace *T, int nbF)
{
if(nbF > 0)
{
glLineWidth(2.0); // Set The Line Width
GLfloat r=0.0f, g=0.0f, b=0.0f;
glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT | GL_LINE_BIT);
glPushMatrix();
glBegin(GL_LINE);
for (int i=0; i<nbF; i++)
{
DrawLine(P[T.v1], P[T.v2], r,g,b);
DrawLine(P[T.v2], P[T.v3], r,g,b);
DrawLine(P[T.v3], P[T.v1], r,g,b);
}
glEnd();
glPopMatrix();
glPopAttrib();
glLineWidth(1.0); // Set The Line Width
}
}



Draw a line...

void DrawLine(GLpoint a, GLpoint b, float R, float G, float B)
{
glPushMatrix();
glDisable(GL_LIGHTING);
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBegin(GL_LINES);
glColor3f(R, G, B);
glVertex3f(a.x, a.y, a.z);
glVertex3f(b.x, b.y, b.z);
glEnd();
glPopMatrix();
}

Advertisement
Seems like you're emitting vertices in immediate mode, so I'm not surprised it's slow! The best thing I can do is point you towards a vertex buffer object tutorial, or Google, because there are quite a few out there.
Also, keep in mind you change the [color=#000000][font=Times][size=1]glPolygonMode to [/font][color=#000000][font=monospace][size=1]GL_LINE[/font][color=#000000][font=Times][size=1] and then you might not have to write a whole bunch of new rendering code. :D[/font]
scottrick49
If you ever venture into geometry shaders you may be interested in http://cgg-journal.com/2008-2/06/index.html

or (sorry for the long google link)

http://www.google.com/url?sa=t&rct=j&q=solid%20wirframe%20nvidia&source=web&cd=1&ved=0CDsQFjAA&url=http%3A%2F%2Fdeveloper.download.nvidia.com%2Fwhitepapers%2F2007%2FSDK10%2FSolidWireframe.pdf&ei=bxZqT871JqGe2gWK-5HvCA&usg=AFQjCNEk0nXCt_9NKuarJ5JdlMSXfIY3lw&cad=rja
A VBO is not likely to help so much here as your data looks to be dynamic and your code is nasty - see further down for help with this; you certainly won't see jumps from 15fps to 60 or more (immediate mode is slow but it's not that slow). I really wish people would stop recommending "use a VBO" as if it's some kind of magic dust that you can just sprinkle on bad or slow code to make it good or fast - it's not.

There are a few things in your code that are hurting you and that you can fix Right Now. VBO or no VBO, these are your primary problems and need to be addressed.

glLineWidth (2.0) - values other than 1 may not be hardware accelerated. Try setting it to 1, if performance jumps dramatically then it's a reasonably decent indication that your driver is falling back on some form of software emulation (perhaps faking lines by using solid quads, perhaps something else).

I expect it won't jump because of...

State changes. The way you're handling these is almost a textbook case of the worst possible way to do it (sorry, but it's true). You've a whole heap of state changes inside of a loop that could easily be moved outside of it, you've a glPushMatrix and glPopMatrix that do absolutely nothing at all (since you don't do any transforms between them) - again inside of the same loop, and you've a second inner glBegin/glEnd pair inside of your first glBegin/glEnd pair (GL_LINE isn't even a valid param for glBegin).

Your drawline function could and should be reduced to this:
void DrawLine(GLpoint a, GLpoint b, float R, float G, float B)
{
glColor3f(R, G, B);
glVertex3f(a.x, a.y, a.z);
glVertex3f(b.x, b.y, b.z);
}

The state changes go outside the for loop in RenderModelMeshAsLines, which then looks like this:
void RenderModelMeshAsLines (GLpoint *P, GLFace *T, int nbF)
{
if (nbF > 0)
{
GLfloat r = 0.0f, g = 0.0f, b = 0.0f;

glLineWidth (2.0);
glPushAttrib (GL_CURRENT_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT | GL_LINE_BIT);

glDisable (GL_LIGHTING);
glEnable (GL_LINE_SMOOTH);
glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBegin (GL_LINES);

for (int i = 0; i < nbF; i++)
{
DrawLine (P[T.v1], P[T.v2], r, g, b);
DrawLine (P[T.v2], P[T.v3], r, g, b);
DrawLine (P[T.v3], P[T.v1], r, g, b);
}

glEnd ();
glPopAttrib ();
glLineWidth (1.0);
}
}

Just this basic clean-up and correction should be enough to see your performance go back up to where you want it to be. If it's still not enough then look at VBOs or geometry shaders, but not before. Correct your code and get it well-behaving first.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

If you're using the depth buffer, make sure you turn on glPolygonOffset to avoid z-fighting. http://www.opengl.org/archives/resources/faq/technical/polygonoffset.htm

This topic is closed to new replies.

Advertisement