Thanks a lot everybody, so what I did is that i stock each triangle in a 4D vector (using glm::tvec4<GLbyte>) where the 4th dimension is used for the color of the triangle.
But I have a problem, not with this thing particularly, I think, but nothing is drawn on the screen.
Here's how my code is structured:
main.cpp
int main(int ac, char **av)
{
g_window = new Renderer;
g_window->init(ac, av);
g_window->createWindow(vector2i(500, 0), vector2i(1280, 720), "OpenGL");
if (g_window->resources())
{
glutDisplayFunc(onDisplay);
glutReshapeFunc(onReshape);
glutIdleFunc(onIdle);
glutSpecialFunc(onSpecial);
glutSpecialUpFunc(onSpecialUp);
glutIdleFunc(onDisplay);
glutPassiveMotionFunc(onMotion);
glutMotionFunc(onMotion);
glutMainLoop();
}
delete (g_window);
return (0);
}
Renderer.cpp
/* Constructor, destructor and other methods */
void Renderer::createWindow(vector2i winPos, vector2i winSize, const char *winTitle)
{
this->_winSize = winSize;
glutInitWindowPosition(winPos.x, winPos.y);
glutInitWindowSize(this->_winSize.x, this->_winSize.y);
glutCreateWindow(winTitle);
this->_chunks.push_back(new Chunk);
this->_chunks.at(0)->createMesh();
if (glewInit() != GLEW_OK)
{
std::cout << "glewInit failed" << std::endl;
exit(1);
}
if (glewIsSupported("GL_VERSION_4_5"))
std::cout << "GLEW Version is 4.5" << std::endl;
else
std::cout << "GLEW 4.5 not supported" << std::endl;
}
bool Renderer::resources()
{
this->_program = createProgram("cube.v.glsl", "cube.f.glsl");
if (this->_program == 0)
return (false);
this->_attribute_coord = glGetAttribLocation(this->_program, "coord");
if (this->_attribute_coord == -1)
{
std::cerr << "Could not bind attribute coord3d." << std::endl;
return (false);
}
this->_uniform_mvp = glGetUniformLocation(this->_program, "mvp");
if (this->_uniform_mvp == -1)
{
std::cerr << "Could not bind attribute mvp." << std::endl;
return (false);
}
glUseProgram(this->_program);
glEnableVertexAttribArray(this->_attribute_coord);
return (true);
}
void Renderer::beginScene() const
{
glm::mat4 view = glm::lookAt(this->_position, this->_position + this->_lookAt, glm::vec3(0, 1, 0));
glm::mat4 projection = glm::perspective(45.0f, 1.0f * this->_winSize.x / this->_winSize.y, 0.01f, 1000.0f);
glm::mat4 mvp = projection * view;
glUniformMatrix4fv(this->_uniform_mvp, 1, GL_FALSE, glm::value_ptr(mvp));
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void Renderer::endScene() const
{
glutSwapBuffers();
}
void Renderer::onDisplay()
{
this->beginScene();
this->renderChunks();
this->endScene();
}
void Renderer::renderChunks()
{
int i;
i = -1;
while (++i < this->_chunks.size())
{
this->_chunks.at(i)->render(this->_attribute_coord);
}
}
/* And others method for mouse movement etc */
Chunk.cpp
void Chunk::render(GLint attribute_coord)
{
if (!this->_currentVertex)
return;
glBindBuffer(GL_ARRAY_BUFFER, this->_vbo);
glVertexAttribPointer(attribute_coord, 4, GL_BYTE, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, this->_currentVertex);
}
So Renderer is a class I made that contains everything needed for rendering, such as the program for the shaders etc.
I'm a begginer in OpenGL and I don't know where the problem is.
Thanks for helping me!
EDIT: Sorry if i wasn't clear in the first post, what i wanted wasn't a trick to resize a VBO but just to add vertices to it (just once, at the creation of it).