From da5b9c73065ad8335bc89d18cef913956e204c60 Mon Sep 17 00:00:00 2001 From: lanariel Date: Wed, 12 Feb 2014 12:05:27 +0100 Subject: [PATCH] Nomal Mapping For TA --- .../Shader/Passes/Gather/GatherPixel.hlsl | 43 ++++++++++++++++++- .../Shader/Passes/Gather/GatherVertex.hlsl | 1 + .../Shader/Passes/Gather/Header.hlsli | 2 +- .../Shader/Passes/Post/PostPass.hlsl | 2 +- Code/Tester/MainTest.cpp | 4 +- 5 files changed, 47 insertions(+), 5 deletions(-) diff --git a/Code/OysterGraphics/Shader/Passes/Gather/GatherPixel.hlsl b/Code/OysterGraphics/Shader/Passes/Gather/GatherPixel.hlsl index b0a2f40f..0fbf0b92 100644 --- a/Code/OysterGraphics/Shader/Passes/Gather/GatherPixel.hlsl +++ b/Code/OysterGraphics/Shader/Passes/Gather/GatherPixel.hlsl @@ -1,9 +1,50 @@ #include "Header.hlsli" + + +float3x3 cotangent_frame( float3 N, float3 p, float2 uv ) +{ + // get edge vectors of the pixel triangle + float3 dp1 = ddx( p ); + float3 dp2 = ddy( p ); + float2 duv1 = ddx( uv ); + float2 duv2 = ddy( uv ); + + // solve the linear system + float3 dp2perp = cross( dp2, N ); + float3 dp1perp = cross( N, dp1 ); + float3 T = dp2perp * duv1.x + dp1perp * duv2.x; + float3 B = dp2perp * duv1.y + dp1perp * duv2.y; + + // construct a scale-invariant frame + float invmax = 1/sqrt( max( dot(T,T), dot(B,B) ) ); + return float3x3( T * invmax, B * invmax, N ); +} + +float3 perturb_normal( float3 N, float3 V, float2 texcoord ) +{ + // assume N, the interpolated vertex normal and + // V, the view vector (vertex to eye) + float3 map = Normal.Sample(S1,texcoord).xyz; + map = map * 255./127. - 128./127.; +#ifdef WITH_NORMALMAP_2CHANNEL + map.z = sqrt( 1. - dot( map.xy, map.xy ) ); +#endif +#ifdef WITH_NORMALMAP_GREEN_UP + map.y = -map.y; +#endif + float3x3 TBN = cotangent_frame( N, -V, texcoord ); + return normalize( mul(transpose(TBN), map) ); +} + PixelOut main(VertexOut input) { PixelOut output; output.DiffuseGlow = Diffuse.Sample(S1, input.UV); - output.NormalSpec = float4(normalize(input.normal), Normal.Sample(S1,input.UV).w); + float3 normal = normalize(input.normal); + + normal = perturb_normal( normal, normalize(-input.ViewPos), input.UV ); + + output.NormalSpec = float4(normal, Normal.Sample(S1,input.UV).w); return output; } \ No newline at end of file diff --git a/Code/OysterGraphics/Shader/Passes/Gather/GatherVertex.hlsl b/Code/OysterGraphics/Shader/Passes/Gather/GatherVertex.hlsl index 4042c224..fd3da6b1 100644 --- a/Code/OysterGraphics/Shader/Passes/Gather/GatherVertex.hlsl +++ b/Code/OysterGraphics/Shader/Passes/Gather/GatherVertex.hlsl @@ -14,6 +14,7 @@ VertexOut main( VertexIn input ) input.normal = mul(boneTrans,float4(input.normal,1)).xyz * Animated + input.normal * int(1-Animated); output.pos = mul(WVP, float4(input.pos,1)); + output.ViewPos = mul(WV, float4(input.pos,1)); output.normal = mul(WV, float4(input.normal,0)).xyz; output.UV = input.UV; return output; diff --git a/Code/OysterGraphics/Shader/Passes/Gather/Header.hlsli b/Code/OysterGraphics/Shader/Passes/Gather/Header.hlsli index a313a649..14e0db5a 100644 --- a/Code/OysterGraphics/Shader/Passes/Gather/Header.hlsli +++ b/Code/OysterGraphics/Shader/Passes/Gather/Header.hlsli @@ -12,7 +12,7 @@ struct VertexIn struct VertexOut { float4 pos : SV_POSITION; - //float4 ViewPos : POSITION; + float4 ViewPos : POSITION; float2 UV : TEXCOORD; float3 normal : NORMAL; //float3 tangent : TANGENT; diff --git a/Code/OysterGraphics/Shader/Passes/Post/PostPass.hlsl b/Code/OysterGraphics/Shader/Passes/Post/PostPass.hlsl index 247efb42..53cfabb2 100644 --- a/Code/OysterGraphics/Shader/Passes/Post/PostPass.hlsl +++ b/Code/OysterGraphics/Shader/Passes/Post/PostPass.hlsl @@ -17,7 +17,7 @@ void main( uint3 DTid : SV_DispatchThreadID ) float4 Light = Diffuse[DTid.xy] + saturate(Specular[DTid.xy]); float4 Amb = float4(Ambient[DTid.xy/2].xyz * Ambient[DTid.xy/2].w, 0); float4 GUI; - uint2 index = DTid.xy/2 + uint2(Pixels.x/2,0); + uint2 index = DTid.xy/2 + uint2((uint)Pixels.x/(uint)2,0); float3 PostLight = Amb.xyz * AmbFactor; PostLight = PostLight + Light.xyz; GUI = float4(Ambient[index]); diff --git a/Code/Tester/MainTest.cpp b/Code/Tester/MainTest.cpp index 4462f060..baf63cb3 100644 --- a/Code/Tester/MainTest.cpp +++ b/Code/Tester/MainTest.cpp @@ -176,7 +176,7 @@ HRESULT InitDirect3D() m->WorldMatrix.m[2][2] = 0.00000005f; m2 = Oyster::Graphics::API::CreateModel(L"char_temporary.dan"); m2->WorldMatrix = Oyster::Math3D::OrientationMatrix(Oyster::Math::Float3::null,Oyster::Math::Float3(4,0,0),Oyster::Math::Float3::null); - Oyster::Graphics::API::PlayAnimation(m2, L"movement",false); + Oyster::Graphics::API::PlayAnimation(m2, L"movement"); t = Oyster::Graphics::API::CreateTexture(L"structure_corp_mdg.png"); t2 = Oyster::Graphics::API::CreateTexture(L"whiteGui.png"); @@ -233,7 +233,7 @@ HRESULT Render(float deltaTime) Oyster::Graphics::API::RenderModel(m2); Oyster::Graphics::API::StartGuiRender(); Oyster::Graphics::API::RenderGuiElement(t,Oyster::Math::Float2(0.5f,0.5f),Oyster::Math::Float2(1,1)); - //Oyster::Graphics::API::RenderGuiElement(t2,Oyster::Math::Float2(0.5f,0.1f),Oyster::Math::Float2(0.5f,0.1f),Oyster::Math::Float3(1,0,0)); + Oyster::Graphics::API::RenderGuiElement(t2,Oyster::Math::Float2(0.5f,0.1f),Oyster::Math::Float2(0.5f,0.1f),Oyster::Math::Float3(1,0,0)); Oyster::Graphics::API::StartTextRender(); std::wstring fps; float f = 1/deltaTime;