Вы находитесь на странице: 1из 7

G53GRA:

Mat h f or Gr aphi cs Pr ogr ammi ng


OpenGL . . . ?
OpenGL (Open Graphics Library) is a standard specification
defining a cross platform, cross language API (Application
Programming Interface) for writing applications that produce
2D and 3D computer graphics.
It is designed as an API that all graphics hardware can
understand no matter what GPU (Graphics Programming
Unit) you are using.
OpenGL is not the only such API to exist, it is in competition
with DirectX/ Direct3D/ SDL.
In recent years Microsoft have pushed DirectX into becoming
industry standard with the release of Xbox 360 and Vista.
These days games and other graphical programs tend to be
written in DirectX due to Microsoft's Monopolistic influence.
What l ooks good?
Graphics programming is not always about
mathematical precision as a heavily
mathematical load on the CPU or GPU will lead
to a slow frame rate (FPS, frames per second)!
Instead we tend to approximate and/ or
simplify the math in order to produce
something that looks good rather than
something that is 100 percent correct but runs
too slow.
Ter ms and Si mpl e Vect or Mat h
Poin t is a location in a co-ordinate
system.
Vect or has a direction and a
magnitude, but no location.
Pos it ion Vect or refers to the arrow
that goes to the point from the co-ordinate
systems origin.
Car t esi an Co-or di nat es
Basis Vectors, i relates to a single unit of movement along the
x axis, j along the y axis and k along the z axis.
Using combinations of i, j and k we can map to any point in
the co-ordinate system by projecting along these units.
Since you can get to any point in the space using only these 3
units then they are called a basis of the co-ordinate system.
They can also be called unit vectors as they are one unit long.
y
z
x i
j
k
i = (1,0,0)
j = (0,1,0)
k = (0,0,1)
How To Expr ess a Vect or
Vector V=(x,y,z)=xi+yj+zk
Let U=(U1,U2,U3)=U1i+U2j+U3k
V=(V1,V2,V3) =V1i+V2j+v3k
U+V=(U1+V1, U2+V2,U3+v3)
U-V = (U1-V1, U2-V2, U3-V3)
aV = (aV1, aV2, aV3)
V=B-A subtracting one p oin t from another
gives you a vector which goes between the
points.
z
y
x
A
B
Mat r i x
a
ij
a11 a12 a13 ...
a21 a22 a23 ...
a31 a32 a33 ...
.
.
.
.
.
.
.
.
.
m

R
o
w
s
j changes
i

c
h
a
n
g
e
s
An mxn matrix
n columns
The numbers in the matrix are called its entries or its elements. To specify
a matrix's size, a matrix with m rows and n columns is called an m-by-n
matrix or m n matrix, while m and n are called its dimensions.
Computer graphics uses matrices to project 3-dimensional space onto a
2-dimensional screen.
Mat r i ces i n t he gr aphi cs case
In our case we will apply matrixs to perform
transformations, scaling or rotation on a given
column vector!
Think of the ai1 components of the matrix as
working on the x column ai2 as the y column and
ai3 as the z column
Mat r i x addi t i on
The sum of two matrixes A and B is defined for
matrices that are of degree mxn only!
4 4 4
9 9 6
9 2 1
7 8 9
18 16 14
18 6 8
3 4 5
9 7 8
9 4 7
a b c
d e f
g h i
j k l
m n 0
p q r
a+j b+k c+l
d+m e+n f+o
g+p h+q i+r
Scal ar Mul t i pl i cat i on
a b c
d e f
g h i
Z
za zb zc
zd ze zf
zg zh zi
3 5 8
4 35 9
8 1 7
10
30 50 80
40 350 90
80 10 70
Tr anspose Mat r i x
The transpose of an m-by-n matrix A is the n-by-
m matrix A
T
a b c
d e f
g h i
T
a d g
b e h
c f i
2 3 4
8 5 1
9 7 6
T
2 8 9
3 5 7
4 1 6
Mat r i x Mul t i pl i cat i on
J oins the effect of two matrixs
N.B. Multiplication is NOT commutative i.e.
AB BA
a b c
d e f
g h i
j k l
m n o
p q r
aj+bm+cp ak+bn+cq al+b0+cr
dj+em+fp dk+en+fq dl+eo+fr
gj+hm+ip gk+hn+iq gl+ho+ir
3 4 5
2 4 7
3 4
2 4
6 2
3.3+4.2+5.6 3.4+4.4+5.2
2.3+4.2+7.6 2.4+4.4+7.2
47 38
56 38
2 4 6
8 9 3
16 4 8
1 0 0
0 1 0
0 0 1
2 4 6
8 9 3
16 4 8
2x3
3x2
2x2
2 4 6
8 9 3
16 4 8
1
6
2
38
68
56
The Ident i t y Mat r i x
The identity matrix is a matrix such that when it
is applied it has no effect.
We use this as a building block to add
transformations to the matrix.
1 0 0
0 1 0
0 0 1
1 0 0
0 1 0
0 0 1
x
y
z
x
y
z
Det er mi nant
det(A) or | A|
A matrix is invertible if and only if its
determinant is nonzero

