0% found this document useful (0 votes)
19 views37 pages

Cs3vr16 Graphics 3

Uploaded by

fatimabuhari2014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views37 pages

Cs3vr16 Graphics 3

Uploaded by

fatimabuhari2014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

CS3VR16 : Graphics

Lecture 3 – Aut 2023


p1 RJM 17/08/23 CS3VR16 – Virtual Reality – Graphics -
3
Components for 3D Graphics
 3D Scene Representation
• Intro
• Coordinate Systems
• 3D Primitives
• Matrix Operations
• For transforming objects we wish to
display
• For calculating where they are rel to the
eye
• For ensuring proper 3D representation in
2D
• This week we build
p2 RJM 17/08/23 CS3VR16on matrices
– Graphics -3 from last week
© Prof Richard Mitchell 2023
3D Scenes – 3D Primitives rots
Matrix Transforms – Rotations general
Rotations are always about the main coordinate axes
Rotations are positive in an anti-clockwise direction
when looking back to the origin along the axis of
rotation
z z

x x

y y

p3 RJM 17/08/23 CS3VR16 – Graphics - 3


© Prof Richard Mitchell 2023
Rotate

[ ]
cos ( 𝜃 ) − sin ( 𝜃 ) 0
In 2D Matrix to
sin ( 𝜃 ) cos ( 𝜃 ) 0
Rotate by angle 0 0 1
θ
In 3D Matrix Rotate by angle θ about x, y or z
axis
Matrix Transforms – Rotations about
x

[ ]
1 0 0 0
0 cos 𝜃 −sin 𝜃 0
0 sin 𝜃 cos 𝜃 0
0 0 0 1

Again cos, -sin, sin, cos; but in row/col of y and z

p4 RJM 17/08/23 CS3VR16 – Graphics - 3


© Prof Richard Mitchell 2023
Rotate about y and z
Matrix Transforms – Rotations about y

[ ]
cos 𝜃 0 −sin 𝜃 0
0 1 0 0
sin 𝜃 0 cos 𝜃 0
0 0 0 1

Matrix Transforms – Rotations about z

[ ]
cos 𝜃 −sin 𝜃 0 0
sin 𝜃 cos 𝜃 0 0
0 0 1 0
0 0 0 1

Again cos, -sin, sin, cos; but not in row/col of axis

p5 RJM 17/08/23 CS3VR16 – Graphics - 3


© Prof Richard Mitchell 2023
Example, Arm of Crane
Matrix to scale and move arm 200x10x1
0

[ ]
200 0 0 60 12x12x1
0 10 0 0 0
0 0 10 95
10x10x8
0 0 0 1
0
Bottom of base at z = 0

Rotate arm by angle a about z axis


Here a is such that so cos(a)=0.8, sin(a)=0.6
How do we find overall transform matrix?
Can you calc positions of two ends of arm

p6 RJM 17/08/23 CS3VR16 – Graphics - 3


© Prof Richard Mitchell 2023
Rotating Arm
Already scaled and moved to right place, now
rotate

[ ][ ]
0.8 − 0.6 0 0 200 0 0 60
0.6 0.8 0 0 0 10 0 0
0 0 1 0 0 0 10 95
0 0 0 1 0 0 0 1

As can see, x and y row, col have changed,


not z
p7 RJM 17/08/23 CS3VR16 – Graphics - 3
© Prof Richard Mitchell 2023
How values changed

[ ]
160 −40
Before rotation, found 5 −5
opposite corners of cube 90 100
at 1 1

[ ][ ]
80 − 3+ 48 − 80+3 +48 125 −29
60 +4 +36 − 60 − 4+ 36 100 −28
= =
− 5+95 5 +95 90 1 00
1 1 1 1
Again, z unchanged
p8 RJM 17/08/23 CS3VR16 – Graphics - 3
© Prof Richard Mitchell 2023
3D Scenes – 3D Primitives
Rotating Objects –
rotate the following object about z by 60
y
This is the result of
applying the Rotz,60
transform.

Was this wanted?


