Friday, January 25, 2013

Graphics Programming 3


Since I'm on fire today, I thought I might as well finish assignment 3 :p

The concept is simple:

Texture coordinate: 
The whole point of texture coordinate is to mark those coordinates on vertex so that each vertex know what part of the texture would it render. In that way, when the coordinates get interpolated into the fragment shader, each fragment know exactly which coordinate it gonna use on the texture, thus get the color info from that point. That's how a triangle gets to know which part of the texture to render.

One thing to note though, the texture coordinate of a texture is actually "flipped", which means the upper-left is actually 0, 0, instead of 0, 1. (So the lower-left is 0,1);

Point light& diffuse light:
In graphics, we assume diffuse light goes into all directions, and the "strength" of the light is determined by the light source (in our case, point light). 
How do we do that? 
By first: get the light direction vector using light's position minus the actual fragment's position (So we know we are gonna do it in the fragment shader, well... we could actually calculate the light results in the vertex shader and let the graphic card interpolate it. But the result could be worse than per-pixel light. And considering how strong the graphic card is nowadays, we can afford it.) 
Second: get the normal vector from the mesh and transform it to world position (that's how I prefer it anyway).
End: dot product these two vector, which is the cosine of their angle, which is exactly the power of the light at that point.

Anyway, it's all simple 3D math.

So, this is how it actually looks:


And the texture resource in PIX:


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.

Here is the code:
Graphics03_Source


No comments:

Post a Comment