Document 7703678

Download Report

Transcript Document 7703678

SI23
Introduction to Computer
Graphics
Lecture 10 – Introduction to 3D Graphics
Si23_03
10.1
Course Outline

Graphics
programming
– Using OpenGL Graphics
with C, C++
Programming
3D
Graphics
OpenGL
API
2D vector
graphics
URL
SVG
Viewer
surfaces
lines,
areas
Image
Display
GIMP
URL
viewing, shading
graphics
algorithms
Si23_03
VRML
viewer
URL
colour
interaction
animation
10.2
2D Graphics – Picture
Description

SVG is a vector
graphics description
language
<?xml version="1.0" ?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG
20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG20010904/DTD/svg10.dtd">
<svg width = "300" height = "300">
<rect x = "100" y = "100" width =
"100" height = "100" style =
"fill:red" />
</svg>
SVG Viewer
Si23_03
10.3
2D Graphics – Programming
Approach


For programming
graphics, there are
a number of APIs, or
Application
Programming
Interfaces
OpenGL is industry
standard
User Program
calling OpenGL
functions
OpenGL Library
– Both 2D and 3D
Si23_03
10.4
Objectives for This Part
Si23_03

To understand how 3D scenes can be
modelled - in terms of geometry and
appearance - and rendered on a
display

To be able to program interactive 3D
graphics applications using industry
standard software (OpenGL)
10.5
Lecture Outline - The Basics

MODELLING and VIEWING
– representing objects in 3D
– transforming objects and composing scenes
– specifying the camera viewpoint

PROJECTION
– projecting 3D scenes onto a 2D display surface

RENDERING
– illumination
– shading
– adding realism via textures, shadows
Si23_03
10.6
Basic Modelling and Viewing
y
z
x
objects represented
as set of faces - ie
polygons- and faces
as a set of points
scenes composed
by scaling, rotating,
translating objects to
create a 3D world
camera
Si23_03
Camera position
specified, together
with its direction of view
10.7
Projection

Projection
– 3D scene is projected onto a 2D plane
camera
Si23_03
view
plane
10.8
A Puzzle
Si23_03
10.9
Rendering
illumination:
??
how is light reflected
from surfaces?
shading:
how do we use our
knowledge of illumination
to shade surfaces in our
world?
Si23_03
10.10
Rendering
Si23_03

texture

shadows
10.11
Creating 3D Graphics

OpenGL is an API
that allows us to
program 3D
graphics
– As well as 2D
Si23_03

VRML is a language
that allows us to
describe 3D
graphics scenes
– Cf SVG for 2D
10.12
Applications - Computer
Games
Si23_03
10.13
Applications - ComputerAided Design


This is Hubble Space
Telescope modeled
using the BRL-CAD
system
Uses CSG modeling
and ray tracing for
rendering
http://ftp.arl.mil/brlcad
Si23_03
10.14
Applications - Virtual Reality


Virtual
oceanarium
built for EXPO
in Lisbon
Example taken
from
Fraunhofer
Institute site
http://www.igd.fhg.de
Si23_03
10.15
Before we
begin...mathematics!

3D Co-ordinate Systems
y
y
z
x
LEFT
z points away
x
z
RIGHT
z points toward
Align thumb with x, first finger with y, then second finger
of appropriate hand gives z direction. Common now to
use a RIGHT HANDED system.
Si23_03
10.16
Points and Vectors

We shall write points as column
vectors
y
P =
x
y
z
P
x
z
Difference of two points gives a direction vector:
D = P 2 - P1
y
P2
z
Si23_03
P1
x
Note: If P1 and P2
are on a plane, then
D lies in the plane
10.17
Magnitude of a Vector
The magnitude of a vector V =
(v1,v2,v3)T is given by:
|V| = sqrt(v1*v1 + v2*v2 + v3*v3)
eg (1,2,3)T has magnitude sqrt(14)
 A unit vector has magnitude 1
 A unit vector in the direction of V is
V / |V|

Si23_03
10.18
Scalar or Dot Product

The scalar product, or dot product, of
two vectors U and V is defined as:
U.V = u1*v1 + u2*v2 + u3*v3

