The laws of the universe

Or

The primary matrices


So why does it stick to my face?

You must be wondering why our RED RING OF DEATH will not stay where we thought it would, or why it is even called a ring, when it’s a circle. After all, when you placed that sphere before we made the material it stood there, obeying the laws of 3d.
Why than?

Let’s look at our two programs again:

float4 mainVS(float4 pos : POSITION) : POSITION
{
	return pos;
}

float4 mainPS() : COLOR 
{
	return float4(1.0, 0, 0, 1.0);
}


The vertex shader receives the original vertex position, and returns it. There is one detail that is not revealed here: the position is in object space. (also called model space)

Object space

What is object space? The object space was defined when the object was created.
Our sphere knows its own right and left. If you ask him to say where his nose is (yes, it has one) he answers: “Above my mouth!” (all the positions are relative)

If you use these coordinates, you place the object without regard to where it is located in the world or relative to the camera. Neither do you address how it should be projected onto the screen.

In order to 'fix' this, we need to put the provided position in context using the three primary matrices.

The primary matrices

1 : the world matrix

The world matrix is the simplest matrix to understand. You just need to look at the render window:
Image

See this? This is the best way to visualize the world matrix (don't take it literally however).
Using the matrix, we position, scale and rotate the object into world space. Practically, it means we put a vertex in context to the rest of the scene.

2 : the view matrix

We can also call this matrix “THE CAMERA” – this way we don’t need to explain too much.
The view matrix is a mathematical representation of where we are observing the world matrix from. We use it to transform from to view space.

3 : the projection matrix

This one is somewhat tricky. We can call the projection matrix is our screen. Using this matrix, we “flatten” the 3d image to into 2d that we can relate to pixels on the screen.

Image
''Im in ur screen,
Projecting mah self''

Using the matrices in practice

So how do we use these matrices in order to place vertices where they should be?
We need to multiply the position vector in each of the matrices and return the product.
Let's begin:
1. first, we need to declare the three matrices, and use the appropriate semantics:
Notice the three new semantics (WORLD, VIEW and PROJECTION):

float4x4 world_m 	: WORLD;
float4x4 view_m 	: VIEW;
float4x4 projection_m 	: PROJECTION;


Unlike the previous arguments we declared, these will be global variables; put them in the top of the program.
2. now we need to multiply
in order to multiply vectors with matrices or matrices with matrices, we use a function called “mul”
Observe:

float4x4 worldview_m 		= mul(world_m, view_m);
float4x4 worldviewproj_m 	= mul(worldview_m, projection_m);
return mul(pos, worldviewproj_m);


This will be our new vertex program. We create a world-view matrix, and multiply it by the projection matrix to create a world-view-projection matrix. Finally, we use this matrix to put the vertex in its place by multiplying its position by the combined matrix.

float4x4 world_m 	: WORLD;
float4x4 view_m 	: VIEW;
float4x4 projection_m 	: PROJECTION;

float4 mainVS(float4 pos : POSITION) : POSITION
{
	float4x4 worldview_m 		= mul(world_m, view_m);
	float4x4 worldviewproj_m 	= mul(worldview_m, projection_m);
	return mul(pos,worldviewproj_m);
}

float4 mainPS() : COLOR 
{
	return float4(1.0, 0, 0, 1);
}

technique technique0 
{
	pass p0 
	{
		VertexShader = compile vs_1_1 mainVS();
		PixelShader = compile ps_2_0 mainPS();
	}
}

Our new program

A closer look

Let’s take that last bit step by step and see what happens:

Using the world matrix alone

Using the world matrix alone
If you multiply the position in the world matrix the result will be:
Image

Other than being the real RED RING OF DEATH that was promised, what is that? And why does it have a hole in the middle?
By using the world matrix, we allocate the vertices in the “real” space, rather than strictly in object space. That means that our sphere can intersect with the view, and because the sphere doesn’t have any in facing vertices, we will see through it.
While in data form the vertices are now positioned in context, this is an invalid product for rendering purposes since we have yet to address view or projection.

World – view matrix

When we multiply the world and view matrix, we receive a combined matrix of both. If we multiply a vertex position by this matrix, we position it within our world, and relative to the camera. As in using the world matrix alone, data-wise this is a valid product for calculations, but not for rendering.

World – view – projection matrix

When we combine the world-view matrix with the projection matrix, we virtually “splat” the 3d image to 2d, fit to display on our screen.
This is similar to how a camera works –light reflected by objects in our 3d world hit the film, and create a 2d image.

Using the wonders of paint, we can visually summarize all of this
Image

Shortcuts

Your lazy mind is probably thinking: “My, oh my… do I have to write THREE WHOLE LINES just to make the simplest thing in the world?” (- View - projection matrix?)

Well – no. smarter people thought of this before you. There are already-made combinations for almost anything you can think of.

Since world-view-projection matrix is pretty much the most common operation in 3d, we have an already made matrix for this. Guess how it’s called?

WorldViewProjection matrix

Using this matrix, we can shorten (and simplify) our last program.
this will be our new vertex program:

float4x4 worldViewProj_m : WORLDVIEWPROJECTION;

float4 mainVS(float4 pos : POSITION) : POSITION
{
	return mul(pos,worldViewProj_m);
}


So why do I need those matrices separate?
Acting smart now are you? Well, not everything revolves around rendering directly. Some things require prior calculations. In many cases, you will need to use one of theses matrices for other purposes.