@MJP:
Thanks, I'll keep tha in mind later on. I already managed to solve the problem anyway but I will use swizzles in the future.
[quote="PropheticEdge]Keep at it, King, you're making some good progress![/quote]
Thanks! I'm actually totally eager to learn more about somewhat advanced 3d programming techniques and improve my knowledge.
Due to this I tried, right after the normal mapping, to tried to add support for parallax mapping (as it looked even more impressive and in theory shouldn't be harder to include). Well, at least I got the shader running, but it doesn't do anything.. the parallax-effect simply isn't applied. Everything looks the same as without it. There eventually is some scrolling of the texture if I rotate an object, but thats all. I've tried 3 different ways to do it and all of them failed. The first one was from the mircsoft directx-sdk-examples. It used some multipass, but didn't do much. Then I tried out an solution I found in an older thread on gamedev.net, which used multiple passes too but without a loop like the sdk. Still no reaction.
My currenct solution is from
here. Thats the pixelshader now with normalmapping and parallax:
// Pixel shader input structure
struct PS_INPUT
{
float2 Texture : TEXCOORD0;
float3 Light : TEXCOORD1;
float3 View : TEXCOORD2;
//float2 Parallax : TEXCOORD3;
float3 Normal : TEXCOORD3;
};
// Pixel shader output structure
struct PS_OUTPUT
{
float4 Color : COLOR0;
};
sampler2D Tex0;
sampler2D BumpMap : register(s1);
PS_OUTPUT ps_main( in PS_INPUT In )
{
PS_OUTPUT Out;
float2 scaleBias = (0.1, -0.02);
float3 v = normalize(In.View);
float height = tex2D(BumpMap, In.Texture).a;
height = height * scaleBias.x + scaleBias.y;
float2 newTexture = In.Texture + (height * v.xy);
float4 bumpNormal = 2 * (tex2D(BumpMap, newTexture) - 0.5);
float4 color = tex2D(Tex0, newTexture);
float3 LightDir = normalize(In.Light);
float3 ViewDir = normalize(In.View);
float3 diff = saturate(dot(bumpNormal, LightDir));
float3 shadow = saturate(4 * diff);
float3 Reflect = normalize(2 * diff * bumpNormal - LightDir);
float3 spec = min(pow(saturate(dot(Reflect, ViewDir)), 3), color.w)*0.5;
float4 Color;
Color.xyz = (0.5 * color + shadow * (color * diff + spec));
Color.w = 1.0;
Out.Color = Color;
return Out;
}
Although I use the new texture coordinates, I get no effect. I quessed it could have something to do with wrong tangents, but after all the normal mapping is working so this isn't very likely. Anyway, here my vertex shader:
struct VS_INPUT
{
float4 Position : POSITION;
float3 Normal : NORMAL;
float2 Texture : TEXCOORD0;
float3 Tangent : TANGENT0;
};
struct VS_OUTPUT
{
float4 Position :POSITION;
float2 Texture : TEXCOORD0;
float3 Light : TEXCOORD1;
float3 View : TEXCOORD2;
//float2 Parallax :TEXCOORD3;
float3 Normal : TEXCOORD3;
};
float4x4 WorldViewProj;
float4x4 World;
float3 Eye;
float3 LightDir;
VS_OUTPUT vs_main( in VS_INPUT In )
{
VS_OUTPUT Out = (VS_OUTPUT)0;
Out.Position = mul(In.Position, WorldViewProj); // transform Position
float3x3 worldToTangentSpace;
worldToTangentSpace[0] = mul(In.Tangent, World);
worldToTangentSpace[1] = mul(cross(In.Tangent, In.Normal), World);
worldToTangentSpace[2] = mul(In.Normal, World);
Out.Texture = In.Texture.xy;
Out.Light.xyz = mul(worldToTangentSpace, LightDir); // L
float4 PosWorld = mul(In.Position, World);
float3 Viewer = Eye - PosWorld; // V
Out.View = mul(worldToTangentSpace, Viewer);
Out.Normal = normalize(In.Normal);
return Out;
}
[s]Does anyone notice whats wrong with these shaders? I don't see anything, because after trying 3 different algorythms which all just caused the graphics to stay the same (not even any glitches at all; except for my custom generated terrain which lacks normals and tangets - a good argument why wrong tangents shouldn't be the problem) and the fact the lighting, which pretty much uses the same tangents and view, I don't know anything to solve that anymore.. maybe someone else?
EDIT: Oh yeah heightmap is alright too because I'm using the original graphics from the microsoft sdk parallax example, which I compiled and tested..
[/s]EDIT2: Nevermind, I eventually figured out that I was just using a different vertex shader the whole time, so tangent space was actually not right set oO. So now I get an effect, but not the one I actually wanted to have. Look at the attachment. Its more like some sort of distrobe-effect, while the textures themself move around on the mesh while moving it.
So, does anyone know whats going on here? Once again I tested all 3 algorythms I found and they all produced the same results. Do I calculate the tangents right? On mesh creation I call D3DXComputeNormals(lpMesh, NULL);
D3DXComputeTangent(lpMesh, 0, 0, 0, 0, NULL);
Is this fine? If yes, are there maybe any calculation mistakes? I really can't figure it out..Normal mapping btw. is with the right vertex shader now working without any problems and is looking absolutly awesome!