You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The m_proj matrix is created as a perspective camera using an field-of-view of PI/4 radians (which is 45 degrees), the aspect ratio of the backbuffer, the near-plane distance of 0.1 and a far-plane distance of 10.
The m_view matrix Is created as a view looking from the position (2,2,2) looking at the position (0,0,0) with an up vector of (0,1,0).
These two matrices handle transforming objects positioned in world coordinates and transforming them to view coordinates and then to screen coordinates.
Positioning an object
The 3D shapes lesson also provides an example of positioning an object.
In the Game class we added a variable for the world matrix for our 3D shape:
DirectX::SimpleMath::Matrix m_world;
We initially set it to the identity in CreateDevice:
m_world = Matrix::Identity
Then in Update we set the matrix to a rotation about the Z-axis based on time:
float time = float(timer.GetTotalSeconds());
m_world = Matrix::CreateRotationZ(cosf(time) * 2.f);
Then later in the lesson we made it into a rotation about the Y-axes based on time:
float time = float(timer.GetTotalSeconds());
m_world = Matrix::CreateRotationY(time);
The m_world matrix transforms the object's local (also known as model) coordinates into world coordinates.
Composing transformations
The power of affine transformation is that you can compose them by multiplying them together. In our 3D shapes lesson, we can modify the Update: to get more interesting motion for the planet earth.
Build and run to see the planet earth moving in a more complex pattern.
Transforming a point
For performance, the majority of the 3D transformations we use in rendering at computed on the GPU using vertex shaders based on the matrices we provide for world, view, and projection. There are times, however, when we want to compute a transformation on the CPU (such as doing collision detection or visibility culling). To transform a point using our variables above we should first concatenate our matrices into a single matrix that combines the transformations.
Note: In our example we are only transforming a single point which means we are doing 3 matrix multiplies and 1 vector-matrix multiply. For a single point it might have been better to do 3 vector-matrix multiplies, but for most usage cases we are likely to transform more than just a single point. Ideally we'd use the array version of Transform to transform many points at once.
Moving the camera
We can animate the camera's position as well as the object's. In the 3D shapes lesson, we will change Update as follows: