shaders/cgshader/Simple.cg

shaders/cgshader/Simple.cg
/*********************************************************************NVMH3****
Path: NVSDK\Common\media\programs
File: simple.cg
Copyright NVIDIA Corporation 2002
TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
*AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS
BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS)
ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF NVIDIA HAS
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
Comments:
******************************************************************************/
// define inputs from application
struct appin
{
float4 Position : POSITION;
float4 Normal : NORMAL;
};
// define outputs from vertex shader
struct vertout
{
float4 HPosition : POSITION;
float4 Color0 : COLOR0;
};
vertout main(appin IN,
uniform float4x4 ModelViewProj,
uniform float4x4 ModelViewIT,
uniform float4 LightVec)
{
vertout OUT;
// transform vertex position into homogenous clip-space
OUT.HPosition = mul(ModelViewProj, IN.Position);
// transform normal from model-space to view-space
float3 normalVec = normalize(mul(ModelViewIT, IN.Normal).xyz);
// store normalized light vector
float3 lightVec = normalize(LightVec.xyz);
// calculate half angle vector
float3 eyeVec = float3(0.0, 0.0, 1.0);
float3 halfVec = normalize(lightVec + eyeVec);
// calculate diffuse component
float diffuse = dot(normalVec, lightVec);
// calculate specular component
float specular = dot(normalVec, halfVec);
// The lit() function is a handy function in the standard library that
// can be used to accelerate your lighting calculations.
//
// This function return a vector containing these values:
// result.x = 1.0;
// result.y = max(diffuse, 0);
// result.z = if (result.y > 0.0) then pow(specular, 32) else 0.0
// result.w = 1.0;
// Use the lit function to compute lighting vector from diffuse and
// specular values
float4 lighting = lit(diffuse, specular, 32);
// blue diffuse material
float3 diffuseMaterial = float3(0.0, 0.0, 1.0);
// white specular material
float3 specularMaterial = float3(1.0, 1.0, 1.0);
// combine diffuse and specular contributions and output final vertex color
OUT.Color0.rgb = lighting.y * diffuseMaterial + lighting.z * specularMaterial;
OUT.Color0.a = 1.0;
return OUT;
}