This is a simple program to draw anti aliased lines. The program works, I have only a problem if I add a projection. I have to use this program in another context, I have to use there an orthographic projection. I think I have to replace the parameter "win_scale" in geometry shader, but I don't exactly how I have to scale the result or how I have to rewrite the function if I'm using a projection. Here are the shaders. Vertex Shader:
#version 330 core
layout (location = 0) in vec2 position;
layout (location = 1) in vec3 color;
uniform mat4 model;
//uniform mat4 view;
uniform mat4 projection;
out VS_OUT {
vec3 color;
} vs_out;
void main() {
gl_Position = projection * vec4(position.x, position.y, 0.0f, 1.0f);
vs_out.color = color;
}
Geometry shader
#version 330 core
layout (points) in;
layout (triangle_strip, max_vertices = 4) out;
uniform vec2 target;
uniform float thickness;
uniform vec2 win_scale; //window width and height
in VS_OUT {
vec3 color;
} gs_in[];
out vec3 fColor;
vec2 screen_space(vec4 vertex) {
return vec2(vertex.xy/vertex.w) * win_scale;
}
void main() {
fColor = gs_in[0].color;
vec2 p0 = screen_space(gl_in[0].gl_Position); // gs_in[0] since there's only one input vertex
vec2 p1 = target * win_scale;
// determine the direction of each of the segments
vec2 v0 = normalize(p1-p0);
// determine the normal of each of the segments
vec2 n0 = vec2(-v0.y, v0.x);
gl_Position = vec4((p0 - thickness * n0)/win_scale, 0.0, 1.0);
EmitVertex();
gl_Position = vec4((p0 + thickness * n0)/win_scale, 0.0, 1.0);
EmitVertex();
gl_Position = vec4((p1 - thickness * n0)/win_scale, 0.0, 1.0);
EmitVertex();
gl_Position = vec4((p1 + thickness * n0)/win_scale, 0.0, 1.0);
EmitVertex();
EndPrimitive();
}
And in the main I'm passing the projection values in the following ways,:
glm::mat4 proj = glm::ortho(0.f, (float)width, 0.f, (float)height, -1.f, 1.f);
glUniformMatrix4fv(glGetUniformLocation(shader.Program, "projection"), 1, GL_FALSE, glm::value_ptr(proj));
Of course, if someone has suggestions about how can I implement better antialiased lines, these are welcome.