I don't use OpenGL myself... this is a question from a customer:
When they draw the outline of a polygon/polyline, with a significantly thick
pen, the lines are not joined with any kind of corner. Exactly as if each
straight line segment where drawn independently.
This happens using LINE_LOOP or a Stencil. Only with a PolygonOffset they can
get mittered joins at the corners.
Is this so, or is there a way to have mittered corners without a polygon offset?
Here the code they're using:
GLfloat top, bottom, left, right;
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT );
glLineWidth(10);
glPushMatrix();
//
//line loop method
//
glColor3f(1.0f, 0.0f, 0.0f);
glBegin( GL_LINE_LOOP );
glVertex2f( 100.0f, 100.0f );
glVertex2f( 100.0f, 200.5f );
glVertex2f( 200.0f, 200.5f );
glVertex2f( 200.0f, 100.5f );
glEnd();
glBegin( GL_LINE_LOOP );
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f( 400.0f, 150.0f );
glVertex2f( 450.0f, 100.5f );
glVertex2f( 425.0f, 200.5f );
glVertex2f( 400.0f, 225.5f );
glEnd();
//
// stencil buffer method
//
left = 100; right = 200; top = 250;
bottom = 350;
glPushAttrib( GL_ALL_ATTRIB_BITS );
glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, -1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glColor3f(1.0f, 1.0f, 0.0f);
glBegin(GL_POLYGON);
glVertex2f(left, top);
glVertex2f(right, top);
glVertex2f(right, bottom);
glVertex2f(left, bottom);
glEnd();
glStencilFunc(GL_NOTEQUAL, 1, -1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(0.0f, 1.0f, 1.0f);
glBegin(GL_POLYGON);
glVertex2f(left, top);
glVertex2f(right, top);
glVertex2f(right, bottom);
glVertex2f(left, bottom);
glEnd();
glPopAttrib();
//
//glPolygonOffset method
//
left = 350; right = 450; top = 250;
bottom = 350;
glPushAttrib( GL_ALL_ATTRIB_BITS );
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(-2.5f, -2.5f);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_POLYGON);
glVertex2f(left, top);
glVertex2f(right, top);
glVertex2f(right, bottom);
glVertex2f(left, bottom);
glEnd();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glColor3f(1.0f, 0.0f, 0.0f );
glBegin(GL_POLYGON);
glVertex2f(left, top);
glVertex2f(right, top);
glVertex2f(right, bottom);
glVertex2f(left, bottom);
glEnd();
glPopAttrib();
glPopMatrix();
TIA
--
Fernando Cacciola
SciSoft Consulting, Founder
http://www.scisoft-consulting.com
Correct.
> Only with a PolygonOffset they can
> get mittered joins at the corners.
>
??
> Is this so, or is there a way to have mittered corners without a polygon offset?
>
Draw a circle at the line ends, with the radius the
same as the line width.
> Fernando Cacciola
> SciSoft Consulting, Founderhttp://www.scisoft-consulting.com
Do I get part of your consulting fee?
--
<\___/>
/ O O \
\_____/ FTB.
http://www.topaz3d.com/ - New 3D editor for real time simulation
http://en.wikipedia.org/wiki/A_picture_is_worth_a_thousand_words
--
ultrasound www.ezono.com
Fair enough... I figured that the issue were so common that I could spare the
trouble of uploading the image somewhere, etc... (I don't have an account on
Flickr or any such thing FWIW)
Anyway, I wanted to confirm that OpenGL does indeed know nothing of line joins.
That seesm to be the case.
Best
> On Dec 11, 4:15 pm, Fernando Cacciola <fernando.cacci...@gmail.com>
> wrote:
>> Hi people,
>>
>> I don't use OpenGL myself... this is a question from a customer:
>>
>> When they draw the outline of a polygon/polyline, with a significantly thick
>> pen, the lines are not joined with any kind of corner. Exactly as if each
>> straight line segment where drawn independently.
>>
>
> Correct.
>
So no line joins in OpenGL?
>> Only with a PolygonOffset they can
>> get mittered joins at the corners.
>>
>
> ??
>
See the bottom of the code I posted. They create a closed polygonal chain that
corresponds to the outline of the thick stroke (this is called buffering btw).
>
>> Is this so, or is there a way to have mittered corners without a polygon offset?
>>
>
> Draw a circle at the line ends, with the radius the
> same as the line width.
This won't really work in the sense that resulting geometry is not that of a
mittered corner, but I get the idea: draw the corner manually.
The one way it will work is to draw and fill the tangled parallelogram that
comes from joining the four corners of the line endingds.
>
>
>> Fernando Cacciola
>> SciSoft Consulting, Founderhttp://www.scisoft-consulting.com
>
> Do I get part of your consulting fee?
>
LOL, we can share the same pat in the back I'm getting for doing this.
Best
Would drawing big points at the line ends be ok?
glPointSize to change the point size
--
ultrasound www.ezono.com
No. OpenGL isn't really designed for that sort of
rendering.
> This won't really work in the sense that resulting geometry is not that of a
> mittered corner, but I get the idea: draw the corner manually.
>
Oh, you want miters...I missed that.
> The one way it will work is to draw and fill the tangled parallelogram that
> comes from joining the four corners of the line endingds.
>
That's one way....
The other way is to draw quads instead of lines and
adjust the endpoints (the graphics card is probably
quads anyway when you change the line width).
> The other way is to draw quads instead of lines and
> adjust the endpoints (the graphics card is probably
> quads anyway when you change the line width).
>
Right... this makes the most sense.
Thanks
Fernando