Hey all, so with my current method I can barely hit 10,000 triangles without my computer slowing down and was wondering how to speed it up. I have a sprite class which has a render method that renders a triangles to the screen. Here’s that code:
Sprite::Sprite(){
GLfloat triangle_vertices[] = {
0.0f, 0.8f,
-0.8f, -0.8f,
0.8f, -0.8f,
};
//Buffer for triangle vertices
glGenBuffers(1, &vbo_triangle);
glBindBuffer(GL_ARRAY_BUFFER, vbo_triangle);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_vertices), triangle_vertices, GL_STATIC_DRAW);
const char* attribute_name = "coord2d";
attribute_coord2d = glGetAttribLocation(program, attribute_name);
if (attribute_coord2d == -1) {
fprintf(stderr, "Could not bind attribute %s
", attribute_name);
}
}
void Sprite::render(){
glEnableVertexAttribArray(attribute_coord2d);
glBindBuffer(GL_ARRAY_BUFFER, vbo_triangle);
glVertexAttribPointer(
attribute_coord2d,
3,
GL_FLOAT,
GL_FALSE,
0,
0
);
glDrawArrays(GL_TRIANGLES, 0, 3);
glEnableVertexAttribArray(0);
}
So as you can see, my constructor creates and binds buffer data, and every time I call render it draws them to the screen, and I call it like this:
void onDisplay()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glUseProgram(program);
for(spriteIterator = spriteVec.begin(); spriteIterator != spriteVec.end(); ++spriteIterator){
(*spriteIterator)->render();
}
glutSwapBuffers();
}
void idle()
{
glutPostRedisplay();
}
What ways can I improve the efficiency of this code? (Eventually the vertices will be dynamic, the single triangle is there for example) Thanks!