• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Tallkotten

Uncertainty with VBO and OpenGL

11 posts in this topic

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
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

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.

2

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites

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!

0

Share this post


Link to post
Share on other sites
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
1

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0