Sign in to follow this  
lukesmith123

Normal map problem

Recommended Posts

lukesmith123    153
I tired to implement the normal mapping from the example in the DirectX sdk using the same shader and the same texture from the example on a different mesh.

It doesnt look the same when I render it however.

[url="http://imageshack.us/f/827/directxprogram201108131.png/"]http://imageshack.us/f/827/directxprogram201108131.png/[/url]

Although there is a specular highlight and a shadow it doesnt look anything like the example.

Also when I try to do parallax offset mapping from the same example it looks exactly the same accept the texture scrolls across the surface when you move the camera.

Has anyone else had anything like this happen before?

I'm generating the tangent/bitangent in the same way as the example does and my normals are stored in the .x file. I cant think what else could cause this to happen.

Share this post


Link to post
Share on other sites
lukesmith123    153
Sorry yeah heres the shader. I copied it from the sdk example so I figured the problem couldnt be in here but I cant understand where it could be coming from.

[code]

float4 g_materialAmbientColor; // Material's ambient color

float4 g_materialDiffuseColor; // Material's diffuse color

float4 g_materialSpecularColor; // Material's specular color

float g_fSpecularExponent; // Material's specular exponent

bool g_bAddSpecular; // Toggles rendering with specular or without


float3 g_LightDir; // Light's direction in world space

float4 g_LightDiffuse; // Light's diffuse color

float4 g_LightAmbient; // Light's ambient color

float4 g_vEye; // Camera's location

float g_fBaseTextureRepeat; // The tiling factor for base and normal map textures

float g_fHeightMapScale; // Describes the useful range of values for the height field

// Matrices:

float4x4 g_mWorld; // World matrix for object

float4x4 g_mWorldViewProjection; // World * View * Projection matrix

float4x4 g_mView; // View matrix

?

?

texture ModelTexture;

texture NormalTexture;

sampler2D textureSampler = sampler_state {

Texture = (ModelTexture);

MipFilter = Linear;

MinFilter = Linear;

MagFilter = Linear;

AddressU = Wrap;

AddressV = Wrap;

};

sampler2D textureSampler2 = sampler_state {

Texture = (NormalTexture);

MipFilter = Linear;

MinFilter = Linear;

MagFilter = Linear;

AddressU = Wrap;

AddressV = Wrap;

};

struct VertexShaderInput
{

float4 position : POSITION0;

float2 TexCoord : TEXCOORD0;

float3 normal : NORMAL;

float3 tangent : TANGENT;

float3 binormal : BINORMAL;

};

struct VertexShaderOutput

{

float4 position : POSITION0;

float2 TexCoord : TEXCOORD0;

float3 vLightTS : TEXCOORD1; // light vector in tangent space, denormalized

float3 vViewTS : TEXCOORD2; // view vector in tangent space, denormalized

float2 vParallaxOffsetTS : TEXCOORD3; // Parallax offset vector in tangent space

float3 vNormalWS : TEXCOORD4; // Normal vector in world space

float3 vViewWS : TEXCOORD5; // View vector in world space

};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)

{

VertexShaderOutput output;

// Transform and outputput input position

output.position = mul( input.position, g_mWorldViewProjection );



// Propagate texture coordinate through:

output.TexCoord = input.TexCoord;

// Transform the normal, tangent and binormal vectors from object space to homogeneous projection space:

float3 vNormalWS = mul(input.normal, (float3x3) g_mWorld );

float3 vTangentWS = mul( input.tangent, (float3x3) g_mWorld );

float3 vBinormalWS = mul( input.binormal, (float3x3) g_mWorld );

// Propagate the world space vertex normal through:

output.vNormalWS = vNormalWS;



vNormalWS = normalize( vNormalWS );

vTangentWS = normalize( vTangentWS );

vBinormalWS = normalize( vBinormalWS );



// Compute position in world space:

float4 vPositionWS = mul( input.position, g_mWorld );



// Compute and outputput the world view vector (unnormalized):

float3 vViewWS = g_vEye - vPositionWS;

output.vViewWS = vViewWS;

// Compute denormalized light vector in world space:

float3 vLightWS = g_LightDir;



// Normalize the light and view vectors and transform it to the tangent space:

float3x3 mWorldToTangent = float3x3( vTangentWS, vBinormalWS, vNormalWS );



// Propagate the view and the light vectors (in tangent space):

output.vLightTS = mul( vLightWS, mWorldToTangent );

output.vViewTS = mul( mWorldToTangent, vViewWS );



// Compute the ray direction for intersecting the height field profile with

// current view ray. See the above paper for derivation of this computation.



// Compute initial parallax displacement direction:

float2 vParallaxDirection = normalize( output.vViewTS.xy );



// The length of this vector determines the furthest amount of displacement:

float fLength = length( output.vViewTS );

float fParallaxLength = sqrt( fLength * fLength - output.vViewTS.z * output.vViewTS.z ) / output.vViewTS.z;



// Compute the actual reverse parallax displacement vector:

output.vParallaxOffsetTS = vParallaxDirection * fParallaxLength;



// Need to scale the amount of displacement to account for different height ranges

// in height maps. This is controlled by an artist-editable parameter:

output.vParallaxOffsetTS *= g_fHeightMapScale;

return output;

}

