i can not draw anything on my gl screen. It just clears the screen. Here is my Main, Display, and Cube classes.
main.cpp
Display d(800, 600, "Hi");
while (!d.IsClosed()) {
d.Clear();
d.Update();
}
return 0;
display.cpp
Constructor initializes glfw and glew.
void Display::Update() {
glfwPollEvents();
m_shader->UseProgram();
m_renderer->Render(*m_cube);
glfwSwapBuffers(m_window);
}
UseProgram function is from Shader class and just glUseProgram(m_programID);
.
Also m_renderer->Render()
is just calls Render function from object in the parameter.
cube.cpp
void Cube::Init() {
//cube vertices - colors
GLfloat data[] = {
-0.5f, 0.5f, 0.0f, m_color.r, m_color.g, m_color.b, m_color.a,
-0.5f, -0.5f, 0.0f, m_color.r, m_color.g, m_color.b, m_color.a,
0.5f, -0.5f, 0.0f, m_color.r, m_color.g, m_color.b, m_color.a,
0.5f, 0.5f, 0.0f, m_color.r, m_color.g, m_color.b, m_color.a,
-0.5f, 0.5f, -0.5f, m_color.r, m_color.g, m_color.b, m_color.a,
-0.5f, -0.5f, -0.5f, m_color.r, m_color.g, m_color.b, m_color.a,
0.5f, -0.5f, -0.5f, m_color.r, m_color.g, m_color.b, m_color.a,
0.5f, 0.5f, -0.5f, m_color.r, m_color.g, m_color.b, m_color.a
};
//cube indices
GLuint indices[] = {
//front
0, 1, 2,
0, 2, 3,
//top
4, 0, 3,
4, 3, 7,
//bottom
1, 3, 6,
1, 6, 2,
//left
4, 3, 1,
4, 1, 0,
//right
3, 2, 6,
3, 6, 7,
//back
4, 5, 6,
4, 6, 7,
};
m_vertexCount = sizeof(indices) / sizeof(GLuint);
//Generate buffers
glGenVertexArrays(1, &m_vaoID);
glGenBuffers(1, &m_vboID);
glGenBuffers(1, &m_eboID);
//Bind vertex array object
glBindVertexArray(m_vaoID);
//Bind vertex buffer object
glBindBuffer(GL_ARRAY_BUFFER, m_vboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GL_FLOAT), (GLvoid*)0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GL_FLOAT), (GLvoid*)(3 * sizeof(GL_FLOAT)));
//Unbind vertex buffer object
glBindBuffer(GL_ARRAY_BUFFER, 0);
//Bind Element buffer object
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_eboID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
//Unbind Element buffer object
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
//Unbind vertex array object
glBindVertexArray(0);
}
Init()
function is called from constructor. This is all cube initialization code. Just binding and unbinding arrays.
void Cube::Render() {
glBindVertexArray(m_vaoID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_eboID);
glDrawElements(GL_TRIANGLES, m_vertexCount, GL_UNSIGNED_INT, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
I can't figure out what problem is. It just doesn't shows anything on screen. If you want i can post my Shader class too.
The stride parameters are wrong. Each consecutive position start 7 floats away from the start of the last one. Same goes for colors. The correct code would be
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(GL_FLOAT), (GLvoid*)0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 7 * sizeof(GL_FLOAT), (GLvoid*)(3 * sizeof(GL_FLOAT)));
Another thing is that you are unbinding the index buffer before unbinding the VAO, thus you effectively remove the binding. The last lines of the VAO setup have to be:
//Bind Element buffer object
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_eboID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
//Unbind vertex array object
glBindVertexArray(0);
//Unbind Element buffer object
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Additionally, the binding/unbinding of the GL_ELEMENT_ARRAY_BUFFER
in the Render()
function should be removed.