Uncertainty with VBO and OpenGL

Started by
10 comments, last by Tallkotten 10 years, 11 months ago

Hi,

I've spent the day trying to understand the use of VBO since i want to boost the FPS in my game.

However i'm having some trouble understanding the life-scope of the VBO and how to properly use it. Every sample code and tutorial i've found has shown a single static scene to render and I've got a pretty complex file architecture in my game.

--Revelation?--

So this literally just occurred to me.

If a VBO behaves kind of like a texture (you generate and bind it) would i be correct to say that each entity in the game should hold it's own VBO?

For example; my player character would create a VBO at runtime and then call a render-function which fills it with data and renders it?

The render code would take the VBO, texture and some placement information. Then my normal code would run (bind the texture, create a clip for the 2D image) then I create a new vertex buffer and a new texture buffer and bind them. Lastly the VBO should be bound and rendered.

A NPC would have it's own VBO just like the player does.

--

Am i thinking right? And even if i am, could you be so kind to explain the scope and use of a VBO? Like i said, all the examples just handled a static scene to render so i'm having trouble understanding how to update it.

My game is in 2D and currently renders everything as quads or circles (all collision is done with rectangles). The size of an object is never changed after creation. When an object animates it'll change the location of the clip within it's loaded imagesheet. So the image itself will rarely change.

I'm kind of tired at the moment so i hope i made myself clear enough for you to understand.

Looking forward to some answer, thanks in advance!

Advertisement
Here is how I use them.

When I load the game, I create a VBO for every type of object in the scene. For example, if there is a certian type of enemy in the game, then I create a single VBO for that enemy, load it in, then reuse the same VBO every time I draw an enemy of that type.
My current game project Platform RPG

VBO's are pretty much the same as your normal vertex buffers in OpenGL the only difference is that they are stored in GPU memory instead of System memory.

So you create a buffer object for each VB you want to have on the GPU and then you fill it with the vertex data for that object, because this stuff is now in GPU memory you want to avoid having to dynamically update these buffers each frame as that is a costly operation.

You should most definetly not create a new VBO each frame only create them once when you create the object that needs it.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Here is how I use them.

When I load the game, I create a VBO for every type of object in the scene. For example, if there is a certian type of enemy in the game, then I create a single VBO for that enemy, load it in, then reuse the same VBO every time I draw an enemy of that type.

But each enemy hold it's own copy of it? Right?

VBO's are pretty much the same as your normal vertex buffers in OpenGL the only difference is that they are stored in GPU memory instead of System memory.

So you create a buffer object for each VB you want to have on the GPU and then you fill it with the vertex data for that object, because this stuff is now in GPU memory you want to avoid having to dynamically update these buffers each frame as that is a costly operation.

You should most definetly not create a new VBO each frame only create them once when you create the object that needs it.

I understanding right now is that i create a VBO when a (lets say) enemy is created. I then store this with the enemy until it's destroyed (how do i unload a VBO?).

You're saying that i should't update the data, is it ok to update the texture and vertex buffer? That shouldn't be a problem right? Because i need to be able to do that every frame.

You don't really want to update vertices each frame if you can avoid this, but yes it is possible to update the vertices in the VBO each frame, be aware that this will cost you some performance.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

So you are suggesting that i check if the entity has changed location and if it has i'll update the vertices for location? And same goes with the texture. That's all i ever need to update, drawing location and texture-coords.

How much performance are we talking about? Lower than using immediate mode?

I am not completely sure what you are trying to achieve here, but generally for movement of objects I don't update the vertices of the model that I am moving.

Why are you not updating a world matrix for positioning of the mesh as that would be faster anyway.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

I might be talking in my sleep since i know very little on the VBO topic. This is my current code for drawing something to the screen:


    float texLeft = (float)clip.x / width;
    float texRight = (float)( clip.x + clip.w ) / width;
    float texTop = (float)clip.y / height;
    float texBottom = (float)( clip.y + clip.h ) / height;


    glPushMatrix();

        glTranslatef(0, 0, 0);

        glScalef(scale,scale,1);
        //set color
        glColor4f(R, G, B, A);

        glBegin( GL_QUADS );

            glTexCoord2f(  texLeft,    texTop );
            glVertex2f( x, y );

            glTexCoord2f( texRight,    texTop );
            glVertex2f( x+w, y );

            glTexCoord2f( texRight, texBottom );
            glVertex2f( x+w, y+h );

            glTexCoord2f(  texLeft, texBottom );
            glVertex2f( x, y+h );

        glEnd();

    glPopMatrix();

