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_ref和fragment_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 "