Problem with glDrawElements and GL_LINES, using glm library

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_indices.push_back(0);m_indices.push_back(1);m_indices.push_back(1);m_indices.push_back(2);m_indices.push_back(2);m_indices.push_back(3);m_indices.push_back(3);m_indices.push_back(0); //indices
//The far clip plane
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

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 :slight_smile:

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()->sendUniform4x4(“modelview_matrix”, modelviewMatrix);
getShader()->sendUniform4x4(“projection_matrix”, projectionMatrix);


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);


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,

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.

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!