θ
x
p9 RJM 17/08/23 CS3VR16 – Graphics - 3
© Prof Richard Mitchell 2023
3D Scenes – 3D Primitives -
Rotate
y y y

θ θ θ
x x x
y

Which of these 60⁰


rotations is wanted?
θ x
p10 RJM 17/08/23 CS3VR16 – Graphics - 3
© Prof Richard Mitchell 2023
3D Scenes – 3D Primitives
Objects
Rotations must be defined by the axis of rotation and
the point on the object that axis goes through.
Then, the rotation can be achieved by
 translating this point to the origin
 affecting the rotation
 translating back to its original location

𝐨𝐛 𝐣𝑟𝑜𝑡𝑎𝑡𝑒𝑑 = 𝐓 𝑇𝑥 , 𝑏𝑎𝑐𝑘 𝐓 𝑅𝑜𝑡 , 𝜃 𝐓 𝑇𝑥 , 𝑡𝑜𝑜𝑟𝑖𝑔𝑖𝑛 𝐨𝐛 𝐣𝑜𝑟𝑖𝑔𝑖𝑛𝑎𝑙

p11 RJM 17/08/23 CS3VR16 – Graphics - 3


© Prof Richard Mitchell 2023
Example – to show difference
Rotate by 60⁰ about x

To rotate 60⁰ around


x axis at point 1,1,1

p12 RJM 17/08/23 CS3VR16 – Graphics - 3


© Prof Richard Mitchell 2023
3D Scenes – 3D Primitives

[ ]
Rotate by 60⁰ about x 1 0 0 0
0 0.5 − 0.866 0
𝐓=
0 0.866 0.5 0
0 0 0 1

Coordinates [0,0,0] and [1,1,1] get transformed


to

[ ][ ] [ ]
1 0 0 0 0 1 0 1
0 0.5 − 0.866 0 0 1 0 − 0.366
=
0 0.866 0.5 0 0 1 0 1.366
0 0 0 1 1 1 1 1
p13 RJM 17/08/23 CS3VR16 – Graphics - 3
© Prof Richard Mitchell 2023
3D Scenes – 3D Primitives
To rotate 60⁰ around x axis at point

[ ][ ][ ]
1,1,1
1 0 0 1 1 0 0 0 1 0 0 −1
0 1 0 1 0 0.5 − 0.866 0 0 1 0 −1
𝐓=
0 0 1 1 0 0.866 0.5 0 0 0 1 −1
0 0 0 1 0 0 0 1 0 0 0 1

[ ] [ ]
1 0 0 0 Two points are now
0 0.5 − 0.866 1.366 0 1
𝐓=
0 0.866 0.5 − 0.366 1.366 1
0 0 0 1 −0.366 1
1 1
p14 RJM 17/08/23 CS3VR16 – Graphics - 3
© Prof Richard Mitchell 2023
3D Scenes – 3D Primitives

z We can rotate about an axis

x We can rotate about an


y
z axis at an arbitrary
point
x Move axis to origin
y
Rotate
z Move axis back
Can we rotate about an arbitrary axis
x
y
Yes, but complex, see Appendix
and watch video if interested
p15 RJM 17/08/23 CS3VR16 – Graphics - 3
© Prof Richard Mitchell 2023
RJM web pages
http://www.reading.ac.uk/~shsmchlr/jscomp/demo3D.
html
Can investigate transforms on
cube
Scale, Rotate, Translate, Shear

And view from different points


Can opt so cube drawn as cylinder

At drawing stage, instead of 6 faces


Have two octagon faces
Plus Eight rectangular faces
Not written yet … pseudo sphere
p16 RJM 17/08/23 CS3VR16 – Graphics - 3
© Prof Richard Mitchell 2023
And Build3D
http://www.reading.ac.uk/~shsmchlr/jscomp/build3D.h
tml
Can build model from 4 cubes/cylinders
Specifying transforms for each

We’ve introduced concept … reinforce next time

p17 RJM 17/08/23 CS3VR16 – Graphics - 3


