# 2D rendering using vertex arrays

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

## 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 on other sites
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? :)

##### 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 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 on other sites

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 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 on other sites
Quote:
 Original post by storageThanks 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

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

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634132
• Total Posts
3015731
×