a b c
d e f
g h i
a
e f
h i
b
d f
g i
c
d e
g h
+ - +
- + -
+ - +
Inver se
I AA A B = =
1 1
,
We need to be able to work out inverses of matrixs to be able to undo
the effect of a transformation
The in ver s e ma t r ix of a r ot a t ion ma t r ix is the t r a n s p os e of the
rotation matrix
The inverse matrix of a (invertible) square matrix M can be obtained by
aligning side by side M and an identity matrix I , and perform the same
row operations on both M and I until M is reduced to an identity matrix,
and I is changed and becomes the inverse matrix of M
A
-1
= [1/ det(A)][adj(A)] is a different but equally good a way to find the
inverse, however we will not look at this method
-1 3 -3
0 -6 5
-5 -3 1
A
We want to find A
-1
, to do this we can use
the identity of the current position and use
row operation to undo the effects of A on
the identity to get back to the right point in
space.
-1 3 -3
0 -6 5
-5 -3 1
1 0 0
0 1 0
0 0 1
1 -3 3
0 -6 5
-5 -3 1
-1 0 0
0 1 0
0 0 1
R1: x (-1)
1 -3 3
0 -6 5
-5 -3 1
-1 0 0
0 1 0
0 0 1
1 -3 3
0 -6 5
0-1816
-1 0 0
0 1 0
-5 0 1
R3: R3+5R1
1 -3 3
0 -6 5
0-1816
-1 0 0
0 1 0
-5 0 1
1 -3 3
0 1 -5/ 6
0-18 16
-1 0 0
0-1/ 16 0
-5 0 1
R2: -1/ 6R2
1 -3 3
0 1 -5/ 6
0-18 16
-1 0 0
0-1/ 16 0
-5 0 1
1 0 1/ 2
0 1 -5/ 6
0 0 1
-1 -1/ 2 0
0 -1/ 16 0
-5 -3 1
R1: R1+3R2
R3:R3+18R2
1 0 1/ 2
0 1 -5/ 6
0 0 1
-1 -1/ 2 0
0 -1/ 16 0
-5 -3 1
1 0 0
0 1 0
0 0 1
3/ 2 1 -1/ 2
-25/ 6 -8/ 3 5/ 6
-5 -3 1
R1:R1-1/ 2R3
R2:R2+5/ 6R3
3D Tr ansf or mat i ons
We may need to transform objects from one
space to another, e.g., world space to view
space/ camera space
Instead of performing each transformation
alone, we accumulate the transformations in a
matrix before applying them to objects by matrix
multiplication.
Homogeneous Condi t i on
In 2D, three elements can be used to represent a
point, e.g., (x,y,1) to represent (x,y) or (wx,wy,w)
w=0 to represent (x,y)
(x,y,w) is a homogeneous coordinate
representing the point (x/ w, y/ w)
Scal i ng
What is scaling ... Increasing or
decreasing the size of an object by a
scale factor along all 3 axis (x,y and z)
All we are doing is applying a strech or
shrink along the axis
Think about the identity matrix when
it is applied it gives us back the
original point. We need to change the
identity matrix so that instead of
giving us our original value it scales it
by our scale factor in each direction
hence;
(1,1,-1)
(2,2,-2)
(1/ 2,1/ 2,-1/ 2)
S
X
0 0 0
0 S
Y
0 0
0 0 S
Z
0
0 0 0 1
Tr ansl at i on
Translation is NOT like scaling in the
way it transforms the point. Scaling
increases the points size relative to the
direction from the origin to the point
where as translation moves the point
around in any given direction;
x = x+a y = y+b z = z+c
Think about this in terms of the identity
matrix. We want to take the given
points, i.e. Apply the identity move then
we want to add on the change to the
points this results in;
(1,1,-1)
(-3,2,5)
1 0 0 tr
X
0 1 0 tr
Y
0 0 1 tr
Z
0 0 0 1
1 0 0 tr
X
0 1 0 tr
Y
0 0 1 tr
Z
0 0 0 1
S
X
0 0 0
0 S
Y
0 0
0 0 S
Z
0
0 0 0 1
x
y
z
w
Sx.x
Sy.y
Sz.z
w
x
y
z
w
x+trx.w
y+try.w
z+trz.z
w
Rot at i on
In 2D rotation is about a point
Take a point in the x-y plane you can only rotate that
point so it is still on the plane!
The only way to rotate the point is to turn the plane
it lies in
a
y
x
2D Rot at i on
(x,y)
u
r
SOH/ CAH/ TOA
rsin(u)
rcos(u)
(x,y)
(x,y)
u
|
r cos u cos | - r sin u sin |
r sin u cos| + r cos u sin |
x= r cos (u+ |) = r cos u cos | - r sin u sin | = xcos | ysin |
Y= r sin (u + |) = r sin u cos| + r cos u sin | = xcos| + ysin |
SO
x = xcos | y sin |
y = ycos | + xsin | |
|
.
|