© Prof Richard Mitchell 2023
3D Scenes – World to Eye
Transform
Our scene has objects, defined relative to their
origin, but resized, rotated, moved etc.
Scene is set of objects placed relative to its origin.
But could look like or

Depends where our eye is.


So transform each screen coord so relative to eye
For eye, z axis points to origin of scene.

p18 RJM 17/08/23 CS3VR16 – Graphics - 3


© Prof Richard Mitchell 2023
3D Scenes – W2E coords
How does it appear to eye?
1,1,-
1 xe
1,1,
1 ze
y
x ye
z
-1,- 1,-
1,1 1,1
Cube defined in Object Coords Need in Eye Coords
To view, transform to find positions relative to eye
If viewer (eye) moves, need to recalculate

p19 RJM 17/08/23 CS3VR16 – Graphics - 3


© Prof Richard Mitchell 2023
3D Scenes – W2E – 5 transforms
Newman and z z’ z y
Sproull: y z’ ’
Eye at xe,ye,ze x ’ x
To find transformation ’ ’
matrix: x y x y
Move to Eye Rot -90⁰ @ x
z y
z y z’ z ye
’ x

’ x ze xe
z’

x y
x y x y
0,0,ze on z’ ax Rot: 0,0,0 on z’ To Left Hand
p20 RJM 17/08/23 CS3VR16 – Graphics - 3
© Prof Richard Mitchell 2023
3D Scenes – W2E - details
If eye in object co-ords at xe,ye,ze, multiply matrices to
Displace by -xe,-ye,-ze
Rotate by -90⁰ about x axis
Rotate, by angle , around y axis
where if r = (xe2 + ye2), cos() = ye/r & sin() =
xe/r
Rotate, by angle , around x axis
and if r2 = (r2 + ze2), cos() = r/r2 & sin() =
ze/r2
Convert from left to right handed co-ordinate system
Resultant matrix, TM,
p21 RJM 17/08/23
applied to transform each x,y,z
CS3VR16 – Graphics - 3
© Prof Richard Mitchell 2023
3D Scenes – W2E matrices
Suppose Eye at 3, 4, 3.75 …
r = (xe2 + ye2) = 5, cos() = ye/r = 0.8, sin() = xe/r =
0.6
r2 = (r2 + ze2) = 6.25, cos() = r/r2 = 0.8, sin() = ze/r2

[ ][ ][ ][ ][ ]
1 =0 0.6
0 0 1 0 0 0 − 0.8 0 0.6 0 1 0 0 0 1 0 0 −3
0 Then
1 0 TM0 0found
0.8 −by
0.6 0 0 1 0 0 0 0 1 0 0 1 0 −4
0 0 −1 0 0 0.6 0.8 0 − 0.6 0 −0.8 0 0 −1 0 0 0 0 1 −3.75
0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1

[ ]
− 0.8 0.6 0 0
− 0.3 −0.4 0.9 0
=
− 0.5 −0.7 − 0.5 5.8
0 0 0 1
p22 RJM 17/08/23 CS3VR16 – Graphics - 3
© Prof Richard Mitchell 2023
3D Scenes – W2E - Examples
Examples
If eye at -3,4,3.75

[ ]
−0.8 − 0.6 0 0
0.3 −0.4 0.9 0
TM =
0.5 − 0.7 −0.5 5.8
0 0 0 1

If eye at -3,4,-3.75 If eye at 3,4,-3.75

p23 RJM 17/08/23 CS3VR16 – Graphics - 3


© Prof Richard Mitchell 2023
Lecture 3 - Summary
In this lecture we have continued using
4*4 transformation matrices
Rotation About a Point
(by translate, rotate, translate back)
Building a cuboid model from cubes
Rotate about a point on arbitrary axis
(very complex, so see appendix)
Conversion from world to eye coordinates
Later we work out which surfaces are
visible…
p24 RJM 17/08/23 CS3VR16 – Graphics - 3
© Prof Richard Mitchell 2023
Appendix 3D Scenes – 3D
Primitives
What about Arbitrary Rotations?
z
If you are interested, the
concepts behind these are on x
y
the next few slides.

