4
\$\begingroup\$

I tried to dig into OpenGL a bit to use in 2D games (mainly because of shaders). There are multiple tutorials out there showcasing how to display and blend textures, and that's fine. But i haven't found a single one that would teach how to display multiple textures at once. Now, I know that one of the popular methods is using a texture atlas and then, provided you have a well-done atlas, it's just a matter of assigning proper UVs to objects using the texture in vertices sent to a buffer array. However, how would one go about it if he didn't want to use a texture atlas. Modern OpenGL offers at least 70something texture units if my memory serves me right, that's something, but then you would have to bind different textures (and possibly end up with an outrageous amount of samplers) which would waste a lot of GPU's computing power. Let's consider the following screen, assuming that everything, including clouds, is a separate object using it's own texture

Screen

How, in pseudocode, should I handle displaying that "the proper way" without a texture atlas to get an effect like that? I experimented with rebinding textures etc. but none of that helped. Also, how shaders should look like because i don't think that having a sampler for every object is a way to go as well. Maybe something with a TEXTURE_2D_ARRAY, but if yes then how to use it properly? Again, what I'm looking at is just a list of steps i need to take to get an image like that written in a pseudocode. Thanks in advance!

\$\endgroup\$
2
  • \$\begingroup\$ Array textures work when all textures have the same size. But for textures with different sizes a texture atlas is the best solution. \$\endgroup\$
    – dari
    Feb 21, 2016 at 20:43
  • \$\begingroup\$ There is very famous lecture on modern OpenGL - Approaching zero overhead, one of the speakers talk could be helpful to answer you question. There is even source code available somewhere online if I am not mistaken. \$\endgroup\$
    – wondra
    Feb 21, 2016 at 21:13

2 Answers 2

2
\$\begingroup\$

For simple 2D games such as this it generally really isn't a big issue to keep rebinding individual textures. If it is starting to become an issue, you could do some simple sorting so that the renderer will bind each required texture once and draw all tiles that use that texture before continuing on to the next, effectively removing all redundant texture switches. Since most maps use one texture very often (look in the photo, most tiles in game are really just that generic blue sky texture and the ground textures).

Samplers aren't an issue either. Really, in the case above, you could create one sampler object and keep it bound.

I'm not really sure why you don't want to use a texture atlas, as they're pretty efficient, and require little-no texture switches. The only issue I can see would be floating point precision issues, which wouldn't really be a problem unless you have a ridiculous amount of tiles in one texture.

Here is a wiki page describing array textures: https://www.opengl.org/wiki/Array_Texture

\$\endgroup\$
4
  • \$\begingroup\$ Obviously the screenshot here is just to help visualize what I'm trying to say. Assuming everything has it's own texture, it would contain around 10 and that indeed shouldn't cause too much of a problem when rebinding. But it doesn't change the fact that according to what i read it is bad practice. In a very small program that works for ten seconds you can easily go away with not releasing resources assigned with new, but it doesn't change the fact that it is just bad and should be avoided, regardless if it is visible or not, i think. \$\endgroup\$
    – Quit
    Feb 21, 2016 at 21:04
  • \$\begingroup\$ I could probably me more helpful if you tell me why you can't use texture atlases. Why don't you want to use them? \$\endgroup\$
    – Avilius
    Feb 21, 2016 at 21:19
  • \$\begingroup\$ Ability to change texture of 1 object w/o affecting others, easier 'bookkeeping' which object uses what, no bleed issues to name a few. But the biggest reason is that I know it is possible to avoid an atlas and I just want to know how to do it properly \$\endgroup\$
    – Quit
    Feb 21, 2016 at 22:05
  • \$\begingroup\$ Your first issue can be solved easily by just changing the coordinates in the atlas for an individual tile. Thats an issue with higher level logic. I don't know what you mean by doing it "properly," as everyone's situation is different. Of course it's possible to not use a texture atlas, but your rendering surely is going to be much slower than without. The point of a texture atlas was to solve the problems that having individual textures caused. Bleeding is a drawback. By using an atlas you're trading efficiency for accuracy. You can solve it by padding textures. \$\endgroup\$
    – Avilius
    Feb 25, 2016 at 4:17
0
\$\begingroup\$

You just use separate texture images and use texture id's to associate them with your 2d models (or tiles not sure how that works in 2d opengl, i'm used to 3d opengl). And in your render pass you loop over each model and before drawing them you just bind the correct texture unit to the sampler.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .