Question

In my simulation snow particles as simple points are falling on a triangulated terrain. Now i want to draw points and keep them at the positions of the terrain texture, where particles are landing. How can i store hundreds or thousands of points. I don't want to store this points in a huge array.

is it possible to draw on a empty texture and reuse this texture in the next frame?

Was it helpful?

Solution

Using Frame buffer objects, you can render to a texture.

You need to accumulate changes on a texture you have 2 options:

  • If you need to sample fallen snow to compute resulting snow layer,you may use two textures and do what's called ping pong rendering

  • If you simply want's to accumulate snow without sampling already fallen pixels, you could omit glClear in your fbo rendering loop and use that texture as an accumulation buffer (link).

For doing ping-pong rendering, the steps are:

  • create two similar textures one source, on destination, swapped each frame.
  • the two are bond to the fbo as two color attachments, switching between them is done by a call to glDrawBuffers with destination colorAtachnmentX passed as argument.
  • you bind your source texture as uniform to your shader
  • render to fbo
  • use destination texture in your scene as accumulated snow.
  • swap dest and source texture for the new run.

To know if snowflake has hit the ground, you could supply falling snowflakes as a texture too with snow particle variables in place of color data... then knowing if snowflakes has hit the ground is tested against fragment world pos, but we're entering simulation world there...

OTHER TIPS

My suggestion is to not try to emulate reality so closely. Fake it.

Have the snow particles die when they pass the bottom of the screen. Then over time have snow accumulate on the surfaces. Snow on the surface doesn't have to equate to the exact particles that fell.

But if you really really want to do it that way...

First you're going to have to deal somehow with collisions between the snow and the ground, and then later between the snow and the snow on the ground. Which since you're not keeping them all in an array any longer would have to be based on the pixels of snow. That's a lot of glreadpixles. But that's not asked in this question so I'll assume you got that one covered.

j-p's answer isn't bad. But I would probably do it a little different:

You can do it with only one dynamic texture. Render like this:

  1. render backplate
  2. render accumulated snow texture on to the backplate
  3. render the snow particles.

Then any time the snow particle hits the ground or whatever it's colliding with do this:

  1. don't glclear it - so it will still contain all the previously fallen snow
  2. render the collided particles to the dynamic snow texture
  3. retire the snow particles that you drew on the dynamic texture - those are now "landed"

Then the next frame/cycle your dynamic accumulated snow texture will have the fallen snow on it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top