How to calculate a thickness as a fixed width like OpenGL does?
Drawing lines with OpenGL using GL_LINES
is projection independent (if I can call it like this). I'd like to achieve the same effect with GL_TRIANGLE_FAN
.
I've got the following setup of vertices in pseudocode
void DrawLine(const std::pair<float, float>& a, const std::pair<float, float>& b, float thickness)
{
const auto [ax, ay] = a;
const auto [bx, by] = b;
const auto [dx, dy] = std::make_pair(bx - ax, by - ay);
auto [normal1x, normal1y] = Normalize(std::make_pair(-dy, dx));
auto [normal2x, normal2y] = Normalize(std::make_pair(dy, -dx));
normal1x *= thickness; normal1y *= thickness;
normal2x *= thickness; normal2y *= thickness;
vertices.emplace_back({ ax + normal1x, ay + normal1y });
vertices.emplace_back({ ax + normal2x, ay + normal2y });
vertices.emplace_back({ bx + normal2x, by + normal2y });
vertices.emplace_back({ bx + normal1x, by + normal1y });
glNamedBufferData(vertexBufferObject, sizeof(float) * 2 * vertices.size(), vertices.data(), GL_STREAM_DRAW);
glDrawArrays(GL_TRIANGLE_FAN, 0, vertices.size());
}
it draws a nice line, but it is not fixed-width.
How can I calculate the fixed width for a line?