Problem mit Lighting in Shadern (HLSL)



  • Hi,

    ich habe folgendes vor:

    Eine Lichtquelle soll immer genau in den Bildschirm strahlen, also immer aus Richtung des Betrachters. Dafür habe ich eine Lichtquelle als Konstante im Shader definiert (als Richtungsvector). Die Matrizen und die Kamera-Position werden über die Semantics von der Applikation gesetzt.

    Leider verhält sich das alles nicht so, wie gewünscht, sondern der specular-Anteil des Lichtes wird sonderbar transformiert. Die Diffuse Komponente scheint richtig zu sein. Daher vermute ich, dass der View-Vector irgendwie falsch ist.

    Kann sich jemand den Shader-Code ansehen und mir sagen, ob dieser IN SICH korrekt ist? Dann müsste ich den Fehler in der Applikation suchen. Ich weiß wirklich nicht mehr weiter.

    // -------------------------------------------------------------
    // variables that are provided by the application
    // -------------------------------------------------------------
    matrix matWorldView     : MAT_WORLD_VIEW;
    matrix matWorldViewProj : MAT_WORLD_VIEW_PROJ;
    
    vector cameraPos        : VEC_CAMERA_POS_OBJ;
    vector k_a              : MATERIAL_AMB;
    vector k_d              : MATERIAL_DIFF;
    vector k_s              : MATERIAL_SPEC;
    float  spec_pow = 64
    
    // external light-source and it's intensity
    float3 Light = { 0.0f, 0.0f, -1.0f };
    float4 I_a   = { 0.1f, 0.1f, 0.2f, 1.0f };    // ambient
    float4 I_d   = { 0.6f, 0.6f, 0.8f, 1.0f };    // diffuse
    float4 I_s   = { 1.0f, 1.0f, 1.0f, 1.0f };    // specular
    
    // -------------------------------------------------------------
    // Input channels
    // -------------------------------------------------------------
    struct VS_INPUT
    {
        float4 Pos  : POSITION;
        float3 Norm : NORMAL;
    };
    
    // -------------------------------------------------------------
    // Output channels
    // -------------------------------------------------------------
    struct VS_OUTPUT
    {
        float4 Pos          : POSITION;
        float3 Normal       : TEXCOORD0;
        float3 LightDir     : TEXCOORD1;
        float3 ViewDir      : TEXCOORD2;
    };
    
    // -------------------------------------------------------------
    // vertex shader
    // -------------------------------------------------------------
    VS_OUTPUT VS(const VS_INPUT In)
    {
        VS_OUTPUT Out = (VS_OUTPUT) 0; 
    
        // compute the view vector (both in object space)
        float3 ViewVec = normalize(cameraPos - In.Pos);
        // transform vectors in view space
        float3 N  = normalize(mul(normalize(In.Norm), (float3x3)matWorldView));
        float3 V  = normalize(mul(ViewVec, (float3x3)matWorldView));
    
        // output the values
        Out.Pos       = mul(In.Pos, matWorldViewProj);
        Out.Normal    = N;
        Out.LightDir  = Light;
        Out.ViewDir   = V;
    
        return Out;
    }
    
    // -------------------------------------------------------------
    // Pixel Shader
    // -------------------------------------------------------------
    float4 PS(VS_OUTPUT In) : COLOR
    {
        // normalize incomming vectors
        float3 N  = normalize(In.Normal);
        float3 L  = normalize(In.LightDir);
        float3 V  = normalize(In.ViewDir);
    
        // diffuse component
        float  n_l   = saturate(dot(N, L));
        float4 diff  = I_a + I_d * n_l; 
    
        // specular component
        float3 H      = normalize(L + V);
        float4 spec  = I_s * pow(dot(N, H), spec_pow);
    
        return spec + diff;
    }
    

    Vielen vielen Dank schonmal.


Anmelden zum Antworten