//shading: phong //lighting model: blinn //lighting type: directional // -------------------------------------------------------------------------------------------------- // PARAMETERS: // -------------------------------------------------------------------------------------------------- //transforms float4x4 tW: WORLD; //the models world matrix float4x4 tV: VIEW; //view matrix as set via Renderer (EX9) float4x4 tWV: WORLDVIEW; float4x4 tWVP: WORLDVIEWPROJECTION; float4x4 tP: PROJECTION; //projection matrix as set via Renderer (EX9) //light properties float3 lDir = {0, -5, 2}; //light direction in world space float4 lAmb : COLOR = {0.15, 0.15, 0.15, 1}; float4 lDiff : COLOR = {0.85, 0.85, 0.85, 1}; float4 lSpec : COLOR = {0.35, 0.35, 0.35, 1}; float lPower = 25.0; //shininess of specular highlight //texture texture Tex ; sampler Samp = sampler_state //sampler for doing the texture-lookup { Texture = (Tex); //apply a texture to the sampler MipFilter = LINEAR; //sampler states MinFilter = LINEAR; MagFilter = LINEAR; }; float4x4 tTex: TEXTUREMATRIX ; float4x4 tColor ; struct vs2ps { float4 PosWVP: POSITION; float4 TexCd : TEXCOORD0; float3 LightDirV: TEXCOORD1; float3 NormV: TEXCOORD2; float3 ViewDirV: TEXCOORD3; }; // -------------------------------------------------------------------------------------------------- // VERTEXSHADERS // -------------------------------------------------------------------------------------------------- vs2ps VS( float4 PosO: POSITION, float3 NormO: NORMAL, float4 TexCd : TEXCOORD0) { //inititalize all fields of output struct with 0 vs2ps Out = (vs2ps)0; // \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // http://local.wasp.uwa.edu.au/~pbourke/surfaces_curves/lemniscape/ float x,y,z,u,v,pi = 0; pi = 3.14159265; /* x = cos(v) sqrt(|sin(2 u)|) cos(u) y = cos(v) sqrt(|sin(2 u)|) sin(u) z = x2 - y2 + 2 x y tan2(v) */ //0 <= u <= pi, 0 <= v <= pi //map u and v u = PosO.x*pi*1.0; v = PosO.y*pi*1.0; x = cos(v)*sqrt(abs(sin(2*u)))*cos(u); y = cos(v)*sqrt(abs(sin(2*u)))*sin(u); z = x*x - y*y + 2*x*y*pow(tan(v),2); PosO.x = x; PosO.y = y; PosO.z = z; // //////////////////////////////////////////////////////////////////// // \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // http://local.wasp.uwa.edu.au/~pbourke/surfaces_curves/monkey/ PosO.z = pow(PosO.x,3)-3*PosO.x*pow(PosO.y,2); // //////////////////////////////////////////////////////////////////// //inverse light direction in view space Out.LightDirV = normalize(-mul(lDir, tV)); //normal in view space Out.NormV = normalize(mul(NormO, tWV)); //position (projected) Out.PosWVP = mul(PosO, tWVP); Out.TexCd = mul(TexCd, tTex); Out.ViewDirV = -normalize(mul(PosO, tWV)); return Out; } // -------------------------------------------------------------------------------------------------- // PIXELSHADERS: // -------------------------------------------------------------------------------------------------- float4 PS(vs2ps In): COLOR { //In.TexCd = In.TexCd / In.TexCd.w; // for perpective texture projections (e.g. shadow maps) ps_2_0 lAmb.a = 1; //halfvector float3 H = normalize(In.ViewDirV + In.LightDirV); //compute blinn lighting float3 shades = lit(dot(In.NormV, In.LightDirV), dot(In.NormV, H), lPower); float4 diff = lDiff * shades.y; diff.a = 1; //reflection vector (view space) float3 R = normalize(2 * dot(In.NormV, In.LightDirV) * In.NormV - In.LightDirV); //normalized view direction (view space) float3 V = normalize(In.ViewDirV); //calculate specular light float4 spec = pow(max(dot(R, V),0), lPower*.2) * lSpec; float4 col = tex2D(Samp, In.TexCd); col.rgb *= (lAmb + diff) + spec; return mul(col, tColor); } // -------------------------------------------------------------------------------------------------- // TECHNIQUES: // -------------------------------------------------------------------------------------------------- technique TPhongDirectional { pass P0 { //Wrap0 = U; // useful when mesh is round like a sphere VertexShader = compile vs_1_1 VS(); PixelShader = compile ps_2_0 PS(); } } technique TFallbackGouraudDirectionalFF { pass P0 { //transformations WorldTransform[0] = (tW); ViewTransform = (tV); ProjectionTransform = (tP); //material MaterialAmbient = {1, 1, 1, 1}; MaterialDiffuse = {1, 1, 1, 1}; MaterialSpecular = {1, 1, 1, 1}; MaterialPower = (lPower); //texturing Sampler[0] = (Samp); TextureTransform[0] = (tTex); TexCoordIndex[0] = 0; TextureTransformFlags[0] = COUNT2; //Wrap0 = U; // useful when mesh is round like a sphere //lighting LightEnable[0] = TRUE; Lighting = TRUE; SpecularEnable = TRUE; LightType[0] = DIRECTIONAL; LightAmbient[0] = (lAmb); LightDiffuse[0] = (lDiff); LightSpecular[0] = (lSpec); LightDirection[0] = (lDir); //shading ShadeMode = GOURAUD; VertexShader = NULL; PixelShader = NULL; } }