PDA

View Full Version : how to use multiple vbos



snowyCoder
03-22-2015, 04:00 AM
Hello, i'm new to OpenGL and i wanted to draw multiple objects. (VC++)
i use opengl 3.3 and all tutorials and code found online is deprecated.
how can i draw multiple objects? need i to use multiple vbos or multiple vaos?
and how can i do it?
sorry for my english, it's not my native language;
p.s. i've put my code as attachment.1678

Velator
03-22-2015, 05:21 AM
Depends largely on your project, but if you're going to draw two very different looking objects, say a cube and a plane, then using two different vaos is a good idea.

What you need to do is bind a vao (say vao_cube), then bind the cube vbo and everything. Once done, you bind the other vao (vao_plane), an bind ITS plane vbo.

Now when drawing, you bind a vao and draw, bind the next vao and draw that. The vao stores the vbo and vertex attributes and stuff in it. So everytime you bind a vao, think of it as setting some drawing settings before doing the draw.

snowyCoder
03-22-2015, 06:28 AM
Depends largely on your project, but if you're going to draw two very different looking objects, say a cube and a plane, then using two different vaos is a good idea.

What you need to do is bind a vao (say vao_cube), then bind the cube vbo and everything. Once done, you bind the other vao (vao_plane), an bind ITS plane vbo.

Now when drawing, you bind a vao and draw, bind the next vao and draw that. The vao stores the vbo and vertex attributes and stuff in it. So everytime you bind a vao, think of it as setting some drawing settings before doing the draw.

thanks but can you say me what i do wrong in my code?
i can't get it to work!1683

Velator
03-22-2015, 06:55 AM
Well, I can't really go through all of that right now. But I noticed you used glBindVertexArray(vao) twice, even though you only have one vao. And I don't see the vertex array for the floor.

What you need to do is something like this:


glGenVertexArrays(1, &vaoCube);
glGenVertexArrays(1, &vaoPlane);
glGenBuffers(1, &vboCube);
glGenBuffers(1, &vboPlane);

//here we set things up for the cube vao
//the vao will store the vbo with it and every time you bind it and call glDrawArrays, it will use the vbo associated with the bound vao.
glBindVertexArray(vaoCube);

glBindBuffer(GL_ARRAY_BUFFER, vboCube);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_Cube), vertices_Cube, GL_STATIC_DRAW); //using the vertices_Cube vertex array

//shader vertex attribs and stuff here


//now settings things up for the plane vao
glBindVertexArray(vaoPlane);

glBindBuffer(GL_ARRAY_BUFFER, vboPlane);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_plane), vertices_plane, GL_STATIC_DRAW); //using the vertices_plane vertex array

//shader vertex attribs and stuff here


//NOW when you want to draw the cube and the plane, you bind its vao first

while(drawloo)
{
//clear and stuff

glBindVertexArray(vaoCube); //bind the cube vao
glDrawArrays(...); //will use vboCube

glBindVertexArray(vaoPlane); //bind the cube vao
glDrawArrays(...);//will use vboPlane

//other stuff
}

snowyCoder
03-22-2015, 08:59 AM
i need to create a shader for every vao???
p.s. it still don't display the second vao.

here my modified code:
init:

// Create Vertex Array Object
glGenVertexArrays(1, &vao);
glGenVertexArrays(1, &vao2);

// Create a Vertex Buffer Object and copy the vertex data to it
glGenBuffers(1, &vbo);
glGenBuffers(1, &vbo2);

glBindVertexArray(vao);

GLfloat vertices[] = { ..... };

glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glBindVertexArray(vao);


GLfloat vertices2[] = { ......};

glBindBuffer(GL_ARRAY_BUFFER, vbo2);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices2), vertices2, GL_STATIC_DRAW);


loop:


// Draw cube
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 36);

glBindVertexArray(vao2);
glDrawArrays(GL_TRIANGLES, 0, 6);

Betrayal
03-22-2015, 09:29 AM
i need to create a shader for every vao???


No you can use the same shader to perform multiple draw calls for data in different VAOs

snowyCoder
03-22-2015, 10:29 AM
No you can use the same shader to perform multiple draw calls for data in different VAOs

so why it doesn't work?

Betrayal
03-22-2015, 10:37 AM
In the setup part of your code you bind "vao" twice: glBindVertexArray(vao) instead of "vao2".

snowyCoder
03-22-2015, 10:59 AM
In the setup part of your code you bind "vao" twice: glBindVertexArray(vao) instead of "vao2".

fixed it but still does not work :(
it doesn't buffer the first vertices

Betrayal
03-22-2015, 11:31 AM
Well, nobody knows the code and shaders you are using now. Difficult to help that way, maybe post all relevant parts of the code.
I don't see that you enabled attributes for both Vaos. Did you actually call glVertexAttribPointer and glEnableVertexAttribArray?

snowyCoder
03-22-2015, 12:00 PM
Well, nobody knows the code and shaders you are using now. Difficult to help that way, maybe post all relevant parts of the code.
I don't see that you enabled attributes for both Vaos. Did you actually call glVertexAttribPointer and glEnableVertexAttribArray?

i used glVertexAttribPointer on uniforms, but i don't know what glEnableVertexAttribArray does.
anyway this (1686) is my full code,
the things in game loop are for rotating objects, don't care about that.
thanks to everyone is answering, you're really helping me a lot!