终于到了Shader部分,不过书中只有vertex shader和pixel shader,呃。而且书中是以CG为例子的,不过都差不多。
写Shader需要在.material中先定义shader。
fragment_program MyFragmentShader1 cg
{
source Ogre3DBeginnersGuideShaders.cg
entry_point MyFragmentShader1
profiles ps_1_1 arbfp1
}
vertex_program MyVertexShader1 cg
{
source Ogre3DBeginnersGuideShaders.cg
entry_point MyVertexShader1
profiles vs_1_1 arbvp1
default_params
{
param_named_auto worldViewMatrix worldviewproj_matrix
}
}
material MyMaterial13
{
technique
{
pass
{
vertex_program_ref MyVertexShader1
{
}
fragment_program_ref MyFragmentShader1
{
}
}
}
}
然后要再创建一个.cg文件,写入shader函数。
void MyVertexShader1(float4 position : POSITION,
out float4 oPosition : POSITION,
uniform float4x4 worldViewMatrix)
{
oPosition = mul(worldViewMatrix, position);
}
void MyFragmentShader1(out float4 color : COLOR)
{
color = float4(1, 0, 1, 0);
}
大概的格式与步骤就是这样了,要是用就直接使用这个材质。
下面再贴一个复杂一点的,使用了纹理,并且实时改变了顶点位置。
fragment_program MyFragmentShader2 cg
{
source Ogre3DBeginnersGuideShaders.cg
entry_point MyFragmentShader2
profiles ps_1_1 arbfp1
}
vertex_program MyVertexShader5 cg
{
source Ogre3DBeginnersGuideShaders.cg
entry_point MyVertexShader5
profiles vs_1_1 arbvp1
default_params
{
param_named_auto worldViewMatrix worldviewproj_matrix
param_named_auto pulseTime time
}
}
material MyMaterial17
{
technique
{
pass
{
vertex_program_ref MyVertexShader5
{
}
fragment_program_ref MyFragmentShader2
{
}
texture_unit
{
texture terr_rock6.jpg
}
}
}
}
对应的CG文件
void MyFragmentShader2(float2 uv : TEXCOORD0,
out float4 color : COLOR,
uniform sampler2D texture)
{
color = tex2D(texture, uv);
}
void MyVertexShader5(uniform float pulseTime,
float4 position : POSITION,
out float4 oPosition : POSITION,
float2 uv : TEXCOORD0,
out float2 oUv : TEXCOORD0,
uniform float4x4 worldViewMatrix)
{
oPosition = mul(worldViewMatrix, position);
oPosition.x *= (2 + sin(pulseTime));
oUv = uv;
}