how to use multiple vbos

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.[ATTACH]947[/ATTACH]

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.

[QUOTE=Velator;1265004]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.[/QUOTE]

thanks but can you say me what i do wrong in my code?
i can’t get it to work![ATTACH]950[/ATTACH]

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
}

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);

[QUOTE=snowyCoder;1265024]i need to create a shader for every vao???
[/QUOTE]

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

so why it doesn’t work?

In the setup part of your code you bind “vao” twice: glBindVertexArray(vao) instead of “vao2”.

fixed it but still does not work :frowning:
it doesn’t buffer the first vertices

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?

[QUOTE=Betrayal;1265030]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?[/QUOTE]

i used glVertexAttribPointer on uniforms, but i don’t know what glEnableVertexAttribArray does.
anyway this ([ATTACH]953[/ATTACH]) 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!