user1235271
user1235271

Reputation: 13

OpenGL draw GL_LINES on top of GL_QUADS

I am drawing a cube and a line grid in 3D in OpenGL:

glBegin(GL_QUADS);
...
glEnd();

glBegin(GL_LINES);
...
glEnd();

Now, independent of the order (if I draw the lines first or the quads first) and independent of the position it always happens that the lines are draw over the cube. I thought OpenGL draws front to back.

What I tried is to use:

glEnable(GL_BLEND);
glBlendFunc (GL_ONE, GL_ONE);

which does work but part of the cube is transparent now. I also tried glDepthFunc(GL_NEVER) with disabling glEnable (GL_DEPTH_TEST) but I get the same problem that the cube appears transparent.

Does anyone have a hint to overcome this problem?

Upvotes: 0

Views: 4278

Answers (2)

SigTerm
SigTerm

Reputation: 26429

Now, independent of the order (if I draw the lines first or the quads first) and independent of the position it always happens that the lines are draw over the cube.

IF your lines have proper depth, then you forgot to enable depth buffer. If you enabled depth buffer, then you must make sure that your library used for initializing OpenGL requested depth buffer.

I thought OpenGL draws front to back.

It does not. OpenGL draws polygons in the same order you specify them. There is no automatic sorting of any kind.

Does anyone have a hint to overcome this problem?

Well, you could clear depth buffer, but this will be slow and inefficient. Technically, you should almost never do that.

glDepthMask(GL_FALSE) will disable writing to depth buffer. i.e. any object drawn after that call will not update depth buffer but will use data that is already stored. It is frequently used for particle systems. So call glDepthMask(GL_FALSE), draw "lines", call glDepthMask(GL_TRUE), then draw cube.

If you combine glDepthMask(GL_FALSE) with glDepthFunc(GL_ALWAYS) then object will be always drawn, completely ignoring depth buffer (but depth buffer won't be changed).

Upvotes: 1

Tim
Tim

Reputation: 35933

If you want to draw the lines in the background, just draw them (and the rest of the background) first, clear the depth buffer, then render the rest of the scene.

Or you can just give the lines a depth such that they will always be behind everything else, but then you have to make sure that none of your gameworld objects go behind it.

Upvotes: 1

Related Questions