Jump to content
  • Advertisement
Sign in to follow this  
brunooo

OpenGL Opengl drawing many textures!

This topic is 3324 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

Hello! I've beeing playing around with opengl doing 2D things. I draw a single and big texture (the scenario) to the screen, with that I got around 400 fps, so far so good. Than I decided to make some tiling, assembling tiles together making the big scenario! I am using 16x16 pixels tiles. In a 800x768 I get 50x48 tiles. I have two types of tiles, a green (grass) and a brown one (dirty), I use them as textures. So if I want all scenario to have grass I would make:
for (int x = 0;x < 50;x++)
{
    for (int y = 0;y < 48;y++)
    {
        glPushMatrix();
        glTranslatef(x*16,y*16,0.0f);
        glBindTexture(GL_TEXTURE_2D,grass);
        glBegin(GL_QUADS);
        ... // Vertexes and Texcoords
        glEnd();
        glPopMatrix();
    }
}
I thought that this would work, and it does work! But the fps drops to around 220! Why drawing many tiny textures to compose a big image is slower than drawing a big texture? What should I do? Any suggestions? Thank you!

Share this post


Link to post
Share on other sites
Advertisement
The texture binding is slow. And you call it 48*50 times per frame.
You could speed it up by drawing the only the green tiles, than only the dirty tiles, so you only have to bind 2 times per frame, and the extra if statement wont slow it down too much.
Same goes for the glBegin-glEnd. If you discard the translation, by applying the proper coordinates to the glVertex calls, you can only have 2 glBegin-glEnd pairs.
I hope thah helps.

Share this post


Link to post
Share on other sites
Quote:
Original post by brunooo
I thought that this would work, and it does work! But the fps drops to around 220! Why drawing many tiny textures to compose a big image is slower than drawing a big texture?
What should I do? Any suggestions?

In general practice, it's best to reduce the total number of computations and operations you have executing within any sort of loop. One slowdown in your example set of for-loops is the repeated binding of the grass texture. When using one texture in many places, you only need to bind it once. Try binding the texture outside of your for-loops and see what sort of improvements you get there.

Edit: szecs beat me to it!

Share this post


Link to post
Share on other sites
This was fast o.O

szecs, what should I do to speed it up?

Omega147, I cant bind the texture outside of the loop, in this example I am theoretically binding the same texture several times, I should check in the position what tile image it is. When my scenario gets detailed what I am going to do? I could have 10 different textures and bind one and check where it needs to be draw and so on, but what if I have 1000 different textures? :/

Share this post


Link to post
Share on other sites
Quote:
Original post by brunooo
Omega147, I cant bind the texture outside of the loop, in this example I am theoretically binding the same texture several times, I should check in the position what tile image it is. When my scenario gets detailed what I am going to do? I could have 10 different textures and bind one and check where it needs to be draw and so on, but what if I have 1000 different textures? :/

In that case, you might look into GameDev's Framebuffer tutorials: Part 1 and Part 2. The second part deals with multiple textures, which will probably interest you the most.

Share this post


Link to post
Share on other sites
In pseudo code, this should be faster:

glPushMatrix
glTranslatef( worldX, worldY, 0 );
glBegin()
for ( textures )
bindTexture()
for ( ties with that texture )
//verts and tex coords
glEnd()
glPopMatrix()


Notice the push/pop happen outside the tile loop.
The textures are set outside the loop.
There is no push/pop/translate in the main loop. The vert coords need to be in world space instead of doing translatef calls for each one.

Share this post


Link to post
Share on other sites
Omega147, thanks, I will take a look on that!

KulSeran, indeed calling glTranslatef only once gave me a performance improvement! But binding textures between glBegin and glEnd does not work, it draw weird things :p

Share this post


Link to post
Share on other sites
Quote:
Original post by szecs
The texture binding is slow. And you call it 48*50 times per frame.
You could speed it up by drawing the only the green tiles, than only the dirty tiles, so you only have to bind 2 times per frame, and the extra if statement wont slow it down too much.
Same goes for the glBegin-glEnd. If you discard the translation, by applying the proper coordinates to the glVertex calls, you can only have 2 glBegin-glEnd pairs.
I hope thah helps.


The answer is in there

bind green texture
loop through tiles
{ if(green[x,y])
draw tile(x,y)
}
bind dirt texture
loop through tiles
{ if(not green[x,y])
draw tile(x,y)
}

In draw tile, you can do the push-transform-draw-pop, or which is better, draw with proper coordinates (as I told you in my prev. post), this way, you could put them outside the loop, just as you could with the binding.
Texture binding and transformations aren't allowed between glBegin-glEnd

Share this post


Link to post
Share on other sites
Quote:
Original post by brunooo
binding textures between glBegin and glEnd does not work, it draw weird things :p

That's because binding textures between Begin and End is an illegal operation.

Also note that getting Begin ... End type rendering to be fast is very difficult for the driver. You'd be better off using vertex arrays in VBOs.

Share this post


Link to post
Share on other sites
If you have a list of these tiles then you could sort them based on the texture (since the texture id is a number should be simple).

Then bind first texture, render tiles untill tile has a different texture, bind the next texture and so on.

Best case its only going to bind 1 texture, worst case (unavoidable anyway) its going to bind every tile.

Sorting shouldn't take too long (you could do it in another thread while doing some of your other game logic if needs be, but with 200 fps I doubt it will be noticable). Its nice because its expandable without any extra work.

[source lang = "cpp"]
SortTiles();

unsigned int currentTexture = -1;

glBegin(GL_QUADS);
for(int i = 0; i < numTiles; i++)
{
if(currentTexture != tiles.texture)
{
glEnd();
currentTexture = tiles.texture;
glBindTexture(GL_TEXTURE_2D, currentTexture);
glBegin(GL_QUADS);
}
// vertices/tex to draw quad (use actual vertices not translate)
}
glEnd();


the begin/end pairs there look pretty nasty but I can't see any reason why its wrong to do it. As they do match up.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!