Skip to content

toaranotdev/webgl-3d-camera

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 

Repository files navigation

WebGL 3D Camera 📷

For a 3D camera, the matrix multiplication chain is particularly note worthy:

  • Object matrix
  • View matrix
  • Projection matrix

The information below is the key take-away. My key take-away, that is. It might just be a load of crap but I hope that's useful to... someone.

Object matrix

Handles object's rotation, position and what not. Fairly simple. Although, in the code, this is handled as a pure matrix instead of a quaternion-matrix combo of some kind. This is mostly because I am too stupid to understand quaternions.

View matrix

The view matrix can be thought of as a change-of-basis matrix. Say with $B_0$ is our standard orthonormal basis, and $B$ being our camera basis. $(B_0 \rightarrow B)$ represents a matrix that takes us from the camera view to world view. It's seems reversed, but that's how the change-of-basis matrix works lol.

Now, we take the inverse of that matrix, which mathematically means $(B_0 \rightarrow B)^{-1} = (B \rightarrow B_0)$. This represents a matrix that takes us from world view to camera view. Which is exactly what the view matrix is.

Projection matrix

$$ \begin{pmatrix} \frac{1}{aspectRatio \space \times \space tan(\frac{fov}{2})} & 0 & 0 & 0 \\ 0 & \frac{1}{aspectRatio \space \times \space tan(\frac{fov}{2})} & 0 & 0 \\ 0 & 0 & - \frac{far + near}{far - near} & - \frac{2 \times far \times near}{far - near} \\ 0 & 0 & -1 & 0 \end{pmatrix} $$

What's tricky is the negative signs thrown around here: Normalized Device Coordinate is a left-handed coordinate system, which looks in the positive $z$ axis, whilst our coordinate system is right-handed which looks in the negative $z$ axis.

Here is a step-by-step to derive the two $z$'s coefficients:

Let $m_1$ and $m2$ be the two coefficients we need to find, after the w-component division. We have the following:

$$ z_{norm} = \frac{Az_{view} \space + \space B}{-z_{view}} $$

We know for a fact when $z_{view} = -near$, $z_{norm} = -1$ and $z_{view} = -far$, $z_{norm} = 1$.

$$ \frac{A \times -far + B}{--far} = 1 $$

$$ A \times -far + B = far $$

$$ B = far + A \times far$$

Substitute into this:

$$ \frac{A \times -near + B}{--near} = -1 $$

Then becomes:

$$ \frac{A \times -near + B}{near} = -1 $$

$$ A \times - near + far + A \times far = -near $$

$$ A \times (- near + far) = - near - far $$

$$ A = \frac{- near - far}{far - near} $$

In the numerator we have a common factor of $-1$, factor that out we have our final result:

$$ A = - \frac{far + near}{far - near} $$

Subtitute that back in our original equation, we have our answer!

$$ B = far - \frac{far + near}{far - near} \times far $$

Find the common denominator:

$$ B = \frac{far \times (far - near) - (far + near) \times far}{far - near} $$

Factor out the $far$ we have:

$$ B = \frac{far \times (far - near - far - near)}{far - near} $$

Finally:

$$ B = - \frac{2 \times far \times near}{far - near} $$

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors