/*******************************************/ /* Shader for normal mapped geoemtry */ /*******************************************/ // input struct vtxin { float4 position: POSITION; float4 normal: NORMAL; float4 color: COLOR0; float4 texCoord: TEXCOORD0; }; // vtx output struct vtxout { float4 position: POSITION; float4 texCoord: TEXCOORD0; // eye position float4 eyePos: TEXCOORD1; float4 normal: TEXCOORD2; float4 worldPos: TEXCOORD3; float4 oldWorldPos: TEXCOORD4; float4 tangent: TEXCOORD5; float4 bitangent: TEXCOORD6; }; // fragment input struct fragin { float4 texCoord: TEXCOORD0; float4 winPos: WPOS; // eye position float4 eyePos: TEXCOORD1; float4 normal: TEXCOORD2; float4 worldPos: TEXCOORD3; float4 oldWorldPos: TEXCOORD4; float4 tangent: TEXCOORD5; float4 bitangent: TEXCOORD6; }; struct pixel { float4 color: COLOR0; float3 normal: COLOR1; float3 offsVec: COLOR2; }; #pragma position_invariant vtx vtxout vtx(vtxin IN, uniform float4x4 viewMatrix, uniform float4x4 modelMatrix, uniform float4x4 oldModelMatrix ) { vtxout OUT; float4 tangent = float4(IN.color.xyz * 2.0f - 1.0f, 1.0f); //float4 tangent = IN.color; //float4 tangent = IN.color; OUT.texCoord = IN.texCoord; // transform the vertex position into eye space OUT.eyePos = mul(glstate.matrix.modelview[0], IN.position); // transform the vertex position into post projection space OUT.position = mul(glstate.matrix.mvp, IN.position); // transform the old vertex position into world space OUT.worldPos = mul(modelMatrix, IN.position); // transform the old vertex position into world space OUT.oldWorldPos = mul(oldModelMatrix, IN.position); // the normal has to be correctly transformed with the inverse transpose // convert to view space OUT.normal = mul(glstate.matrix.invtrans.modelview[0], IN.normal); OUT.tangent = mul(glstate.matrix.invtrans.modelview[0], tangent); OUT.bitangent = float4(cross(OUT.tangent.xyz, OUT.normal.xyz), 1); //OUT.bitangent = float4(cross(OUT.normal.xyz, OUT.tangent.xyz), 1); return OUT; } /******************************************************************/ /* Shader computing the MRT output for normal mapped geometry */ /******************************************************************/ pixel frag(fragin IN, uniform float4x4 viewMatrix, uniform sampler2D normalMap: TEXUNIT0) { pixel pix; // hack: use comination of emmisive + diffuse (emmisive used as constant ambient term) pix.color = glstate.material.diffuse + glstate.material.emission; // eye space depth pix.color.w = length(IN.eyePos.xyz); // the scene entity id //pix.id = glstate.fog.color.xyz; // the offset to the world pos from old frame pix.offsVec = IN.oldWorldPos.xyz - IN.worldPos.xyz; // compute tanget space to world space trafo => // transform basis vectors back into world space by multiplying with inverse view. // since transforming normal with T means to // multiply with the inverse transpose of T, we multiple with // Transp(Inv(Inv(view))) = Transp(view) float3 normal = normalize(mul(transpose(viewMatrix), IN.normal).xyz); float3 tangent = normalize(mul(transpose(viewMatrix), IN.tangent).xyz); float3 bitangent = normalize(mul(transpose(viewMatrix), IN.bitangent).xyz); // compute tangent space - world space transform which is the transpose, // as it is the inverse of a rotation matrix //float3x3 tangToWorldTrafo = transpose(float3x3(tangent, bitangent, normal)); //float3x3 tangToWorldTrafo = transpose(float3x3(bitangent, tangent, normal)); //float3x3 tangToWorldTrafo = transpose(float3x3(tangent, normal, bitangent)); //float3x3 tangToWorldTrafo = transpose(float3x3(normal, bitangent, tangent)); float3x3 tangToWorldTrafo = transpose(float3x3(normal, tangent, bitangent)); //const float3 tangentSpaceNorm = tex2Dlod(normalMap, float4(IN.texCoord.xy, 0, 0)).xyz; //const float3 tangentSpaceNorm = float3(0, 1, 0); const float3 tangentSpaceNorm = float3(0, 0, 1); pix.normal = mul(tangToWorldTrafo, tangentSpaceNorm); //pix.color.xyz = float3(abs(dot(tangent.xyz, normal.xyz))); //pix.color.xyz = float3(abs(dot(bitangent.xyz, tangent.xyz))); //pix.color.xyz = float3(abs(dot(normalize(IN.normal.xyz), normalize(IN.tangent.xyz)))); //pix.color.xyz = float3(abs(dot(IN.normal.xyz, IN.tangent.xyz))); //pix.color.xyz = normal.xyz; //pix.color.xyz = IN.tangent.xyz; return pix; } pixel fragtex(fragin IN, uniform sampler2D tex: TEXUNIT0, uniform float4x4 viewMatrix, uniform sampler2D normalMap: TEXUNIT1 ) { float4 texColor = tex2D(tex, IN.texCoord.xy); // account for alpha blending if (texColor.w < 0.5f) discard; pixel pix; // save color in first render target // hack: use combination of emmisive + diffuse (emmisive used as constant ambient term) pix.color = (glstate.material.emission + glstate.material.diffuse) * texColor; // compute eye linear depth pix.color.w = length(IN.eyePos.xyz); // the scene entity id //pix.id = glstate.fog.color.xyz; // the offset to the world pos from old frame pix.offsVec = IN.oldWorldPos.xyz - IN.worldPos.xyz; // compute tanget space in world space => transform basis vectors back into world space by // multiplying with inverse view. since transforming normal with T means to // multiply with the inverse transpose of T, we multiple with // Transp(Inv(Inv(view))) = Transp(view) float3 normal = mul(transpose(viewMatrix), IN.normal).xyz; float3 tangent = mul(transpose(viewMatrix), IN.tangent).xyz; float3 bitangent = mul(transpose(viewMatrix), IN.bitangent).xyz; // compute tangent space - world space transform which is the transpose, // as it is the inverse of a rotation matrix float3x3 tangToWorldTrafo = transpose(float3x3(IN.tangent.xyz, IN.bitangent.xyz, IN.normal.xyz)); const float3 tangentSpaceNorm = tex2Dlod(normalMap, float4(IN.texCoord.xy, 0, 0)).xyz; pix.normal = mul(tangToWorldTrafo, tangentSpaceNorm); return pix; }