Sign in to follow this  
Juliean

[DX9]: SSAO not working

Recommended Posts

Hi,

I tried getting SSAO to work, however it is not really working. Here is what my implementation looks like:

[code]float random_size = 64.0f;
float g_sample_rad = 1.0f;
float g_intensity = 0.5f;
float g_scale = 0.1f;
float g_bias = 0.01f;

struct PS_INPUT_LIGHT
{
float2 vTex0 : TEXCOORD0;
};

VS_OUTPUT_LIGHT vsQuad(VS_INPUT_LIGHT i)
{
VS_OUTPUT_LIGHT o;

o.vPos = i.vPos;

o.vTex0 = i.vTex0;

return o;
}

float3 getPosition(in float2 uv)
{
return tex2D(ScenePositionSampler,uv).xyz;
}

float3 getNormal(in float2 uv)
{
return normalize(tex2D(SceneNormalSampler, uv).xyz * 2.0f - 1.0f);
}

float2 getRandom(in float2 uv)
{
return normalize(tex2D(RandomSampler, g_screen_size * uv / random_size).xy * 2.0f - 1.0f);
}

float doAmbientOcclusion(in float2 tcoord,in float2 uv, in float3 p, in float3 cnorm)
{
float3 diff = getPosition(tcoord + uv) - p;
const float3 v = normalize(diff);
const float d = length(diff)*g_scale;
return max(0.0,dot(cnorm,v)-g_bias)*(1.0/(1.0+d))*g_intensity;
}

float4 psAmbient(PS_INPUT_LIGHT i) : COLOR0
{
float4 vDiffuseMaterial;
vDiffuseMaterial.rgb = tex2D(SceneMaterialSampler, i.vTex0).rgb;
vDiffuseMaterial.rgb *= LightAmbient;

vDiffuseMaterial.a = 1.0;

const float2 vec[4] = {float2(1,0),float2(-1,0),
float2(0,1),float2(0,-1)};

float3 p = mul(float4(getPosition(i.vTex0), 1.0f), View);
float3 n = normalize(mul(float4(getNormal(i.vTex0), 0.0f), View));
float2 rand = getRandom(i.vTex0);

float ao = 0.0f;
float rad = g_sample_rad/p.z;

int iterations = 4;
for (int j = 0; j < iterations; ++j)
{
float2 coord1 = reflect(vec[j],rand)*rad;
float2 coord2 = float2(coord1.x*0.707 - coord1.y*0.707,
coord1.x*0.707 + coord1.y*0.707);

ao += doAmbientOcclusion(i.vTex0,coord1*0.25, p, n);
ao += doAmbientOcclusion(i.vTex0,coord2*0.5, p, n);
ao += doAmbientOcclusion(i.vTex0,coord1*0.75, p, n);
ao += doAmbientOcclusion(i.vTex0,coord2, p, n);
}
ao/=(float)iterations*4.0;

return vDiffuseMaterial * ao;
};[/code]

However, my results looks like this (depending on the view angel, sometimes I get only black..):

[URL=http://s1.directupload.net/file/d/2664/r2j4y8za_jpg.htm][IMG]http://s1.directupload.net/images/111001/temp/r2j4y8za.jpg[/IMG][/URL]

I really don't know whats going wrong. Can somebody help me? I was quite sure my implementation was valid, however the results aren't what I expected.. Thanks in advance for any tips!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this