The operations are found using


lots
and lots
and lots of algebra …

p25 RJM 17/08/23 CS3VR16 – Graphics - 3


© Prof Richard Mitchell 2023
3D Scenes – 3D Primitives
What about Arbitrary Rotations?
A more general rotation can be achieved by defining
 A point p1 relative to (or within) the object
 A normalised direction vector, k, defined by a
second point p2 relative to point p1 and
 The angle  about this axis to rotate
The final rotation (defined on following slides) is
𝐨𝐛 𝐣𝑟𝑜𝑡𝑎𝑡𝑒𝑑 = 𝐓 𝑇𝑥 ,+ 𝐩 𝐓 𝑅𝑜𝑡 , 𝐤 , 𝜃 𝐓𝑇𝑥 ,− 𝐩 𝐨𝐛 𝐣𝑜𝑟𝑖𝑔𝑖𝑛𝑎𝑙
1 1

p26 RJM 17/08/23 CS3VR16 – Graphics - 3


© Prof Richard Mitchell 2023
3D Scenes – 3D Primitives
Equivalent Axis Rotations 𝐓 𝑅𝑜𝑡, 𝐤 , 𝜃
Achieved by
aligning equivalent axis with one principle axis, eg
z, rotating about that axis,
putting everything back in its original orientation
This initial alignment requires 2 rotations and hence
2 reverse rotations plus the required rotation in
middle
This gives FIVE rotations in total
See next slide; maths + example at end if interested
p27 RJM 17/08/23 CS3VR16 – Graphics - 3
© Prof Richard Mitchell 2023
3D Scenes – 3D Primitives
Equivalent Axis Rotations 𝐓 𝑅𝑜𝑡 , 𝐤 , 𝜃
y

𝐴= √ 𝜅 +𝜅
kx
kz
k 2 2
sin 𝛽=𝜅𝑥
q
A 𝑦 𝑧
ky
5 g
1 x
3
4
z
q -b 2

𝐓 𝑅𝑜𝑡, 𝐤 ,𝜃 = 𝐓 𝑅𝑜𝑡 , 𝐱 ,+𝛾 𝐓 𝑅𝑜𝑡 , 𝐲 ,− 𝛽 𝐓 𝑅𝑜𝑡, 𝐳 ,𝜃 𝐓 𝑅𝑜𝑡 , 𝐲 ,+𝛽 𝐓 𝑅𝑜𝑡 , 𝐱 ,−𝛾


p28 RJM 17/08/23 CS3VR16 – Graphics - 3
© Prof Richard Mitchell 2023
3D Scenes – 3D Primitives –
EAR
Equivalent Axis Rotations 𝐓 𝑅𝑜𝑡 , 𝐤 , 𝜃
An equivalent axes rotation transform is then found

[ ]
as
𝑥𝑥 𝑣 +𝑐 𝑥𝑦 𝑣 − 𝑧𝑠 𝑥𝑥 𝑣 + 𝑦𝑠 0
𝑦𝑥 𝑣 + 𝑧𝑠 𝑦𝑦 𝑣+ 𝑐 𝑦𝑧 𝑣 − 𝑥𝑠 0
𝐓 𝑅𝑜𝑡 , 𝐤 , 𝜃 =
𝑧𝑥 𝑣 − 𝑦𝑠 𝑧𝑦 𝑣+ 𝑥𝑠 𝑧𝑧 𝑣+ 𝑐 0
0 0 0 1

where shorthand is used, with


s = sin() c = cos() v=1–c
and 𝐤 = [ 𝑥 , 𝑦 , 𝑧 ] ‖𝐤 ‖ = 1
p29 RJM 17/08/23 CS3VR16 – Graphics - 3
© Prof Richard Mitchell 2023
3D Scenes – 3D Primitives –
EAR

