Sign in to follow this  

SSAO strange problem

This topic is 2665 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

Ok I implemented SSAO as given here.
And here is the whole code:

struct Mtrl
{
float4 ambient;
float4 diffuse;
float4 spec;
float specPower;
};

struct DirLight
{
float4 ambient;
float4 diffuse;
float4 spec;
float3 dirW;
};

uniform extern float4x4 gView;
uniform extern float4x4 gWorld;
uniform extern float4x4 gWorldInv;
uniform extern float4x4 gWVP;
uniform extern Mtrl gMtrl;
uniform extern DirLight gLight;
uniform extern float3 gEyePosW;
uniform extern texture gTex;
uniform extern texture gNormalMap;
uniform extern float4x4 gView;
uniform extern float4x4 worldView;
uniform extern float4x4 worldViewInverseTranspose;

uniform extern texture sceneMaterialMap, sceneNormalMap, scenePositionXYMap, scenePositionZMap, gEnvMap;

sampler EnvMapS = sampler_state
{
Texture = <gEnvMap>;
MinFilter = Anisotropic;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};

sampler textureSampler = sampler_state
{
Texture = <sceneMaterialMap>;
MinFilter = ANISOTROPIC;
MaxAnisotropy = 16;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};

sampler normalSampler = sampler_state
{
Texture = <sceneNormalMap>;
MinFilter = ANISOTROPIC;
MaxAnisotropy = 16;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};

sampler positionXYSampler = sampler_state
{
Texture = <scenePositionXYMap>;
MinFilter = ANISOTROPIC;
MaxAnisotropy = 16;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};

sampler positionZSampler = sampler_state
{
Texture = <scenePositionZMap>;
MinFilter = ANISOTROPIC;
MaxAnisotropy = 16;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};

sampler TexS = sampler_state
{
Texture = <gTex>;
MinFilter = ANISOTROPIC;
MaxAnisotropy = 16;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};

sampler NormalMapS = sampler_state
{
Texture = <gNormalMap>;
MinFilter = ANISOTROPIC;
MaxAnisotropy = 16;
MagFilter = LINEAR;
MipFilter = LINEAR;
//AddressU = WRAP;
//AddressV = WRAP;
};

struct OutputVS
{
float4 vPos : POSITION0;
float2 vTex0 : TEXCOORD0;
float3 vWorldPos : TEXCOORD1;
float3 vWorldNrm : TEXCOORD2;

};


struct OutputPS
{
float4 vMaterial : COLOR0;
float4 vWorldNrm : COLOR1;
float4 vWorldPosXY : COLOR2;
float4 vWorldPosZ : COLOR3;
};



OutputVS NormalMapVS(float3 posL : POSITION0,
float3 tangentL : TANGENT0,
float3 binormalL : BINORMAL0,
float3 normalL : NORMAL0,
float2 tex0 : TEXCOORD0)
{

OutputVS outVS = (OutputVS)0;
float4x4 ViewSpace = mul(gWorld, gView);
outVS.vWorldPos = mul(float4(posL, 1), ViewSpace);
outVS.vPos = mul(float4(outVS.vWorldPos, 1), projectionMatrix);
outVS.vTex0 = tex0;
outVS.vWorldNrm = normalize(mul(float4(normalL, 0), worldViewInverseTranspose));

return outVS;
}

OutputPS NormalMapPS(OutputVS i) : COLOR
{
OutputPS o;
float4 vDiffuseMaterial = tex2D(TexS, i.vTex0);
clip(vDiffuseMaterial.a - 0.2f);

o.vMaterial.rgb = vDiffuseMaterial;
o.vMaterial.a = 1.0;

o.vWorldNrm.xyz = 0.5f * (i.vWorldNrm + 1.0);
o.vWorldNrm.w = 0.0;

o.vWorldPosXY = float4(i.vWorldPos.xy, 0, 0);
o.vWorldPosZ = float4(i.vWorldPos.z, 0, 0, 0);

return o;
}

float3 getPosition(in float2 texCoord)
{
float3 position;
position.xy = tex2D(positionXYSampler, texCoord).xy;
position.z = tex2D(positionZSampler, texCoord).x;
return position;
}

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 deferredLighting(float2 tex0:TEXCOORD0):COLOR
{
float3 diffuseMaterial = tex2D(textureSampler, tex0);
float3 worldNormal = 2*tex2D(normalSampler, tex0) - 1;

float3 worldPos;
worldPos = getPosition(tex0);
float2 rand = normalize(tex2D(randomNormalSampler, (1024 * 768) * tex0 / (64 * 64)).xy * 2.0f - 1.0f);

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


float2 vec[16] =
{
float2(0.355512, -0.709318 ),
float2(0.534186, 0.71511 ),
float2(-0.87866, 0.157139 ),
float2(0.140679, -0.475516 ),
float2(-0.0796121, 0.158842 ),
float2(-0.0759516, -0.101676 ),
float2(0.12493, -0.0223423 ),
float2(-0.0720074, 0.243395 ),
float2(-0.207641, 0.414286 ),
float2(-0.277332, -0.371262 ),
float2(0.63864, -0.114214 ),
float2(-0.184051, 0.622119),
float2(0.110007, -0.219486 ),
float2(0.235085, 0.314707 ),
float2(-0.290012, 0.0518654),
float2(0.0975089, -0.329594 )
};


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

ao += doAmbientOcclusion( tex0, coord1 * 0.25f, worldPos, worldNormal );
ao += doAmbientOcclusion( tex0, coord2 * 0.50f, worldPos, worldNormal );
ao += doAmbientOcclusion( tex0, coord1 * 0.75f, worldPos, worldNormal );
ao += doAmbientOcclusion( tex0, coord2, worldPos, worldNormal );
}

ao /= (float)iterations * 4.0f;


ao = (1 - ao);


float3 lightDir = mul(gLight.dirW , worldView);
lightDir = normalize(lightDir);
//float3 lightDir = normalize(gLight.dirW);
float4 color;
color.rgb = ao * diffuseMaterial +
saturate(dot(lightDir, worldNormal)) * gLight.diffuse * diffuseMaterial;

color.a = 1.0f;

return color;

}




But however I am having strange plus sign all over the screen:

And sometimes:


The area covered by the square formed at the intersection point of the 2 lines has the accurate SSAO. Please help.

[Edited by - ayush3017 on September 1, 2010 12:13:56 PM]

Share this post


Link to post
Share on other sites

This topic is 2665 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.

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