0
\$\begingroup\$

I am new to OpenGL and game dev, I have been to courses and trying to learing everything about this. My task is to implement a Texture Atlas in a 2D tile based game (very similar to Tibia) using OpenGL.

Now the game have the sprite sheet loaded into memory and on each tile the program generates an image, load to a texture, bind and make a draw call. You can imagine how ineficcient it is. So what I have to do is to load this sprite sheet as a Texture Atlas and get the advantage that it has.

The problem is that in the first step while generating an image, the program makes some combinations on the sprites. For example, the character has an outfit and player changes its colors (boots, pants, t-shirt, hair) to do this there is a sprite for the base outfit, and a sprite for each element of the outfit, so the program apply a mask on color for each element and combine this five sprites into an image, then make the process that I mentioned above. This is an example that happens with outfit, but it happens to other things too, so this is not a special case with outfit only.

My question is: is there any way to make this combinations using OpenGL elements? Combining this multiple textures in my Texture Atlas into one draft and do it in a single draw call?

\$\endgroup\$
1
\$\begingroup\$

I personally would simply not include stuff like your character sprite into your texture atlas. There is no need to actually go so far and combine you whole game art into a single atlas. If you look at what the big 3D Engines do, they also have unique textures(even multiple ones) for each Mesh and are still able to render quite a significant amount of them. And they need to render a whole 3D-Model instead of a single rectangle for a sprite.

What shreds your performance is having a seperate texture for each tile. Depending on your tilesize that number can get actually get pretty insane. For a simple 100x100 map you would already end up with 10.000 unique draw calls.

On the game i am currently working on we have one texture atlas that contains every possible tile. We then create our vertices with the corresponding texture coordinates based on the output of our procedural map generator and store them into a single vertex buffer. That way we are able to render the entire tilemap with a single texture bind and draw call.

The actual actors like your player character or the enemies each have their own texture and are then rendered on top of the tilemap afterwards. For everything that appears more than once on the screen (for example a background tree) we use instanced drawing.

This basicly boils down to one draw + texture bind call for the map and one for each unique game object in your scene. Unless you need to have a gigantic amount of different objects in your scene that are not part of the tilemap your performance will fine. A somewhat modern GPU should be easily able to handle at least a few hundred drawcalls before you start running into problems.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.