PDA

View Full Version : Problem regarding line drawing



qqtolene
10-19-2017, 02:04 AM
Hello I have been struggling lately to overcome some difficulties regarding drawing lines. My final goal is to use lines to represent edges of a skeleton. Since I will later use the following class on a 3d mesh I thought I would use indexed drawing.

With that in mind I tried to draw one line using the following code to draw a simple line segment to start with:


class LineDrawer {

std::vector<glm::vec3> m_vertices{glm::vec3(0, 0, 0), glm::vec3(0.5, 0.5, 0)};
std::vector<uint> m_indices{0, 1};

uint VAO, VBO, EBO;

public:

void Draw(Shader *skeletonShader, Shader *shader) {
glBindVertexArray(VAO);
glDrawElements(GL_LINES, m_indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}

void initializeDrawingBuffers() {

glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);

glBindVertexArray(VAO);

glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, m_vertices.size() * sizeof(glm::vec3),
&m_vertices[0], GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indices.size() * sizeof(GLuint),
&m_indices[0], GL_STATIC_DRAW);

glVertexAttribPointer(0, 1, GL_FLOAT, GL_FALSE, 0, (GLvoid *)0);
glEnableVertexAttribArray(0);

glBindVertexArray(0);

}

};

The vertex shader


#version 330 core

layout (location = 0) in vec3 position;

void main()
{
gl_Position = vec4(position, 1.0f);
}

The fragment shader


#version 330 core

out vec4 color;

void main()
{
color = vec4(0.0f,1.0f,0.0f, 1.0f);
}

Using m_vertices{glm::vec3(0, 0, 0), glm::vec3(0.5, 0.5, 0)} i expect to get a line segment starting in the center of the screen and ending in the center of the top right part of the screen. Instead I get A line segment on the x axis as if I had used m_vertices{glm::vec3(0, 0, 0), glm::vec3(0.2, 0, 0)}; check this picture: 2522. The shader ignores the coordinates in the y and z axis and I am seeking for an answer for why this is happening..

Is my initializeDrawingBuffers() function correct or do you see anything odd in my code?