COMP3329 Ch6 3D Graphics 2
COMP3329 Ch6 3D Graphics 2
2023-2024
COMP3329 Computer Game Design and Programming
Dr. T.W. Chim (E-mail: [email protected])
Department of Computer Science, The University of Hong Kong
Agenda
Basic Mathematics for 3D Graphics
Points, Vectors, Matrices, Transformations, Homogeneous
Coordinates
Euler Angles, Quaternions, Quaternion Interpolations
Model / World / View Spaces
Cameras
Lights
2
Rendering a 3D Scene
Basic steps:
1. Setting up a virtual scene
2. Positioning a virtual camera
3. Defining light sources
4. Defining the visual properties of surfaces
5. Computing the color and intensity of the light rays "shining" on a
screen pixel
3
Rendering a 3D Scene
Basic steps:
1. Setting up a virtual scene
2. Positioning a virtual camera
3. Defining light sources
4
Model Space
The points of an object in a game world is positioned
relatively to its own local coordinate system called
model space, local space or object space.
The origin of model space is either the center of an
object or at some convenient location
5
World Space
The game world has a coordinate system common
to all objects, and this is called the world space.
Each object has a position and orientation relative to
the world origin.
Apply a model-to-world transformation to transform
all points on an object to the world space.
6
Rendering a 3D Scene
Basic steps:
1. Setting up a virtual scene
2. Positioning a virtual camera
3. Defining light sources
7
Cameras
The position and orientation of a camera determines
how objects are ultimately displayed on screen.
Camera parameters:
❖ Position
❖ Up/Right/Look vectors
▪ orientation
❖ Aspect ratio
▪ view aspect
Projection parameters:
❖ Near / Far clip planes
▪ clipping distances of the
view volume (contains
everything to be projected
on screen) 8
Projection
Determines how a 3D scene is rendered onto the 2D
display.
Perspective projection
9
Projection
Determines how a 3D scene is rendered onto the 2D
display.
Orthographic projection
10
Projection
Perspective projection gives a sense of depth, while
orthographic projection is better if measurements or
comparisons of lengths are needed.
12
Lights
Real world light sources are complex, e.g., sunlight,
neon lights, car headlights, indirect light bounces off
surfaces. We use simplified light models to
approximate real world light sources
Ambient light
uniform amount of light is applied to every object in the scene
Independent of viewing angle and has no specific direction
[unity]
[unity]
14
Use of spotlight:
[unity]
15
Lights
Directional light
Without position but is emitted from a specific direction
Parallel light from infinity
Best for emulating sunlight
[unity]
16
Lights
Area light
Simulate light coming from a large surface area, e.g.,
large rectangular ceiling light boxes
Can cast soft shadow that add realism to a scene
Computationally intensive, cannot be done in real-time
Can be simulated using several point lights or spotlights.
[geekatplay.com] [peterkutz.com]
17
Lights in Unity (Examples)
18
Lights in Unity (Examples)
19
Rendering a 3D Scene
Basic steps:
4. Defining the visual properties of surfaces
5. Computing the color and intensity of the light rays
"shining" on a screen pixel
20
Object Properties
Objects in a scene might be
Opaque: light cannot pass through its volume
Transparent: light passes through without scattered
Translucent: light passes through and is scattered in all
directions
opaque
transparent
translucent
21
Object Properties
Opaque objects can be rendered by considering only
their surfaces.
For transparent or translucent object, we need to model
how light is reflected, refracted, scattered, and absorbed
as it passes through the object's volume.
We usually use an alpha value to describe the opacity (or
transparency) of a surface and render a
transparent/translucent object almost the same way as
an opaque object.
Hence, games primarily concern about rendering
surfaces.
22
Surface Representations
A surface is in theory a two-
dimensional sheet comprising an
infinite number of points in 3D space.
Spline surfaces: defines a surface as
patches using a small number of
control polygons.
Subdivision surfaces: like spline
surfaces, a surface is represented by a
mesh of control polygons. Subdivision
rules (e.g., Catmull-Clark algorithm)
can be applied to iteratively subdivide a
polygon and thereby refining a surface.
Subdivision level can be up to pixel [Pixar]
resolution. 23
Surface Representations
Game developers mostly modeled their surfaces
using triangle meshes, which are piecewise linear
approximation to a surface.
25
Triangle Meshes
Coarse decimation may result in bad visual quality,
but fine decimation entails a large storage space
Use meshes at different levels of detail (LOD),
depending on the distance between object and
camera.
26
Vertex Attributes
Each vertex in a mesh is attached with some
attributes which determine the visual properties of
the surface, e.g.,
Position: defines the shape
Texture coordinates: for texture mapping
Surface normal: used for shading computation
28
Shading
Shading specifies how the surface of a triangle is
filled in for every pixel that it occupies on screen.
To determine how a triangle should look on screen,
it needs to undergo some lighting computations (to
account for how lights and surfaces interact in a
scene).
Flat shading is to carry out light calculation once for
each triangle, and the entire triangle will be filled
with the same color.
29
[www.dma.ufg.ac.at/app/link/Grundlagen%3A3D-Grafik/module/9728]
Gourand Shading
Lighting calculation is done once for each vertex,
which may then have different colors
The rest of the triangle is then filled by bilinearly
interpolating the colors at the 3 vertices.
31
Lighting Models
Local illumination models account for direct lighting
in which light is emitted, bounces off a single object
in the scene, and then proceeds to the image plane
of the camera
The most common local illumination model is the
Phong reflection model, which models light
reflected from a surface as a sum of three terms:
Ambient
Diffuse
Specular
32
Phong Reflection Model
Ambient
An approximation of the overall illumination of the scene
Regions in shadow will not appear totally black
Independent of the location of lights and the camera
33
Phong Reflection Model
Diffuse
Accounts for lights that is reflected uniformly in all
directions from each direct light source (directional, point,
or spotlights) affecting an object
Depends on N (surface normal) and L (vector from surface
to light); independent of view direction
35
Phong Reflection Model
36
Global Illumination
Global illumination achieves true photorealism by
accounting for indirect lighting, where light bounces
multiple times off many surfaces before reaching
the virtual camera.
Can model a wide
range of optical
phenomena: shadow,
reflection,
transparency
37
References
Game Programming Algorithm and Techniques,
Chapters 4
Introduction to Game Development, Chapter 4.1
Game Engine Architecture, Chapters 10
Credits
Images from Wikipedia or the above references, if not
otherwise credited
38
Chapter 6.
End
2023-2024
COMP3329 Computer Game Design and Programming
Dr. T.W. Chim (E-mail: [email protected])
Department of Computer Science, The University of Hong Kong