Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

999318 Posts in 39213 Topics- by 30620 Members - Latest Member: blockerz

April 23, 2014, 05:14:53 AM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)2D quad size in OpenGL
Pages: [1]
Print
Author Topic: 2D quad size in OpenGL  (Read 386 times)
Ethan_Hall
Level 0
**


View Profile Email
« on: January 06, 2013, 09:32:23 AM »

Hi all,

I'm making a 2D game in OpenGL 3.3 with some friends as a capstone project. I am finally, able to map a texture onto a quad and render it to the screen.

Going forward I have 2 things that are bugging me:

I know that ultimately I will have one file containing the vertex buffer objects that I need to access my sprites. Originally I was going to arrange the objects based on character; one VBO would correspond to the protagonist's spritesheet, another VBO would contain Enemy #1's spritesheet... but after seeing the way others do it, I'm not so sure how to organize everything. Would having a spritesheet for each character mean too many vertex buffers? I guess I could pack multiple characters into a single VBO, and I guess it depends on the number of sprites.

I am more concerned with the second issue; what size should I make my quad/triangle strip that I render my sprite to? For example, say my protagonist is in a frame of animation with dimensions 20x20. My VBO would then be set up so that my quad is 20x20. But then, on the next frame, the sprite is 22x23. Now the sprite is larger than the size of the VBO. I wouldn't want to use glBufferSubData to edit the vertices every time the frame changes to be a different size! That would be crazy. One of my friends suggested that in my spritesheet, I should make each sprite for the protagonist the size of his biggest frame. Then the quad for that character would always be the same size. But I feel like there is an easier solution... I feel like I can still keep a spritesheet or texture atlas with a bunch of unevenly sized sprites and still be able to access them without having to change the vertices of the VBO all the time. Is this possible?

Thanks for any/all help. I've posted on TIGsource a few times now and there is always an awesome community willing to help.

-Ethan_Hall
Logged
zacaj
Level 3
***


void main()


View Profile WWW Email
« Reply #1 on: January 06, 2013, 09:55:34 AM »

Why not just upload the vertices for every frame into a single VBO?  You don't always have to draw the whole VBO, you can just select a subset of the vertices.   As to storing multiple characters on one sheet, I doubt it'll really matter.  If you have some tool to pack them all on there, then go ahead, but for a 2D game I really doubt you're going to run into any performance problems that could be helped by storing two characters in one VBO.

In fact, why not just have a single VBO, with a single quad stored in it, and then just scale it using the transformation matrix depending on which sprite you're drawing?  You wouldn't need to swap VBOs ever, and it would be the most efficient memory wise.  Since you're already uploading the transformation matrix to specify movement, it wouldn't even cost anything.
Logged

My twitter: @zacaj_

Quote from: mcc
Well let's just take a look at this "getting started" page and see--
Quote
Download and install cmake
Noooooooo
Polly
Level 3
***


View Profile
« Reply #2 on: January 06, 2013, 10:38:15 AM »

First of all, GL_QUADS are deprecated in OpenGL 3.3 Wink

Would having a spritesheet for each character mean too many vertex buffers?

No, you're going to run into texture memory problems before that would ever happen.

One of my friends suggested that in my spritesheet, I should make each sprite for the protagonist the size of his biggest frame.

In most circumstances that would be a waste of texture space / memory.

I feel like I can still keep a spritesheet or texture atlas with a bunch of unevenly sized sprites and still be able to access them without having to change the vertices of the VBO all the time. Is this possible?

Certainly, what you can do is create a static VBO for each spritesheet containing the vertices for the maximum number of instances you want to support for that sheet ( the sprites can simply be located at 0,0 and be 1,1 in size ). Provide the sheet data ( pixel coordinates ) to the vertex shader, pass the current frame for each sprite as a uniform, and render the visible amount by setting the count argument of glDrawArrays / glDrawElements. That way you never have to update your VBO's, don't have to re-allocate any memory, and keep the amount of data you have to upload to the GPU at a minimum.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic