0
\$\begingroup\$

So as a part of learning OpenGL, I've now decided to try and be a bit more creative with shaders, as part of a practice game I'm making using C/OpenGL.

I'm completely new when it comes to working with shaders, so I'm still trying to get used to the thought process behind creating something with it. To this extend, here's what I want to attempt.

As a part of the gameplay, the entire background of the game consists of a white, plain, 2D texture. The white texture should slowly animate black circles into it over time, until finally the entire texture is completely black.

I've considered some different approaches to the problem, and would like to know which one will be the most practical when it comes to shaders.

Using framebuffers: As I've understood from looking around the internet, you can't really, say, apply certain colors to a specific area of fragments in your fragment shader (like creating a black circle), and then save the state of those fragments for the next frame. Instead, I would have to apply the effect, and render the texture to a framebuffer and then re-use it again the next frame. I could then continuously do this slowly add black circles to the texture. The problem is, is this really too much work to do for something simple as this? Or would it be fast enough? As far as I've seen, framebuffers are usually used to post-process effects for entire scene frames. This would not be for the entire scene, only for the background. The advantage of this approach would be that it's relatively simply to work with as you're only working on one texture.

Creating individual circle objects with own fragment shaders: This was another approach I thought about doing. Basically, instead of having one shader work on the entire background texture, I would simply spawn textured quads that each individually run a fragment shader that just turns their texture black over time using a simple percentage value input as a uniform. The disadvantage of this solution is that you make it a bit more complex by having to handle more objects at runtime, instead of simply working with one texture. More vertices would also have to be handled when rendering.

Of these two approaches, I don't know really what would be a good solution to run with. I would like to use the framebuffers approach but I don't know if I would "abuse" the technique with this kind of problem or not.

Or perhaps there's another way of doing this that I've completely missed because I'm new to shaders, in that case other suggestions would be welcome!

Thank you all for your help :)

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

It sounds like what you called the framebuffer approach would work fine. You'd just have an framebuffer that would be modified a little each frame, gradually accumulating the summed result of all the circles. That's a bit different from how framebuffers are often used - clearing them and re-rendering them from scratch each frame - but that's no big deal.

The second approach is similar to how a particle system would work. It would also work fine as long as you don't have millions of circles, but you're right that it does require more code infrastructure and has more moving parts, so to speak. (For learning purposes, it might be a good exercise to implement the second approach at some point, as it could serve as an introduction to dynamic vertex buffers and instancing.)

\$\endgroup\$
  • \$\begingroup\$ Thank you so much for the response, it is much appreciated! :) If you have the time, I would love to just quickly ask if you have any recommendations when having to learn about shaders and how to use them? :) I want to be as practical as possible, but it's always great having some learning material by hand as support :) Graphics programming is mostly completely new to me, but I'm falling in love with it and want to continue it. At the same time it seems very scary and overwhelming at first :) But I'm fighting my way through it. \$\endgroup\$ – CodingBeagle Aug 13 '13 at 21:40
  • 2
    \$\begingroup\$ There are plenty of OpenGL/shader tutorials out there. One I often recommend is Learning Modern 3D Graphics Programming; another is opengl-tutorial.org. Also, if you don't mind spending a bit of cash, it's well worth picking up a good book like Real-Time Rendering or Computer Graphics: Principles and Practice. \$\endgroup\$ – Nathan Reed Aug 13 '13 at 22:05
  • \$\begingroup\$ Great! Thank you so much for your time :) I will take a look at it all. Actually I'm already following the arcsynthesis book, it's great :) \$\endgroup\$ – CodingBeagle Aug 13 '13 at 22:26

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.