简体   繁体   English

HLSL Pixel Shader 5.0 NdotL照明示例

[英]HLSL Pixel Shader 5.0 NdotL lighting sample

I am going through a tutorial for Pixel Shader 5.0 using directX. 我正在使用DirectX进行Pixel Shader 5.0的教程。 I was doing fine until I got to the fourth lesson, which has me create a PixelShader.hlsl file with the following code: 在进入第四节课之前,我一直表现不错,这使我可以使用以下代码创建PixelShader.hlsl文件:

 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. 文件末尾的for循环让我感到困惑。 Int i is being initialized to a constant string, and there is no closing parenthesis. 我正在将i初始化为常量字符串,并且没有右括号。 Similarly, the code within the for loop looks incorrect as well. 同样,for循环中的代码也看起来不正确。 The float4 struct appears to have a string "saturate" being appended to it, which is also a function? float4结构似乎附加了一个字符串“ saturate”,这也是一个函数吗? 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' 当我尝试在VS2012中编译代码时,出现以下错误:错误X3017:无法从'const string'隐式转换为'int'

I tried fixing the for loop code by changing it to 我尝试通过将其更改为来修复for循环代码

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 我在for循环中得到了一个不同的代码错误:错误X3022:标量,向量或矩阵均应

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. 事实证明,一个XML转换器会自动格式化代码示例并弄乱了一些东西。 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;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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