Why multiply vector if you normalize it afterwards?

Started by
2 comments, last by sQuid 10 years, 3 months ago

So I was looking through the rastertek website,and I found this snippet:


 If the light intensity is greater than zero we will do specular light calculations.

    if(lightIntensity > 0.0f)
    {

Here is where we sample the specular map for the intensity of specular light at this pixel.

        // Sample the pixel from the specular map texture.
        specularIntensity = shaderTextures[2].Sample(SampleType, input.tex);

In the reflection calculation we use the bump map normal instead of the regular input normal.

        // Calculate the reflection vector based on the light intensity, normal vector, and light direction.
        reflection = normalize(2 * lightIntensity * bumpNormal - lightDir); 

        // Determine the amount of specular light based on the reflection vector, viewing direction, and specular power.
        specular = pow(saturate(dot(reflection, input.viewDirection)), specularPower);

Now that we have the amount of specular light at this pixel we then multiply it by the specular intensity from the specular map to get a final value.

        // Use the specular map to determine the intensity of specular light at this pixel.
        specular = specular * specularIntensity;
		
        // Add the specular component last to the output color.
        color = saturate(color + specular);
    }
	
    return color;
}


    // Calculate the amount of light on this pixel based on the bump map normal value.
    lightIntensity = saturate(dot(bumpNormal, lightDir));

reflection = normalize(2 * lightIntensity * bumpNormal - lightDir);

All my interest is in that line. First,why multiply the vector in the first place,if you'll normalize it later.Second,why multiply by 2?(just for a better effect?) And finally,why multiply by lightIntensity too? I know that the dot product gives a result that is connected with the length of the vectors but still...i can't connect it all out.

Advertisement

Dot product is |a||b|cos(Theta), so assuming that lightdir is also normalized, they are looking to calculate cosTheta, and not some scalar of cosTheta.

lightdir and bumpnormal are probably not usually collinear in this case, so the direction wil not usually be the same.

Try reading it again, you'll see that they're not multiplying the entire argument vector by 2. :-)

This topic is closed to new replies.

Advertisement