0
votes

I am currently trying to render a list of points on the screen but all I get is the following, a single dot which moves with my camera and stays in the centre of the screen.

enter image description here

The list of points generated change with each frame and defined in world space coordinates. I want to know where I am going wrong with the code below and what are the best practices for drawing a dynamic list of points like this without using immediate mode calls. Most online resources refer to glBegin() and glEnd() when drawing points.

 void RenderPoints(const CamInfo & camInfo, const std::vector<vec4>& listofpoints )
{
Shader->Use();
Shader->setUniform("MVP",camInfo.proj * camInfo.view);
Shader->setUniform("Color",glm::vec3(0.0f,1.0f,1.0f));
glGenVertexArrays( 1, &M_VAO );
glBindVertexArray( M_VAO );


glGenBuffers( 1, &M_VBO );
glBindBuffer( GL_ARRAY_BUFFER, M_VBO ); 
glBufferData( GL_ARRAY_BUFFER, listofpoints.size()*sizeof(glm::vec4), NULL, GL_STATIC_DRAW );
glBufferSubData( GL_ARRAY_BUFFER, 0,  listofpoints.size()*sizeof(glm::vec4), (const GLvoid*)(&listofpoints[0]) );
GLuint pos_location = glGetAttribLocation(Shader->GetProgramID(), "position");
glEnableVertexAttribArray(pos_location ); 
glVertexAttribPointer( pos_location, 4, GL_FLOAT, GL_FALSE, 0, 0 );
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glGenBuffers(1, &M_IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, M_IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_vertex_indices.size()*sizeof(GLuint), (const GLvoid*)(&m_vertex_indices[0]), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glPointSize(10.0f);
glBindVertexArray(M_VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, M_IBO);
glDrawElements(GL_POINTS,m_vertex_indices.size(),GL_UNSIGNED_INT,&m_vertex_indices);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
glBindVertexArray(0);
Shader->Disable();

}
2
You should debug more. You set color to be Cyan (0,1,1) but the dot is white. That's puzzling. Maybe your scale or matrices are wrong.Kromster

2 Answers

0
votes

You are using a buffer for your vertex indices, but passing the address your index array (or perhaps the address of an std::vector?) to glDrawElements. When a buffer is bound to GL_ELEMENT_ARRAY_BUFFER the fourth argument of glDrawElements is an offset into that buffer, so in your case you should pass 0:

glDrawElements(GL_POINTS, m_vertex_indices.size(), GL_UNSIGNED_INT, 0);
0
votes

You can't do this:

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

By unbinding the vertex buffer before you unbind the vertex array you're telling the vertex array not to record the appropriate buffer. When you unbind the vertex array with glBindVertexArray(0) the state machine should be in exactly the state (with regard to vertex buffers) you would have it when you call glDrawElements. That means that you have no vertex buffer bound during your draw call, so it's probably using random values (and probably generating an error if you check with glGetError.

Try swapping those two lines.