I managed to fix it myself.
Hi everyone.
I’m following a tutorial and got stuck at an early stage trying to draw a triangle using vertex array object. The screen is just black.
Here are the relevant parts of my code, I’ll post a link to a public repo of my project at the end of this post, in case you want to see the context of all this. I’m using C++ and SFML by the way.
Initialization
void RenderUtil::initGraphics()
{
glewInit();
glClearColor(0.0, 0.0, 0.0, 0.0);
glFrontFace(GL_CW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
}
void RenderUtil::clearScreen()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
Mesh
Mesh::Mesh()
{
glGenBuffers(1, &m_vbo);
m_size = 0;
}
void Mesh::addVertices(Vertex* vertices, int array_size)
{
m_size = array_size * sizeof(Vertex);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, m_size, vertices, GL_STATIC_DRAW);
}
void Mesh::draw()
{
glEnableClientState(GL_VERTEX_ARRAY);
glColor3f(0.4,0.0,0.0);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glVertexPointer(3, GL_FLOAT, m_size / sizeof(Vertex), 0);
glDrawArrays(GL_TRIANGLES, 0, m_size);
glDisableClientState(GL_VERTEX_ARRAY);
}
Game
Game::Game(Display& display)
: m_display(&display)
, mesh()
{
Vertex vertices[] = {Vertex(Vector3f(-1.0, -1.0, 0.0)),
Vertex(Vector3f(0.0, 1.0, 0.0)),
Vertex(Vector3f(1.0, -1.0, 0.0))};
int array_size = sizeof(vertices) / sizeof(Vertex);
mesh.addVertices(vertices, array_size);
}
void Game::render()
{
mesh.draw();
}
I think those are the most relevant parts, where it’s most likely that I’m commiting some mistake. Here is a link to the rest:
[snip]
Ok so, please help me figure out what silly mistakes I’m making that prevents a triangle from rendering.