Frage zu Shader für Diffuses Licht
-
Guten Morgen,
ich bin im Internet auf ein Tutorial für Pixel- und Vertexshader (HLSL, XNA) für diffuses Licht gestoßen. Bin Anfänger auf dem Gebiet.
Warum wird im Vertexshader (s.u.) die Normale des betroffenen Knotens nicht wie der Knoten selbst auch mit der matWorldViewProj-Matrix (Das Produkt aus der Welt-, Sicht- und Projektionsmatrix ) multipliziert?
Stattdessen wird er mit der inversen Weltmatrix multipliziert. Also warum wird die Normale an eine andere Position transformiert als der dazugehörige Knoten?
Oder habe ich das mit den Transformationen sowieso komplett missverstanden?Die nichtssagende Erklärung des Autors ist folgende:
..., but in addition, we have the InverseWorld matrix that is used to calculate a correct normal related to the world matrix,...
Danke für eure Anteilnahme.
// Global variables // Can be accessed from outside the shader, using Effect->Parameters["key"] where key = variable name float4x4 matWorldViewProj; float4x4 matInverseWorld; float4 vLightDirection; struct OUT { float4 Pos: POSITION; float3 L: TEXCOORD0; float3 N: TEXCOORD1; }; OUT VertexShader( float4 Pos: POSITION, float3 N: NORMAL ) { OUT Out = (OUT) 0; Out.Pos = mul(Pos, matWorldViewProj); Out.L = normalize(vLightDirection); Out.N = normalize(mul(matInverseWorld, N)); return Out; } float4 PixelShader(float3 L: TEXCOORD0, float3 N: TEXCOORD1) : COLOR { float Ai = 0.8f; float4 Ac = float4(0.075, 0.075, 0.2, 1.0); float Di = 1.0f; float4 Dc = float4(1.0, 1.0, 1.0, 1.0); return Ai * Ac + Di * Dc * saturate(dot(L,N)); } technique DiffuseLight { pass P0 { VertexShader = compile vs_1_1 VertexShader(); PixelShader = compile ps_1_1 PixelShader(); } }
-
Schätzungsweise, weil man Normalen anders transformieren muss als Punkte und Richtungen. Erster Treffer bei Googlesuche nach "transform normals": http://www.unknownroad.com/rtfm/graphics/rt_normals.html
-
Ah Danke,
klar auf Normalenvektoren (Richtungsvektoren) kann man nicht einfach dieselbe Transformationsmatrix wie für Punkte (Ortsvektoren) anwenden. Sie könnten bei einer Sklaierung verfälscht werden. Also nimmt man die transponierte inverse Matrix. Hoffe so hab ichs richtig verstanden.
Schönen Sonntag noch