Jump to content

  • Log In with Google      Sign In   
  • Create Account

Uncertainty with VBO and OpenGL


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 Tallkotten   Members   -  Reputation: 288

Like
0Likes
Like

Posted 19 May 2013 - 12:47 PM

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!


Edited by Tallkotten, 19 May 2013 - 01:17 PM.


Sponsor:

#2 HappyCoder   Members   -  Reputation: 2661

Like
0Likes
Like

Posted 19 May 2013 - 05:38 PM

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.

#3 NightCreature83   Crossbones+   -  Reputation: 2823

Like
2Likes
Like

Posted 20 May 2013 - 07:32 AM

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, Mad Max

#4 Tallkotten   Members   -  Reputation: 288

Like
0Likes
Like

Posted 21 May 2013 - 10:35 AM

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.



#5 NightCreature83   Crossbones+   -  Reputation: 2823

Like
1Likes
Like

Posted 22 May 2013 - 02:15 PM

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, Mad Max

#6 Tallkotten   Members   -  Reputation: 288

Like
0Likes
Like

Posted 22 May 2013 - 02:32 PM

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?



#7 NightCreature83   Crossbones+   -  Reputation: 2823

Like
1Likes
Like

Posted 22 May 2013 - 04:05 PM

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, Mad Max

#8 Tallkotten   Members   -  Reputation: 288

Like
0Likes
Like

Posted 23 May 2013 - 02:46 AM

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!



#9 NightCreature83   Crossbones+   -  Reputation: 2823

Like
1Likes
Like

Posted 23 May 2013 - 11:45 AM

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[i].GetVertexBuffer();
		        IndexBuffer ib = meshMap[i].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.

Edited by NightCreature83, 23 May 2013 - 11:50 AM.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#10 Tallkotten   Members   -  Reputation: 288

Like
0Likes
Like

Posted 23 May 2013 - 02:03 PM

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"?



#11 NightCreature83   Crossbones+   -  Reputation: 2823

Like
0Likes
Like

Posted 24 May 2013 - 01:18 AM

Yes effectively I am saying that, but you can move rotation, scale and translation into one matrix and us glMultMatrixf(matrix); to set it all in one go.


Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#12 Tallkotten   Members   -  Reputation: 288

Like
0Likes
Like

Posted 24 May 2013 - 02:10 AM

Ok, thanks!

 

I'm going to look at how some other people do it and ultimately decide on a way to go. But yours sound solid.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS