class: center, middle # Creación de Videojuegos ### 3D Graphics --- class: center, middle # Vectors --- # Remember: The Dot Product $$ \vec{A} \cdot \vec{B} = \begin{pmatrix} x_1 \\\\ y_1 \\\\ z_1 \end{pmatrix} \cdot \begin{pmatrix} x_2 \\\\ y_2 \\\\ z_2 \end{pmatrix} = x_1 \cdot x_2 + y_1 \cdot y_2 + z_1 \cdot z_2\\\\ \vec{A} \cdot \vec{B} = \cos(\alpha) \cdot |\vec{A}| \cdot |\vec{B}| $$ --- # The Cross Product With three dimensional vectors, we have another way of multiplying them: The cross product $$ \vec{A} \times \vec{B} = \begin{pmatrix} x_1 \\\\ y_1 \\\\ z_1 \end{pmatrix} \times \begin{pmatrix} x_2 \\\\ y_2 \\\\ z_2 \end{pmatrix} = \begin{pmatrix} y_1 \cdot z_2 - z_1 \cdot y_2\\\\ z_1 \cdot x_2 - x_1 \cdot z_2 \\\\ x_1 \cdot y_2 - y_1 \cdot x_2 \end{pmatrix}\\\\ \vec{A} \times \vec{B} = \sin(\alpha) \cdot |\vec{A}| \cdot |\vec{B}| \cdot \vec{n} $$ The main use of this (for us) is to find normal vectors for surfaces, i.e. vectors that point "away" from something. --- class: small # Dot Product: Example 2 There is a wall going through the points `a` and `b` extending "far" in both directions $$ a = \begin{pmatrix} -2 \\\\ 8 \\\\ 0 \end{pmatrix} \quad b = \begin{pmatrix} 6 \\\\ 2 \\\\ 0 \end{pmatrix} $$ Is the player `p` at the same side of that wall as the king `k`? $$ p = \begin{pmatrix} -4\\\\ 10\\\\ 1 \end{pmatrix} \quad k = \begin{pmatrix} -7\\\\ 11\\\\ 1 \end{pmatrix} $$ How far away is the player from the wall? --- # Another Example
--- # Summary * Vectors can be added to other vectors (result: vector) * Vectors can be multiplied with a number (result: vector) * Two vectors can be multiplied with the dot product (result: **number**) * Two (3D) vectors can be multiplied with the cross product (result: vector) --- class: center, middle # Matrices --- # Matrices * A matrix is a two-dimensional array of numbers * We will only work with 2x2, 3x3 and 4x4 matrices * Like vectors, matrices can be added and multiplied with scalars * We can also multiply two matrices --- # Matrix Multiplication .left-column[ $$ A \cdot B $$ The number of columns of A and the number of rows of B **must be the same**. Note: Each entry is the *dot product* of a row of A with a column of B. ] .right-column[
image/svg+xml
Matrix multiplication diagram
2007-08-11
mathematics
matrix
mutliplication
A
B
a
1,1
a
3,1
a
3,2
a
2,1
a
2,2
a
4,1
a
4,2
a
1,2
b
1,2
b
2,2
b
1,3
b
2,3
b
1,1
b
2,1
] --- # Matrix-Vector Product $$ A \cdot \vec{v} = \begin{pmatrix} a_1 & b_1 & c_1 \\\\ a_2 & b_2 & c_2 \\\\ a_3 & b_3 & c_3 \end{pmatrix} \cdot \begin{pmatrix} x \\\\ y \\\\ z \end{pmatrix} = \begin{pmatrix} a_1 \cdot x + b_1 \cdot y + c_1 \cdot z \\\\ a_2 \cdot x + b_2 \cdot y + c_2 \cdot z \\\\ a_3 \cdot x + b_3 \cdot y + c_3 \cdot z \end{pmatrix} $$ As you can see, a matrix-vector multiplication performs many operations. We can use this to represent a wide variety of operations on vectors (technically: Linear Transformations) --- # Matrix-Vector Multiplication Example $$ A = \begin{pmatrix} 3 & 0 & 0 \\\\ 0 & 3 & 0 \\\\ 0 & 0 & 3 \end{pmatrix} $$ What might this do? -- Scale a vector by a factor of 3. $$ A \cdot \vec{v} = \begin{pmatrix} 3 & 0 & 0 \\\\ 0 & 3 & 0 \\\\ 0 & 0 & 3 \end{pmatrix} \cdot \begin{pmatrix} x \\\\ y \\\\ z \end{pmatrix} = \begin{pmatrix} 3 \cdot x \\\\ 3 \cdot y \\\\ 3 \cdot z \end{pmatrix} $$ --- # Matrix-Vector Multiplication Example $$ A = \begin{pmatrix} 1 & 1 \\\\ 0 & 1 \end{pmatrix} $$ What might this do? -- This is a "shear" operation: Move points in x-direction the further "up" they are (in y direction) $$ A \cdot \vec{v} = \begin{pmatrix} 1 & 1 \\\\ 0 & 1 \\\\ \end{pmatrix} \cdot \begin{pmatrix} x \\\\ y \end{pmatrix} = \begin{pmatrix} x + y \\\\ y \end{pmatrix} $$ --- class: center, middle # Triangles
--- # Some definitions * Vertex (plural Vertices): A point in 3D space * Triangle: Three vertices * Face: The "front" side of a triangle * Normal: A vector that points "away" from the face --- # Why triangles? * Triangles are always flat * Flat makes it easier to calculate lighting, color, etc. * Triangles are convex * Convex makes it easier to interpolate between the vertices * Any polygon can be divided into triangles --- class: smallp # An Example Consider the triangle $$ a = (2,2,3), b = (3,3,4), c = (3,1,3) $$ with the normal vector $$ \vec{n} = \begin{pmatrix} \frac{-1}{\sqrt{6}} \\\\ \frac{-1}{\sqrt{6}} \\\\ \frac{2}{\sqrt{6}} \end{pmatrix} $$ and a point $$ p = (3\sqrt{6},0, 2\sqrt{6}) $$ ## Does the point p lie in front of or behind the triangle? --- # Color * We can assign a color to a triangle * But what if we want to have different colors? * Ideally, we want a gradual change * Idea: Assign color to the vertices and interpolate * It's such a good idea, 3D libraries have it built-in --- # Interpolation Recall: $$ a + t (b-a) = p $$ `p` is "some distance" between `a` and `b`. Rewritten: $$ p = (1-t) \cdot a + t \cdot b $$ --- # Interpolation Halfway between two values: $$ p = \frac{1}{2} a + \frac{1}{2}b$$ Closer to a than to b $$ p = \frac{2}{3} a + \frac{1}{3} b$$ or, generally: $$ p = \theta \cdot a + (1- \theta) \cdot b$$ --- # Interpolation What if we have more than two values? e.g. r, s, t $$ p = \theta \cdot r + \phi \cdot s + \psi \cdot t$$ with $$\theta + \phi + \psi = 1$$ Note: r, s, and t don't have to be numbers, they could also be vectors! In particular, they can be the vertices of a triangle. Every point in the triangle can then be represented as $$p = (\theta,\phi,\psi)$$ --- # Barycentric coordinates
image/svg+xml
Barycentric Coordinates in a triangle
(1,0,0)
(0,1,0)
(0,0,1)
(1/2,1/2,0)
(1/2,0,1/2)
(0,1/2,1/2)
(1/4,1/4,1/2)
(1/4,1/2,1/4)
(1/2,1/4,1/4)
(1/3,1/3,1/3)
--- # Interpolation What can we do with this representation? $$p = (\theta,\phi,\psi)$$ Calculate color values! $$c(p) = \theta \cdot c(r) + \phi \cdot c(s) + \psi \cdot c(t)$$ --- # A colored triangle
--- # An example Consider the triangle $$ a = (1,0,0), b = (1,1,0), c = (0,1,0) $$ ## If a has the color (99,0,0), b has the color (0,39,0), and c has the color (0,81,81), what color does the barycenter of the triangle have using linear interpolation? Which color does the point that is twice as close to a as to b and c (in the barycentric sense) have? --- class: medium # Normals * Normals are (unit) vectors that point away from a face * They are used for lighting * What does "away" mean? * Simplest case: perpendicular to the triangle * What if we have adjacent triangles? * We can assign normals to each vertex and interpolate, just as with colors --- # Textures * Colored triangles are boring! * What if we want to show something like wood? * Use an image as the texture * Assign texture (uv) coordinates to each vertex * *Interpolate* between the vertices to get the uv coordinate for each pixel! --- # Textured Triangle Texture coordinates (uv): (0,0), (1,1), (1,0)
--- # Textured Triangle Let's use (0.5,0.5) as the UV value for one vertex.
--- # An example Consider the triangle $$ a = (2,1,3), b = (4,3,4), c = (3.5,2.5,4.5) $$ The texture coordinates are $$ t(a) = (\frac{3}{5}, \frac{3}{8}), t(b) = (\frac{3}{10}, \frac{3}{16}), t(c) = (\frac{3}{7}, \frac{3}{10}) $$ ## The point p is the closest point to c on the line from a to b. What texture coordinates does p have? --- # Meshes * Triangles are boring! * A *Mesh* is a collection of triangles (and normals, uv coordinates, colors) * How about something more exciting, like a cube? * We can assemble a cube out of triangles! * Or a bunny! --- # Stanford Bunny
--- class: small # Back to the cube * Cubes have 8 vertices and 6 faces (12 triangles!) * Vertices: - v1 = (0,0,0) - v2 = (1,0,0) - v3 = (0,1,0) - v4 = (1,1,0) - v5 = (0,0,1) - v6 = (1,0,1) - v7 = (0,1,1) - v8 = (1,1,1) * Faces: - f1: (v1,v2,v3) - f2: (v3,v2,v4) - etc. --- class: small # The .obj file format
v 0 0 0
v 1 0 0
v 0 1 0
v 1 1 0
v 0 0 1
v 1 0 1
v 0 1 1
v 1 1 1
f 2 1 3
f 2 3 4
f 8 5 6
f 5 8 7
f 6 1 2
f 1 6 5
f 3 8 4
f 8 3 7
f 1 7 3
f 7 1 5
f 8 2 4
f 2 8 6
Download file
here
--- # .obj files * List of vertices, faces, uv coordinates, normals, etc. * You could hand-write one (like for the cube!) * Or you write code to generate the file * Unity (and most other 3D software) reads them! --- # Cube (Controls: w, a, s, d)
--- # Level of Detail * Many modern meshes have a lot of detail * But what if it is far away? * Idea: Use a lower-resolution (fewer triangles) version of the same mesh! * Can often be precomputed automatically * Limitation: Visible "jumping" when the low-res mesh is replaced with the higher-res one --- # Stanford Bunny and Level of Detail
--- class: small # Billboards * Many background objects like trees, grass, rocks only exist as decoration * It may not be important for them to have a lot of detail * Actually, as long as it looks like a tree from all sides, it's probably fine * Just show a picture of a tree, and turn it so it always faces the camera! * Also useful for health bars or similar information * `transform.LookAt(2 * transform.position - Camera.main.transform.position);` --- # Shaders
--- class: medium # References * Book: *Mathematics for Game Developers*, by Christopher Tremblay * [Barycentric coordinate system](https://en.wikipedia.org/wiki/Barycentric_coordinate_system) * [NeHe OpenGL tutorials](http://nehe.gamedev.net/) (slightly outdated, but explains the basics well) * [OpenGL Billboard tutorial](http://www.opengl-tutorial.org/intermediate-tutorials/billboards-particles/billboards/) * [three.js](https://threejs.org/) (JavaScript library for easy 3D rendering) * [OpenGL Render Pipeline](https://www.khronos.org/opengl/wiki/Rendering_Pipeline_Overview)