OpenTKstart's picture

Draw Lines and Quads with the same points

hey,

when i draw quads and lines with the same points they overlay each other. How can i overlay the lines above the quads (green). See pictures:

thx


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tksuoran's picture

You could push filled polygons further in depth by using GL.PolygonOffset(), also enable polygon fill offset.

OpenTKstart's picture
tksuoran wrote:

You could push filled polygons further in depth by using GL.PolygonOffset(), also enable polygon fill offset.

Thx, but i have troubles by calculating the two values (factor, units). See pics:
http://www.opengl.org/resources/faq/technical/polygonoffset.htm

Front is good :)

Side is bad :(

thx

tksuoran's picture

Use larger values?

OpenTKstart's picture

No effect. Maybe i use it wrong?:

GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
                    GL.PolygonOffset(200.0f, 200.0f);
                   GL.Begin(BeginMode.Quads);
       //       .......add the Quads....
                    GL.End();
OpenTKstart's picture

push

mOfl's picture

Usually you would not draw objects with filled polygons and visible lines like this, but use a wireframe-like texture for this task. But if you don't want to do it that way, you will have to draw the object twice.

// enable depth test for correct visibility
GL.Enable(EnableCap.DepthTest);
 
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
GL.Begin(BeginMode.Quads);
// draw ALL green quads
GL.End();
 
// disable depth test to draw the lines over everything
GL.Disable(EnableCap.DepthTest);
 
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
GL.Begin(BeginMode.Quads);
// draw ALL quads again with line color
GL.End();
 
// enable depth test again here if you need it later in the code

This should give you a cuboid where ALL lines are visible. To display only the lines facing the eye, you have to do backface culling as well, i.e. before drawing the quads the second time (or the first time, it will even save you some time), you add:

GL.Enable(EnableCap.CullFace);

It might happen that this culls the wrong set of polygons, i.e. the ones you want to display. In this case, you have to change the FrontFaceDirection in the initialization process for your window with one of these calls (you have to try which one is the right one, it depends on what order (clockwise or counter-clockwise) the vertices of your quads have):

GL.FrontFace(FrontFaceDirection.Ccw);
// or
GL.FrontFace(FrontFaceDirection.Cw);

With this, it should work as intended.

Additionally, in your window initialization you can add:

GL.Hint(HintTarget.LineSmoothHint, HintMode.Nicest);
GL.Hint(HintTarget.PolygonSmoothHint, HintMode.Nicest);

This will smoothen lines and polygons and looks much better!

OpenTKstart's picture

First Thanks for your answer,

i tried everything but i dont get what i want:

GL.Enable(EnableCap.DepthTest);
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
GL.PolygonOffset(1.0f, 1.0f);
...draw everything...
GL.End();
 
GL.Disable(EnableCap.DepthTest);
GL.Enable(EnableCap.CullFace);
 
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
...draw everything...
GL.End(); 
GL.Disable(EnableCap.CullFace);
GL.Enable(EnableCap.DepthTest);

and i tried to use...

GL.FrontFace(FrontFaceDirection.Ccw);
// or
GL.FrontFace(FrontFaceDirection.Cw);

RESULT:

I just want to see the lines facing the eye.
Furthermore i think its not very efficient to draw it twice, can you please explain me how to make, as you said a "wireframe-like texture" ?

Thx

Edit1:

I got it now...

GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.PolygonOffsetFill);
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
...draw everything...
GL.End();
 
GL.Disable(EnableCap.EnableCap.PolygonOffsetFill);
GL.Enable(EnableCap.PolygonOffsetLine);
 
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
...draw everything...
GL.End();

Result:

But my problem that i draw everything twice is still present.

mOfl's picture
OpenTKstart wrote:

I just want to see the lines facing the eye.
Furthermore i think its not very efficient to draw it twice, can you please explain me how to make, as you said a "wireframe-like texture" ?

Sorry, I forgot a line. The three calls you should/need to make to set up correct backface culling are:

GL.Enable(EnableCap.CullFace);
GL.FrontFace(FrontFaceDirection.Ccw);
GL.CullFace(CullFaceMode.Back);

The first one enables or disables the culling - usually you do your rendering in such a way that you always cull the backfaces as it speeds up rendering.
The second one specifies what order the vertices of your faces do have.
The third one tells OpenGL what face(s) to clip. This is the one I forgot to mention. You will usually want to cull the backfaces (i.e. those which are not visible), so if the wrong lines are shown, you can either cull the frontfaces or - preferrably - switch the front face order.

A very simple wireframe-like texture would be a texture which has different colors for edges and face-interior regions, for example:

You would load this image as a texture and when you draw your quads you would specify texture coordinates such that the lower left corner of your quad corresponds to the lower left corner of the texture (i.e. texture coordinates 0.0f, 0.0f), the lower right corner 1.0f, 0.0f, upper left corner 0.0f, 1.0f and upper right corner 1.0f, 1.0f. You would just map the image of an empty quad (the texture) onto a filled quad which would make it look like the edges were visible. This will, however, only work for quads (for triangles you would have to load a triangle texture, for pentagons a pentagon texture...) and will only look good if all the quads have more or less the same size. You can give it a try but it's neither faster than the wireframe approach nor easier to implement. It would work in one pass but - depending on the purpose of your application - i would not recommend it.

OpenTKstart wrote:

But my problem that i draw everything twice is still present.

Don't be irritated by that. You will find many situations where you need to draw basic geometry several times to come to your final image. You absolutely do not need to worry about performance with this amount of geometry.

OpenTKstart's picture

Ok thanks very much. I have another question...as i said i can draw now the lines over the filled quads with PolygonOffset, but when i import the project to VS2010 the effect will be lost. I tried it on several PC´s, always the effect will lost and i get the same result as on my first post.

Whats the different between VS08 and VS10?