Snow accumulation

Hi all
I would like to implement the nvidia snow accumulation program in GLSL
The method consists in rendering the scene into a depthmap from the sky and looking down with a 2D ortho frustum (1st pass). Then using this depthmap and compare depth componant and identify the fragment that will be “snowed” (2nd pass). It really looks like the shadow map method.

Well I tried to implement this with shaders, but it just does’nt work :eek:

Here is the fragment shader for the 1st pass, it creates the depth map

void main(void)
{
  float z = gl_FragCoord.z / gl_FragCoord.w;
  gl_FragColor = 1.0 - z;
}

Then the vertex shader for the 2nd pass

uniform mat4 mViewFromSky;
uniform mat4 mProjFromSky;
varying vec4 texCoord;

void main(void)
{
  mat4 bias = mat4(0.5, 0.0, 0.0, 0.0,
                  0.0, 0.5, 0.0, 0.0,
		  0.0, 0.0, 0.5, 0.0,
		  0.5, 0.5, 0.5, 1.0);
  texCoord = bias * mViewFromSky * mProjFromSky * gl_Vertex;
  texCoord = texCoord / texCoord.w;
  gl_Position = ftransform();
}

mViewFromSky is a up located looking down camera model view matrix (like the light model view matrix for a shadowmap)
mProjFromSky is a 2D ortho camera projection matrix

Then finally the 2nd pass fragment shader:

uniform sampler2DShadow depthMap;
varying vec4 texCoord;
void main(void)
{
  vec4 color = ComputeFragment();

  float depthMapDepth = shadow2DProj(depthMap, texCoord).x;
  if(depthMapDepth == 0.0)
    color = vec4(1,1,1,1);

  gl_FragColor = color;
}

My idea is to use shadow2D GLSL function to know if the fragment is “snowed” or not.

My problem is that all my object are white now …

I think my problem may come from the texCoord computing, but I really do not know how to compute it.

Any help much appreciated …

thanks

Cool idea. Have a link to the original code handy?

For starters it looks like you’re comparing a post projective z to a linear z computed in your first pass (or not).

Your second pass order of transformations strikes me a bit fishy. Try swapping view and proj.

(shadow2DProj is deprecated…)

Firstable thanks a lot for your answer

You can find the nvidia orignal code on this page (look for snow accumulation) http://http.download.nvidia.com/developer/SDK/Individual_Samples/samples.html

Here is the whitepaper : http://http.download.nvidia.com/develope…ccumulation.pdf

Well, I swapped proj and view matrix, you were right, I have now some results.
But the depthmap is not projected correctly…

here is the new shaders
1st pass fragment shader (depthmap calculation):

void main(void)
{
  float z = gl_FragCoord.z;
  gl_FragColor = 1.0 - z;
}

2nd pass vertex shader

uniform mat4 mViewFromSky;
uniform mat4 mProjFromSky;
varying vec4 texCoord;
void main(void)
{
  mat4 bias = mat4(0.5, 0.0, 0.0, 0.0,
                  0.0, 0.5, 0.0, 0.0,
		  0.0, 0.0, 0.5, 0.0,
		  0.5, 0.5, 0.5, 1.0);
  texCoord = bias * mProjFromSky * mViewFromSky * gl_Vertex;
  gl_Position = ftransform();
}

and 2nd pass fragment shader

uniform sampler2DShadow depthMap;
varying vec4 texCoord;
void main(void)
{
  vec4 color = ComputeFragment();

  float depthMapDepth = shadow2D(depthMap, texCoord).x;
  if(depthMapDepth == 0.0)
    color = vec4(1,1,1,1);

  gl_FragColor = color;
}

(no more shadow2DProj, you right)
As you can see in my screenshots, the depthmap is not correctly projected on my object, have no idea why. I have checked mViewFromSky and mProjFromSky, they are correct, they contain my 2nd camera model view and projection matrix.
Have you got another brilliant idea ? ^^

here is the depthmap, view from my 2nd camera, (rendering to texture)

here my 2nd pass shader result

the depthmap is projected on the both sides of the cow.

Ok I think I finaly did it !

I am using OpenSceneGraph for my application (maybe you have recognize the OSG’s cow…)

I think my problem is that OSG space is different from OpenGL classic space.
In OSG, Z axis is up, Y is on -Z, and X is the same. My bad, I didn’t think about it.

So I need to rotate the model view matrix with a -PI/2 angle on the X axis before passing it to my shader.

Now the shadow is correctly projected on my scene. I can go on with the snow accumulation method.

Hi again
My snow accumulation shaders are now working

But I would like to make a snow trail on the snow receivers surface (my plane) when an animated object (the little sphere) is moving over it (hope I am clear enought …)

Does anyone have any idea of how doing that with shaders ?
Is it possible ?

thx

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.