float4 ComputeIllumination( float2 texCoord, float3 vLightTS, float3 vViewTS, float fOcclusionShadow )

{

// Sample the normal from the normal map for the given texture sample:

float3 vNormalTS = normalize( tex2D( textureSampler2, texCoord ) * 2 - 1 );



// Sample base map:

float4 cBaseColor = tex2D( textureSampler, texCoord );



// Compute diffuse color component:

float3 vLightTSAdj = float3( vLightTS.x, -vLightTS.y, vLightTS.z );



float4 cDiffuse = saturate( dot( vNormalTS, vLightTSAdj )) * g_materialDiffuseColor;



// Compute the specular component if desired:

float4 cSpecular = 0;

if ( g_bAddSpecular )

{

float3 vReflectionTS = normalize( 2 * dot( vViewTS, vNormalTS ) * vNormalTS - vViewTS );



float fRdotL = saturate( dot( vReflectionTS, vLightTSAdj ));

cSpecular = saturate( pow( fRdotL, g_fSpecularExponent )) * g_materialSpecularColor;

}



// Composite the final color:

float4 cFinalColor = (( g_materialAmbientColor + cDiffuse ) * cBaseColor + cSpecular ) * fOcclusionShadow;



return cFinalColor;

}

?

float4 PS_NormalMap(VertexShaderOutput input) : COLOR

{

// Normalize the interpolated vectors:

float3 vViewTS = normalize( input.vViewTS );

float3 vLightTS = normalize( input.vLightTS );



float4 cResultColor = float4( 0, 0, 0, 1 );

// Start the current sample located at the input texture coordinate, which would correspond

// to computing a bump mapping result:

float2 texSample = input.TexCoord;

// Compute resulting color for the pixel:

cResultColor = ComputeIllumination( texSample, vLightTS, vViewTS, 1.0f );



return cResultColor;

}

float4 PS_ParallaxNormalMap(VertexShaderOutput input) : COLOR

{

const float sfHeightBias = 0.01;



// Normalize the interpolated vectors:

float3 vViewTS = normalize( input.vViewTS );

float3 vLightTS = normalize( input.vLightTS );



// Sample the height map at the current texture coordinate:

float fCurrentHeight = tex2D( textureSampler2, input.TexCoord).a;



// Scale and bias this height map value:

float fHeight = fCurrentHeight * g_fHeightMapScale + sfHeightBias;



// Perform offset limiting if desired:

fHeight /= vViewTS.z;



// Compute the offset vector for approximating parallax:

float2 texSample = input.TexCoord + vViewTS.xy * fHeight;



float4 cResultColor = float4( 0, 0, 0, 1 );

// Compute resulting color for the pixel:

cResultColor = ComputeIllumination( texSample, vLightTS, vViewTS, 1.0f );



return cResultColor;

}

?

?

technique BasicColor

{

pass Pass0

{

VertexShader = compile vs_2_0 VertexShaderFunction();

PixelShader = compile ps_2_0 PS_ParallaxNormalMap();

}

}

[/size][/font][/size][/font][/code]

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