PDA

View Full Version : Problem with glDrawElements and GL_LINES, using glm library



Lodeman
04-11-2013, 10:29 AM
Hi all,

I'm having problems with drawing the outlines of a camera frustum. Basically, I'm not seeing any lines drawn at all and I don't really have a clue why it's not drawing them.
My shader compiles fine and has no errors (verified with cgc). I also bind the single attribute to the shader on creation.
For my algebraics, I'm using the GLM library.
Rougly, the relevant code is as follows:

I have a class Frustum with following members:
GLuint m_vertexBuffer;
GLuint m_indexBuffer;
std::vector<glm::vec3> m_vertices;
std::vector<GLuint> m_indices;

The vectors and buffers are initialised with following code, (where m_nearClip and m_farClip are arrays of glm::vec3):
//The near clip plane
m_vertices.push_back(m_nearClip[0]);
m_vertices.push_back(m_nearClip[1]);
m_vertices.push_back(m_nearClip[2]);
m_vertices.push_back(m_nearClip[3]);
m_indices.push_back(0);m_indices.push_back(1);m_in dices.push_back(1);m_indices.push_back(2);m_indice s.push_back(2);m_indices.push_back(3);m_indices.pu sh_back(3);m_indices.push_back(0); //indices
//The far clip plane
m_vertices.push_back(m_farClip[0]);
m_vertices.push_back(m_farClip[1]);
m_vertices.push_back(m_farClip[2]);
m_vertices.push_back(m_farClip[3]);
int i = 4;
m_indices.push_back(i+0);m_indices.push_back(i+1); m_indices.push_back(i+1);m_indices.push_back(i+2); m_indices.push_back(i+2);m_indices.push_back(i+3); m_indices.push_back(i+3);m_indices.push_back(i+0); //indices
//Edges between
m_indices.push_back(0);m_indices.push_back(i+0);m_ indices.push_back(1);m_indices.push_back(i+1);m_in dices.push_back(2);m_indices.push_back(i+2);m_indi ces.push_back(3);m_indices.push_back(i+3);

glGenBuffers(1, &m_vertexBuffer); //Generate a buffer for the vertices
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer); //Bind the vertex buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3)*m_vertices.size(), &m_vertices[0], GL_STATIC_DRAW);

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

m_vertices and m_indices get filled in as I'd expect them to be, so I don't really think there's an issue here, but perhaps I'm overlooking something :-)

Now, the render method is essentially this (sendUniform is a verified method I use, I know it succesfully sends in uniforms to the binded shader, I also debugged and modelview and projectionMatrix are as expected):
getShader()->bindShader();
getShader()->sendUniform4x4("modelview_matrix", modelviewMatrix);
getShader()->sendUniform4x4("projection_matrix", projectionMatrix);

glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
glVertexAttribPointer((GLint)0, 3, GL_FLOAT, GL_FALSE, 0, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer);
glDrawElements(GL_LINES, m_indices.size(), GL_UNSIGNED_INT, 0);

glDisableVertexAttribArray(0);

That's it for relevant code. Here come the simple shaders!

Now, my vertex shader looks like this:

#version 130

uniform mat4 projection_matrix;
uniform mat4 modelview_matrix;

layout (location = 0) in vec3 a_Vertex;

void main(void)
{
vec4 pos = modelview_matrix * vec4(a_Vertex, 1.0);
gl_Position = projection_matrix * pos;
}

And my fragment shader is as follows, it's just supposed to render my lines in red:

#version 330

layout(location = 0) out vec4 outColor;

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

I appreciate any help given very much!

Thanks and kind regards,
Lodeman

mobeen
04-11-2013, 11:14 AM
Hi,
First up, these forums have a very useful [ code ] tags that could help others in reading your code snippets so please use them they are really helpful.

Shouldn't your vertex shader version also be
#version 330 instead of
#version 130

Second, try to render a simple line with two vertices at (0,0,0) and (1,0,0) and see if you get it on screen.

Lodeman
04-11-2013, 11:42 AM
Thanks Mobeen I'll keep the code snippets in mind for next time.
I actually just managed to figure out my problem.

The code I mentioned to initialise and load up my buffers was executed "too early" during construction of the Frustum. Moving it to another method and calling it after construction makes it work.

Thanks for the reply though, trying to render those two vertices in the render loop made me realise my code was somehow not buffering correctly.
Cheers and rainy greetings from Belgium!