HLSL:高级着色语言。微软开发、用于在DirectX中编写在GPU上运行的程序的高级着色语言,与Cg语言非常相似。

目录

OGRE中使用HLSL

HLSL着色器在Ogre中的使用非常像其它着色语言(比如Cg,GLSL)的使用情况,但是有一点小小的不同,我将针对这个不同点作出解释。首先,使用着色器的材质的声明应该像如下材质声明的例子那样:

material Test13/RockWall

{

        technique

        {

               pass

               {

                       vertex_program_ref Deferred_nm_vs

                       {

                       }

                       fragment_program_ref Deferred_nm_ps

                       {

                       }

 

                       texture_unit

                       {

                               // sampler s0

                                // ...

                       }

                       texture_unit

                       {

                               // sampler s1

                                // ...

                       }             

                }

    }

}

vertex_program_ref 指向一个顶点程序声明,fragment_program_ref指向一个片断程序声明。这些声明可以放在.program 脚本文件中或者直接放在使用它的.material脚本中.在声明中指明了源代码(HLSL代码)的文件,目标配置(target)和入口函数(entry_point),以及默认参数(default_params):

vertex_program Deferred_vs hlsl

{

        source Deferred_vs.hlsl

        target vs_1_1

        entry_point main

       

        default_params

        {

               param_named_auto worldView worldview_matrix

               param_named_auto worldViewProj worldviewproj_matrix

        }

}

fragment_program Deferred_ps hlsl

{

        source Deferred_ps.hlsl

        target ps_2_0

        entry_point main

        default_params

        {

               param_named specularity float 0.0

        }

}

<!--[if !supportLists]-->1.      <!--[endif]-->source 指明了程序源代码文件的名字

<!--[if !supportLists]-->2.      <!--[endif]-->entry_point 指明了为每个顶点或者片断调用的着色主函数,通常被命名为main.

<!--[if !supportLists]-->3.      <!--[endif]-->target 表示顶点着色器或者像素着色器将要被编译成的版本。对于片断程序来说,可以取ps_1_1, ps_1_4, ps_2_0, ps_2_x, ps_3_0 或者ps_3_x (可以看看Declaring Vertex and Fragment Programs ).对于顶点程序来说,可以取vs_1_1, vs_2_0, vs_2_x or vs_3_0。建议尽可能尝试取最低的版本要求来实现你要实现的功能,因为这样通常可以运行得更快,以及被大多数显卡支持。

<!--[if !supportLists]-->4.      <!--[endif]-->default_params 指明了着色器中的默认参数值。这些值可以在vertex_program_reffragment_program_ref块中重新赋值

参数

命名参数可以声明在HLSL程序的全局范围内:

float4x4 worldViewProj;

float4x4 world;

float4x4 worldView;

Ogre将保证这些参数包含它们在.program或者.material脚本中通过param_named或者param_named_auto赋予的值。

采样器和纹理单元

.hlsl片断程序中,你可以通过特定的语法来定义采样器,这样保证Ogre正确识别出那一个采样器对应的是哪一个文理单元:

sampler Tex0: register(s0);

sampler Tex1: register(s1);

register(s0) 指向第一个文理单元,register(s1)指向第二个文理单元,并且可以依此类推。

顶点着色器

一个最小的接收POSITION, NORMAL TEXCOORD0 顶点属性,为片断着色器输出POSITION TEXCOORD0 信息的顶点着色器程序如下:

struct VS_OUTPUT {

   float4 pos: POSITION;

   float2 texCoord0: TEXCOORD0;

};

 

float4x4 worldViewProj;

 

VS_OUTPUT main(

        float4 Pos: POSITION,

        float3 normal: NORMAL,

        float2 texCoord0: TEXCOORD0

)

{

   VS_OUTPUT Out;

  

   Out.pos = mul(worldViewProj, Pos);

   Out.texCoord0 = texCoord0;

 

   return Out;

}

片断着色器

一个最小的对输入坐标纹理进行采样并输出采样后的颜色值的片断着色器程序如下:

sampler Tex0: register(s0);

 

float4 main(float4 texCoord0: TEXCOORD0): COLOR0

{

        return tex2D(Tex0, texCoord0);

}

Tangent-vectors

If you need tangent-vectors in your HLSL-shaders, you can ask Ogre to give them. However, you will have to use the semantic TEXCOORD for that. So the input structure for your vertex-shader may look like this:

struct a2v

{

     float4 position : POSITION0;

     float3 normal : NORMAL;

     float2 tex : TEXCOORD0;

     float3 tangent : TEXCOORD1;

};

To make sure Ogre can give you these tangents, either tell the exporter you use for your models to generate them, or tell Ogre to generate them by using the following code:

unsigned short src, dest;

if (!pMesh->suggestTangentVectorBuildParams(src, dest))

{

    pMesh->buildTangentVectors(src, dest);

}

Note that Ogre does not generate any binormals. If you need those to, you can calculate them yourself by simply taking the cross product of the tangent and the normal in the vertex shader.

Links

OGRE manual: Declaring Vertex and Fragment programs

Microsoft HLSL reference

Retrieved from "http://www.ogre3d.org/wiki/index.php/HLSL"