1

I'm trying to draw two lines which's vertices are:

glm::vec2(100, 100),
glm::vec2(300, 100),

glm::vec2(100, 100),
glm::vec2(100, 300)

The other line seems to not overlap with the other one like seen in the picture:

enter image description here

Instead the other line starts from the left of this line. How could this be resolved so that it would be overlapping the other line?

Vertex shader (test.vs):

#version 120

attribute vec2 position;
attribute vec4 in_color;

varying vec4 color; 

uniform mat4 projection;
uniform mat4 view;

void main() {
    color = in_color;

    gl_Position = projection * view * vec4(position, 0, 1);
}

Fragment shader (test.fs):

#version 120

varying vec4 color;

void main()
{
    gl_FragColor = color;
} 

Source code:

#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include <iostream>

#include <fstream>
#include <sstream>

#include <glm/gtx/transform.hpp>

void checkCompileErrors(GLuint shader, std::string type)
{
    GLint success;
    GLchar infoLog[1024];
    if (type != "PROGRAM")
    {
        glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
        if (!success)
        {
            glGetShaderInfoLog(shader, 1024, NULL, infoLog);
            std::cout << infoLog << '\n';
        }
    }
    else
    {
        glGetProgramiv(shader, GL_LINK_STATUS, &success);
        if (!success)
        {
            glGetProgramInfoLog(shader, 1024, NULL, infoLog);
            std::cout << infoLog << '\n';
        }
    }
}

int main( void )
{
    const glm::vec2 window_size(640, 480);

    GLFWwindow *window;
    if (!glfwInit())
    {
        return -1;
    }

    window = glfwCreateWindow(window_size.x, window_size.y, "window", 0, 0);

    if (!window)
    {
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);

    if (glewInit() != GLEW_OK) {
        std::cout << "glew not ok" << '\n';
    }

    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
    glEnable( GL_BLEND );

    glViewport( 0.0f, 0.0f, window_size.x, window_size.y); 

    std::ifstream fvertex;
    std::ifstream ffragment;

    std::stringstream vss, fss;

    fvertex.open("test.vs");
    ffragment.open("test.fs");

    vss << fvertex.rdbuf();
    fss << ffragment.rdbuf();

    fvertex.close();
    ffragment.close();

    std::string vs;
    std::string fs;

    vs = vss.str();
    fs = fss.str();

    unsigned int vertex, fragment;

    const char* vsc = vs.c_str();
    const char* fsc = fs.c_str();

    vertex = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertex, 1, &vsc, 0);
    glCompileShader(vertex);
    checkCompileErrors(vertex, "VERTEX");
    // fragment Shader
    fragment = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragment, 1, &fsc, 0);
    glCompileShader(fragment);
    checkCompileErrors(fragment, "FRAGMENT");

    uint32_t id = glCreateProgram();
    glAttachShader(id, vertex);
    glAttachShader(id, fragment);

    glLinkProgram(id);
    checkCompileErrors(id, "PROGRAM");

    glDeleteShader(vertex);
    glDeleteShader(fragment);

    glm::vec2 vertices[] =
    { 
        glm::vec2(100, 100),
        glm::vec2(300, 100),

        glm::vec2(100, 100),
        glm::vec2(100, 300)
    };

    glm::vec4 colors[] = {
        glm::vec4(1, 0, 0, 0.6),
        glm::vec4(1, 0, 0, 0.6),

        glm::vec4(0, 1, 0, 0.4),
        glm::vec4(0, 1, 0, 0.4)
    };

    uint32_t vao = 0, position_vbo = 0, color_vbo = 0;

    glGenVertexArrays(1, &vao);
    
    glGenBuffers(1, &position_vbo);
    glGenBuffers(1, &color_vbo);

    glBindVertexArray(vao);

    glBindBuffer(GL_ARRAY_BUFFER, position_vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    int location = glGetAttribLocation(id, "position");

    glVertexAttribPointer(location, 2, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(location);

    glBindBuffer(GL_ARRAY_BUFFER, color_vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);

    location = glGetAttribLocation(id, "in_color");

    glVertexAttribPointer(location, 4, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(location);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindVertexArray(0);


    while (!glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT);

        glm::mat4 projection(1);

        projection = glm::ortho(window_size.x * 0.5, window_size.x + window_size.x * 0.5, window_size.y + window_size.y * 0.5, window_size.y * 0.5, 0.1, 1000.0);

        glm::mat4 view(1);

        view = glm::translate(view, glm::vec3(window_size.x * 0.5, window_size.y * 0.5, -700.0f));

        view = view * glm::lookAtLH(glm::vec3(0, 0, 0), glm::vec3(0, 0, 1), glm::vec3(0, 1, 0));

        glUseProgram(id);

        glUniformMatrix4fv(glGetUniformLocation(id, "projection"), 1, GL_FALSE, (float*)&projection[0][0]);
        glUniformMatrix4fv(glGetUniformLocation(id, "view"), 1, GL_FALSE, (float*)&view[0][0]);

        glBindVertexArray(vao);
        glDrawArrays(GL_LINES, 0, 4);
        glBindVertexArray(0);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();

    return 0;
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
pesuww
  • 105
  • 11
  • 4
    I don't know OpenGL, but maybe the coordinate isn't from the very end of the line, but rather the center of the square at the end. If this is the case, then the two lines _do_ overlap, but it's that the green was drawn on top of the red, completely obscuring the red end. – Justin May 17 '21 at 21:37
  • I'm not sure if I understood correctly, but if you meant that the other color would just replace the other, then no, it has alpha channel, so it would be behind the other color. If I were to move the red line one pixel to the left, then I can see it overlapping the other, but then it would have other x coordinate than the other, which is a bit odd... – pesuww May 17 '21 at 21:43
  • Does this answer your question? [Opengl pixel perfect 2D drawing](https://stackoverflow.com/questions/10040961/opengl-pixel-perfect-2d-drawing) – Jeffrey May 17 '21 at 22:02
  • In one of the answers, person says that if we add 0.5 to coordinates, it would fix it. Well it does, for this case. If I were to add another line to the end of the red line, we are back to square one. – pesuww May 17 '21 at 22:40
  • Adding 0.5s to coordinates works with anti alias, but I cant produce same view without anti alias. – pesuww May 17 '21 at 23:19

0 Answers0