It is important in computer graphics
because we can show that also:
U.V = |U|*|V|*cosq
where q is the angle between U and V

This lets us calculate angle q as
cos q = (u1*v1 + u2*v2 + u3*v3) / (|U|*|V|)
Si23_03
10.19
Diffuse Lighting

Diffuse reflection depends on angle
between light direction and surface
normal:
reflected intensity = light intensity * cosine
of angle between light direction and
surface normal
light
normal
q
Si23_03
scalar product lets
us calculate cos q
10.20
Vector or Cross Product

The vector or cross product is defined as:
UxV = (u2v3 - u3v2, u3v1 - u1v3, u1v2 - u2v1)

We can also show that:
UxV = N |U||V| sin q
where N is unit vector orthogonal to U and V (forming a
right handed system) and q is angle between U and V

This allows us to find the normal to a plane
– cross-product of two directions lying in plane , eg (P3P2), (P2-P1), where P1, P2, P3 are three points in the plane
Si23_03
10.21
Exercises



Si23_03
Convince yourself
that the x-axis is
represented by the
vector (1,0,0)
What is the unit
normal in the
direction (2,3,4)?
What is the angle
between the vectors
(1,1,0) and (1,0,0)?


Which vector is
orthogonal to the
vectors (1,0,0) and
(0,1,0)?
What is the normal
to the plane through
the points (1,2,3),
(3,4,5) and (0,0,0)?
10.22
Polygonal Representation

Any 3D object can be represented as
a set of plane, polygonal surfaces
V7
V8
V3
V4
V6
V5
V2
V1
Note: each vertex part of several
polygons
Si23_03
10.23
Polygonal Representation

Si23_03
Objects with curved surfaces can be
approximated by polygons improved approximation by more
polygons
10.24
Scene Organisation
Scene = list of objects
 Object = list of surfaces
 Surface = list of polygons
 Polygon = list of vertices

scene
Si23_03
object
vertices
surfaces
polygons
10.25
Polygon Data Structure
V7
Object Obj1
V8
V3
V2
V4
V6
V5
P2
V1
P1
Object Table
Obj1 P1, P2, P3,
P4, P5, P6
.
Si23_03
...
Polygon Table
P1
V1, V2, V3, V4
P2
V1, V5, V6, V2
.
...
Vertex Table
V1 X1, Y1, Z1
V2 X2, Y2, Z2
.
...
10.26
Typical Primitives
Order of Vertices

Graphics systems such as OpenGL typically
support:
– triangles, triangle strips and fans
– quads, quad strips
– polygons

How are vertices ordered?
– convention is that vertices are ordered counterclockwise when looking from outside an object
– allows us to distinguish outer and inner faces of a
polygon
Si23_03
10.27
Complex Primitives

OpenGL has utility libraries (GLU and
GLUT) which contain various high-level
primitives
– Sphere, cone, torus
– Polygonal representation constructed
automatically
Similarly for VRML
 For conventional graphics hardware:

– POLYGONS RULE!
Si23_03
10.28
Automatic Generation of
Polygonal Objects

3D laser scanners are able
to generate computer
representations of objects
– for successive heights, 2d
outline generated as object
rotates
– contours stitched together
into 3D polygonal
representation

Si23_03
Cyberware Cyberscanner
in Med Physics at LGI able
to scan human faces
10.29
Modelling Regular Objects

Sweeping
sweep axis
2D Profile

Spinning
R1
R2
spinning axis
Si23_03
10.30
Sweeping a Circle to Generate
a Cylinder as Polygons
V13
V4
V5
V9
V2
V10
V14
V15
V18
V17
V16
vertices at z=depth
V8
V7
vertices at z=0
Si23_03
V11
V3
V1
V6
V12
V1[x] = R; V1[y] = 0; V1[z] = 0
V2[x] = R cos q; V2[y] = R sin q; V2[z] = 0 (q=p/4)
Vk[x] = R cos qk; Vk[y] = R sin qk; Vk[z] = 0
where
qk = 2 p (k - 1 )/8, k=1,2,..8
10.31
Exercise and Further Reading

Spinning:
– Work out formulae to spin an outline (in
the xy plane) about the y-axis

READING:
– Hearn and Baker, Chapter 10
Si23_03
10.32