SFML community forums

Help => Graphics => Topic started by: Senzin on April 28, 2012, 08:15:25 am

Title: Better Line Thickness and OpenGL
Post by: Senzin on April 28, 2012, 08:15:25 am
So I've spent a few hours on the forums and experimenting. It seems that in SFML2, the general consensus is that if you want to draw lines, pass a vertex array to draw(). And if you want to draw lines with thickness, use rotated rectangles.

I think using rotated rectangles is bad for several reasons.
If the draw() function could take a vertex array and a thickness, that would make things much cleaner and faster.

Here is what I've been using (ht = half thickness; n1/n2 = node 1/2)
float dx = n2->x - n1->x;
float dy = n2->y - n1->y;
float rot = atan2(dy, dx) * RADTODEG;
rect.setSize(sf::Vector2f(std::sqrt(std::abs(dx)*std::abs(dx) + std::abs(dy)*std::abs(dy)), ht*2));
rect.setOrigin(0, ht);
rect.setPosition(n1->x, n1->y);
rect.setRotation(rot);

This code must be run whenever a node in an edge is moved (which is a lot since I'm building a graph editor for creating a navigation mesh).

It just seems strange that if a user wants a line with adjustable thickness (a pretty reasonable request), that this graphics package makes them basically hack it out themselves.

Finally, OpenGL has function calls for drawing lines with thickness:
glLineWidth(width);
glBegin(GL_LINES);
glVertex2i(x1, y1);
glVertex2i(x2, y2);
glEnd();

I figure somewhere you're calling these OpenGL functions when you draw vertex arrays. So why not just allow thickness as well? I've read several threads where people discuss problems with how their particular implementation of lines with thickness behave (since everybody does it themselves, all slightly differently). But OpenGL's documentation suggests glLineWidth plays very nicely in different situations, including with and without anti-aliasing: http://www.opengl.org/sdk/docs/man/xhtml/glLineWidth.xml
Title: Re: Better Line Thickness and OpenGL
Post by: Laurent on April 28, 2012, 09:34:04 am
Hi

Your arguments are valid, but it's not so easy.

glLineWidth is marked as deprecated in OpenGL 3.0, kept in OpenGL 3.1+ but then the standard says that line widths > 1 are supposed to return an error. In the end, it is highly implementation dependant. Even when width > 1 works, there's a limit which can be lower than you would expect.

That's why so many people create lines with quads, rather than lines + glLineWidth.