View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1luckyyyyyy  Members

Posted 11 March 2012 - 06:56 AM

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[i].v1], P[T[i].v2], r,g,b);
DrawLine(P[T[i].v2], P[T[i].v3], r,g,b);
DrawLine(P[T[i].v3], P[T[i].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();
}



### #2RobinsonUK  Members

Posted 11 March 2012 - 07:33 AM

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.

### #3scottrick49  Members

Posted 16 March 2012 - 02:11 PM

Also, keep in mind you change the glPolygonMode to GL_LINE and then you might not have to write a whole bunch of new rendering code.
scottrick49

### #4web383  Members

Posted 21 March 2012 - 12:02 PM

If you ever venture into geometry shaders you may be interested in http://cgg-journal.com/2008-2/06/index.html

### #5mhagain  Members

Posted 21 March 2012 - 03:13 PM

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[i].v1], P[T[i].v2], r, g, b);
DrawLine (P[T[i].v2], P[T[i].v3], r, g, b);
DrawLine (P[T[i].v3], P[T[i].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.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

### #6carangil  Members

Posted 21 March 2012 - 04:46 PM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.