[ ]
RECAP 𝑥𝑥 𝑣 +𝑐 𝑥𝑦 𝑣 − 𝑧𝑠 𝑥𝑧 𝑣 + 𝑦𝑠 0
𝑦𝑥 𝑣 +𝑧𝑠 𝑦𝑦 𝑣+𝑐 𝑦𝑧 𝑣 − 𝑥𝑠 0
𝐓 𝑅𝑜𝑡 , 𝐤 , 𝜃 =
𝑧𝑥 𝑣 − 𝑦𝑠 𝑧𝑦 𝑣+𝑥𝑠 𝑧𝑧 𝑣+𝑐 0
0 0 0 1
The final Transform is found as:
1. Translate to origin
2. Rotate about k by 
3. Translate back to original position

𝐨𝐛 𝐣𝑟𝑜𝑡𝑎𝑡𝑒𝑑 = 𝐓 𝑇𝑥, 𝑏𝑎𝑐𝑘 𝐓 𝑅𝑜𝑡 ,𝐤 , 𝜃 𝐓 𝑇𝑥, 𝑡𝑜𝑜𝑟𝑖𝑔𝑖𝑛 𝐨𝐛 𝐣𝑜𝑟𝑖𝑔𝑖𝑛𝑎𝑙


p30 RJM 17/08/23 CS3VR16 – Graphics - 3
© Prof Richard Mitchell 2023
3D Scenes – 3D Primitives –
EAReg
Example
The origin of an object is at a position [10,20,30].
The object is to be rotated by 150 about an axis
defined by the vector pointing from the object origin
to another point on the object at [25, 10, 10].
Find the transform that implements the above.

𝐓1 = 𝐓𝑇𝑥 ,𝑏𝑎𝑐𝑘 𝐓 𝑅𝑜𝑡, 𝐤 , 𝜃 𝐓𝑇𝑥 ,𝑡𝑜 𝑜𝑟𝑖𝑔𝑖𝑛

p31 RJM 17/08/23 CS3VR16 – Graphics - 3


© Prof Richard Mitchell 2023
3D Scenes – 3D Primitives –
EAReg
Example 𝐓1 = 𝐓𝑇𝑥 ,𝑏𝑎𝑐𝑘 𝐓 𝑅𝑜𝑡 , 𝐤 , 𝜃 𝐓𝑇𝑥 ,𝑡𝑜 𝑜𝑟𝑖𝑔𝑖𝑛

[ ] [ ]
1 0 0 − 10 1 0 0 +10
0 1 0 − 20 0 1 0 +20
𝐓𝑇𝑥 ,𝑡𝑜 𝑜𝑟𝑖𝑔𝑖𝑛 = 𝐓 =
0 0 1 − 30 𝑇𝑥 ,𝑏𝑎𝑐𝑘
0 0 1 +30
0 0 0 1 0 0 0 1

The rotation matrix is

[ ]
𝑥𝑥 𝑣 +𝑐 𝑥𝑦 𝑣 − 𝑧𝑠 𝑥𝑧 𝑣 + 𝑦𝑠 0
𝑦𝑥 𝑣 + 𝑧𝑠 𝑦𝑦 𝑣 +𝑐 𝑦𝑧 𝑣 − 𝑥𝑠 0
𝐓 𝑅𝑜𝑡 , 𝐤 , 150 =
𝑧𝑥 𝑣 − 𝑦𝑠 𝑧𝑦 𝑣+ 𝑥𝑠 𝑧𝑧 𝑣+ 𝑐 0
0 0 0 1

p32 RJM 17/08/23 CS3VR16 – Graphics - 3


© Prof Richard Mitchell 2023
3D Scenes – 3D Primitives –
EAReg
Example 𝐓1 = 𝐓𝑇𝑥 ,𝑏𝑎𝑐𝑘 𝐓 𝑅𝑜𝑡 , 𝐤 , 𝜃 𝐓𝑇𝑥 ,𝑡𝑜 𝑜𝑟𝑖𝑔𝑖𝑛
The direction vector is:

[ ]
15
−10

[][][ ] [ ]
25 10 15 −20 0.5571
𝐕 01 = 10 − 20 = − 10 𝐤 = = −0.3714
10 30 − 20 √ 1 52 +1 02 + 20 2 − 0.7428

