So this one took a little bit of time to finish. Since we are adding scene files, entity files, materials and effects, we are kind of forced to re-factory the code again and write a bunch of parser. Which is pretty good of course since we have a chance to make our own file formats and have a pretty good sense of what data we need to pass to the renderer. "Data-driven" is what this is and it's important because this is commonly used in applications.
On the other hand, the shader part is not that hard actually. I added an ambient light, an attenuation and color to the point light that I have, and also specular lighting to a few materials. The way I'm doing this is to assign the texture sampler to the diffuse color and multiply it by the fragment color from the vertex shader.
Then I get the light direction L and the normal of that fragment N, normalize L, then get the dot product and get the strength of the point light and clamp it:
strength = clamp(dot(L, N), 0 ,1);
After that, I calculate the light attenuation. First I check the distance d from the point light and fragment position. Then get the light radius r from the scene file. Then I do something like this: 1/(1 + (d/r))^2 to get the attenuation.(More information about the equation: here)
So the diffuse lighting = light color * light attenuation * strength.
After that is specular lighting, first I get the reflection R, then I get the camera view V from the fragment position to the camera position. Then I get the dot product of these two: saturate(dot(V, R)); And I can get the specular lighting by power it by an exponent. (We also do strength and attenuation because you shouldn't see any light on the back side of the mesh.)
So the specular lighting = power(dotproduct, exp) * strength * attenuation.
So the final output is diffuseColor * (diffuse lighting + ambient light) + specular lighting.
Here is the screenshot of the actual effect:
Here is the screenshot of the PIX:
Here is the source code:
Graphics04_Source
Control:
To control the point light:
use "I" - up, "K" - down, "J" - left, "L" - right, "U" - backward, "O" - forward;
To control the camera: Arrow Keys
To control the box: WASD.


No comments:
Post a Comment