Best way to render / animation 2d using openGL?

So, I am making a game in C ++. And I'm at a fork in the road.

Now I plan how I will process the animation and optimize the rendering. I use the immediate mode that I was told about was very slow. I am looking for alternatives, and then a bunch of different ways, and it differs depending on how you handle the animation, so I thought I would just ask what would be a good optimal way for both.

For animations, I thought about using sequences of images stored in memory, but only testing with 50 sequences of images, the memory jumped to 200 mb when she was 30 years old. (There is no leak, it stays at 200, but if I have high levels, it looks like I will run out of memory). Did sprite sheets help in this case?

I was told that atlases or sprite sheets were used because snapping different textures was an expensive operation, so the only way this would work is to paste the whole texture that I will use for level 1, for example, in one huge texture so I only I bind once. Is this possible dynamically?

Will using one giant PNG be better than loading 50 PNGs?

Any help with optimizing the rendering or some information about the flaws of these animation methods would really be appreciated!

+4
source share
2 answers

I was told it is very slow

I recommend ignoring this. If you are not going to use an insane amount of sprites (tens of thousands), your bottleneck will read data from textures, which is limited by the video card transmission speed. In other words, if you have a 1280x1024 viewport and you draw a 1280x1024 texture that spans the entire viewport (and each texel map displays the screen pixel), your FPS will drop, and VBOs will not help. Also, instant mode is easier to use for 2D than VBOs. Therefore, continue to use immediate mode until you decide to use a HUGE amount of sprites.

I was thinking about using sequences of images stored in memory

You need to squeeze as many sprites as you can into one texture. Switching textures will lead to a significant slowdown, so the best idea would be to use a huge texture (4096x4096 or more - 16384x16384 would be nice) and load all the sprites into it. This can be done using glSubTexImage2D . Saving each texture in a separate frame will lead to a significant slowdown. Please note that you do not need to store sprites on disk, combined into one texture. You can glue them together at boot time.

Will using one giant PNG be better than loading 50 PNGs?

A PNG image file format that is stored on disk. It has nothing to do with OpenGL. Once you load a texture, it is no longer a “png” - it is completely unpacked. So, how you store image data on disk is largely irrelevant. This will affect the image loading speed, but texture performance / memory usage has nothing to do with the image storage format. If you run out of memory, you will have to do something - implement streams of game resources or adjust the "budget" of the sprite / texture level.

+4
source

Use VBO instead of immediate mode: http://www.opengl.org/wiki/Vertex_Buffer_Object

Sprite sheets have two advantages, first of all, as you noticed, you only need to snap one texture. As for memory, sheets give you the advantage of continuity in memory (without fragmentation), but the actual amount of memory will not be less. Downloading one large .png will, of course, be much faster in terms of download speed compared to 50 smaller ones. One thing you should know is that you can free the actual memory of the image after you load it into the openGL texture. Otherwise, you have this in RAM and GPU, which does not make much sense.

Another thing you can do is use various animation techniques such as keyframes or skeletal animation (also a valid 2D approach) to move sprites instead of saving an extra image / sprite for each state. This will certainly reduce the use of GPUs and RAM!

Hope this helps!

+4
source

Source: https://habr.com/ru/post/1402181/


All Articles