PDA

View Full Version : OGL noob question - Writing a line upon a line, avoiding conflict

Prospero
12-16-2014, 09:21 PM
Hi all,

I'm writing a 3D path following algorithm. The path is made of up of a few hundred points. Initially, the path is plotted using OGL GL_LINE_STRIP in a light grey colour. This works just fine:

glLineWidth(2.0);
glBegin(GL_LINE_STRIP);
glColor4f(0.75, 0.75, 0.75, 1.0);
for (n = 0; n < 256; n++)
glVertex3f(wp[n].v.y, -wp[n].v.z, -wp[n].v.x);
glEnd();

This done, I want now to highlight a single segment of this path using a different colour, say in orange, so I do something like:

glLineWidth(2.0);
glBegin(GL_LINES);
glColor4f(1.0, 0.5, 0.0, 1.0);
glVertex3f(wp[79].v.y, -wp[79].v.z, -wp[79].v.x);
glVertex3f(wp[80].v.y, -wp[80].v.z, -wp[80].v.x);
glEnd();

The problem is there's now a conflict, so the segment kinda flashes between the two colours. Please tell me there's a simple way to solve this problem! Please explain in basic terms because I am a real noob.

Many thanks and best regards,

Anthony (Prospero)

Agent D
12-17-2014, 12:16 AM
Do you use depth buffering?
If yes, your problem may be related to z-fighting (http://en.wikipedia.org/wiki/Z-fighting) where you draw two pices of geometry with the same z-component and due to
rounding errors, some pixels will be considered on top and some below which might change inbetween frames.

If not, do you always draw the orange line at the end or does that vary?

Do the original and the new line segents flicker betwen the two colors, or the line segment and other line segments?

On a side note, why do you pass the the vertices in a flipped manner, i.e. x=Y, y=Z andd z=X?

Prospero
12-17-2014, 07:09 PM
Hi Agent D,

Remember, I'm a dumb noob. Can I ask you a couple of questions / make a few comments?

1) I always draw the orange segment line LAST, having already drawn the grey multipoint line.

2) What is the command to enable / disable depth buffering? I would like to try this and see if it helps.

3) You ask, 'Do the original and the new line segments flicker betwen the two colors, or the line segment and other line segments?' My answer is BOTH.

4) The reason why the vertices are flipped and signs are changed is because I am using North, East, Down (NED) aeronautical convention within my program, and this has to be converted to the OGL axes system, which amounts to East, Up, South (EUS). Fortunately, both systems are right-handed.

Best regards,

Anthony (Prospero)

Agent D
12-18-2014, 09:04 AM
Enabling and disabling depth test: glEnable( GL_DEPTH_TEST ) and glDisable( GL_DEPTH_TEST )

Depth buffering is a technique to draw pixels of polygons in the right order without having to pre-sort by depth or
having to worry about overlapping polygons. In addition to a color buffer, you have a depth-buffer that stores a
depth-value for every pixel (basically a distance from the view point), when a new pixel is written, the depth value
is compared to the existing value and the new pixel is discarded if it is determined to be behind what the previous
value.

If you use depth buffering and you have two polygons with very close depht values (e.g. due to insufficient precision or
they actually are overlapping), some pixels of the polygon further away will pass the depth test and "poke through" the
closer polygon, leading to a flickering effect known as Z-fighting (http://en.wikipedia.org/wiki/Z-fighting).

If, for instance, you want to draw a solid mesh with a wire-frame overlay, you would use a polygon offset (https://www.opengl.org/sdk/docs/man3/xhtml/glPolygonOffset.xml), i.e. an offset
added to the depth values of the wireframe overlay. Something like this:

glEnable( GL_POLYGON_OFFSET_LINE );
glDisable( GL_POLYGON_OFFSET_FILL );
glPolygonOffset( -1.0f, -1.0f );

This way, the lines still interact propperly with the depth buffer, i.e. get obstructed by other geometry and vice-versa, but are made sure to not
get obstructed by the mesh they are supposed to overlay.

Simply drawing a bunch of solid color lines shouldn't require depth-testing at all. And drawing the colored lines after the
other lines are done, should make sure that they are drawn on top of everything that was drawn before.

Prospero
01-19-2015, 06:37 PM
OK... I've had a long think about this and I've had time to identify exactly when the line 'flashes' between the two colours. Let me explain this as simply as possible.

I've mentioned that the path is plotted using GL_LINE_STRIP in a light grey colour. Now, here's the important bit: sometimes the path is 'repeated', for example when I'm circling (this is an aircraft autopilot). Therefore I can be plotting the same path several times on top of itself (obviously all in light grey).

But the problem is more subtle than this. Yes, it is on these 'repeated' segments of the path that the problem arises - but ONLY when the endpoints of the line segments are different, and the line happens to coincide. When this situation occurs I cannot overwrite the light grey path with the orange segments without the flashing happening.

Does this make any sense?

Best regards,

Anthony