Check that the direction vector is of unit length

|𝐤| = √ ( 0.5571 ) + (− 0.3714 ) + ( −0.7428 ) = 1.000025 ...


2 2 2

OK to the 4DP retained in this illustration


NB Programmes need much higher accuracy
than 4DP
p33 RJM 17/08/23 CS3VR16 – Graphics - 3
© Prof Richard Mitchell 2023
3D Scenes – 3D Primitives –
EAReg
[] [ ]
𝑥 0.5571
Example 𝐤 = 𝑦 = − 0.3714
Equivalent axis rotation,  = 150 𝑧 −0.7428
s = sin() = 0.5 c = cos() = –0.866 v=1–c

[ ]
= 1.866
− 0.2869 − 0.0147 −0.9578 0
− 0.7575 − 0.6086 0.2362 0
𝐓 𝑅𝑜𝑡 , 𝐤 , 150 =
− 0.5865 0.7933 0.1635 0
0 0 0 1

𝑇
Check that resulting matrix
𝐑 × 𝐑 is= a𝐈3true rotation

(The inverse of a rotation matrix is the transpose of


the
p34 RJM33
17/08/23rotation matrix).
CS3VR16 – This is- 3true to ~4DP here
Graphics
© Prof Richard Mitchell 2023
3D Scenes – 3D Primitives –
EAReg
Example 𝐓1 = 𝐓𝑇𝑥 ,𝑏𝑎𝑐𝑘 𝐓 𝑅𝑜𝑡 , 𝐤 , 𝜃 𝐓𝑇𝑥 ,𝑡𝑜 𝑜𝑟𝑖𝑔𝑖𝑛
The final transform is

[ ][ ][ ]
1 0 0 +10 − 0.2869 −0.0147 − 0.9578 0 1 0 0 −10
0 1 0 +20 − 0.7575 −0.6086 0.2362 0 0 1 0 −20
𝐓1 =
0 0 1 +30 − 0.5865 0.7933 0.1635 0 0 0 1 −30
0 0 0 1 0 0 0 1 0 0 0 1

[ ]
− 0.2869 −0.0147 − 0.9578 41.8981
− 0.7575 −0.6086 0.2362 32.6608
=
− 0.5865 0.7933 0.1635 15.0932
0 0 0 1

p35 RJM 17/08/23 CS3VR16 – Graphics - 3


© Prof Richard Mitchell 2023
3D Scenes – 3D Primitives –
EAReg
Example 𝐓1 = 𝐓𝑇𝑥 ,𝑏𝑎𝑐𝑘 𝐓 𝑅𝑜𝑡 , 𝐤 , 𝜃 𝐓𝑇𝑥 ,𝑡𝑜 𝑜𝑟𝑖𝑔𝑖𝑛
The final transform is

[ ]
− 0.2869 −0.0147 − 0.9578 41.8981
− 0.7575 −0.6086 0.2362 32.6608
=
− 0.5865 0.7933 0.1635 15.0932
0 0 0 1
Top left 33 matrix is compound rotation of object
Top right 13 matrix represents additional
translation

p36 RJM 17/08/23 CS3VR16 – Graphics - 3


© Prof Richard Mitchell 2023
3D Scenes – 3D Primitives -
Final
Example 𝐓1 = 𝐓𝑇𝑥 ,𝑏𝑎𝑐𝑘 𝐓 𝑅𝑜𝑡 , 𝐤 , 𝜃 𝐓𝑇𝑥 ,𝑡𝑜 𝑜𝑟𝑖𝑔𝑖𝑛
Note the 2 points that define the axis of rotation are
• The origin at a position [10,20,30]
• The second point at [25, 10, 10]
Neither point (or any points along the axis) should
be changed by application of the compound
transformation
• This is easily verified (e.g. Matlab)
• This is also a good test to ensure your code is
correct
(Also easily seen: off-axis
p37 RJM 17/08/23 CS3VR16 points are
– Graphics - 3 transformed as
© Prof Richard Mitchell 2023
required)

You might also like