📄 phongtexturebumppixelshader.cg
字号:
//===========================================================================
//= This fragment shader follows the parameters indicated in the VitralSDK =
//= shader programming guidelines, as documented in the file =
//= ./doc/shaderStandards.txt inside the main VitralSDK distribution =
//= directory. Please refer to that file to get a clear understanding of =
//= variable names and common programming patterns used. =
//= Note that any variable name or registry assigment change could make the =
//= shader incompatible with main VitralSDK shader framework! =
//===========================================================================
/**
Note that first sampler2D is by default associated with default OpenGL
texture map (the one currently binded). Aditional sampler2D maps can
be explicity specified using the cgGLEnableTextureParameter function.
Remember that in Cg, the `lit` function gives:
clampedTerms.y = (NdolL > 0)?NdolL:0.0;
clampedTerms.z = (NdolL > 0 && NdotH > 0)?pow(NdotH, phongExponent):0.0;
and is recommended to use it as can give access to optimized operations in
the GPU.
*/
half4 main(uniform sampler2D textureMap,
uniform sampler2D normalMap,
uniform half3 lightColor,
uniform half3 phongExponent,
uniform half withTexture,
in float3 PView : TEXCOORD0,
in half3 NView : TEXCOORD1,
in half2 uvTextureCoordinate : TEXCOORD2,
in half3 cameraPositionView : TEXCOORD3,
in half3 lightPositionView : TEXCOORD4,
in half3 Ka : TEXCOORD5,
in half3 TView : TEXCOORD6,
in half3 BView : TEXCOORD7,
in half3 Kd : COLOR0,
in half3 Ks : COLOR1) : COLOR
{
//-----------------------------------------------------------------------
// Prepare basic illumination model variables.
half3 N = normalize(NView);
half3 Ps = normalize(TView);
half3 Pt = normalize(BView);
half3 L = normalize(lightPositionView - PView);
half3 V = normalize(cameraPositionView - PView);
half3 H = normalize(L + V);
//-----------------------------------------------------------------------
// Evaluation of Blinn's wrinkle surface model for normal perturbation.
// Note that Blinn's equation is not here, because this is a different
// formulation based on previous normal/tangent/binormal vector transform
// obtained from vertex shader.
half Bu = ((half)tex2D(normalMap, uvTextureCoordinate).x) * 2 - 1;
half Bv = ((half)tex2D(normalMap, uvTextureCoordinate).y) * 2 - 1;
N = N + (Bu*Ps + Bv*Pt);
N = normalize(N);
//-----------------------------------------------------------------------
half NdotL = dot(N, L);
half NdotH = dot(N, H);
half3 clampedTerms = lit(NdotL, NdotH, phongExponent);
// Apply illumination model's equations
half3 diffuseComponent;
diffuseComponent = Kd * clampedTerms.y;
if ( withTexture > 0.0 ) {
diffuseComponent = diffuseComponent *
(half3)tex2D(textureMap, uvTextureCoordinate).xyz;
}
half3 specularComponent = Ks * clampedTerms.z;
half3 finalColor =
Ka + lightColor * (diffuseComponent + specularComponent);
//-----------------------------------------------------------------------
// Return final color, setting alpha value to 1
return half4(finalColor, 1);
}
//===========================================================================
//= EOF =
//===========================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -