Jump to content
  • Advertisement
Sign in to follow this  
Juliean

[DX9]: SSAO not working

This topic is 2630 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

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

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;
};


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

r2j4y8za.jpg

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 hints!

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!