Each call i update the position and the texture-coords.

From the tutorials i've read on VBO's you have to do the same: (a straight copy-paste from the tutorials)


GLfloat vertices_position[8] = {
	x, y,
	x+w, y,
	x+w, y+h,
	x, y+h,
};

GLfloat texture_coord[8] = {
	texLeft, texTop,
	texRight, texTop,
	texRight, texBottom,
	texLeft, texBottom,
};

// Allocate space for vertex positions and texture coordinates
glBindBuffer(GL_ARRAY_BUFFER , vbo);
glBufferData(GL_ARRAY_BUFFER , sizeof(vertices_position) + sizeof(texture_coord), NULL, GL_STATIC_DRAW);

// Transfer the vertex positions:
glBufferSubData(GL_ARRAY_BUFFER , 0, sizeof(vertices_position), vertices_position);

// Transfer the texture coordinates:
glBufferSubData(GL_ARRAY_BUFFER , sizeof(vertices_position), sizeof(texture_coord), texture_coord);

This wouldn't in any way decrease the performance? If it will, how else do i define position and texture-coords when using a VBO?

Thank you for taking your time, i really appreciate it!

That tutorial code is only run once at init of the object, rendering with Vertex Arrays or VBO's is different from rendering in immediate mode. In immediate mode you are specifying the vertices of an object on the fly, in retained mode you setup your resources on the GPU and then only update the parts that need updating.

So usually a model has a position stored as a 3D point which is fed into the glTranslatef function call instead of updating each vertex.

Rendering with a vertex buffer looks more like this.
//Set the vertex buffer to render with
//Set the index buffer to render with
//Call a render index primitive command
//Some actual code
void Rasterizer::RenderVertexBuffers()
{
	glDisable(GL_CULL_FACE);

	ShaderManager* sm = ShaderManager::GetInstance();
	TextureManager& tm = TextureManager::getInstance();
	DB::iterator meshIt = m_renderList.begin();
	while (meshIt != m_renderList.end())
	{
		RenderPair rp = *meshIt;
		vector<Mesh> meshMap = rp.GetModel().GetMeshes();
		glPushMatrix();
		glMultMatrixf(rp.GetWorld().GetArray());
		for (unsigned int i = 0; i < rp.GetModel().GetNumberMesh(); i++)
		{			
		        VertexBuffer vb = meshMap.GetVertexBuffer();
		        IndexBuffer ib = meshMap.GetIndexBuffer();
			vb.BindBuffer();	
			glVertexPointer(3, GL_FLOAT, vb.GetVertexSize(), (void*)0);
                        glTexCoordPointer(2, GL_FLOAT, vb.GetVertexSize(), (void*)12);			
			glNormalPointer(GL_FLOAT, vb.GetVertexSize(), (void*)20);
			
			glEnable(GL_NORMAL_ARRAY);	
			glEnable(GL_VERTEX_ARRAY);
			ib.BindBuffer();
			glDrawElements(GL_TRIANGLES, ib.GetNumberIndices(), GL_UNSIGNED_SHORT, 0);
			glDisable(GL_NORMAL_ARRAY);
			glDisable(GL_VERTEX_ARRAY);
		}
		glPopMatrix();
		meshIt++;
	}
}

//Vertex and index buffer bind both look like this
void VertexBuffer::BindBuffer()
{
	m_glx->glBindBufferARB(GL_ARRAY_BUFFER_ARB, m_buffer);
}
I removed all the material code from this render loop to keep it a bit simpler, vertex and index buffers are just wrapper classes around gl VBO functions. This is all written for OpenGL 2.0, I haven't touched GL after that so I can't comment if anything would currently be different for GL3 or 4.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Ok, so you are saying: "Store a static VBO in the memory with some coordinates and texture data and then use glTranslate to move it on the screen"?

This topic is closed to new replies.

Advertisement