\
|
|
|
.
|

\
|
=
|
|
.
|

\
|
y
x
y
x


sin cos
sin cos
'
'
In 3D, You have 2 degrees of freedom in which
to rotate the plane. You can rotate it in its
current position and you can rotate its angle in
the z axis for example.
This presents a problem in making a matrix that
can deal with this amount of freedom. Therefore
we think of rotation as 3 separate types around
each of the axis.
In general rotation vector does not pass through
coordinate origin the idea is to turn rotations
about an arbitrary axis into ones about the
coordinate centre
x
y
z
x
y
z
x
y
z
Rot a t i on in x a xis :
The position in the x
axis is fixed, spin the
y-z plane to rotate
the points
Rot a t i on in y a xis :
The position in the y
axis is fixed, spin the
x-z plane to rotate
the points
Rot a t i on in z a xis :
The position in the z
axis is fixed, spin the
x-y plane to rotate
the points
about X axis
about Y axis
about Z axis
(
(
(
(

=
1 0 0 0
0 0
0 0
0 0 0 1
u u
u u
cos sin
sin cos
x
R
(
(
(
(

=
1 0 0 0
0 0
0 0 1 0
0 0
u u
u u
cos sin
sin cos
y
R
(
(
(
(


=
1 0 0 0
0 1 0 0
0 0
0 0
u u
u u
cos sin
sin cos
z
R
Identity in x
Rot at i on on a Ar bi t r ar y axi s
We want to rotate around U but to do
this we need to embed it into our axis to
use the standard rotation matrixes
In this example we will embed it into the
z axis.
To embed into the z axis we need to
rotate U by o degrees around x and then
| degrees around y and centre the U axis
over the origin with a translation.
The rotation matrix to rotate x is R
x
(o)
the rotation matrix to rotate around y is
R
y
(|)
Apply these rotations by multiplying
them together
R
x
(o) . R
y
(|).T
To un apply these matrixs after the
required rotation
T
-1
. R
y
(|)
-1
. R
x
(o)
-1
U axis
o
|
Projection of
U in the y-z
plane
Projection of
U in the x-z
plane
Pr oj ect i ng ont o Y-Z pl ane f or a r ot at i on
ar ound x
If U is our arbitrary axis and we are
embedding to Y-Z to rotate around
x we know that on the Y-Z plane all
x=0
We know the height y (=b) of the
point on the y-z plane
We know the depth z (=c) of the
point on the z plane
From this we can work out what sin
and cos of the angle we require is
and therefore by taking the inverse
of them we can work out the angle
value to apply in the rotation matrix
Exactly the same method can be
used to work out | in the x-z plane
y
z
(0,b,c)
o
2 2
c b d + =
b
c
Soh/ cah
Dot Pr oduct , Scal ar Pr oduct
The dot product takes two vectors and returns a single s ca la r value.
In Euclidean geometry, the dot product relates the length and angle
between to vectors.
a =(a1,a2,a3) b =(b1,b2,b3)
a . b = a ibi = a 1b1+a 2b2+a 3b3
(1,3,-5).(4,-2,-1) = 1*4+3*(-2)+(-5)*(-1) = 4-6+5 = 3
a . b =| | a | | | | b | | cos
=>
a . b = cos
| | a | | | | b| |
=>
= cos
-1
( a . b )
| | a | | | | b| |
a . b = 0 iff a and b are at right angles as cos=0

| A| cos

| A|
A
B
SOH/ CAH/ TOA
Normalise: = a
| | a | |
now has unit length
| A| cosis the scalar projection of A onto
B
| | a | | =a . a
Cr oss Pr oduct , Vect or Pr oduct
Takes two vectors and returns a normal vector to the two vectors
A
B
AxB
BxA=
-AxB

i x j = k
k x i = j
j x k = i
Let a = (a1,a2,a3)= a1i +a2j+a3k
b = (b1,b2,b3)= b1i +b2j+b3k
a xb = (a1,a2,a3)x(b1,b2,b3)
= (a2b3-a3b2) i+(a3b1-a1b3) j+(a1b2-a2b1)k
= (a2b3-a3b2, a3b1-a1b3, a1b2-a2b1)
= i j k
a1 a2 a3
b1 b2 b3
= a2 a3 i a1 a3 j + a1 a2 k
b2b3 b1 b3 b1 b2
We can use the cross product to
work out the normals to surfaces
if we know 2 vectors on the
surface
Cr oss Pr oduct Exampl e
Let a = (3, -3, 1) b = (4, 9, 2)
a x b = i j k = -3 1 i - 3 1 j + 3 -3 k
3 -3 1 9 2 4 2 4 9
4 9 2
= ((-3)x21x9)i-(3x2-1x4)j+(3x9-(-3)x4)k
= -15i-2j+39k
= (-15, -2, 39)
Vect or Equat i on of a Li ne
A, B are two know points on the line whose position vectors are a , b respectively and
u is the vector obtained by subtracting A from B.
Y=mx+c
To find the equation of the line we simply need to find a way to represent an arbitrary
point on the line.
We already know two points on the line which are a and b.
If a is already on the line, to get another point on the line we just need to move a up
or down the line in the direction of the gradient u .
Let P be an arbitrary point on the line and Ps position vector be r .
r = a + AP = a +
(x,y,z) = (a1,a2,a3) + (u1,u2,u3)
EX:
a = (1, 3, 0) b = (3, 4, 1)
u = (3, 4, 1) (1, 3, 0) = (2, 1, 1)
r = (1, 3, 0) + (2, 1, 1)
Equat i on of a Pl ane
Given A, B, C which are points on a plane and P
is an arbitrary point on that plane. To get from
point A to P you need to move As position vector
a set amount in the B direction and a set amount
in the C direction:
AP = AB + AC
r -a = (b-a ) + (c-a )
r = (1- -)a + b+c
So any point on a plane can be expressed in the
above form.
n
B A
P: r =(x,y,z) C
Nor mal t o a Pl ane
Any point P, on the plane, must be perpendicular (at
right angles) to the normal of the plane. If they are
at right angles then this implies that the dot product
must be zero.
n .AP=0
=>
n . (r -a )=0
=>
n . r -n . a =0
=>
n r =k (k is some scalar)
n
Equat i on of a Pl ane Exampl e
A: (0, 1, -7)
B: (3, 1, -9)
C: (0, -5, -8)
b = (0, 1, -7) (3, 1, -9) = (-3, 0, 2)
C = (0, 1, -7) (0, -5, -8) = (0, 6, 1)
Normal: n = b xc = i j k = (0x1-2x6)i-(-3x1-3x0)j+(-3x6-0x0)k = -12i+3j-18k
-3 0 2
0 6 1
n .(r -a ) = 0
(-12, 3, -18).(x-0, y-1, z+7) = 0
=>
-12x+ 3(y-1) 18(z+z) =0
=>
-12x+3y-18z-129=0 [This is the equation for any point (x,y,z) on the plane]
Di st ance of Poi nt t o Pl ane
n
A
P
r -a
O
a
r
The distance from A to a plane is the projection of AP on the planes normal
vector n . In other words it is the absolute value of a .n . Given that the plane
equation is r .n =c (some constant)
P
A
n
| AP
|

| AP| cos
(r -a ).n = r .n -a .n = c a .n
n n n
| Ax1+By1+Cz1+D|
A
2
+B
2
+C
2
(1)
(2)
Di st ance f r om poi nt t o pl ane exampl e
Given P = (4, -4, 3) and the plane equation is 2x-2y+5z+8=0
From the plane equation (2) we get A=2, B=-2, C=5, D=8
From P we get x1=4, y1=-4, z1=3
So the distance d becomes
d= | 2x4 + (-2)x(-4) + 5x3 +8| = 39 6.8
2
2
+(-2)
2
+5
2
33
Int er sect i on of Li nes and Pl anes
Where a line and a plane intersect a point will be formed
Line Equation:
r =a +(a 2-a 1)
Plane equation:
n .r = k
let k = n .a 3 (a new point on the plane)
=> {subbing r into the plane equation}
n .(a 1+(a 2-a 1))= n .a 3
{extracting the dot products}
n .a 1+n .((a 2-a 1))= n .a 3
=> {scalar value does not affect the dot product}
n .a 1+ n .(a 2-a 1)= n .a 3
{re-arranging}
n .(a 2-a 1) = n .a 3- n .a 1
{re-arranging and unexpanding dot product}
= n . (a 3-a 1)/ n .(a 2-a 1)
Now we have the distance to move from point a 1 to get to the point of intersection so
sub this into the line equation to get
r = a 1 +[n . (a 3-a 1)/ n .(a 2-a 1)] (a 2-a 1)
Int er sect i on of Pl anes
Where two planes intersect a line will be formed
with equation:
r =a +
The two planes have the same position vectors r
but different normals so you get two equations
for the two different planes.
n
1
.r = p n
2
.r = q
The intersecting line should be parrallel to the
cross product of the two normals n
1
x n
2
!
Int er sect i on of Pl anes Exampl e
Plane 1: 2x-5y+3z=12
Plane 2: 3x+4y-3z=6
First work out the normal of the plane by finding values on the plane and using the cross product on
them.
Normal for Plane 1: n 1 = (2,-5,3)
Normal for Plane 2: n 2 = (3,4,-3)
NB: these vectors arent parallel so the planes DO meet!
Now use the cross product on the two normal vectors to give a vector parrallel to the intersection line.
n 1 x n 2 = (3, 15, 23)
We need to find a point that lies on the line of intersection of the planes. Both planes will have points
for which x=0 so sub in (0,y,z) for both planes and solve simultaneously to give:
-5y+3z=12 [plane1+plane2]
4y-3z=6
=>
-y=18
=>
y=-18
=>
z=-26
So (0,-18,-26) lies on the line of interection
therefore the equation of the line is
r = (0,-18,-26) + t(3,15,23)

Вам также может понравиться