Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: data/branches/Shader_HS18/programs/Cg/Common.cg @ 12216

Last change on this file since 12216 was 12115, checked in by wiesep, 6 years ago

Changed folder structure, deletet some unused files and cleaned up code

File size: 2.2 KB
Line 
1struct outPixel
2{
3  float4 colour  : COLOR0;
4};
5
6// General functions
7
8// Expand a range-compressed vector
9half4 expand(half4 v)
10{
11    return v * 2 - 1;
12}
13half3 expand(half3 v)
14{
15    return v * 2 - 1;
16}
17half2 expand(half2 v)
18{
19    return v * 2 - 1;
20}
21half1 expand(half1 v)
22{
23    return v * 2 - 1;
24}
25
26// Returns light direction from light position and vertex position
27half3 getLightDirection(float4 lightPosition, float4 position)
28{
29    // calculate tangent space light vector
30    // Get object space light direction
31    // Non-normalised since we'll do that in the fragment program anyway
32    return lightPosition.xyz -  (position.xyz * lightPosition.w);
33}
34
35// Returns eye direction from eye position and vertex position
36half3 getEyeDirection(float3 eyePosition, float4 position)
37{
38    return eyePosition - position.xyz;
39}
40
41// Returns a Tangent Binormal Normal matrix
42half3x3 getTBNMatrix(float3 tangent, float3 normal)
43{
44    // Calculate the binormal (NB we assume both normal and tangent are
45    // already normalised)
46    // NB looks like nvidia cross params are BACKWARDS to what you'd expect
47    // this equates to NxT, not TxN
48    float3 binormal = cross(tangent, normal);
49       
50    // Form a rotation matrix out of the vectors
51    return half3x3(tangent, binormal, normal);
52}
53
54// Returns expanded normal vector from texture map
55half3 getNormalMapVector(sampler2D normalMap, float2 uv)
56{
57    // get bump map vector, again expand from range-compressed
58    return expand(tex2D(normalMap, uv).xyz);
59}
60
61// Returns displacement vector from normalmaps alpha channel
62half getDisplacement(sampler2D normalMap, float2 uv, half scale, half bias)
63{
64    // get the height using the tex coords
65    half height = tex2D(normalMap, uv).a;
66
67    // calculate displacement   
68    return (height * scale) + bias;
69}
70
71// Returns a specular component from normal vector, specular colour and specular power
72half3 getSpecularComponent(float3 normal, float3 halfAngle, float3 specularcolour, float specularPower)
73{
74    return pow(saturate(dot(normal, halfAngle)), specularPower) * specularcolour;
75}
76
77// Returns a per-pixel lighted component from normal vector, lightdir and colour
78half3 getLightingComponent(float3 normal, float3 lightDir, float3 colour)
79{
80    return saturate(dot(normal, lightDir)) * colour;
81}
Note: See TracBrowser for help on using the repository browser.