Jump to content
  • Advertisement
Sign in to follow this  
storage

2D rendering using vertex arrays

This topic is 4763 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How would my 2D tile engine benefit from using vertex arrays for rendering textued quads, instead of using glBegin(GL_QUADS); and glEnd(); blocks? So, I'm wondering where I can find more information on rendering in 2D using vertex arrays. I've been searching Google, I've found some, but they were too complicated, and not commented enough for me. I'm also wondering how to make use of the alphachannel in PNG/TGA image files. I'm currently replacing all of the pixels of a specified color with pixels with alpha set to 0. One last thing, how could my tile engine benefit from a framebuffer? Thanks in advance, storage :)

Share this post


Link to post
Share on other sites
Advertisement
I found this code:

void display ( void )
{
// Draw scene 10000 times
static int count = 0;
if ( ++count == 10000 )
exit( 0 );

// "tile"-size
float x_step = width / 20;
float y_step = height / 16;

glClear ( GL_COLOR_BUFFER_BIT );

static float vertices[20*16*4*2];
int n = 0;

// Setup vertex array
for ( int x = 0; x < 20; ++x )
{
for ( int y = 0; y < 16; ++y )
{
vertices[n++] = x * x_step;
vertices[n++] = y * y_step;

vertices[n++] = x * x_step;
vertices[n++] = (y+1) * y_step;

vertices[n++] = (x+1) * x_step;
vertices[n++] = (y+1) * y_step;

vertices[n++] = (x+1) * x_step;
vertices[n++] = y * y_step;
}
}

glVertexPointer( 2, GL_FLOAT, 0, vertices );
glDrawArrays( GL_QUADS, 0, 20*16 );

glutSwapBuffers ( );
}




Posted by Luctus in this thread, and I pretty much think I understand it, but, when I try the code, it renders nothing, even if I bind my texture and enable GL_TEXTURE_2D and remove the glClear(); from his code.

How do I make it render my textures, instead of nothing? :)

Thanks in advance!

Share this post


Link to post
Share on other sites
I don't think vertex arrays are going to be any faster in a 2d tiling engine. You would only be able to render one tile at a time anyway, which defeats the purpose of vertex arrays. A way around this is if you use one massive texture, but that may be more trouble than it is worth. I think immediate mode(glBegin) is fast enough to ddraw in a 2d tiling engine.

Share this post


Link to post
Share on other sites
not strictly true, how many games do you see which have a unique tile for EVERY quad? Setup properly and with the proper application of batching you wont have to draw one quad at a time.

Infact, off the top of my head I'd use a system with a premade VA (or VBO) which covers the screen and then build a series of index arrays to draw the correct sections as needed.

Granted, I dont know if it would be a win over the immediate mode solution (however you can apply the same technic to it to batch the texture state changes), but its certainly possible todo it.

Share this post


Link to post
Share on other sites
Thanks for your opinions :)

I'm planning on using the same engine in more than one game, and also not only in tile-based games, but also in platform games similar to Super Mario etc.

So I was planning on implementing a layer-sorting kind of thing, where the textures are bunched together, then sorted, and then rendered in different layers, for example backgrounds on the first layer, then the "platforms", which the characters are standing on, and then the characters on a third layer.

Should I use vertex arrays for this?

And I'm also still wondering how I can make the textures show up using the code I posted earlier, just so I can do some testing :).

Share this post


Link to post
Share on other sites
Quote:
Original post by _the_phantom_
not strictly true, how many games do you see which have a unique tile for EVERY quad? Setup properly and with the proper application of batching you wont have to draw one quad at a time.

Infact, off the top of my head I'd use a system with a premade VA (or VBO) which covers the screen and then build a series of index arrays to draw the correct sections as needed.

Granted, I dont know if it would be a win over the immediate mode solution (however you can apply the same technic to it to batch the texture state changes), but its certainly possible todo it.


Agreed. My point though is that in the end, I think that the difference in speed between the simple way(immediate mode) and the complex way(vertex arrays/batching) would be a small amount in my opinion not worthy of messing with it. You could batch same texture tiles and use those, or put a few in a bigger tile, but to make an in general working case for an engine, I think that the simple way is better. If you batch a set of tiles according to a level, customizing it to the game, that is fine, but it may not be reusable for another game, whereas the simple method, works no matter what the tiles because there is no special batching etc... Anyway, that is what I think. I really like reusable code, as long as it is my own.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by storage
Thanks for your opinions :)

I'm planning on using the same engine in more than one game, and also not only in tile-based games, but also in platform games similar to Super Mario etc.

So I was planning on implementing a layer-sorting kind of thing, where the textures are bunched together, then sorted, and then rendered in different layers, for example backgrounds on the first layer, then the "platforms", which the characters are standing on, and then the characters on a third layer.

Should I use vertex arrays for this?

And I'm also still wondering how I can make the textures show up using the code I posted earlier, just so I can do some testing :).

You don’t have to do any sorting for layers, just use the depth buffer and depth testing capabilities that are in every video card out there. Just simply use

glTranslatef(0,0,Depth_of_quad);
//render quad;

The ones further in the back will automatically be obscured by closer ones.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!