   Two unnecessary statements
////////////////////////////////////////////////////////////////////////////// // File: diffuse.txt// // Author: Frank Luna (C) All Rights Reserved//// System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0 //// Desc: Vertex shader that does diffuse lighting.////////////////////////////////////////////////////////////////////////////////// Global variables we use to hold the view matrix, projection matrix,// ambient material, diffuse material, and the light vector that describes// the direction to the light source.  These variables are initialized from// the application.//matrix ViewMatrix;matrix ViewProjMatrix;vector AmbientMtrl;vector DiffuseMtrl;vector LightDirection;//// Global variables used to hold the ambient light intensity (ambient// light the light source emits) and the diffuse light intensity (diffuse// light the light source emits).  These variables are initialized here// in the shader.//vector DiffuseLightIntensity = {0.0f, 0.0f, 1.0f, 1.0f};vector AmbientLightIntensity = {0.0f, 0.0f, 0.2f, 1.0f};//// Input and Output structures.//struct VS_INPUT{    vector position : POSITION;    vector normal   : NORMAL;};struct VS_OUTPUT{    vector position : POSITION;    vector diffuse  : COLOR;};//// Main//VS_OUTPUT Main(VS_INPUT input){    // zero out all members of the output instance.    VS_OUTPUT output = (VS_OUTPUT)0;    //    // Transform position to homogeneous clip space    // and store in the output.position member.//    output.position = mul(input.position, ViewProjMatrix);    //    // Transform lights and normals to view space.  Set w    // componentes to zero since we're transforming vectors// here and not points.    //    LightDirection.w = 0.0f;    input.normal.w   = 0.0f;    LightDirection   = mul(LightDirection, ViewMatrix);    input.normal= mul(input.normal,   ViewMatrix);    //    // Compute cosine of the angle between light and normal.    //    float s = dot(LightDirection, input.normal);    //    // Recall that if the angle between the surface and light    // is greater than 90 degrees the surface recieves no light.    // Thus, if the angle is greater than 90 degrees we set    // s to zero so that the surface will not be lit.    //    if( s < 0.0f )   s = 0.0f;    //    // Ambient light reflected is computed by performing a// component wise multiplication with the ambient material    // vector and the ambient light intensity vector.    //    // Diffuse light reflected is computed by performing a// component wise multiplication with the diffuse material    // vector and the diffuse light intensity vector.  Further    // we scale each component by the shading scalar s, which    // shades the color based on how much light the vertex received    // from the light source.    //    // The sum of both the ambient and diffuse components gives    // us our final vertex color.    //output.diffuse = (AmbientMtrl * AmbientLightIntensity) + (s * (DiffuseLightIntensity * DiffuseMtrl));   return output;}    LightDirection   = mul(LightDirection, ViewMatrix);    input.normal= mul(input.normal,   ViewMatrix);It seem to be unnecessary. after transform 2 direction together to camera view and the angle of them will not be changed.so am I right?[Edited by - luasitdown on December 28, 2005 6:42:03 AM]
  (v*M) . (w*M) =(v*M) * (w*M)T = v * (M * MT) * wT =v*(M*MT) . wSo if M is orthonormal, then M*MT == I. And usually view matrices are orthonormal. Here we got ViewMatrix, which is in fact world*view, but should be orthonormal anyway, if is used to transform normals, otherwise this whole angle measure system used here would colapse.So yes, those two multiplies are unnecessary.EDIT:changed orthogonal to orthonormalfixed equation   ;Quote:Original post by luasitdown *** Source Snippet Removed ***    LightDirection   = mul(LightDirection, ViewMatrix);    input.normal= mul(input.normal,   ViewMatrix);It seem to be unnecessary. after transform 2 direction together to camera view and the angle of them will not be changed.so am I right?Yes. The only requirement for diffuse illumination is that the light and normal are in the same space.That shader also seems to make the assumption (if that's all the code?) that the vertex normals are in world space too (i.e. that the world matrix will always be identity).Usually if a shader is only computing diffuse and the world matrix could be non-identity (e.g. containing rotations), you'd rotate the vertex normal into world space (from object space) by transforming by the top-left 3x3 portion of the "world" matrix. As an optimisation (for a diffuse only shader), you can alternatively transform the light direction into object space (using the top left 3x3 inverse [or transpose, see deffer's post] of the world matrix) on the CPU and avoid transforming any normals in the shader.However, that code does look like part of a tutorial - if the same tutorial later introduces specular illumination, then that is likely to be the reason why the normal and light vector are being transformed into camera/view space.   