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:
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;
}