Computer Graphics: Difference between revisions
Line 1: | Line 1: | ||
Basics of Computer Graphics | Basics of Computer Graphics | ||
==Homogeneous Coordinates== | |||
[http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/ http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/] | |||
Points and vectors are represented using homogeneous coordinates in computer graphics.<br> | |||
Points are <math>(x,y,z,1)</math> and vectors are <math>(x,y,z,0)</math>.<br> | |||
The last coordinate in points allow for translations to be represented as matrix multiplications.<br> | |||
Transformations consists of translations, rotations, and scaling | |||
===Translation Matrix=== | |||
<math> | |||
\begin{bmatrix} | |||
1 & 0 & 0 & X\\ | |||
0 & 1 & 0 & Y\\ | |||
0 & 0 & 1 & Z\\ | |||
0 & 0 & 0 & 1 | |||
\end{bmatrix} | |||
</math> | |||
===Rotation Matrix=== | |||
Rotations can be about the X, Y, and Z axis.<br> | |||
Below is a rotation about the Z axis by angle <math>\theta</math>.<br> | |||
<math> | |||
\begin{bmatrix} | |||
\cos(\theta) & -\sin(\theta) & 0 & 0\\ | |||
\sin(\theta) & \cos(\theta) & 0 & 0\\ | |||
0 & 0 & 1 & 0\\ | |||
0 & 0 & 0 & 1 | |||
\end{bmatrix} | |||
</math> | |||
===Scaling Matrix=== | |||
<math> | |||
\begin{bmatrix} | |||
X & 0 & 0 & 0\\ | |||
0 & Y & 0 & 0\\ | |||
0 & 0 & Z & 0\\ | |||
0 & 0 & 0 & 1 | |||
\end{bmatrix} | |||
</math> | |||
==MVP Matrices== | ==MVP Matrices== | ||
To convert from model coordinates <math>v</math> to screen coordinates <math>w</math>, you do multiply by the MVP matrices <math>w=P*V*M*v</math> | To convert from model coordinates <math>v</math> to screen coordinates <math>w</math>, you do multiply by the MVP matrices <math>w=P*V*M*v</math> | ||
* The model matrix <math>M</math> applies the transform of your object. This includes the position and rotation. <math>M*v</math> is in world coordinates. | * The model matrix <math>M</math> applies the transform of your object. This includes the position and rotation. <math>M*v</math> is in world coordinates. | ||
* The view matrix <math>V</math> applies the transform of your camera. | * The view matrix <math>V</math> applies the transform of your camera. | ||
* 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=== | ===View Matrix=== |
Revision as of 20:10, 6 April 2020
Basics of Computer Graphics
Homogeneous Coordinates
http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/
Points and vectors are represented using homogeneous coordinates in computer graphics.
Points are \(\displaystyle (x,y,z,1)\) and vectors are \(\displaystyle (x,y,z,0)\).
The last coordinate in points allow for translations to be represented as matrix multiplications.
Transformations consists of translations, rotations, and scaling
Translation Matrix
\(\displaystyle \begin{bmatrix} 1 & 0 & 0 & X\\ 0 & 1 & 0 & Y\\ 0 & 0 & 1 & Z\\ 0 & 0 & 0 & 1 \end{bmatrix} \)
Rotation Matrix
Rotations can be about the X, Y, and Z axis.
Below is a rotation about the Z axis by angle \(\displaystyle \theta\).
\(\displaystyle
\begin{bmatrix}
\cos(\theta) & -\sin(\theta) & 0 & 0\\
\sin(\theta) & \cos(\theta) & 0 & 0\\
0 & 0 & 1 & 0\\
0 & 0 & 0 & 1
\end{bmatrix}
\)
Scaling Matrix
\(\displaystyle \begin{bmatrix} X & 0 & 0 & 0\\ 0 & Y & 0 & 0\\ 0 & 0 & Z & 0\\ 0 & 0 & 0 & 1 \end{bmatrix} \)
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 \mathbf p\) looking at target \(\displaystyle \mathbf t\) and up vector \(\displaystyle \mathbf u\).
We can calculate the forward vector (from target to position) as \(\displaystyle \mathbf{f}=\mathbf{p} - \mathbf{t}\).
We can calculate the right vector as \(\displaystyle \mathbf u \times \mathbf f\).
Then the view 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 }
Shading
Flat Shading
Gourard Shading
Phong Shading
More Terms
- Diffuse reflection - reflection scattered in many directions (i.e. matte)
- Specular reflection - mirror reflection
- Refraction - change in direction of light as it passes through a material