简体   繁体   中英

HLSL Pixel Shader 5.0 NdotL lighting sample

I am going through a tutorial for Pixel Shader 5.0 using directX. I was doing fine until I got to the fourth lesson, which has me create a PixelShader.hlsl file with the following code:

 cbuffer ConstantBuffer : register( b0 )
  {
    matrix World;
    matrix View;
    matrix Proj;

    float4 vLightDir[2];    
    float4 vLightColor[2];
    float4 vOutputColor;
  }

  struct PS_INPUT
  {
    float4 Pos : SV_POSITION;
    float3 Norm : TEXCOORD0;
  };

  //--------------------------------------------------------------------------------------
  // Pixel Shader
  //--------------------------------------------------------------------------------------
  float4 main( PS_INPUT input) : SV_Target
  {
    float4 finalColor = 0;

    //do NdotL lighting for 2 lights
    for(int i=0; i<2; i=""
    {
        finalColor += "saturate"( dot=""( (float3)vLightDir=""[i=""],input.Norm="") * vLightColor=""[i=""
    }
    finalColor.a = "1"
    return="" finalColor="";
  }

The for loop at the end of the file has me confused. Int i is being initialized to a constant string, and there is no closing parenthesis. Similarly, the code within the for loop looks incorrect as well. The float4 struct appears to have a string "saturate" being appended to it, which is also a function? A function without proper closing braces and no semicolon no less.

It looks like there are some typos in the sample code, and I'm struggling to fix them. Any thoughts as to what should be written here?

When I try to compile the code in VS2012, I get the following error: error X3017: cannot implicitly convert from 'const string' to 'int'

I tried fixing the for loop code by changing it to

for(int i=0, i<2, i++)

and I get a different error for the code within the for loop: error X3022: scalar, vector, or matrix expected

Thanks in advance for your help

I finally got a response from the site author. Turns out an XML converter auto-formatted the code sample and messed some things up. Here is the proper code:

    cbuffer ConstantBuffer : register( b0 )
  {
    matrix World;
    matrix View;
    matrix Proj;

    float4 vLightDir[2];    
    float4 vLightColor[2];
    float4 vOutputColor;
  }

  struct PS_INPUT
  {
    float4 Pos : SV_POSITION;
    float3 Norm : TEXCOORD0;
  };

  //--------------------------------------------------------------------------------------
  // Pixel Shader
  //--------------------------------------------------------------------------------------
  float4 main( PS_INPUT input) : SV_Target
  {
    float4 finalColor = 0;

    //do NdotL lighting for 2 lights
    for(int i=0; i<2; i++)
    {
        finalColor += saturate( dot( (float3)vLightDir[i],input.Norm) * vLightColor[i] ); 
    }
    finalColor.a = 1;
    return finalColor;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM