Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to render/animate 2d using openGL? [closed]

Tags:

c++

opengl

2d

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

I am now planning how I am going to handle animation and optimize rendering. I'm rendering using immediate mode which I was told was very slow. I go looking for alternatives and then there's a bunch of different ways, and it differs depending on how you handle the animations, so I thought I'd just ask what would be a good optimal way to do both.

For animations, I thought about using sequences of images stored in memory, but only testing with a 50 image sequence, memory jumped to like 200 mb, when it was 30 before. (There's no leak, it stays at 200, but if I have large levels, it looks like I'll be running out of memory). Would sprite sheets help in this case?

I was told the reason atlases or sprite sheets were used was because binding different textures was an expensive operation, so the only way this would work is by putting in all the texture I will use for level 1 for example in one huge texture so I'm only binding once. Is this possible dynamically?

Would using one gigantic PNG be better memory-wise than loading 50 PNG's?

Any help with either optimizing rendering or some information about the drawbacks of these animation techniques would be really appreciated!

like image 917
Omar Shehata Avatar asked Feb 21 '23 02:02

Omar Shehata


1 Answers

I was told was very slow

I recommend to ignore this. Unless you're going to use insane number of sprites (tens of thousands), your bottleneck will be reading data from textures, which is limited by videocard's memory transfer rate. In other words, if you have 1280x1024 viewport and you draw 1280x1024 texture that covers entire viewport (and every texel maps to screen pixel), your FPS will drop and VBOs won't help. Also immediate mode is easier to use for 2D than VBOs. So keep using immediate mode until you decide to use HUGE number of sprites.

I thought about using sequences of images stored in memory

You need to cram as many sprites as you can into single texture. Switching textures will produce big slowdown, so the best idea would be to use huge texture (4096x4096 or larger - 16384x16384 would be nice) and load all sprites you can into it. This can be done using glSubTexImage2D. Storing every frame separate texture will produce huge slowdown. Please note that you don't have to store sprites on disk merged into single texture. You can glue them together while loading.

Would using one gigantic PNG be better memory-wise than loading 50 PNG's?

PNG image file format that is stored on disk. It has nothing to do with OpenGL. Once you load the texture it is no longer a "png" - it is fully decompressed. So how you store image data on disk is pretty much irrelevant. It will affect image loading speed but texture performance/memory usage has nothing to do with image storage format. If you're running out of memory then you'll have to do something about it - implement game asset streaming, or adjust level's sprite/texture "budget".

like image 132
SigTerm Avatar answered Mar 03 '23 00:03

SigTerm