Jump to content

  • Log In with Google      Sign In   
  • Create Account

Opengl drawing many textures!


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
17 replies to this topic

#1 brunooo   Members   -  Reputation: 100

Like
0Likes
Like

Posted 11 September 2009 - 09:44 AM

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!

Sponsor:

#2 szecs   Members   -  Reputation: 2185

Like
0Likes
Like

Posted 11 September 2009 - 09:49 AM

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.

#3 Omega147   Members   -  Reputation: 532

Like
0Likes
Like

Posted 11 September 2009 - 09:51 AM

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!

#4 brunooo   Members   -  Reputation: 100

Like
0Likes
Like

Posted 11 September 2009 - 09:56 AM

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? :/

#5 Omega147   Members   -  Reputation: 532

Like
0Likes
Like

Posted 11 September 2009 - 10:16 AM

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.

#6 KulSeran   Members   -  Reputation: 2584

Like
0Likes
Like

Posted 11 September 2009 - 10:19 AM

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.

#7 brunooo   Members   -  Reputation: 100

Like
0Likes
Like

Posted 11 September 2009 - 03:02 PM

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

#8 szecs   Members   -  Reputation: 2185

Like
0Likes
Like

Posted 11 September 2009 - 06:02 PM

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

#9 Prefect   Members   -  Reputation: 373

Like
0Likes
Like

Posted 11 September 2009 - 09:35 PM

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.

#10 Nanoha   Members   -  Reputation: 300

Like
0Likes
Like

Posted 11 September 2009 - 11:46 PM

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[i].texture)
{
glEnd();
currentTexture = tiles[i].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.

#11 brunooo   Members   -  Reputation: 100

Like
0Likes
Like

Posted 12 September 2009 - 12:12 AM

Thank you, I will need to group the textures together to not bind them many times.

VBO's and Frame buffers will work on every computer with a gpu? Or it will only work on newer ones like I read that Shader only work in newer gpus (correct me if I am wrong). I want my game to run on my grandma computer with a very old gpu of 32 MB :) (At the moment it is working smoothly :D)

#12 szecs   Members   -  Reputation: 2185

Like
0Likes
Like

Posted 12 September 2009 - 12:50 AM

Why it is a wrong method I posted?
Nanoha's approach has one drawback: if the texture changes many times (for example checked pattern), it will mean no speed boost.
glBindTexture(GL_TEXTURE_2D, texture1);
glBegin(GL_QUADS);
for(int i = 0; i < numTiles; i++)
{
if( tiles[i].texture == texture1)
{ vertices/tex to draw quad (use actual vertices not translate)
}
}
glEnd();
glBindTexture(GL_TEXTURE_2D, texture2);
glBegin(GL_QUADS);
for(int i = 0; i < numTiles; i++)
{
if( tiles[i].texture == texture2)
{ vertices/tex to draw quad (use actual vertices not translate)
}
}
glEnd();
glBindTexture(GL_TEXTURE_2D, texture3);
.
.
.//or place it in a loop too.




This way, you have exactly the same number of binding/begin-end, as your types of textures. The extra time spent in the more loops is negligible compared to the other methods. I'm beginning to think that my posts are invisible, or maybe I became the legendary ABARABA?

#13 BertS   Members   -  Reputation: 124

Like
0Likes
Like

Posted 12 September 2009 - 01:33 AM

To Szecs: Nanoha's approach is cleaner and more general (ie no advance knowledge of tile textures needed). He also mentioned to sort the tiles based on texture, so a checkerboard will not cause needless state changes :)


#14 Nanoha   Members   -  Reputation: 300

Like
0Likes
Like

Posted 12 September 2009 - 01:58 AM

Quote:
Original post by szecsor maybe I became the legendary ABARABA?


Lol Szecs, I did mine based on yours :P so your certainly not invisible. Its yours just a little more general/expandable. Each tile should be sorted based on texture first. So if its a checkbord type texture all the tiles would be sorted white first, then all the black ones so only 2 texture changes will be done.

If there are 200 different textures then yes, there will have to be 200 different texture changes but thats a problem with any method.

#15 szecs   Members   -  Reputation: 2185

Like
0Likes
Like

Posted 12 September 2009 - 01:58 AM

I see, I overlooked the sorting thing. (So I am really like ABARABA)

#16 brunooo   Members   -  Reputation: 100

Like
0Likes
Like

Posted 12 September 2009 - 02:00 AM

BertS is right, Ive been thinking, I cant use glRotatef between glBegin()/glEnd(), can I?

#17 szecs   Members   -  Reputation: 2185

Like
0Likes
Like

Posted 12 September 2009 - 02:02 AM

Quote:
Original post by szecs
Texture binding and transformations aren't allowed between glBegin-glEnd
The same with state changes (glEnable/Disable)

#18 brunooo   Members   -  Reputation: 100

Like
0Likes
Like

Posted 12 September 2009 - 03:08 AM

Thanks again! I used stl map to group the textures with same texture object, and every loop I clear and refill it. I was not gaining performance than I profiled my program and found that the problem now is with stl maps :p, I will do something to not clear and refill it every time!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS