Computer Graphics

# for QR codes use inline
%matplotlib inline
qr_setting = 'url'
# for lecture use notebook
# %matplotlib notebook
# qr_setting = None
%config InlineBackend.figure_format='retina'
# import libraries
import numpy as np
import matplotlib as mp
import pandas as pd
import matplotlib.pyplot as plt
import laUtilities as ut
import slideUtilities as sl
import demoUtilities as dm
import pandas as pd
from importlib import reload
from datetime import datetime
from IPython.display import Image
from IPython.display import display_html
from IPython.display import display
from IPython.display import Math
from IPython.display import Latex
from IPython.display import HTML
from IPython.display import YouTubeVideo
from IPython.display import Audio

Computer Graphics is the study of creating, synthesizing, manipulating, and using visual information in the computer.

Today we’ll study the mathematics behind computer graphics.

Recommended Textbook: Mathematics for 3D Game Programming and Computer Graphics, Third Edition


Entertaiment: film productions, film effects, motion capture, gaming

# Source: CS 184 Lecture Slides, UC Berkeley, Ng Ren
display(Image("images/ComputerGraphics/motivation-entertainment.png", width=900))

Science and Engineer: product design, visualization, computer-aided desingn

# Source: CS 184 Lecture Slides, UC Berkeley, Ng Ren
display(Image("images/ComputerGraphics/motivation-product-visual.png", width=750))

Graphic Arts: graphical user interface, digital photography, graphic design, fine arts

# Source: CS 184 Lecture Slides, UC Berkeley, Ng Ren; CS 680 Lecture Slides, Boston University, Emily Whiting
display(Image("images/ComputerGraphics/motivation-finearts.png", width=700))

Architectural design

Visual Simulation

Virtual Reality, Augmented Reality, 3D Printing


We have talked about Geometric Linear Transformations of \(\mathbb{R}^2\):

  1. Reflection over the \(x_1,x_2\) axes, over the lines \(x_2=x_1, x_2=-x_1\), and through the origin.

  2. Horizontal and vertical expansion.

  3. Horizontal and vertical shearing.

  4. Projection onto the \(x_1,x_2\) axes.

Today, we will talk about 3D transformations and its applications.

In the application aspects, why are we studying transformation? Why 3D transformations?

  1. Tranform can describe position of object instances

display(Image("images/ComputerGraphics/robot01.png", width=750))
  1. Transform can describe relative position of connected body part

display(Image("images/ComputerGraphics/robot02.png", width=750))

Jurassic Park movie: Behind the scene.

display(YouTubeVideo(id=id, width=900, height=400))

The computer graphics seen in movies and videogames works in three stages:

  1. A 3D model of the scene objects is created;

  2. The model is converted into (many small) polygons in 3D that approximate the surfaces of the model; and

  3. The polygons are transformed via a linear transformation to yield a 2D representation that can be shown on a flat screen.

There is interesting mathematics in each stage, but the transformations that take place in the third stage are linear, and that’s what we’ll study today.

# source
display(Image("images/ComputerGraphics/Akira_class_CGI_wireframe_model_by_ILM.jpg", width=750))

Initially, object models may be expressed in terms of smooth functions like polynomials.

However the first step is to convert those smooth functions into a set of discrete pieces – coordinates and line segments.

All subsequent processing is done in terms of the discrete coordinates that approximate the shape of the original model.

The reason for this conversion is that most transformations needed in graphics are linear.

Expressing the scene in terms of coordinates is equivalent to expressing it in terms of vectors, eg, in \(\mathbb{R}^3\).

And linear transformations on vectors are always matrix multiplications, so implementation is simple and uniform.

The resulting representation consists of lists of 3D coordinates called faces. Each face is a polygon.

The lines drawn between coordinates are implied by the way that coordinates are grouped into faces.


Here is a view of a ball-like object. It is centered at the origin.

The ball is represented in terms of 3D coordinates, but we are only plotting the \(x\) and \(y\) coordinates here.

Colors correspond to faces.

# load ball wireframe
import obj2clist as obj
with open('snub_icosidodecahedron.wrl','r') as fp:
    ball = obj.wrl2flist(fp)
# set up view
fig = plt.figure()
ax = plt.axes(xlim=(-5,5),ylim=(-5,5))
# plot the ball
for b in ball:


Imagine that we want to circle the camera around the ball. This is equivalent to rotating the ball around the \(y\) axis. (See Maya Showcases)

This is a linear transformation. We can implement it by multiplying the coordinates of the ball by a rotation matrix. To define the rotation matrix, we need to think about what happens to each of the columns of \(I: {\bf e_1, e_2, e_3.}\)

To rotate through an angle of \(\alpha\) radians around the \(y\) axis,

the vector \({\bf e_1} = \left[\begin{array}{r}1\\0\\0\end{array}\right]\) goes to \(\left[\begin{array}{c}\cos \alpha\\0\\\sin \alpha\end{array}\right].\)

Of course, \({\bf e_2}\) is unchanged.

And \({\bf e_3} = \left[\begin{array}{r}0\\0\\1\end{array}\right]\) goes to \(\left[\begin{array}{c}-\sin \alpha\\0\\\cos \alpha\end{array}\right].\)

So the entire rotation matrix is:

\[\begin{split}\begin{bmatrix}\cos \alpha&0&-\sin \alpha\\0&1&0\\\sin \alpha&0&\cos\alpha\end{bmatrix}.\end{split}\]
# set up view
import matplotlib.animation as animation
mp.rcParams['animation.html'] = 'jshtml'

fig = plt.figure()
ax = plt.axes(xlim=(-5,5),ylim=(-5,5))
ballLines = []
for b in ball:
    ballLines += ax.plot([],[])
#to get additional args to animate:
#def animate(angle, *fargs):
#    fargs[0].view_init(azim=angle)
def animate(frame):
    angle = 2.0 * np.pi * (frame/100.0)
    rotationMatrix = np.array([[np.cos(angle), 0, -np.sin(angle)],
                               [0,             1,              0],
                               [np.sin(angle), 0, np.cos(angle)]])
    for b,l in zip(ball,ballLines):
        rb = rotationMatrix @ b
# create the animation 
animation.FuncAnimation(fig, animate,