View Full Version : Line Cap for Polygon border lines

06-30-2008, 04:04 AM
I want to draw border line of a filled polygon in different color.

I've used the following code.

glLineStipple (stipple_factor, stipple_pattern);


glVertexPointer (2, GL_FLOAT, 0, vertex);
glDrawArrays (mode, 0, vertex_count);

glDisable (GL_LINE_STIPPLE);

By the above code, i'm getting the border line. but the edges of the border line have inappropriate line caps. The lines end directly at the vertices. As a consequence with line widths > 1
there are visible gaps between the edges if the angle is <> 180 degrees.

Can you suggest me how to overcome this situation? is there any way to use square, circle or triangle cap like in Microsoft Windows GDI+.

06-30-2008, 07:36 AM
One *very* dirty way would be to draw the polygon a bit enlarged, specifying the border color you want ( & disable for this the depth writes if necessary). Afterwards draw the polygon on top of it, at it's normal size & color. & there you go, you have it! :)

06-30-2008, 08:11 AM
hmmm, I have noticed this problem a long time ago, but it did not trouble me. Maybe you can draw vertices as points to make a kind of cap.

babis> I don't think it is possible with your technique (or it is a bit complicated), you can do the same just drawing lines directly after drawing the mesh in fill mode and using a polygon offset. With GL_LINE_STIPPLE you can specify a line pattern.

06-30-2008, 10:01 AM
Here is an opensource vector graphic lib.
You may want to have a look at their GL backend.

06-30-2008, 11:53 AM
oops yes it's a bit complicated now that I thought of it, forget what I said!

06-30-2008, 09:54 PM
Hi dletozeun,

Thanks for your guidance. It solved my problem when I draw the Vertices as Points with glPointSize () setting equivalent to the Line Width.

07-01-2008, 12:40 AM
a better way is to make your own line function that draws them as quads

VEC3 qA = cameras_position - p0;
VEC3 wA = ( p1 - p0 );
VEC3 nA = return_VECTORS_normal(CROSSPRODUCT( qA,wA )) * line_width;

thus the 4 verts of the quad are p0+nA,p0-nA,p1+nA,p1-nA,

this quad is only for a single line
but for your polygon
what u want to do is string multiple lines together, adding a triangle if the angle between the joins > 180degrees

07-01-2008, 01:02 AM
zed, your technique will require more work to make rounded edges.

07-01-2008, 12:22 PM
well theyre sort of round, as theres no breaks in the lines, if u want perfect round corners then u will need to tesselate the triangle at the corner into lotsa triangles, eg like a GL_TRIANGLE_FAN

had a go at the rounded points, yes Zbuffer a lot more work (+ its still not 100%), i think ill stick to the single triangle :)


btw single triangle, if the lines arent that wide (+ the OPs ones wont be i think) youll hardly notice the difference + also texturing them is a lot simplier