Skip to main content
deleted 1706 characters in body; edited tags; edited title
Source Link
user1430
user1430

setting up modern How should I render multiple objects in OpenGL renderer?

This question might be a bit simplistic but I am just getting my head wrapped around modern OpenGL, I never learned the previous and so this is my first dive into graphics programming.

I have the concept of setup a shader, setting up the VAO and binding it, setting up a VBO binding it, adding the data, unbind the VBO then unbind the VAO. In the draw. Here's some example code below.

void init(){
shder = createShader();

    //init OpenGL vbo, vao, ebo, etc.
    glGenBuffers(1, &vbo1);      //vertex buffer object to hold data
    glGenBuffers(1, &ebo);       //index buffer object to hold index data
    glGenVertexArrays(1, &vao1); //vertex array object to hold all my vbo and their indices
    
    glBindVertexArray(vao1);             //bind the vertex array object
    glBindBuffer(GL_ARRAY_BUFFER, vbo1); //configure the vertex buffer object
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); //inform shaders about vbo data structure
    glEnableVertexAttribArray(0); //enable shaders to accept the vertex information
    
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); //configure the index data
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    glBindVertexArray(0); //unbind the vao as not to mess it up

}

void render() {
shader.bind(); //glusepgrogram call
bindVAO //glbindvertexarray

gldrawelements(gl_triangles, num, gl_unsigned_int, 0);

unbindVAO;
shader.unbind();
}

my main question is about the init part of the code above, with all the glen, glBind, etc calls.

Let's say that I would like to draw 10 objects, how would I structure this codethat? Would I call glDrawElements(...)glDrawElements(...) in a for loop 10 times?

void render() {
  shader.bind(); //glusepgrogram callcalls glUseProgram
  bindVAO();     //glbindvertexarray calls glBindVertexArray

  for(int i = 0; i < 10; i++)
  {
    glDrawElements(....)
  }

  unbindVAO;
  shader.unbind();
}

setting up modern OpenGL renderer

This question might be a bit simplistic but I am just getting my head wrapped around modern OpenGL, I never learned the previous and so this is my first dive into graphics programming.

I have the concept of setup a shader, setting up the VAO and binding it, setting up a VBO binding it, adding the data, unbind the VBO then unbind the VAO. In the draw. Here's some example code below.

void init(){
shder = createShader();

    //init OpenGL vbo, vao, ebo, etc.
    glGenBuffers(1, &vbo1);      //vertex buffer object to hold data
    glGenBuffers(1, &ebo);       //index buffer object to hold index data
    glGenVertexArrays(1, &vao1); //vertex array object to hold all my vbo and their indices
    
    glBindVertexArray(vao1);             //bind the vertex array object
    glBindBuffer(GL_ARRAY_BUFFER, vbo1); //configure the vertex buffer object
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); //inform shaders about vbo data structure
    glEnableVertexAttribArray(0); //enable shaders to accept the vertex information
    
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); //configure the index data
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    glBindVertexArray(0); //unbind the vao as not to mess it up

}

void render() {
shader.bind(); //glusepgrogram call
bindVAO //glbindvertexarray

gldrawelements(gl_triangles, num, gl_unsigned_int, 0);

unbindVAO;
shader.unbind();
}

my main question is about the init part of the code above, with all the glen, glBind, etc calls.

Let's say that I would like to draw 10 objects, how would I structure this code? Would I call glDrawElements(...) in a for loop 10 times?

void render() {
shader.bind(); //glusepgrogram call
bindVAO //glbindvertexarray

for(int i = 0; i < 10; i++)
 {
  glDrawElements(....)
}

unbindVAO;
shader.unbind();
}

How should I render multiple objects in OpenGL?

I am just getting my head wrapped around modern OpenGL. Let's say that I would like to draw 10 objects, how would I structure that? Would I call glDrawElements(...) in a for loop 10 times?

void render() {
  shader.bind(); // calls glUseProgram
  bindVAO();     // calls glBindVertexArray

  for(int i = 0; i < 10; i++) {
    glDrawElements(....)
  }

  unbindVAO;
  shader.unbind();
}
Source Link
sean
  • 51
  • 1
  • 1
  • 2

setting up modern OpenGL renderer

This question might be a bit simplistic but I am just getting my head wrapped around modern OpenGL, I never learned the previous and so this is my first dive into graphics programming.

I have the concept of setup a shader, setting up the VAO and binding it, setting up a VBO binding it, adding the data, unbind the VBO then unbind the VAO. In the draw. Here's some example code below.

void init(){
shder = createShader();

    //init OpenGL vbo, vao, ebo, etc.
    glGenBuffers(1, &vbo1);      //vertex buffer object to hold data
    glGenBuffers(1, &ebo);       //index buffer object to hold index data
    glGenVertexArrays(1, &vao1); //vertex array object to hold all my vbo and their indices
    
    glBindVertexArray(vao1);             //bind the vertex array object
    glBindBuffer(GL_ARRAY_BUFFER, vbo1); //configure the vertex buffer object
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); //inform shaders about vbo data structure
    glEnableVertexAttribArray(0); //enable shaders to accept the vertex information
    
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); //configure the index data
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    glBindVertexArray(0); //unbind the vao as not to mess it up

}

void render() {
shader.bind(); //glusepgrogram call
bindVAO //glbindvertexarray

gldrawelements(gl_triangles, num, gl_unsigned_int, 0);

unbindVAO;
shader.unbind();
}

my main question is about the init part of the code above, with all the glen, glBind, etc calls.

Let's say that I would like to draw 10 objects, how would I structure this code? Would I call glDrawElements(...) in a for loop 10 times?

void render() {
shader.bind(); //glusepgrogram call
bindVAO //glbindvertexarray

for(int i = 0; i < 10; i++)
{
  glDrawElements(....)
}

unbindVAO;
shader.unbind();
}