Computer Graphics: Difference between revisions
Created page with "Basics of Computer Graphics ==MVP Matrices== To convert from model coordinates <math>v</math> to screen coordinates <math>w</math>, you do multiply by the MVP matrices <math>..." |
|||
Line 7: | Line 7: | ||
* The projection matrix <math>P</math> applies the projection of your camera, typically an orthographic or a perspective camera. The perspective camera shrinks objects in the distance. | * The projection matrix <math>P</math> applies the projection of your camera, typically an orthographic or a perspective camera. The perspective camera shrinks objects in the distance. | ||
===View Matrix=== | |||
[https://webglfactory.blogspot.com/2011/06/how-to-create-view-matrix.html Reference]<br> | |||
[https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/lookat-function Lookat function]<br> | |||
The view matrix is a 4x4 matrix which encodes the position and rotation of the camera.<br> | |||
Given a camera at position <math>p</math> looking at target <math>t=p-f</math> with up vector <math>u</math> and right vector <math>r</math>, | |||
this matrix is written as: | |||
<pre> | |||
r_x r_y r_z 0 | |||
u_x u_y u_z 0 | |||
f_x f_y f_z 0 | |||
p_x p_y p_z 1 | |||
</pre> | |||
<pre> | |||
Matrix lookAt(camera_pos, target, up) { | |||
forward = normalize(camera - target) | |||
up_normalized = normalize(up) | |||
right = normalize(cross(up, forward) | |||
// Make sure up is perpendicular to forward | |||
up = normalize(cross(forward, right) | |||
m = stack([right, up, forward, camera], 0) | |||
return m | |||
} | |||
</pre> | |||
==Shading== | ==Shading== |
Revision as of 15:54, 3 April 2020
Basics of Computer Graphics
MVP Matrices
To convert from model coordinates \(\displaystyle v\) to screen coordinates \(\displaystyle w\), you do multiply by the MVP matrices \(\displaystyle w=P*V*M*v\)
- The model matrix \(\displaystyle M\) applies the transform of your object. This includes the position and rotation. \(\displaystyle M*v\) is in world coordinates.
- The view matrix \(\displaystyle V\) applies the transform of your camera.
- The projection matrix \(\displaystyle P\) applies the projection of your camera, typically an orthographic or a perspective camera. The perspective camera shrinks objects in the distance.
View Matrix
Reference
Lookat function
The view matrix is a 4x4 matrix which encodes the position and rotation of the camera.
Given a camera at position \(\displaystyle p\) looking at target \(\displaystyle t=p-f\) with up vector \(\displaystyle u\) and right vector \(\displaystyle r\),
this matrix is written as:
r_x r_y r_z 0 u_x u_y u_z 0 f_x f_y f_z 0 p_x p_y p_z 1
Matrix lookAt(camera_pos, target, up) { forward = normalize(camera - target) up_normalized = normalize(up) right = normalize(cross(up, forward) // Make sure up is perpendicular to forward up = normalize(cross(forward, right) m = stack([right, up, forward, camera], 0) return m }