You are on page 1of 187
| DirectX Pipeline and Programming : Introduction To DirectX 11 : COM, Textures and Resources Formats, The swap chain and Page flipping, Depth Buffering, Texture Resource Views, Multisampling Theory and MS in Direct3D, Feature Lev Direct3D 11 Rendering Pipeline : Overview, Input Assembler Stage (IA), Vertex Shader Stage (VS), The Tessellation Stage (TS), Geometry Shader Stage (GS), Pixel Shader Stage (PS), Output merger Stage (OM), Understanding Meshes or Objects, Texturing, Lighting, Blen | Interpolation and Character Animation : Unit IT lee + The Trigonometric Ratios, Inverse Trigonometric Ratios, | Trigonometric Relationships, The Sine Rule, The Cosine Rule, Compound Angles, Perimeter Relationships, Interpolation : Linear Interpolant, Non-Linear Interpolation, Trigonometric, Interpolation, Cubic Interpolation, Interpolating Vectors, _Interpolating Quaternions. Curves : Circle, Bezier, B-Splines | Analytic Geometry : Review of Geomeuy, 2D Analytic Geometry, | Intersection Points, Point in Triangle, and Intersection of circle with straight line. (Refer chapters 3, 4 and 5) 1s) Introduction to Rendering Engines : Understanding the current market Rendering Engines. Understanding AR, VR and MR.Depth Mappers, Mobile Phones, Smart Glasses, HMD’s. Unity Engine : Multi-platform publishing, VR + AR : Introduction and Unit 11 | working in Unity, 2D, Graphics, Physics, Scripting, Animation, Timeline, Multiplayer and Networking, UI, Navigation and Pathfinding, XR, Publishing. Scripting : Scripting Overview, Scripting Tools and Event Overview. XR: VR, AR, MR, Conceptual Differences. SDK, Devices. (Refer chapters 6, 7, 8 and 9) 151 (Book Code : MCOI8A) he : NW Game Programming (MU B.Se. Comp. Sem-V) _1 Table of Contents UN Chapter 1: Cartesian Coordinate System and Vectors 1-10 1-27 v 1.1‘ The Cartesian XY-Plane. v Syllabus Topic : Function Graphs. 1.2 Function Graphs... v Syllabus Topic : Geometric Shape: 13 Geometric Shapes v Syllabus Topic : Polygonal Shapes... 14 Polygonal Shape: v Syllabus Topic : Areas of Shapes 1.5 Areas of Shapes nn - ¥ Syllabus Topic : Theorem of Pythagoras 1.6 Theorem of Pythagoras in 2D v Syllabus Topic : Co-Ondinate 1.7 3D Co-Ordinates ¥ Syllabus Topic : Theorem of Pythagoras in 3D... 1.7.1 Theorem of Pythagoras in 3D .. v Syllabus Topic : 3D Polygons. 1.7.2 3D Polygons... ¥ Syllabus Topic : Euler's Rule .nnsinnnsn 18 Euler's Rule... 19 Vectors. 19.1 2D Vectors. 1.92 Vector Notation... 1.9.3 Graphical Representation of Vectors, 1.9.4 Magnitude of a Vector. 195 3D Vector v Syllabus Tople : Vector Manipulation. 1.10 Vector Manipul: v Syllabus Topic : Multiplying a Vector by a Scalar. 1.10.1. Multiplying a Vector by a Scalar svn ¥ Syllabus Topic : Vector Addition and Subtraction 1.10.2 Vector Addition and Subtraction... 1.103 ¥ Syllabus Topie : Unit Vectors. 1.10.4 Unit Vectors... a eee c ne ny 3G Game Programming (MU B.Se. Comp. SemV)_2 23 1-16 Syllabus Topic : Canesian Vectors. 1.105 — Cartesian Vectors. ‘Syllabus Topic : Vector Multiplication. Vector Multiplication Syllabus Topic : Scalar Product LULL Scalar Product. ‘Syllabus Topic : Example of the Dot Product. 1.11.2. Example of the Dot Product 1.11.3 ‘The Dot Product in Lighting Calculations. Syllabus Topic : The Dot Product in Back-Face Detection. 1.11.4 The Dot Product in Back-Face Detection. Syllabus Topic : The Vector Product. LILS — The Vector Product. Syllabus Topic : The Right-Hand Rul ‘The Right-Hand Rule 124 Syllabus Topic : Deriving a Unit Normal Vector fora Triangle 1-24 Deriving a Unit Normal Vector for a Triangle... 14 Areas. 1-25, Syllabus Topic : Calculating 2D Areas. Calculating 2D Areas 22.1 Translation. 2.2.2 Scaling.. 22.3. Reflection... 2.3.2. The Determinant of a Matrix. Syllabus Topic : Homogeneous Coordinates. Homogeneous Coordinates... 24.1 2D Translatics 2.4.2 2D Scaling. 2.43 2D Reflections... 2.44 2D Shearing.. 2.45 2D Rotation .. 2.46 2D Scaling 26 27 24.7 2D Reflections. 248 2D Rotation about an Arbitrary Poi Syllabus Topic : 3D Transformations 3D Transformations... 25.1 3D Translation. 252 3D Scaling.. 2.5.3 3D Rotations 2.5.4 Gimbal Lock. 25.5 — Rotating about an Axis. 25.6 3D Reflections. Syllabus Topic : Change of Ax 2-23 ‘Change of Axes 2.6.1 2D Change of Axes Syllabus Topic : Direction Cosines. Direction Cosines .. 271 Positioning the Virtual Camera. 2.72 Direction Cosines 273 Euler Angle Syllabus Topic : Rotating a Point about an Arbitrary Axis.. Rotating a Point about an Arbitrary Axis. 28.1 Quatemions.. 28.2 Adding and Subtracting Quaternion’ 283 — Multiplying Quaternions 284 — The Inverse Quatemion.. 285 Rotating Points about an Axis. 28.6 Roll, Pitch and Yaw Quatemnions. 28.7 Quatenions in Matrix Form.... 288 Frames of Reference. Syllabus Topic : Transforming Vectors. Transforming Vectors. Determinants Syllabus Topic : Perspective Projection, Interpolation. Perspective Projection, Interpolation . DirectX Syllabus Topic : Understanding GPU and GPU Architectures ‘Understanding GPU and GPU Architecture Syllabus Tople : How they are Different from CPU Architectures? How they are Different from CPU Architectures Syllabus Topic : Understanding how to Solve by GPU?. ‘Understanding how to Solve by GPU’ © Chapter Ends 4 Table of Contents gain Comp Se) 4 Tal Cor ‘Chapter 3: Introduction to DirectX 11 Syllabus Topic : Introduction To DirectX 1 3.1 Introduction To DireetX 11 3.11 Setting Up Project in Visual Studio 2010. 3.L.L1 Linking the DiretX Libraries 3412 To Link the Library File Perform the following Steps.. 3.1.1.3 Setting up the Search Paths... a 3.1.1.4 Adding the Source Code and Building the Project. Syllabus Topie : COM. 32 Con... ee Syllabus Topic : Textures and Data Resource Formats 39 3.3 Textures and Data Resource Formats 39 a Syllabus Topic ; The Swap Chain and Page Flipping. 3.4 The Swap Chain and Page Flipping. v Syllabus Topic : Depth Buffering 3.5 Depth Buffering... Syllabus Topic : Texture Resource Views. 3.6 Texture Resource Views... Syllabus Topic : Multisampling Theory 3.7 Multisampling Theory.. Syllabus Topic : Multisampling in Direct3D. 3.8 Multisampling in DirecD ... ¥ Syllabus Topic : Feature Level 3.9 Feature Levels © Chapter Ends. Chapter 4: Direct 3D 11 Rendering Pipeline 41 to 4 ¥ Syllabus Topic : Overview of The Rendering Pipeline 4al ‘Overview of the Rendering Pipeline. Y Syllabus Topic : The Input Assembler Stage 4.2 The Input Assembler Stage .. 421 Vertices 4.2.2 Primitive Topology. 4.2.2.1 Point List 42.2.2 Line Strip 4.2.2.3 Line List... 4.2.2.4 Triangle Str 4.2.2.5 Triangle List a W ame Programming (MU B.Sc. Comp.- Sem) _ 8 Table of Contents 48 49 4.10 41 46 42.2.6 Primitives with Adjacency 422.7 Control Point Patch List 423 Indices.. Syllabus Topic : The Vertex Shader Stage. ‘The Vertex Shader Stage... 43.1 Local Space and World Space... 432 View Spac Projection and Homogeneous Clip Space. Defining a Frustum Projecting Vertices nm Normalized Device Coordinates (NDC) ‘Writing the Projection Equation with a Matrx.. Normalized Depth Valué 43.3.6 XMMatrixPerspectiveFovLH. Syllabus Topic : The Tessellation Stages (TS). The Tessellation Stages ‘Syllabus Topic : The Geometry Shader Stage (GS) ‘The Geometry Shader Stag Syllabus Topic : Clipping Clipping. The Raseization Stage. 4.7. Viewport Transfort 0 47.2 Backface Culling 473 Vertex Attribute Interpolation 0. Syllabus Topic : The Pixel Shader Stage (PS).. ‘The Pixel Shader Stage... Syllabus Topic : The Output Merger Stage (OM) ‘The Output Merger Stage. Syllabus Topic: Understanding Meshes or Objects, Tearing, Lighting, Blending. Understanding Meshes or Objects, Texturing, Lighting, Blending 4.10.1 Understanding Meshes or Object... 4.10.2 Texturing 4.10.3 Texture Coordinates 4.10.4 Creating and Enabling a Texture. 4.111 Light and Material Interaction 4,112 Normal Vectors. 4,11.2.1 Computing Normal Vectors 4.11.22 Transforming Normal Vectors 4.11.3. Lamber’s Cosine Law 4.11.4 — Diffuse Lighting 4.115 Ambient Lighting ... 4.11.6 — Specular Lighting 47 4118 4119 4.11.9.1 Atteauation. 4119.2 Range. 4.11.10 Spotlights.. 4.11.10.1 Lighting Structures. i Syllabus Topic : Blending. | 4.12 Blending .. 4.12.1 The Blending Equatic 4.12.2 Blend Operations. 4.12.3 Blend Factors. 4.124 Blend State. © — Chapter Ends. eS ‘Chapter 5: Interpolation and Character Animation “ EE —E———————————— EE ¥ Syllabus Topic : Trigonometry 5.1.1 The Trigonometric Ratios .. 2 | 5.2 Exampl $2 v Syllabus Topic : Inverse Trigonometric Ratios .. 53 5.1.3. Inverse Trigonometric Ratios. 53 v ‘Syllabus Topic : Trigonometric Relationshi 53 5.1.4 Trigonometric Relationships.. 53 ¥ Syllabus Topic : The Sine Rule S15 The Sine Rule ¥ Syllabus Topic : The Cosine Rule. 5.1.6 The Cosine Rul ¥ Syllabus Topic : Compound Angles 5.1.7 Compound Angles. | v Syllabus Topic : Perimeter Relationships. 5.1.8 Perimeter Relationships. v Syllabus Topic : Interpolation. 5.2 Interpolation ¥ Syllabus Tople : Linear Interpolant 5.2.1 Linear Interpolant.. ‘Game Programming (MU B.Sc. Comp. - Sem-V) _7 Table of Contents Syllabus Topic : Non-Linear Interpolatio 5.22 Non-Linear Interpolation. Syllabus Topic : Trigonometrc Interpolation. $22.1 Trigonomettic Interpolatios Syllabus Topic : Cubic Interpolation $22.2 Cubic Interpotatios Syllabus Topic : Interpolating Vectors 523 Interpolating Vectors Syllabus Topic :Interpolating Quaternion 5.24 Interpolating Quaternion Syllabus Topic : Curves. Curves Syllabus Topic : The Circe... 53.1 The Circle 53.2 The Ellipse. Syllabus Topic : Bezicr Curves. Brezier Curves 54.1 Bemstein Polynomials. 54.2 Quadratic B’ezier Curves 543 Cubic Bemstein Polynomials 544 A Recursive B’ezier Formula. 545 Bezier Curves Using Matrices. Syllabus Topie : B-Splin. B-Splines 55.1 Uniform B-Splines 552 553 554 Analytic Geometry Syllabus Topic : Review of Geometry. 5.6.1 Review of Geometry. 5.6.1.1 Angles. 5.6.1.2 Intercept Theorems. $6.13 Golden Section 5.6.14 Triangles... 5.6.15 Centre of Gravity of a Triangle. 56.1.6 Isosceles Triangle. 5.6.1.7 5.6.1.8 5.6.1.9 ‘Game Programming (MU 5.10 $.6.1.10 ‘Theorem of Pythagoras. S.6.1.11 Quadrilaterals $.6.1.12 Trapezoid. 5.6.1.13 Parallelogram. 5.61.14 Rhombus... 5.61.15 Regular Polygon (n-gon) $.6.1.16 Circe... Syllabus Topic : 2D Analytical Geometry 2D Analytical Geometry 5.7.1” Equation of a Straight Lin $7.2 ‘The Hessian Normal Form 5.7.3 Space Partitioning, 5.7.4 ‘The Hessian Normal Form (HNF) from Two Points ‘Syllabus Topic : Intersection Points. Intersection Points... 58.1 Iotersection Pont of Two Sight Liss 5.8.2 Intersection Point of Two Line Segment Syllabus Topic : Points inside a Triangle. Points inside a Triangle 5.9.1 5.9.2 Chapter Ends.. ‘Chapter 6: Introduction to Rendering Engines v 61 v 62 Syllabus Topic : Understanding the Curent Market Rendering Engines. Understanding the Current Market Rendering Engines. Syllabus Topic : Understanding AR, VR and MR. Understanding AR, VR and MR 6.2.1 Augmented Reality. Applications of AR. Virtual Reali Applications of VI Mixed Reality Applications of MR ‘Syllabus Topic : Mobile Phones Mobile Phones Y Syllabus Topic : Smart Glasses. 64 Smart Glasses 64.1 Applications of small glasses 6-17 64.2 Disadvantages of smart glasses 6-18 % Syllabus Topic : HMD's..... “ 6-18 65 HMD's © Chapter Ends. —————————_—_— vm ‘Chapter 7: Unity Engine: Multi-platform Publishing VR + AR TA to7-61 a ¥ Syllabus Topic : Introduction and Working in Unity. re ionainiaaesmeres ol 7.1 Introduction and working in Unit TA TAA Steps for Unity Installatios Tl v 12 ¥ Syllabus Topic : Graphic: 13 Graphics ¥ Syllabus Topic : Physis.. 74 Physics. ¥ Syllabus Topic : Scripting... 73 Scripting . 75. MonoBehaviour. ¥ Syllabus Topic : Animation 7.6 Animation. ‘Animation workflow Using the Animation VieW soon ‘The Animated Properties Lis. ‘The Animation Timelin Dopesheet Timeline Mode... Curves Timeline Mode. Fitting Your Selection to the WindOW om Playback and frame navigation controls Locking the Window. Creating a New Animation Clip. ‘Adding Another Animation Cli How it Fits Together. Animating a GameObje Recording Keyframes... TTA Creating Keyframes in Preview Mode... 19 7.10 783 Reusing Timeline Assets. =a 784 Difference between the Animation window andthe Timeline Window. 7841 7842 785 Creating a Timeline Asset and Timeline instance, 7.85.1 Recording Basic Animation with an Infinite Cli ‘Syllabus Topic : Multiplayer and Networking. Matiplayer and Networking Multiplayer Overview High Level Scripting API _ Engine and Editor Integration Intemet Service. ‘NetworkTransport real-time Transport Layer Setting up a Multiplayer Project ‘The Network Manager. ‘Networked Player GameObjects... ‘Multiplayer-aware Script ‘Using the Network Manager Getting Started with the Network Manager ‘Game State Management 7.10.1.2 Render Modes. 7.10.1.3. Basic Layout. 7.10.14 Visual Components 7.10.2 Interaction Components 7 7.103 7.104 7104.1 Understanding Layout Elements 7.1042 Layout Element Component 7.1043 Understanding Layout Controllers 7.1044 Content Size Fite 7.1045 Layout Groups. 7.10.46 Driven Rect Transform properties. 7.10.5 Rich Test Syllabus Topic : Navigation and Path Finding ‘Navigation and Path Finding. TALL NavMesh Building Components 7112 NavMesh Modifier. : 7.113 NavMesh Modifier Volume TALA NavMesh Link... TALS — Creating a NavMesh Agent. 7.16 Creating the Character. 7.11.7 Creating an Off-mesb Link. 7.118 Navigation Areas and Costs.. Syllabus Topic : Publishing. Publishin © Chapter Ends Seripting Syllabus Topic : Scripting Overvies Scripting Overview. 8.1.1 Creating and Using Scripts. 8.12 Creating Scripts Anatomy of a Script file Variables and the Inspector Controlling GameObjects using Components. Accessing Components Accessing Other Objects. Linking GameObjects with Variables Event Functions 18 Physics Events. Time and Frame Rate Management. 83.1 Maximum Allowed Timestep. 832 Time Scale... 833 Capture Framerate 8s S38 sssi ase 85:10 ssn SS. Specal Folders ent Seige Compson Onset SS112 Script Compilation and Ascomly Defisiton Files ABS mmr How to Use Assembly Defines Fes" Mutuple Assembly Definition Files les Foie Hiecrehy ——————~ Assembly Defininon Files art Not Bald Systeen Fi68 n= SS123 Backwards Companhia and begitct Dependencies —————-— ssuz 865.1 8652 866 866.1 3662 W Game Programming (MU B Se, Comp.- SemV)_13 Table of Contents ‘Chapter 9: XR BIZ — Event Trigger rune 8.67 Ahead-of-Time Compile 8.6.8 Generic Virtual Methods Syllabus Tople : Scripting Tools and Event Overview.. ‘Scripting Tools and Event Overview — a 87.1 Obsolete API Warnings and Automatic Updates ....oroneononnsromen 8-51 8.7.2 Adjusting the Line Count 8.7.3 Stack Trace Logging. 8.74 Universal Windows Platform. 874.1 WebGL 875 Defining A Custom Messages 8.75.1 Input Modules. 8.75.2 Supported Events. 8753 Raycasters 8.7.6 Event System Manager. 87.7 — Graphic Raycaste 8.78 Physics Rayeasternnesnn 87.9 Physics 2D Raycaster .... 8.7.10 Standalone Input Module 8.7.11 Touch Input Module... © Chapter Ends... v on 92 Syllabus Topic : XR : VR, AR, MR, Conceptual Differences, SDK, Devices. 91 XR Differences, SDK and Devices .. 9.1.1 Google VR... OLD Daydream vs. Cardboard. 9.1.2 Hardware and Software Requirements for: ‘Google VR. 9.1.2.1 Recommended Hardware and Software Requirements. 9.1.2.2 Supported APIS and SDKS conn 9.1.3. Google VR Controllers and Input Devices... 9.1.3.1 Supported Feature Sets. 9-2 9.23.2 Differences between Holographic and Immersive Devices 9.23.3 Differences between Hololens and Immersive Device 9.23.4 WMR Hardware and Software Requirements. 96 97 vv Working on WMR——————" 93.1 Installation of Tools" Getting Staned (Gear VR). ‘Naming Convention and Detection. OpenVR.. 9.6.1 Input for Open VR Controllers.. 9.6.2 Naming Convention and Detection. Unity Input. HTC Vive Controllers -.... Upgrading a Project that Contains the SteamVR Package Model Question Paper.. ‘Appendix-A University Question Paper eat Cartesian Coordinate System and Vectors Syllabus Cartesian Coordinate system : The Cartesian XY-plane, Function Graphs, Geometric Shapes, Polygonal Shapes, Areas of Shapes, Theorem of Pythagoras in 2D, Coordinates, Theorem of Pythagoras in 3D, 3D Polygons, Euler's Rule. Vectors : Vector Manipulation, multiplying a Vector by a Scalar, Vector Addition and Subtraction, Position Vectors, Unit Vectors, Cartesian Vectors, Vector Muttiplication, Salar Product, Example of the Dot Product, The Dot Product in Lighting Calculations, ‘The Dot Product in Back-Face Detection, The Vector Product, The Right-Hand Rule, deriving a Unit Normal Vector for a Triangle Areas, Calculating 2D Areas. Syllabus Topic : The Cartesian XY-Plane 1.1 The Cartesian XY-Plane ~The Cartesian xy plane provides a method for translating pairs of related variables into a ‘graphical format. = Ttuses variables x and y normally. These variables describes a function like y = 3x +2 ~ Each value-of x has a corresponding value of y, and these values are located on intersecting axes as shown in Fig. 1.1.1. ~The straight line is formed by the set of points, itis given by the equation y = mx +c, The Xx axis represents the independent variables horizontally, whereas y axis represents the dependent variable vertically. ~ Theaxis x and y intersects at 90+ at a point called the origin. - i XQ came Programming (MU B.Sc. Comp.-Sem-V)_1-2_ Cartesian Coordinate System & Vectors v a Fig. 1.1.1 : xy Cartesian Plane for the e equation y = 3x +2 ‘The variables should be represented by x and y letters. The letters at the opposite end of the alphabet in order should substitute numbers. So the equation is written like y = axtbxte The estimation of the right and the above are positive where on the other hand the estimation of the left and the below are negative. On the Cartesian plane any point P is perceived as an ordered pair of numbers (x, y). The variables x and y are known as the Cartesian coordinates of P. Inside the program the ‘Mathematical functions and geometric shapes are given as lists of coordinates. ‘Syllabus Tople : Function Graphs 1.2 Function Graphs There are wide varieties of functions like linear, quadratic, cubic, trigonometric. The equation for these functions are given as : Linear function 1 yame+e Quadratic function : yravtbrtc sar thx ter+d sin(x) Cubic function ‘Trigonometric function | W came Programming (MU B.Sc. Comp.-Sem-V)_1-3_Carteslan Coordinate System & Vector ‘These functions generate a graph and recognize the function's origin. For the linear functions the graph is straight line, it is parabola for quadratics, itis ‘s’ shape for cubics and for trigonometric it is a wave like trace. In animation the graphs are used to control the movements of the objects, virtual camera and lights. As we know graph shows the relationship between x and y but it also shows the relationship between an activity like movement, size, brightness, rotation, color, etc., with time. Fig. 1.2.1 depicts the relationship between the brightness and the frame number. The progress of the time in animation frame is marked on horizontal, and related brightness of a virtual light source is recorded on the vertical axis. Such functions help the animator to understand the brightness of the light source for all frame of animation. Animator uses interactive software tools are used to make the changes to the function. 2468 10 12 14 16 18 20.24 26 28 30 32 Frames ——> Brightnoss —> Fig. 1.2.1: A function curve showing relationship between brightness and frame number —— Syllabus Topic : Geometric Shapes —eeeEeeeeeeeeeeEeeEeE 1.3 Geometric Shapes Computer graphics needs 2D shapes and 3D objects. For these 2D shapes and 3D objects there is some sort of numerical description. The geometric 2D shapes which are included are circle, polygons, mathematical functions, fractals, arbitrary curves etc. The 3D objects can be smooth, faceted, gaseous, furry, bumpy, etc. Here we are considering the 2D shapes. Syllabus Topic : Polygonal Shapes —SocVsea 1.4 Polygonal Shapes To construct a polygon a sequence of vertices is needed as shown in Fig. 1.4.1 ‘The neighbouring vertices pair is connected by a straight line. Middle points on the line are not clearly stored. If the starting of the sequence of vertices is not mentioned then the software's will state that the polygon has clockwise or anti clockwise sequence of vertices, The Fig. 1.4.1. Shows the anti clockwise sequence and it is also given in table form, where it shows that the starting vertex is (1, 1), but this is random. Fig. 14.1 : Polygon drawn using the four vertices given in the table ‘The list of vertex coordinates given in table can be used for various mathematical and arithmetical operation such as: ~ If the values of x and y are doubled and the vertices are drawn again then you can see the size of the shape is doubled and the form of the shape is preserved. ~ If the values of x and y are divide by 2 then the size of the shape is divided equally but its shape is preserved. ~ If you add 1 in every x coordinate and 2 to every y coordinate and e you again draw the Vertices then the size of the vertices will remain same but the vertices will get shifted 1 ‘unit horizontally and 2 units vertically. ‘This arithmetic manipulation of vertices is the foundation of shape and object transformations. Syllabus Topic : Areas of Shapes Areas of Shapes To calculate the area of the polygon shape the chain of coordinates is required . For example, list of coordinates is given as follows : | ee eee Vectors | ¥s %/¥% The area is calculated by 1 Uo Ys ~ 1 Yo) + (Ry Yo—XaYs) + (Xe Ya — Xa Yo) + (3 Yo- NOY] (1.5.1) ‘Now you can see that the calculation is a sum of the result of multiplication of x and next ‘y, minus the multiplication of next x and current y. If the last vertex is selected then it is paired with the first vertex to complete the process. The result is then divided equally to give the area. y Fig, 15.1 : Caleulation of the distance between two points For example, let's apply (1.5.1) to the shape described in Fig. 1.4.1 : FI x 1-31) +B x2-3 x1) + x3-1%2)4(1x1-1%3)] $24347-2) = 3 We found after examination thatthe calculated area is correct area. The features of this method are: 1, This method work with any number of vertices and with any type of arbitrary shape. 2. If the original set of coordinates is given clockwise then the result of calculated area is negative that is we will get negative area. This means that vertex sequence and area are calculated. W ame Programming (MU B.Sc, Comp.-SemV)_1-6_Cartesan Coordinate Systom & Veen, To demonstrate his feature, the orginal verices are reversed toa clockwise sequence ag follows: Fl x31 1) + 1x2-3x3) + X1-3x2)+]X1- 1D) $e-7-342) = ‘The minus sign shows that the vertices are in a clockwise sequence, Syllabus Tople : Theorem of Pythagoras In 2D 1.6 Theorem of Pythagoras In 2D To calculate the distance between two points we can use the theorem of Pythagoras. Let apply the theorem of Pythagoras on the two arbitrary points Pl (x,, y;) and P2 (x2, y3) shown in Fig. 1.5.1. The distance Ax = x, ~ x, and Ay = y, ~y, therefore, the distance d between P1 and P2 is calculated by a= Jaeeay eu(1.6.1) @ o Fig. 1.6.1: (a) A left-handed system. (b) A right-handed system Syllabus Topic : Co-Ordinates 17 3D Co-Ordinates ‘As we have seen that to locate a point on 2D Cartesian plane we need x and y coordinates but it is not in case of 3D. In 3D we need x, y, and z coordinates to locate a point. But for Positioning the third z- axis we have two choice. Fig. 1.6.1 shows the two possibilities, left handed axial system and right handed axial system. - In the left-handed system the left hand is aligned with axes so the thumb can aligns with the x axis ,middle finger align to 2 axis and first figure align to y axis. Re ge 2 W ame Programming (MU B.Sc. Comp.- SemV)_1-7_Cartesian Coordinate System & Vectors = The right-handed system uses the right hand for alignment, and the alignment is same as left hand coordinate system. The choice of using the axial system between these two is depends on you but you should know about the system which is used in commercial computer graphics packages, - The issue arises when we project the 3D point on 2D plane but in general it has a lefi-handed axial system. It will be used in perspective projections. In this book we are using a right-handed system as shown in Fig.1.7.1, which also dipicts a point P with its coordinates. ‘Syllabus Topic : Theorem of Pythagoras in 3D 1.7.1 Theorem of Pythagoras In 3D ‘The 3D Pythagoras theorem is an of 2D Pythagoras rule. In 3D Pythagoras theorem works ‘on higher dimensions. Here, two random points P, and P; are given, they are Pi (Xs ¥4, 2) and P2 (Xp, Yo, 2) The distance between x, y and zis calculated as: Ax = m-% ay = ya-¥1 ba = 4-2, Now the distance between P,and P, is calculated as follows: d= Vaeeayeae eT) Syllabus Topic : 3D Polygons 1.7.2 3D Polygons - Triangle is the simplest 3D polygon. The three vertices of the triangle lie on a unique plane so we can say that Triangle is always a planar. - In computer graphics rendering algorithms always assume that polygons are planar, so Planarity is important in computer graphics. For example, you can easily define a quadrilateral in 3D where the vertices are not located on one plane. When you render and animate such type of polygon you will get fake highlights as a result. The reason behind itis geometric techniques give tise to errors. Game Programming (MU B.Sc. Comp, - Sem-V)_1-8_Carteslan Coordinate System & Vectors set + Right-handed axial system for the coordinates of a point P Fig. 1 Syllabus Topic : Euler’s Rule 1.8 Euler's Rule This rule shows the relationship between vertices, edges and the faces of the 3D polygon object. . faces + vertices = edges +2 +(1.8.1) For example, there is a cube with 8 vertices, 12 edges and, 6 faces, so the following ‘equation is satisfied. Faces + vertices = edges +2 6+8 1242 14 14 1.9 Vectors You can also apply Euler's rule on the geometric database to discover whether it consists of fake features. ~ Vectors provide few powerful techniques for computing angles between lines and the orientation of surfaces. What's more vectors additionally give a coherent structure to compute the behaviour of dynamic objects in computer animation and illumination models in rendering. — We frequently use a single number to represent daily lives quantities like age, height, shoe size, waist and chest measurements. Based on our age the magnitude of the number is depending and also the use of metric. Such type of quantities is known as scalars. In computer graphics scalar quantities contains width, color, height, brightness depth, number of frames, etc. eee EO EEE W came Programming (MU B.Sc. Comp. - Sem-V)_1 Cartesian Coordinate System & Vectors - There are few things which need in excess of one number to represent them like weight, wind, force, sound and velocity, and one cannot represent them accurately by using a single number. For example, the force used to pick up an object has a value and a direction. Likewise, the speed of a moving object is estimated as far as its speed (eg. miles every hour) and a direction such as, north-west. The mariner realizes that wind has a magnitude and a direction and the similar way Sound, as well, has intensity and a direction, Such quantities are known as vectors. - Incomputer graphics, vectors are generally made of two or three numbers. 1.9.1 2D Vectors Computer graphics utilizes 2D and 3D vectors. First we will consider 2D context vector notation and then 3D vector notations. 1.9.2 Vector Notation - Ascalar is a name for a single numeric number, for example, x. The vector contains two ‘or more numbers and by using the bold font vectors symbolic name is printed. It will be helpful to distinguish it from a scalar variable. For example, n, I and Q. When you assign a value to a scalar, standard algebraic notations are used. X=4 - On the other hand, when a vector is assigned its numeric values, as following then it is known as column vector . z me (] = The numbers 3 and 4 are called the components of n their positions in bracket are major. ‘A row vector is formed when the components are transposed horizontally, n = (3 4] The superscript reminds us of the transposition. 1.9.3 Graphical Representation of Vectors ~ An arrow is used to show direction of a number and to specify magnitude, as vectors have to set direction and magnitude. Weather map uses this method to interpret graphical data, but practically itis not possible for algebraic manipulation, = Cartesian coordinates offer an outstanding system for visualizing vectors and allowing them to be included within the classical structure of mathematics. A vector represented by a short line segment is shown in Fig. 1.9.1. Game Prograrnming (MU B.Sc. Comp. - Sem-V)_1-10_Cartasian Coordinate Systom & Vectors — Vectors magnitude is presented by the length of the line, and direction is defined by the orientation, In the figure the line doesn’t have a direction. If you add an arrow overheag then also the arrowhead has no mathematical realism. Fig. 19.2 : Two vectors r and s with same magnitude and opposite directions. To determine the direction of the line, first find the vector’s tail and then measure its ‘components along the x and y axes. ~ As shown in Fig. 1.9.2. The tail of the vector ris given by (x), y,) = 1 =(1, 2) and its head by (Xz y2) = (2, 3). On the other hand for vector s, tail is given by (x3, y;) = (2, 2) and its head by (x,, 1,1). ~The x-and y-components for r are calculated as follows : % = (%)-%) % = M-y) x = 2-N=1 y, = G-2=1 x= y =1 ~ While the components for s are calculated as follows : (%-%) — These are the negative values of x, and y, that decides the vector’s direction. Generally the coordinates of a vector's head and tail are (X, yy) and (x, y.) correspondingly, its components Ax and Ay given as follows: AX = (%-%) dy = M%-y) oo(1.9.1) ~~ W came Programming (MU B.Sc. Comp.-Sem-V)_1-11_Catesian Coordinate System & Vectors You cansee that the vector does not have unique position in space. It doesn’ translation matter where we place the vector : providing we preserve it’s length and oricentation, its component will not change. 1.9.4 Magnitude of a Vector - The magnitude of the vector r is given by liril and is calculated by using the theorm of Pythagoras to itscomponents. rll = fax +ay? (1.9.2) ‘The example illustrate the idea : For example, the vector r is given by (x. y) = (3, 4) and Gey = (LD). The x and y components are 2 and 3 respectively. So, its magnitude is equal toJ2+3" = 3.606. Fig. 1.9.3 shows different vectors, and their properties are listed in Table 1.9.1. ‘Tuble 1.9.1 : Values assoclated with the vectors shown in Fig.1.9.3 Sa [Ye |] ¥| Ax | Ay | llvectoril 2/0 j]0/0)2)0 z 0|2;o0;0)/0)}2 2 -2] 0 |0/0|-2]} 0 2 0 |-2/0)0) 0 |-2 2 | a0) 0) 212) 8, Ne -1} 1 fo}of-1}1] V2 -1}-1Jofo]1|-1] 1{-1fo}o{1}-1] Fig. 1.9.3 : Eight vectors for the coordinates given in the Table 1.9.1 Game Programming (MU B.Sc. Comp.- Sem-V)_1-12_Cartaslan Coordinate System & Vecto,, 1.9.5 3D Vectors = The 3D vectors play very important part in animation. As we have seen Vector exampjq, in 2D. - You can easily extend the notations of 2D to embrace an extra dimension Fig. 1.9.4 depicts the 3D vector r with its components, head, tail and magnitude annotateg The components and the magnitude of the 3D vectors are given by Ax = (%-X%) ++(1.9,3) dy = W-YW ++(1.9.4), Az = (4-2) +=(1.9.5) rll = fore ay ear (1.9.6) Fig 1 : The 3D vector with components Ar, Ay, Az, which are the differences between the head and tail coordinates, ‘Syllabus Toplc : Vector Manipulation 1.10 Vector Manipulation ~ Vectors are not same as scalars they are different, so a set of rule is prepared to control the interaction between the two mathematical entities with each other. - For example, we have to not only consider the vector addition, subtraction and multiplication but also how a vector can be modified by a scalar. - Accordingly let's begin with multiplying a vector by a scalar, Game Programming (MU B.Sc. Comp. - Sem-V) _1-13_Carteslan Coordinate System & Vectors Syllabus Toplc : Multiplying a Vector by a Scalar 1.10.1 Multiplying a Vector by a Scalar - The vector n is given and 2n means the components of the vector are doubled. - Forexample: 2 4 n = |3) then 2n=| 6 4. 8. — You can see the logical similarities. If we divide the vector n by 2 then its components are halved. ~ Keep in mind that only magnitude of the vector changes its direction remains same. Ifyou want to increase all the components of vector n by 2 then simply add another vector ‘whose components are equal to 2. Syllabus Topic : Vector Addition and Subtraction 1.10.2 Vector Addition and Subtraction Given vectors r and s, 1 +8 is defined as, A 8 XX Hs =| Y+% (1-101) uth, sis defined as, Roky rs =| ¥-% (110.2) 4%, Vector addition is commutative, atb= bea (1.103) Ell -E) E+ I Game | yning (MU B.Sc. Comp.-Sem-V)_1-14 Cartesian Coordinate & Vectorg Unlike scalar subtraction, vector subtraction is not commutative, a-b # baa (1104) El-El-EE] ~ Fig. 1.10.1 shows the graphical representation of vectors r and s, To the head of the vector ¥ the tail of the vectors is attached. ~The resultant vector t = r + s is the addition of the equivalent components of r and s together. ~The graphical representation for rotation-s is shown in Fig. 1.10.2. To create an equal and the opposite vector the component of the vector are reversed and then they are attached to rand added, x Fig. 110.1 : Addition of vectors r+s Fig. 1.10.2 : Subtraction of veetors r-s Syllabus Tople : Position Vectors 1.10.3 Position Vectors For any given point P (x, y, 2), a position vector is created by assuming that P is the head of the vector and the origin is the tail of the vector. The coordinates of the origin (0, 0, 0) and the components of the vectors are x, y, z, thus, magnitude of the vector IPII is equal Tyee. For example, point (3,4,5) creates a position vector P relative to origin. z p= |4 5. Wl = Faas = 9416425 = 50 =7.071 SaaS W cams Programming (MU B.Sc. Comp. SemV)_1-15_Carteslan Coordinate System & Vectors —————————— Syllabus Toplc : Unit Vectors 1.10.4 Unit Vectors & Magnitude of unit vector Is 1. Following Is the example of It fe - In vector multiplication unit vectors are very useful. Multiplic magnitude. If we use unity then multiplication becomes simplified. ation of vectors take - Application vectors are utilized in computer graphics to give orientation of surface, direction of light source and virtual camera. - Asunit vectors have length, so it reduces estimation time required for vector operations. ‘The process of converting a vector into unit vector is called normalizing. - Normalizing is accomplished by dividing a vectors component by its magnitude. - Tobe aware of this process, consider a vector r and its component x, y, z. The magnitude Urll =x"+y"+2 . The unit form of ris given as follows : 1 fr B= wail? (1.10.5) = The magnitude ofr, is 1 : aE) = ee a1 = Example of conversion of vector r into unit form, mE tl = Te F 4s = 14 al 0.267 = | 0.535 0.802. Well 1-16 _ Cartesian Coordinate (MU B.Sc. Comp. - Sem-V) | ‘Syllabus Topic : Cartesian Vectors 1.10.5 Cartesian Vectors ~ For the algebraic manipulation of vectors the scalar multiplication of vectors, vectoy addition and unit vectors is combined. AA first three Cartesian unit vectors i,j,k are defined and these unit vectors are aligneg with x, y, z axes correspondingly. Ms om — So, any vector aligned with the x, y, 2 axes are given by a scalar multiple of the unit ‘vectors i, j, and k correspondingly. ~ Foriinstance, a vector 15 unit long aligned withthe x axis is 15i and a vector 25 units long aligned with z axis is 25k. So, we create a vector r by adding Cartesian vectors as give, below: rs aisbj+ck -+(1.107) Itis equivalent to writing r as a Url = (1.10.8) c. ‘The magnitude of r is calculated as Il = \ea0 re ++-(1.10.9) Any pair of Cartesian vectors like r and s vectors can be combined as follows: Fs altbj+ck ++(1.10.10) s = dite+hk +1101) rts = (atdit+(+e)j+(c+Nk * 4.10.12) For example, given r= 3i+4j+5k and s= ris = 9i411j+13k +7)+8k and Uirtsil = OFT FIs 814 121+ 109 B71 = 19.26136 WW came Programming (MU B.Sc. Comp.-Sem-V)_1-17_Carteslan Coordinate System & Vectors ‘Syllabus Tople : Vector Multiplication 1.11 Vector Multiplication - The vector addition and subtraction is used to solve various problems. vector multiplication is used to angles and orientation of surface. ‘We all are aware about the multiplication of two scalars, for example, 6 x 5 or 5 x 6 = 30. Very often we see this operation as a rectangular area where 6 and 5 are the dimensions of a rectangles sides, and 30 is the area. In the multiplication of vector actually two 3D lines are multiplied together and it is very difficult to visualize this operation. 1 ‘You can multiply the vectors together by using the two ways: 1, Scalar result 2, Vector result. Let's begin with the scalar product ‘Syllabus Tople : Scalar Product 4.11.1. Scalar Product There are two vectors r and s, now they are multiplied by utilizing the product of their magnitudes : lets. Even if it is a valid operation, but it ignores the orientation of the vectors and orientation is an important feature so it does not get us anywhere. So, angles between the vectors are included to make the operation useful. - Fig. 1.11.1 shows two vectors r and s that have been drawn, for ease, in such a way that their tails touch, ~ Here, sis taken as a reference vector arbitrarily and the projection of r is computed on s by considering their relative orientation. The length of r on sis til cos(B). The magnitude of s is multiplied by the projected length of r: Isl cos(B). ~The scalar product is given as s+r = [sl] [Il] cos B o-(LUL) Re nie eee Gave So Sem Canesian Coordinate Wee, = The scalar multiplication is represented by using the dot symbol ‘*", to distinguish i trop the vector product, which uses ‘x’ symbol, Because of dot ‘." symbol, the scalar prodycy , frequently referred to as the dot product. - Now, how to compute the dot product. Let's say r and ¢ is two Cartesian vectors and cay,, (on to multiply them collectively using the dot product definition: . = alsbjeck oA 8 = ditgehk ESCn TR} thus, 1:8 = (Gi +bj +ck)(di + ej + fk) mai (di +e] + fh)+ bj (di tej +k) +k. (dit ej + fk) ts = ad(i-i) + ac(i j) + afi -k) + bdG- i) + beG-j) + bfG -k) +0d (ki) +celk 5) +f (k-4) e(L114) — We have generated different dot product terms, for example, (i+ i), @*j), (k*k), etc. ~ These terms are divided into two groups: the first group contains those that involve the Same unit vector, and the second group contains those that reference different unit vectors ~ By using the dot product definition, terms for instance (i+ i), G+ j) and (k* k) = 1, as the angle between i and i, j and j, or k and kis 0° and cos(0*) = 1. ~The other vector combinations are separated by 90», and cos(0°) = 0, all outstanding terms end to zero. Keeping in mind that the magnitude of a unit vector is 1, so we write it as, Usll [irl cos (B) = ad+be+cf +-(LILS) ~ It shows that the dot product is a scalar quantity. z Fig. 1.11.1 : The projection of r on s produces the bass for the scaler product Gore. (Mu B Se - Sere) 1-49 Canssan Comrtrate sven a ETE eee eT Syllabus Topic | Example of the Dot Product OOOO 4.11.2, Example of the Dot Product = The angle between two vectors r and s is found as follows 2 [3 te [23] m1 ¢] 4 Lio. Wl = VF +CW +4’ 25385 Is = YS4 OF = 12689 ‘Therefore Isl ell cos (B) = 2x5+(-3)x6e4x 10232 12.689 x 5.385 x cos (B) = 32 32 cos (B) = Tray xaa85 = 0468 B = cos (0.468) = 62.1° = The angle between the two vectors is 62.1°. When we compute the dot product we get the angles range between 0° and 180° = If the angle between two vectors increases outside 180°, then the returned angle fis constantly the smallest angle related with the geometry ‘Syllabus Tople : The Dot Product in Lighting Calculations 1.11.3, The Dot Product In Lighting Calculations = Lambert's law states that the intensity of illumination on a diffuse surface 1s proportional to the cosine of the angle between the surface normal vector and the light source direction. - You can see in Fig. 1.11.2, the light source is placed at (20, 20, 40) and the illuminated point is (0, 10, 0). = Now we will calculate cos(f). When cos (B) is multiplied by the light source intensity, 1t gives the incident light intensity on the surface. Here, nis a unit vector, and its magnitude isla fl ¥ Game Programming (MU B. Sem-V) 1-20 _ Cartesian Coordinate t2m & Voor, q ~The path of the light source from the surface is represented by the vector: 20-07 [20 s = |20-10] =/.10 40-0] L40, Us| = 20% 10+ 40" = 45.826 Mol] Isll cos (B) = 0x20+1x 10+0x40=10 1% 45.826 x cos (B) = 10 10 cos(B) = 75 _g9g = 0.218 ‘Therefore the light intensity atthe point (0,10, 0) is 0.218 ofthe original light intensity 4 20, 20, 40). Due to the inverse-square law of light propagation it do not take into accoy, the attenuation 2 ght source Fig. 1.11.2: Lambert’s law —————— ‘Syllabus Topic : The Dot Product In Back-Face Detection 1.11.4 The Dot Product In Back-Face Detection ~ We can identify the back-facing polygons relative to the virtual camera by calculating the angle between the polygon’s surface normal and the line of sight between the camera anq the polygon. If the calculated angle is less than 90°, then the polygon is visible. And if the angle is equal to or greater than 90° then the polygon is invisible. ~ Fig. 1.11.3 shows this geometrical representation. Itis clear from the Fig. 1.11.3 that the Tight-hand polygon is hidden to the camera. - Now, let's prove it algebraically that this is so. — If the camera is placed at (0,0,0) and the vertex of polygon is (10, 10, 40). The normal vector is [5 5-2)" [3] [In] = VS"4S"+ C2) = 7.348 ramming (MU m-V) 1-21 _Cartesian Coordinate System & Vectors Com ‘The camera vector cis given by : 0-10] f-10 ¢ = [0-10] =|-10 0-40] [-40. Hell = VE 10+ 10S" + C40) = 42.426 Unl] [lel] cos(B) = 5x (- 10) +5 x (- 10) + (-2) x (-40) 7.348 x 42.426 x cos(B) = -20 on cos(B) = 7548 x 42406) =~ 9.0634 B = cos”! (-0,0634) = 93.635° So, 20 = Itdescribes that the polygon is hidden. Fig. 1.11.3: Polygon’s visibility shown by the angle between the surface normal and the camera’s line of sight Syllabus Topic : The Vector Product 1.11.5 The Vector Product - There are two ways to find the product of two vectors. The first way is the scalar product, and the second way is the vector product, this is also known as the cross product because of the ‘x’ symbol utilized in its notation. As per definition two vectors r and s can be multiplied together to generate a third vector t ; rxs=t (111.6) — where [tl] =I «Isl sin(A), and is the angle between r and s. Vector #is a normal (90°) to the plane and it contains the vectors r and s, — Ithelps in estimating the surface normal to a polygon. Once more let's define two vectors and carry on to multiply them together: ait bj +ck ve (LIL) ditej+mk (1.11.8) r 0 s | Game BSc Comp.-Sem-V)_ 122 _ Cartesian Coordinate 2 Vectors Ixs = (ithe) x Gite +tk) = SiN (G +e] + (jx (Gi + gj + ft ck x i tej + fk) TxS = adi xi) sac (xj) +af (xk) +b G xi) + bef xj) + BIG xb) +odkxi) +eek x) +efkxk) (L119, The dot product has 2 groups of vector terms: 1) reference the same unit vecto, 2) reference two different unit vectors. ~ By utilizing the cross product definition, operations like (i x i), (xj) and (k x k) resuyy, iM a vector whose magnitude is 0. ~ The reason behind it is the angle between the vectors is 0°, and sin (0°) = 0. According}, these terms vanish and we are left with TXS = ac xj) + afi xk) + bdG xi) + bEG XK) +cdlk xi) + cok xj) - (1.11.19, ~ ‘The mathematician Sir William Rowan Hamilton wiedixj=k butjxi=-kjxk=;, dot k x j =, and k x i= j, bot ix k =—j and this worked, but it contradicted 1. ‘commutative multiplication law of scalars where 6x7= 76. ‘So now it is accepted that vectors do not act upon all the rules of scalars, which is ar imeresting result, Proceeding, thea, with Hamilton's mles, we reduce the eross produ. terms of equation(111.10) XS = ack) taf) +bd CW) + bf) +edG) +02 i) = Qf-ce)i+(cd-aj+(e-bdk + (L111) To get the symmetric reselt modify the middle term = (bf ce) i+ (fod) j+ (2e—ba = (ALI TER is wrinen in the deceminent form we will get: na oe sh-{5 “Hi he ALIAS Lerl lat de a ~ Whssre, the deomminan: sives the scalar for each unit vector. There is a difference betwee: the progacts of Gangomal eas fox the determinant of a2 x 2 matrix. But there is a ~ There is 2 simple sqylishiness o> shis resolt that allows the cross product to be calculate: ‘ery fase. Wet he veces i sight enquence to get the cross products. Keep in mind th:: Tx s does mot equal sxc Fee mierxs: (LIL 14 Programming (MU 8, Carteslan Coordinate & Vectors Comp. - Sem- ~ Fori the scalar multiplier is (bf - ec). We get it by ignoring the i components and looking at the scalar multipliers of j and k. — For —j the scalar multiplier is (af - de). We get it by ignoring the j components and looking at the i and k scalars. ~ Fork the scalar multiplier is (ae ~ db). We get it by ignoring the k components and ooking atthe i and j scalars, s see the example on it. First we see the vector product works with the unit vectors, i,jandk. 80, (0x0-1x0)i-(1x0-0x0)j+(1x1-0x0)= xk = (1x1-0x0)i-(0x1-0x0)j+@x0-0x1)k= ixj = @x0-0x1)i-@x0-1x1)j+@x0-1x0)k=j = For example there are two vectors r and s and calculate the normal vector t. Fig. 1.11.4 depicts the r and s vectors and the normal vector t. = Table 1.11.1 has coordinates of the vertices which forms the two vectors. %-% %-% Ya~Ya s=|%-Y2 2 2-7. r= -i4j = -i+k rXs = (1x1-0x0)i-(-1x1-0)j+C1x0-GC))xDk = itj+k This confirms what we expected from Fig. 1.11.4. Now reverse the vectors to show the importance of vector sequence: s=-itk ore -itj Y, ‘3 ‘1 SP x Fig. 1.11.4: The vector t is normal to the vectors r and s. f ) W Game Programming (MU B.Sc. Comp.- Sem-V)_1-24 antsian Coordinate Seem & Voc, Table 1.11.1 co-ordintes of vertices 1.16 [verter [v__}ofoft v, {1 jojo Vy o fifo sx = ((Ox 0)-(1x 1) i-(Cx0 + (-1)x I) j+ (C11) -(-D) x0) jk =-i-j-k This is in the opposite direction to r xs. Syllabus Topic : The Right-Hand Rule 1.12__ The Right-Hand Rule ~The right-hand rule is used to calculate the orientation of the cross product vector. The operation r x s is given, if we align the right-hand thumb with r, middle finger points in the direction oft the first finger with s. ———— Syllabus Topic : Derlving a Unit Normal Vector for a Triangle 1.13 _ Deriving a Unit Normal Vector for a Triangle In the Fig. 1.13.1 a triangle with vertices is given in an anti-clockwise order from its visible side. — We need the surface normal to point upwards from the visible side. From the given following data we first compute the surface normal using the cross product. - Next convert the surface normal to a unit normal vector. Construct vector r between v, and v,, and construct vector s between v, and vs. The r, $ and x s are given as : rx = t=(1x2-0x0) i-(-1%2-0x-1)j+(-1%0-1x- Dk t= %+2j+k Vz +241 =3 2ied jee rz ou = The unit vector t, can be now utilized in illumination calculations; because of its unit Tength dot product calculations are simplified. Va( 1.1.2) x Fig. 1.13.1 : The normal vector t is derived from the cross product r xs 1.14 Areas = _ Inthe cross product let's check what is the physical meaning of [[l- [Isl sin (B). - There are two 2D vectors r and s is given Fig. 1.13.1. The height given is h = |[s|] sin(B), so the area of the parallelogram i given as: Url] h = [ie - Isl] sin (B) o(L 14.1) ~ It gives the magnitude ofthe cross product vector t. Hence, when we compute 1x s, the length of the normal vector tis equal to the area of the parallelogram created by r and s. ~The triangle made up of halving the parallelogram is half the area. ‘Area of parallelogram = [| w(L142) Area of triangle = [tl - Calculating the surface area of an object build from triangle / parallelogram is very easy. — It is easy to calculate the surface area of an object constructed from triangles or parallelograms. - Fora triangulated surface, add the magnitudes of the normals and divide the result. Fig. 1.14.1: Vector r and s forms an area of parallelogram is equal to lil! - Isl sin B. I - = — yv Game Programming (MU 8.Ss. Comp. -Sem-V)__1-26__Carteslan Coordinate System & Vor Seen ee? Syllabus Tople : Calculating 2D Areas 1.15 Calculating 2D Areas ~ There are 3 vertices of a triangle Py (Ry yo). Py (xy yx) and Ps (¥2 y2) given in Fig. 1.1 The vertices are in an anti-clockwise sequence. The triangle is on the z = 0 pl therefore the z-coordinates are zero. y Fig. 1.15.1 : Vector r and s forms an area of triangle and itis half the magnitude of their cross product ~The vectors rotation and s are calculated as follows : Write equation numbers -% %-% r= |yi-¥0] s=|¥~Yo 0 0 r= (y-mMi+M-Y0i S = &-mM)i+G.-YI ex sl] = (1-5) G2-¥- G-%0) Gi-Yo) =X (¥2—Yo)—%o (¥2- Yo — ¥2 1 - Yo) + Xo (1 - Yo) 1¥2— Yo XaY2~ FoYo~ ¥2¥i + %2Yo* MY Koo = XyYa—% Yo ¥oVa~ Rai + H2Yo* oY (oYi ~ Xoo) + (1 Ya~X2¥1) + (Yo + X02) But the area of triangle is formed the 3 vertices and given by Area = die xsfl Therefore Area Flys — Mayo) + (Ys -HY) + OaYot HYD at a2 as a4 as aé a7 a8 ag Explain the Cartesian XY plane ? What are function graphs ? Explain in brief Theorem of Pythagoras in 2D 7 Explain Polygonal Shapes ? Explain 2D coordinates ? Explain Euler's Rule ? Explain 2 vectors in detail ? Explain 3D vectors in detail ? Explain vector manipulation ? Q. 10. What is right hand rule? Q. 11. How one can derive a unit normal vector for a triangle? Q.12 Howto calculate 2D areas ? Chapter Ends... goo Transformations and DirectX Syllabus ‘Transformations : 20 Transformations, Mates, Homageneous Gooranates 30 ‘Transformations, Change of Axes, Direction Cosins, rotating a Point about an Artitray Axis, Transforming Vectors, Determinants, Perepectiv Projection, Interpolation, DirectX : Understanding GPU and GPU architectures. How they are diferent from Cpy Architectures? Understanding how to solve by GPU?. Transformations Transformations are used for translating; rotating, scaling, reflecting and shearing th, Shapes of the objects, as well as you can apply different effects on object by changing their coordinate values, ‘The basic notation for transformations is algebra but you can also express it as matrices The advantage of matrices is you can view the transformations and also provide interfacing to many types of computer graphics hardware. Syllabus Topic : 20 Transformation —_————— 2.2 2D Transformations 2.2.1 Translation ‘The one to one relationship between aumber and shape is provided by the Cartesian coordinates. The relationship is given in such a way that if we change a coordinates of shape then we change its geometry. For example there is a vertex P(x, y) is on a shape, and to translate it we apply the operation x = x +3. We get the new point P(x, y) thee units to the right. Game Programming (MU B.Sc. Comp. -Sem-V)_ 2:2 Similarly, y = y + 1 operation creates a new point P(x, y) sifted one unit vertically. Both the transforms are applied on the shape for each vertex of the original shape and the shape is shifted as shown in Fig. 2.2.1. Y Fig. 2.2.1 : Translation of original shape by adding 3 to every x and 1 to every y coordinate 2.2.2 Scaling You can obtain the scaled shape of an object by multiplying coordinates as follows : X= ok y = sy vee(2.261) This transform results in a vertical scaling of 1.5 and a horizontal scaling of 2 and a vertical scaling of 1.5, as shown in Fig. 2.2.2. Keep in mind that a point located at the origin does not change its place, so scaling is relative to the origin. Fig. 2.2.2: Scaling the shape by multiplying each x coordinate by 2 and every y-coordinate by 1.5 22.3 Reflection The reflection of the shape relative to y axis is created by reversing the sign of the x coordinate, keep the y-coordinates unchanged xe =x ¢ ime Programming (MU B.Sc. Comp.-Sem-V)_23 Jrensiomatons and Diag way (2.2.9, “4) The reflection of the shape relative to x axis is created by reversing the sign of y, Y coordinate, keep the x coordinates unchanged 7 Nex yerry 223) Fig. 2.2.3 shows the example of reflections. Fig. 2.2.3 : Three reflections by reversing the signs of the co-ordinates at the top right hand shape 2.3 Matrices ~ Matrix notations are used for transformations. Matrix transformations are no, commutative ie. T, x T; T; x Tj. Such observations are clarifies by the matrix notations. For example, take the transformation T, x’ = ax+by y' = ox+dy = One more transformation T, that transforms T, : x" = AX +BY y’ = Cx’+Dy’ — After substituting the full definition of T, we get : x” = A (ax +by) +B (cx+ dy) y” = C(ax+by)+D (x+y) This simplifies to Ty 223.1) +-(23.2) T,xT, TXT, (233) yme Programming (MU B.Sc. x” = (An+ Bo) x+(Ab+Bd)y y” = (Ca+De)x+(Cb+Dd)y Separate the constants from the variables as follows : x ab)[x ld Ls The transformation is given in the square matrix of constants in the center. By taking the top variable x and the = sign the algebraic form is created. The top row of the constants {a_b) are multiplied individually by the last column vector which contains the x and y. Now examine the second variable y and introduce the = sign. Next, multiply the bottom row of constants fc d] individually by the last column vector which contains x and y, to create 1, xT, x = axtby y = cx+dy (2.3.6) The product of T, x Tyis : [ | [ aie lI *] 23.1) yJrleoily Let's simplify it x" AB ab x ("] festa] If we multiply the two inner matrices together they must produce : x" = (Aa+Bo)x+(Ab+Bd) y y” = (Ca+Dc)x+(Cb+Dd)y +(2.3.9) In matrix form [*] . [are orn ‘| a y’ Ca+De Cb+Dd] ‘Ly or else the two systems of notation will be inconsistent. This implies that Aa+Bc Ab+Bd AB)[ab ees oa -[¢ oll: il eon) ‘How the matrices are multiplied. Here are the rules for matrix multiplication : Game Programming (MU B.Sc. Comp.-SemV)_ 25 Transformations and Direct 1, The top leftchand comer element Aa + Bc is the product of the top row of the fi, matrix by the left column of the second matrix. 2. The top right-hand element is Ab + Bd. It is the product of the top row of the firs, ‘matrix by the right column ofthe second matrix. 3. The bottom left-hand element is Ca + De. It is the product of the bottom row of th, first matrix by the left column of the second matrix. 4. The bottom right-hand element is Cb + Dd. It is the product of the bottom row of the first matrix by the right column of the second matrix. ~ _ Itis now a small work out to confirm Gauss’s observation that T, x T; # T; XT, since after reversing the transforms T, x T, to T, xT, we obtain less on] I [2 | Ca+De cb+Dd} “Leal ‘Leo (23.12) - One drawback of this notation is that there is no clear method to add or subtract a constan, like c orf: X = axtbyte ¥ = dx+eytf ~ By using homogeneous coordinates the above problem can be solved. 2.3.1 Systems of Notation There are two systems of matrix notation : 1) _In first method the matrix multiplies a column vector, as described above. 2) Insecond method a row vector multiplies the matrix wri= tes] +9(2.3,13) (2.3.14) sr — _ a 26 ‘Transformations and Direct X Keep in mind that the rows and columns of the matrix are transposed when moving between the two systems. 2.3.2 The Determinant of a Matrix ‘The determinant of a 2 x 2 matrix isa scalar quantity calculated. Given a Matrix [31] ed its determinant is ad — cb and is represented by ab [: Al ++:2.3.15) For example, the determinant of [? a 1 2 |83x2-1x2 = 4 Syllabus Topic : Homogeneous Coordinates 2.4 Homogeneous Coordinates — Homogeneous coordinates are also known barycentric coordinates or areal coordinates because of their area-calculating properties. For defining a point in a plane Homogeneous coordinates uses three coordinates instead of two. — If there is a point P with coordinates (x, y) then there exists a homogeneous point (x, y, t) in such a way that X = w/t and Y= y/t. - For example, for a point (3, 4) there are three homogeneous coordinates (6, 8, 2), because 62=3 and 8/2=4. However, the homogeneous point (6, 8, 2) is not unique to (3, 4); As there are (12, 16, 4), (15, 20, 5) and (300, 400, 100) possible homogeneous coordinates for (3, 4). . - These coordinate system is called ‘homogeneous’ because it is possible to transform functions such as f (x, y) into the form f (x/, y/t) without disturbing the degree of the curve. Inthe field of projective geometry itis a very powerful concept. vt Game Programming (MU B. Transformations and Dire y «___ Fig. 2.4.1 £20 homogenous coordoates seen as plane in 3D space, sual wbere#™ or cag ~ _ Itis possible to visualize that a collection of homogeneous points of the form (x,y, t) ex, ‘on an xy-plane, and e tis the z coordinate, as shown in Fig. 2.4.1. As you can see in Fig. 2.4.1 that a on a more distant triangle on the t= 1 plane, and a jj, triangle, much larger, plane, So it is better to work on an arbitrary xy plane in thre, dimensions instead of working in two dimensions. The t or z coordinates of a plane are not relevant as the x and y coordinates are fing, scaled by 1. It is better to select f = Ithat is the point (x, y) has homogeneous coordinate, , y, 1), making scaling needless. To every (x,y) pair we have to attach 1 to every (%, y) pair if you substitute 3p homogeneous coordinates for traditional 2D Cartesian coordinates. When transform Point (, y, 1), it will become (x, y, 1), and we discard the 1. It will solve the problem of creating a translation transformation. Let’s see the following transformation on the homogeneous point (x, y, 1) : x abe x y}=l|detlyy (24.1) 1 ool 1 This expands to x’ = axtbyte ¥ = dxtey+f lel oon2.4.2) ~ The above problem is solved by adding a constant. Now we will see how homogeneo., coordinates are used in practice. Game Programming (MU B.Sc. Comp. - Sem Transformations end Direct X 24.1 2D Translation - The algebraic notation and matrix notation for 2D translation is given as X= xt y= yey (2.4.3) = Matrices notation is, x 10K] 7x y}fefory}dy (2.4.4) 1 ooitdli 2.4.2 2D Scaling - The algebraic notation and matrix notation for 2D scaling is given as x = 5x y=sy (24.5) = Matrix notation is, x 5 007 [x y]=]oso]-dy (2.4.6) 1 oo1 1 ‘The scaling is relative to the origin, i. the point (0, 0) remains (0, 0). All other points move away from the origin. If you want to scale relative to a new point (p, ,p,) then you have toper form the following operations : 1, First Subtract the point (p, , p,) from (x, y) correspondingly. It will translate the reference point (p, ,p,) back to the origin. 2. Now perform the scaling operation. 3. Next, add point (pp p,) back to (x, y) correspondingly, to compensate for the original subtraction. - _ Itis given algebraically as: X= 8, (8p) +Px y¥ = y&-p)+Py (2.4.7) = Wecan simply it as, SX +p, (1-8) y = syy+p,(1-s,) (2.48) x mV) _ 2:9 Game Programming (MU B.Sc. Ce In a homogeneous matrix form itis given as, x 5, 0 p(l-s) ] [x y | =| % 5 py-s) | y 249) 1 oo 1 1 For example x 20-1) [x y}=]o2-1]-4y 1 ooilli 243 2D Reflections ‘The matrix notation for reflecting about the y-axis is given as : x -100 x yle= o10l-ly +(2.4.10) 1 o ois li ‘The matrix notation for reflecting about the about the x ~axis is given as: x 100 x y|s : -10]-y +(2.4.11) 1 ooisli ~ There will be a need of more algebraic tricks for the reflection about an arbitrary vertical or horizontal axis. For example, 1. If you want to create a reflection about the vertical axis where x = 1, then first we have to subtract 1 from the x coordinate. It makes the x= 1 axis concurrent with the major y axis, 2. Ifyou wish to do the reflection by reversing the sign of the changed x coordinate then, add 1 to the reflected coordinate to retum for the original subtraction. — These 3 steps are algebraically given as m= x-1 m = -(x-1) x = -(e-1)41 (2.4.12) Simplify, x= -x42 y=y (2.4.13) ee cat els OL ec Game Pr wning (MU B.Sc. Comp. - Sem-V)_2-10. Transformations and Direct X Or, in matrix form, x -102) [x yl= | o1 ol] (24.14) 1 ooisli Fig. 2.4.2 shows this process. Usually, following transform is required to reflect a shape about an arbitrary y axis, year X= -(x-a)+a,=-x+2a, y= 7y: +-(2.4.15) r, in a matrix form x -10 %®7Px ¥|-=-[— 010 Y (2.4.16) 1 ooidli eae a cece Fig. 2.4.2: The shape on the right is reflected about the x = 1 axis This transform is also used for reflections about an arbitrary x-axis, ys a ¥ = -(Y-a)+a=-y+2a, 224.17) or, in matrix form, x 10 0 x EG 1 oo1 1 2.4.4 2D Shearing 42 shows the ~ A shape can be sheared by leaning it over at an angle A. Fig “s fees and we observe that the y coordinate is aot changed it fem * Coordinate is a function of y and tan (f). x = x+ytan(B) Ysy or, in matrix form, x 1 tan(B) 0 * y|e= [: 1 O}-y 1 o o 1dlt horizontal shit is Fig. 2.4.3 : Shape is sheared to the right by an angle f, and the horizont proportional to y tan (9) 2.4.5 2D Rotation ~ A point P(x, y) is to be rotated by an angle f about the origin to P(x, y) is s! Fig. 2.4.3. It can be observed that x’ = Roos (6+) R sin (0 +B) Therefore, x R (cos (8) cos (B) - sin (8) sin (B)) = R (sin (8) cos (B) +c0s (8) sin (B)) = Rove @)-Lsin) « " * 0 / = R( Zoos G)-Zsin®) x cos (B)-y sin (B) x sin (B) + cos (B) « ae Aw 4 Rass yamming (MU B.So. Comp. - Sem-V)_ 2:12 ‘Transformations and Direct X of, in matrix form, x cos (B) -sin(B) 0 x ¥ | =] sin@®) cos) 0 | +} y (2.4.23) 1 0 o idli For example, to rotate a point by 90° the matrix becomes x 0-10 x y]=]to ofdly (2.4.24) 1 ooisli Y Poy) ¥ Fig. 2.4.4: The point P(, y) is rotated by an angle f to P(x, y) ‘Therefore the point (1, 0) becomes (0, 1). If the matrix is rotated by 360° then the matrix becomes x 100] [x y|=}o1o]dy 1 ooisli ‘These types of matrix are known as an identity matrix and this matrix has a null effect. For rotating a point (x, y) about an arbitrary point (p, ,p,) first subtract (p, , p,) from the coordinates (x, )) correspondingly. It first permits us to do the rotation about the origin, second, to do the rotation, and third, add (p,,p,) to compensate for the original subtraction. The following are the steps : 1, Subtract (px, py) from the coordinates (x, y) : x = (-p) v= Y-Py)s 2. Rotate f about the origin : % = (x-p,) cos (B) - (y-p,) sin (B) Gamo (MU Semv)_2-13 ney ee a, Ya = (X~py) sin) + (y-p,) cos B) 3. Add (px, py X = (x-p)cos ®)-(y-p)sin 6) +P. ¥ = (x-p,)sinG)+(y-p,) cos 8) +P, Simplify, x’ = x cos B)~y sin) +p, (1—cos 6) +P, sin @) Y= xsin B) +y 00s ) +p, (100s (B))-Pssin @) 2.425, ‘The matrix form is x cos (B) ~sin(B) p,(1-cos (B)) +p, sin(@®) ] [x ¥ | =|] sini) cos(B) p,(1-cos(B))-p.sin@®) | +] ¥ 24.25) 1 0 0 1 : If the rotating point is rotated by 90° about the point (1, 1) the matrix operation is CMe The example is to the point (2, 1) for (x, y): first it is it is transformed ,, (A, 2). By using the algebraic approach you can easily derive the above transforms, Yo, can derive the compound transformations like scaling and rotation relative to arbitran, Point and reflection relative to an arbitrary line. ~ Such types of transformations are called affine, because parallel lines stay parallel aft, being transformed. ~ Scaling transformations can change the angles and lengths as diverse x and y scaling factors are used, so there is assurance that angles and lengths are conserved, For unity, ir, these transformations from a matrix perspective. 24.6 2D Scaling - To scale a point (x, y) which is relative to some arbitrary point (p, p,), first, translat (-P..-p,) , second, carry out the scaling, and third, translate (p,, p,) in the matrix forn these three transforms are represented in matrix form: x x [ | = [translate (, , p,)] - [scale (5, , ,)] - [translate (-p, .-p,)] | 7 1 1 hh nels “SE. ies Game Programming (MU B.Sc. Comp.-Sem-V)__ 2-14 Transformations and Direct X This expands to x 10p] fs 007 P10-p7 Fx y}|=]o1p]]os0]-101-p, }-ly = (24,21) 1 001 ooitloo.1 1 - Here, important is the transforms sequence as it creates the confusion. The first transform performing on the point (x, y, 1) is translate (-p, .p,), then scale (s,, s,), next translate (Pc. P,) If you the sequence then the transforms are not commutative. - Multiply all the matrices together to get the single matrix. Multiplication is done in any sequence, as long as we preserve the original order. - Let's begin with scale (5, s,) and translate (-p,,~p,). This produces x7 10p s 0 -5:P.] [x y |=] O1p | 05, -sp, |-ly id ool OO 1 and finally x) s 0 pls) ] [x y¥ | =] 05 pd-s) | fy +-e(2.4.28) 14 00 1 1 2.4.7 2D Reflections - Areflection about the y-axis is given by x] [-100] [x y|=]o1o0}4+y 4.(24.29) id ooisli - Therefore, using matrices, we can reason that a reflection transform about an arbitrary axis x = ax, parallel with the y-axis, is given by x x ¥ | = [translate (a, 0)}- [reflection] - {translate (- a, ,0)]-| y 14d 1 which expands to x7 10a] f-100)710-a] [x y }=]010]-/ 0 10] 01 0 Jy t2 ool 001 ool 1 x Game Programming (MU B.Se. Comp. - Sem-V) Transformations and Direc, = Moltply all the matrices together to get the single matrix. Start with multiplying , reflection and the translate (~a, 0) matrices together. This produces x] 10a -10a x F [eels i 1J Loor ooidli and lastly | 1 1 oot 0 0 1 and lastly x cos (B) -sin(B) p, (1-cos (B)) +p, sin (®) ] [ x [*] = | sin(B) cos (B)_p, (1 cos (8))—p, sin (8) (] wo. 1 0 0 1 1 wv Game Programming (MU B.Sc. Comp. - Sem-V)_2-16 Transformations and Direct X oe Syllabus Toplc : 3D Transformations 2.5 3D Transformations - Here, we are performing transformations in three dimensions. We are applying same transformations of 2D like Scaling, translation and rotation. Here, scaling, translation is same but in rotation we are rotating an object about an axis. In 2D we rotated a shape about a point. 2541 3D Translation The algebra for 3D translation is very simple. You can write the homogeneous matrix directly: x 1004 x 010 ales 4 y +-(2.5.1) z OPOHIE lez 1 ooo! 1 25.2 3D Scaling - The 3D scaling algebra is defined as follows: x ; y=sy w= 43 +(2.5.2) = This in matrix form is x 5 0007;7x y}_]%s5 00] y 053) z oo0s0]|z “ 1 oooitli — The scaling is relative to the origin. So, it is shown that scaling is relative to an arbitrary point (p, .P, p,) with the help of following algebra : X= 8 (K- py) + Pi (=, (¥-Py) + Py 2 = 4@e-p) +p, (254) = This in matrix form is me 5, 0 0 p(l-s) oe (25.5) mH Nex y z 00s, p,(l-s) 1 ooo 1 The 2D shape is rotated about a point, the origin o is vome arbitrary position. An obj. ‘rotated (0 SD about wn ania, it can be any 4, y or ¢ ania, or some arbitrary axis, ‘We rotate the vertea about one of the three orthogonal axex; such rotations are cq) Euler rotwanons A general 2D-rotation transform is given by x cos (A) - in (A) 0 x y | =| sn@) coo 0 |; Qs, 1 ° oot 1 ‘Which in 3D can be seen as rotating a point P(x, y, 2) on a plane parallel with the xy py, 5 shown in Fig. 2.1. In algebraic form it can be written as x 008 (f)~y sin (B) x sin (B) + y cos (6) ver +A2.5> ‘Therefore, the 3D transform can be written as x cos (f) -sin®) 0 0 y sin () cos) 0 0 v|" 1 o 0 10]| o 0 o1 aN ee Fig. 25.1 : Rotate point P about the z axis . x a! LS Serv) Ni fomdamentally rotates 4 pount aboot the fant ‘When 1 anit is tated then x conedinant remains ccitanant and the 7 sod ¢ eo-crdimanes ae changed It is piven algebraically ax weet y = yoo (Pi-esin (Py ¥ © y win (Bh + 2 con (6) asm itis piven in im matrix form ax follows, x t o o e y © con (Ps ~ ain ep 0 | | y r 0 singh) conf | |e 1 o 0 o 1 I When we rotate sbout the y axis. the y coontinate remains constant while the + and ¢ coordinates are changed. Algebraically, this 1 given x = rein (B) +1008 (B) y= = zeos(B)-x sin (B) asi asm or, in matrix form, x cos (B) O sin(B) 0 y o 10 0 oie 1 25.12 sin (B) 0 cos (B) 0 o 0 o tdhr ‘The above rotations are also known as yaw, pitch and roll. There are two ax vyvicens 2 left-handed system and a right-handed system. So, out of these two any atis system can be used. It happens sometimes that 2 left-banded system of axes is used, and the vertical axis can be the y atis or the z axis The matrix that represents the rotauon varies greatly We are using the mght handed Cartesian coordinate systems, and the vertical axis 1s always the y-aus ‘The angles roll, pitch and yaw are defined as follows (© rollis the angle of rotation about the ¢ axis © pitch s the angle of rotation about the x axis © uw is the angle of rotation about the y axis. a (MU B.Sc. Comp.-Sem-V) 2-19 ‘Transformations and Dir, ~ The three rotations and the sign convention are depicted in Fig, 2.5.2.The homogen, matrices that represents these rotations are as follows : rotate roll about the z-axis : cos (roll) -sin (roll) 0 0 sin(oll) 0 00 oS 0 0 o1 Totate pitch about the x ~axis : io an) 0 cos (pitch) —sin (pitch) 0 © ‘sin (pitch) cos (pitch) 0 0 0 0 1 rotate yaw about the y-axis cos (yaw) 0 sin (yaw) | o 1 0 0 ~sin (yaw) 0 Det 0 B. Fig. 2.52 : roll pitch and yaw angles conventions QS To apply these rotations the common sequence used is roll, pitch, yaw, as given in following transform : x = ale [yaw] - pitch} - fot] : f : +25 1 1 Game Programming (MU B.Sc. Comp. -Sem-V)_ 2-20 Transformations and Direct X Incase a translation is involved, x x a 2 = [translate] (yaw (pitch -(rll]] =-(25.17) 1 z 1 ‘When we apply these rotation transforms, the vertex first rotate about the z axis (roll), next rotate about the x axis (pitch), and next rotate about the y axis (yaw), Euler rotations are relative to the fixed frame of reference. Itis difficult to observe as one’s attention is normally with the rotating frame of reference. For example, an axial system is subjected to a pitch rotation a yaw rotation relative to fixed frame of reference. We start with two frames of reference XYZ and XYZ mutually aligned. The orientation of XY Z after it is subjected to a pitch of 90° about the x -axis is shown in Fig. 2.5.3(a). The final orientation after XY Z is subjected to a yaw of 90° about the y-axis is shown in Fig. 2.5.3(b). 2.5.4 Gimbal Lock Let's consider example starting from the point where the two axial systems are equally aligned. The orientation of XYZ after it is subjected to a roll of 45° about the z axis is shown in Fig. 2.5.3(c). The orientation of XY Z after it is subjected to a pitch of 90° about the x -axis is shown in Fig. 2.5.3(@). If a yaw of 45° is performed about the z axis, it would rotate the x axis towards the x axis; prevent the effect of the original roll. Yaw is a negative roll rotation, caused by the 90° pitch. This condition is known as gimbal lock, because of the loss of the one degree of rotational freedom. Y pitch = 80 Fig. 2.5.3 : Contd... | — ~ Game Programming (MU B.Sc. Comp. - Sem-V)_ 2-21 Transformations ang Sex, @ Fle, 253 : (a) The XYZ axa otem tera ich of 90°) The XYZ all sem ltr ayy 90° (The XY Z axial system Fo aaa a" (@) The XYZ axial system afer pitch of 9." The significant shortcoming of Euler angles : under specific conditions it js j,, conceivable to rotate an object around two axes. ‘One method for dodging this is to make an auxiliary arrangement of axes developed from three orthogonal vectors that are additionally rotated alongside an object oF viry,) camera. Be that as it may, rather than making the rotations relative tothe fixed frame of reference the roll, pitch and yaw rotations are relative to the rotating frame of reference, Anothe, strategy is to utilize quaternions. 2.5.5 Rotating about an Axis The above rotations we have seen were with respect to the x, y and 2 axes. Presently Consider rotations around an axis parallel to one ofthese axes. To begin with, we will rotate about an axis parallel with the z axis, as appeared jn Fig. 2.5.4. The situation is extremely suggestive of the 2D case for rotating a point around @ arbitrary point, and the general change or transform is given by x x y y z | Translate p, py» 0]- [rotate] (translate -p,,-P, N+] 7 | (25.18) 1 1 The matrix is, x 05 (B) -sin(B) 0 p,(1-cos (6) +p, sin(®) ] x ¥ | _} sin (B) cos(B) 0 p,(1-cos(B))-p,sin(B) | | y Es) z 0 o 1 0 z 1 0 0 0 1 1 Game Programming (MU B.Sc. Cor Transformations and Di Fig. 2.54: To rotate a point P about an axis parallel with the z axis - You might have seen the resemblance between rotating in 3D and 2D: the x and y coordinates are changed while the z -coordinate is held constant. We would now be able to express the other two matrices for rotating about an axis parallel with the x and y axis : - Rotating about an axis parallel with the x-axis : 7 1 0 0 0 x y A 0 o (B) -sin (B) 2 (1-cos 6) + sin (B) y -2.5:20) z 0 sin(B) cos(B) p, (1-cos (B))-p, sin (B) z 1 0 0 0 1 1 - Rotating about an axis parallel with the y-axis : x cos (B) 0 sin(B) p, (1 -cos (B))—p, sin (B) x y o 10 0 y z |] -sin@) 0 cos) p,(1-cos (6) +p, sin) |'] a ft o 0 0 1 1 2.5.6 3D Reflections - In 3D reflections occur with respect to a plane, as‘a substitute to an axis. The reflection matrix relative tothe yz plane is x -1000 x o100]ly z|7|oo1o0lle (2.5.22) 1 oooitli { ~£V Gamne Programming (MU B Se. Comp. Sem-V)_ 223 Tnlomatons era rey ~The reflection matrix relative to a plane parallel to, and a, units from, the yz plane is Riva, as x -100%,7 7x 0100 ¥\. ind 523 Zz oo10} lz ) 1 0001 1 ‘Syllabus Topic : Change of Axes — ll 2.6 Change of Axes Points in a single coordinate system as often as possible must be referenced in any, Coordinate system. For instance, to see a 3D view from an arbitrary location, a virty camera is situated on the world space utilizing a progression of transformations, An object's coordinates in respect to the world frame of reference, are computed in resp, to the camera's axial system, and after that used to build up a point of view projection, Presently first how about we see the changing axial systems in two measurements, 2.6.1 2D Change of Axes Shows a point P(x, y) relative to the X¥ axes is shown in Fig, 2.6.1. However, we want 1, know the coordinates relative to the XY axes. So, it is necessary to know the relationship between the two coordinate systems, and if possible apply a technique that works in 2} and 3D. — If in case as shown in Fig. 2.6.1 the second coordinate system is a simple translatic, (tp t,) relative to the reference system,, the point P(x, y) has coordinates relative to th, translated system x’ 10-4 x y |=] O1-y].y +(2.6,) 1 001 1 If you rotate the XY axes, as shown in Fig. 2.6.1 by f relative to the XY axes, a pois P(x, y) relative to the XY axes has coordinates (x, y) relative to the rotated axes given by x cos (~B) -sin(-B) 07) [x y | =] sin@B) cos(-B) 0 |.) ¥ 1 0 o idli Bes a ee ea Game Programming (MU B.Sc. Comy V) 224 Transformations and Direct X Simplify, x cos (B) sin(B) 07) [x y | =| -sin(®) cos(B) 0 |.| y +(2.6.2) 1 0 o idl y+ ‘y Ply) = PUY) x >x +X % Fig. 2.6.1 : The XY -axes are translated by (tr, ty) Fig. 2.6.2 : The secondary set of axes are rotated by 2 ~ When you rotate and translate a coordinate system relative to the reference system, a point (x, y) has coordinates (x, y) relative to the new axes and they are is given by x cos (B) sin(f) OF [10-47 [x [?] = [ “mi cos (B) “lf: 1 “|p| 1 0 o i1tloo1 1 Simplify, x cos (B) sin (B) —t, cos (B)—t, sin (B) ] [x [7] = [= cos (B) t, sin (B) - t, cos (B) It 1 0 0 1 ~"W came Programming (MU B.Sc. Comp. -SemV)_2-25 Translation and Deg rr Syllabus Toplc : Direction Cosines eer 2.7 Direction Cosines ~The cosines of the angles between a vector and the axes are known as Direction cosing, ‘They are also known as unit vectors as they are the vector’s components. As you can in Fig. 2.7.1 there are two unit vectors X and ¥. ~The direction cosines for X are cos (f) and cos (90° ~ B), you can rewrite it as cos (A) ay, sin (B). The direction cosines for ¥ is cos (90° + f) and cos (B you can rewrite it as — .,, B) and cos (). ~ These direction cosines cos(f), sin(B), ~ sin(f) and cos (f) are the four elements of y,, rotation matrix used above : [ cos (B) sin) ] —sin (B) cos (B) ~ The top row and bottom row contains the direction cosines for the X axis and Y axi, Tespectively. This relationship also holds in 3D form the elements of the rotation matrix, +27.) Fig. 2.7.1 : Direction Cosine for X and Y axes unit vectors ~ Take one example of 2D, a set of axes is rotated 45° as shown in Fig. 2.7.2. The correc transform is given as: ay cos (45°) sin (45°) 07) [x y¥ | = | -sin 5%) 00s 145°) df 1 0 o isl 0.707 0.707 0] [x = | -0.707 0.707 0 |. ] o oli ‘Transformations and Direct X Programming (MU B.Sc. Comp. - Sem-V) = The four vertices on a unit square become (0,0) + (0,0) (1,0) — (0.707, - 0.707) (1.1) > 1.414, 0) ©,1) ~ (0.707, 0.707) - The inspection of equation 2.7.1 shows to be correct. (0.1) | (0.0707, 0.207) | (1) (1.414, oy" 1.0)" (0.0707, 0.7077 Fig. 2.72 Unit square vertices in relation to the two axial systems 2.7.1 Positioning the Virtual Camera In computer graphics pipeline four coordinate systems are used: object space, camera space , world space, and image space. ~ Inthe object space thee objects are modelled and assembled. = In the world space the objects are animated and located through proper transforms. It hosts a virtual camera. = The camera space is a transform of the world space to the camera's point of view. The image space is a projection of the camera space onto an image plane. Till now we have studied how to manipulate and locate the objects within world space.. It is important to know that how a virtual camera is placed in a world space and what is the process for converting the world coordinates to camera coordinates. This is depending upon the procedure employed to define the camera’s frame of reference within the world space. This, procedure may include the use of direction cosines, Buler angles or quaternions. 2.7.2 Direction Cosines The [xy z]" are the 3 components of the 3D unit vector. These 3 components are equal to the 3 orthogonal axes as well as to the cosines of the angles created between the vect ‘These angles are known as direction cosines. a Game Programming (MU B.Sc. Comp. - SemV)_2.27 ‘Transformations an Take the dot product of vector and the Cartesian unit vectors to compute the an; can see in Fig. 2.7.3 the angles and the direction cosines. = These direction cosines allow any point P(x, y, z ) in one frame of referer transformed into P(x, y, z) in another frame of reference [as given below : 3 Ty Ta hy 0 x x y|_| ee 0 dy Zz Ty Taz yy 0 4 \ ooo1 Here, iw Try ria direction cosines ofthe secondary x axis, Tay Try Tag. direction cosines of the secondary y axis, Tay Typ Poy direction cosines of the secondary z axis. To explain this operation let's see the Fig, 2.7.4, Figure shows two axial systen aligned. ~The direction cosine result is evaluated in the following matrix transformation: , 1000 x x y - 0100 dy Zz oo10| |? 1 1. 0001 Fig. 2.7.3 : The unit vector components are equal to the cosines of the angles between the axes and the vector Y Fig. 2.7.4 : Two axial systems equally aligned ‘The identity matrix implies that (x, y, z) = (x, y, 2). The Fig. 2.7.5 shows the associated transform and it is: 2 1000 x x , -1000 Vale y Al oneal ou(27.4) 1 1 0001 When we substitute the (1, 1, 0) for (x, y, z) then it generates the values of (I,~ 1, 0) for (x y, 2) in the new frame of reference, which by testis correct. If for the virtual camera the offset is (t, 6, f,) , then the relating transform point in world space to camera space can be given as a composite operation, Fig. 2.7.5: After the roll of 90 XY Z axial system Considering a translation reverse, to the origin, next a change of axial systems. It is expressed as : va [tutti 07] 7100-1 ttm tO] | 010-4 tty ty0 || 001-4 ooo ooo1 For example, consider the condition shown in Fig. 2.7.1. The (ty ty f) have the values (10, 1,1), and the direction cosines are as given in the following matrix operation : * ++:(2.7.5) le ‘Which concatenates to -10 0 -10 ‘ x a o10-1]y ye ; z| | 00-11 : 4 0001 — _ If you substitute (0, 0, 0) for (x,y, 2) in the above transform then it will give (10, for (x, y, 2), from Fig. 2.7.6 it can be confirmed. likewise, if you substitute (0, 1, 1) 4 2) it will give you (10, 0, 0) for (x, y, 2), which is also correct. Fig. 27.6 : The secondary axial system for yaw =180° and an offset of (10, 1, 1). 2.7.3 Euler Angles Buller angle is one more approach used for positioning the virtual camera but Euler go through from gimbals lock. Ifthe virtual camera i located in the world space by the Euler angles, then to achieve the transform of world coordinates to camera coor inverse operations is performed. = The yaw, pitch, roll matrices given below are the orthogonal matrices, sin inverse matrix is a transpose of the original rows and columns. thus to rotate tl angles -roll,-pitch and -yaw, we uilize. Game Programming (MU B.Sc. Comp. V) 2-30 Transformations and Direct X - Rotate roll about the z axis, cos (roll) sin (roll) 0 0 ~sin (roll) cos (roll) 0 0 0.16) 0 0 10 0 0 01 "Rotate pitch about the x axis : 1 0 o 0 0 cos(pitch) sin (pitch) 0 * wT.) 0 —sin (pitch) cos (pitch) 0 o 0 Ot — Rotate yaw about the y axis 0s (yaw) 0 ~ sin (yaw) 0 o 1 0 0 «.(2.7.8) sin (yaw) 0 cos(yaw) 0 (0) en eee If you substitute the -roll, ~pitch, ~yaw in the original matric result. As you see the virtual camera is translated from the origin by (tx, transform from the world space to the camera space is given as follows: ces , you will get the similar ty, f2), $0 the x x ¥ | = ferolt)- (pitch) - yaw) - [- translate (ty —ty— 1] * uf , 1. 1 (2.7.9) = This can be represented by a single homogeneous matri. 5 Tr Ta Tis Te q Ts Ta Ta Tu | fy yfof "PP yy 2.7.10) z ty To Ta Tu | [fF 1 Ta Ta Tos To Here, cos (yaw) cos (roll) + sin (yaw) sin (pitch) sin (roll) os (yaw) sin (roll) + sin (yaw) sin (pitch) cos (oll) 231 Transformations 1 Ty = sin (yaw) 608 (pitch) Ty 20 Th = 0s (pitch) sin (oll) Ty, = 008 (pitch) cos (roll) Ty = sin pitch) in (yaw) cos (ol) + cos (ya) sin (pith) sin (ol) ‘Tyy = sin (yaw) sin ll) + c0s (yaw) sin (pitch) cos (rol) Tyy = 0s (yaw) c0s (piteh) 0 -GTath Tate Ty) ~GTath Toth Ta) 4 Tu th Toth Ts) Wel Ty = Tt is illustrated with an example, consider the condition shown in Fig. 2.7.6 subsequent conditions overcome: roll = 0° pitch = 0° yaw=180° 4 = 10° yel hel The transform is r -100 10 = x yf} obey z oo-11 ||? 1 1 0001 It is equal to the equation utilized for direction cosines. One more example j Fig. 2.7.7, where the following conditions are present: roll = 90° pitch yaw = 0° 4 80° 05 1 = 05 hell wv Game Programming (MU B.Sc. Comp. - Sem-V)_ 2-32 Transformations and Direct X ‘The transform is , 0 -10 05 7 x y} [71 e eos], z 0 o-1n |]? 1 1 001 - If you substitute (1, 1, 1) for (, y, 2), it will generates (- 0.5, - 0.5, 10) for (x, y, z)- Likewise, if you substitute (0, 0, 1) for (x, y, 2), it will generate (0.5, 0.5, 10) for (xy, 2s you can see this in Fig. 2.7.7. (1.11) Fig. 2.7.7 : The secondary axial system with a roll of 90°, a pitch of 180°, and a translation of (05,05, 11) Syllabus Topic : Rotating a Point about an Arbitrary Axis 2.8 Rotating a Point about an Arbitrary Axis ~ There are two approaches of a matrix for rotating a point about an arbitrary axis : 1) Employ vector analysis and is quite concise. 2) Matrices and trigonometric evaluation, ~The second approach is less analytic and laborious. Both the approaches give same result. Fig. 2.8.1 shows three views of the geometry related with the task at hand. ~The overall scenario is given in the left-hand image; the side elevation is depicted in the ‘middle image; plan elevation is shown in the right-hand image. oO oO Fig. 2.8.1 : Geometries 3 views related with rotating a point about an arbitrary axis ‘The axis of rotation is set by the unit vector 0 = ai + bj + ck ~The point P(x, y,, z,) tobe rotated by an angle a and P(x, , Z,) is a rotated point is denoted by O, as p and p are location vectors for P and P correspondingly Fig. 2.8.1, i) P= ON+NQ+QP ~ To locate ON nil = Iplicos =p thus, = ON = n=f(-p) To locate — NQ = Bre NGracosar however, p= ntrah(t-p)+r therefore, =” p-8(i-p) = and NQ = [p-2@ -p)leosa W came Programming (MU B.Sc. Comp.-Sem-V)_ 2.94 “Transformations and Direct X To find Let ff = xpsw Where, tiwil = 11 1l-lipllsin @= lil sin® But, inl = ll sind thus, wil = te Now, P’ Pp’ 4 7 2 AF sing $0, OF = wsina= (xp) sina next P= D@-p)+[p-2@- py) cosa+(@xp) sina furthermore P= poosa+f (Rp) (1-cos a) + (xp) sina. Let K = 1-cosa next P= peosa+n(n-p)K+(Axp)) sina furthermore 7 " (xpi + Ypj +2) cos ot + (ai + bj + ck) (ax, + by, + c2,) K + [(bz,—cy,) i + (ex, —a2,) j + (ay,—bx,) k] sine P= [x,cos a+a(x, + by, + c2,) K+ (bz, —cy,) sin a] i + [yp cos a+ b(ax, + by, +.2,) K] + (ex, —az,) sin ce) j + [z, cos a+ c(ax, + by, + cz,) K] + (ay, —bx,) sin al) k P= [ky (@ K+cosa)+y, (abK-csina) +z, (ac K +b sina] i +[x, (ab K +csina) +y, (b” K + cos a) +2, (be K-asina)]j +x, (ac K-b sina) +y, (be K+asin a) +2,(¢° K + cosa)} k Ke tttt—~— |. NW came Progamming (MU Se, Comp.-Sem\)_ 235 ‘Transformations ‘And the transformation becomes x. @K+cosa abK-csina acK+bsina) 7, y,| = | oK+csina WK+cosa beK-asina | -/Yp z acK-bsina beK+asina CK+cosa where K = 1-cosa ~ Now let's solve the problem utilizing transforms and trigonometric identities. (& y, z) in the Fig. 2.8.2 shows is rotated through an angle a to P(x, y, z) abi given by v=al +b] + ck, where llvll= 1. The following transform is used to achieve this operation: x x y | =T5xTxT)xT, xT, x] z Tt aligns the axis of rotation with the x-axis, carry out the rotation of Pa about and finally returns the axis of rotation to its original location. Fig. 2.8.2 : The geometry associated with rotating a point about an arbitrary ax Hence, T, rotates @ about the y-axis ; Tarotates ~ about the z-axis T, rotates a about the x-axis ; T, rotates 8 about the z-axis T, rotates — @ about the y-axis. Where cos 0 sing cos} sing 0 T=] 0 1 0 | Te} -sing cosp 0 -sing 0 cosh o o1 eee a Transformations and DirectX Game jrammit cos -sing 0 , 0 T, =| 0 cosa -sina sino ane i 0 sina. cosa ol esti 0 ve The sing 0 mr Let Ey By Bis TXT, XTX TXT, = | Fx Ber Fas Ey Ba Bs From Fig. 2.8.2 e058 = 1b = cos*0=1-bF sin@ b=>sin'e=b? a 2 os = FG ment o= 78 2 sin@ = Top 2atees To find E,, E,1 = c0s"6 c0s"0 + cos sin* 0 cos c+ sin’ cos a 2 2 ¢ By = ; Tage eos a 2 ° é ae pe 8+ 05 0 22 2, (c+ab’ = oe (E222) ona 2 a+b ec : n 24-2 cos.0 2 '(1-cos &) +05 |! Game £ En Ei To find B, mee. som) 297 Transformations ar 1-cos PK+cosa cos cos 8 sin cos sin O cos .—sin 6 cos 8 sin cos a >=: Toe apt no ab—ab cos a—csin a ab(1 cos a)—c sin o abK -csina 08 6 sin 6 c0s78 + cos 6 sin’ cos a.+sin’d sin 8 sin & +05" sin 8 sin o1—cos 6 sin} cos a cos 6 sin 6 cos’8 + cos 6 sin’ cos c+ sin O sin ot sin" + co = cos 6 sin cos a cos 6 sin 6 cos’8 + cos @ sin sin’® cos «+ sin @ sin ct sin’ —cos $ sin $ cos 0s 6 sin 6 cos" + cos @ sin 6 sin’6 cos a + sin 8 sin cos 6 sin $ cos a ae To Far Oa ae ema a 8 a1) : (aps °8 E+ sin a= ac (cos a) +b sina acK +b sina, ac+ac 298 ‘Transformations and Direct X = sin 8 cos 8 cos $— cos 6 sin @ cos 6 cos a. +-cos O sin 6 sin ot Ey = bVi=b as -Vi-0 bape +Vi-8 “Fargitinanab-abeosa+esina & 1 = ab(1—cos a) +csina=abK +csina. TofindE,. sin’ 8 + cos" @ cos a= b+ (1—b%) cos b +008 0~b* cos a= BK +005 a 0 (1 -cos a) + cos a fof To find T, sin @ cos 0 sin ~ cos 8 sin 8 cosc—cos 8 cos ¢ sin & ey = bint ES i-0 -b. -Vi-0 ‘Jone ° be—be cos o.~ ot sin ct = be - cos a) - oe sin beK - a sina. & f Ex = cos $ sin @ cos" 8 +.cos sin sin“8 cos c2—cos% sin @ sin a 008 6 sin 6 cos a. Spies Cee 1 So Eu = op yo oa fae Poo in 0 si 26 + sin’) -—8— .—_£ pane sing (eos +t) Tore But sin’ +c0s" = 1 Ey [pil -b4) cos ab sina Ey, = ac—accos a-b sina Ey, = ac(I—cos a)—bsin a. Eu acK ~b sina. le Game Programming (MU B.Sc. Comp.- Sem-V)_ 2-99 Transformations and To find, Exz = sin 6 cos 0 sin 0 — sin 4 sin 8 cos 0 cos 0+ cos 6 cos @ sin a. Ey Taw os eae T=? «cos a + Foe iP sina By = be-becos + a.sin a=be (1 -cos a) + o-sin & Ey, = bcK+asina To find Ey, Ey sin’ 9 cos? @ + sin” 6 sin” 6 cos a.— cos 6 sin @ sin @ sin + cos sin 6 sin @ sin c+ cos’ @ cos a 2 Eyy = c°+(1-c') cos c=c?(1—cos a) +008 a By = CK+cosa, ‘Therefore the transform is Ey @K+cosa abK-csina acK+bsina. y x =| abK+csina b’K+cosa beK-asina iH acK-bsina beK+asina cK+cosa. 1-cos a This is equal to the transformation resulting from the first approach. With the he! example let's test the matrix. — If we rotate the point P(10,5,0), (360°) about an axis given by v=i+j +k, it mus to itself creating P(10,5,0) = yoo a = 360° cosa = 1 sina = 0 k=0 Furthermore , a= Ib=le=1 , 100 x 10 y| =| 10]. 5 2 001 Itis an identity matrix P’= P. 2.8.1 Quaternions = Ifyou want to rotate points about an arbitrary axis and the orientation of virtual camera and objects, Quaternion. - To perform this transformation first understand the action of quaternion's in context of rotations. = Aquaternion q is a quadruple of real numbers and is given as a= [sv] -.(2.8.1) - Here s is a scalar as well as a 3D vector. If vector is given in terms of its components, then it's algebraic form is q = [s+xi+yj+zk] wn(2.8.2) - Heres, x,y and are real numbers 2.8.2 Adding and Subtracting Quaternion's - There are 2 quaternion’s ql and q2 are given below: = [sevil=[stxityij+zk) @ = yvJ=[s+mityj+2k) +-(2.83) ~ These 2 quaternions are equal if, their related terms are equal. They are also added and subtracted like vectors as follows: gta > (648) +, #x)i+Q,+ydj+@#2)k) -2.8.4) 2.8.3. Multiplying Quaternions ~ Special rules must be used for multiplying quatemions. It is discovered by Hamilton : Mose repemeg ones cop sen) ET j= kjksikisj ik = -kij=-iik=—j ~ The multiplication of the quaternion is non-commutaive. ~The following are the 2 quaternions q, and a = bbuyl=l nity jtael @ = byvd=(gtmityjtnk ~ The product of quaternion’s qiqa is given as follows: Gee = [65.19% Ye“ BE) + OFA VED! + (Giyat Sy +2 -ZAD) + a th HV MK ~ By using the dot and cross product notation it is rewrite as WG = [66 .- Vs Vd, 5V2t SM FMAM 2.8.4 The Inverse Quatemnion — The quaternion is given as q = [stxityjtz4] — The inverse of the quaternion q~' is qt = Gescyind igi ~ Here Iq) is the magnitude modulus, of g, and is equal to Il = VF axe er — Its also shown that aq’ = q'q=1 2.8.5 Rotating Points about an Axis 285 +283, (2.8.8, 2.89) (28.10) (2.8.11 one(2.8.12 ~ Fundamentally, quaternions are related with vectors not with the individual points alon: these lines, on the off chance that you need to control single vertex; you should firs transform it into a position vector, which has its tail vertex atthe origin, - A vertex can then be represeated in quaternion form by its equal position vector and zero scalar term. For example, a point P(x, y, 2) is given in quaternion form as follows p = [O+xi+yj +24) on(2.8.15 - To transform it into another position vector use the procedure given below. Th: components of the rotated position vector are the coordinates of the rotated point. aaa EE : : = Gamo Pr (UB.Sc. Comp. -Sem-V) _ 2-42 ‘Transformations and Direct X Let's observe how this i achieved. Ii given that a postion vector p is rotated about an axis by some angle by using the following operation: P = apg" (2.8.14) ‘The rotation axis and rotation angle are set within the unit quaternion q, and it's modulus is 1, and p is the rotated vector. For example, to rolate a point P(x, y, 2) through an angle @ about an axis, the following steps are used : 1, Convert the point P(x, y, z) to a quaternion p p= (O+xi+yj+2k] 2. Describe the axis of rotation asa unit vector u us [xityj+nk) 3. Describe the transforming quaternion q ls] o> [oll 4, Describe the inverse ofthe transforming quatemion q-1 "= [eoff)-sQ)e] g' = |cos(>)—sinl5)u P = 4Pq 5. Compute p 6. Unpack (x’, y’,z’) Ky .z)p = (O+xityj+70 With the help of example let's check the action of the above transform. Consider the point P (0, 1, 1) given in Fig. 2.8.3, this point is to be rotated 90° about the y-axis. ‘You can notice that the rotated point P has the coordinates (1, 1, 0), which we will prove algebraically, ‘The quaternion P represents point P, and is rotated by evaluating the quaternion P: Fig. 28.3 : The point P (0,1, 1) is rotated to P (1,1, 0) using a quaternion concurrent ith the y axis a ‘ame Programming (MU B.Sc. Comp.-Sem-V)_2-43 Transformations and Din P = aPq' It stores the rotated coordinates. The axis of rotation is [j], therefore the unit quaterni is given by a= [eos (22°) sia 2%") 0,101] = eos 45, 0, sn 45%), 011 The inverse quaternion q”’ is given by ‘Now evaluate qPq_" in two stages : (qP)q* gp = [cos (45°), (0, sin (45°), 0] - (0,1,1]] = [sin (45°), [sin (45°), cos (45°), cos (45°)]] @P)q* = [-sin (45°), [sin (45°), cos (45°), cos (45°)]] + [eos (45°), [0, —sin (45°), Ol] = [0,[2 sin (45%) cos (45°), 1, cos (45°), cos (45°) — sin (45°) sin (45 P = (0,(1,1,0)) The vector component of P proved that P is certainly rotated to (I, 1, 0). Let's see more example, consider a rotation about the z-axis as given in Fig. 2.8.4. ‘The coordinates of the original point are (0, 1, 1 ) and is rotated -90. From the figun view that this should finish at (1, 0, 1). This time the quaternion q is described by [C$ so) = [eos (45°), (0,0, ~ sin (45°)]] q Inverse is q' = [cos (45°), (0,0,sin (45°))] And the point to be rotated in quaternion form is P = (0, (0, 1, 1]]. After evaluating two stages we get @ = [cos (45)°, (0,0,-sin(45°)]] - [0, [0,1,1]} = [sin (45°), [sin (45°), cos (45°), cos (45°)]} ==: __2_2_2___&——_—_7. | W came Progaming UBS: Conp.-SemV)_2-44 ‘Transformations and Direct X y 0,1) Fig. 2.84: The point P(0, 1, 1)is rotated ~90e to PU, 0, 1) using a quaternion coincident with the z axis 2 (@P)q”' = [sin (45°), (sin (45°), cos (45°), cos (45°)] [eos (45°),(0,0,sin (45°)]] = (0, [sin (90%), cos (90°), 11] ‘The vector component of P proves that P is rotated to (1,0, 1). 2.8.6 Roll, Pitch and Yaw Quaternions — Now we are aware of roll, pitch and yaw rotations, now define them as quaternions: wu = [>on wm = [ff so] Qu = [es @ sin @ waa] (2.8.15) — Here @ is the angle of rotation. To represent a compound rotation these quaternions are multiplied together to create single quaternion. For example, ifthe quaternions are described as Gon = [eos (4) sin (3) 100.1] ics = [ow (4) sin (a) 110.1] yen = [ew (=), sin &) (a1.0)] (2.8.16) ~ They can be concatenated toa single quaternion q : = Gan Sich Qu = [5 +41 + yi + 2k] 28.17) bi ‘Transformations and Di ). »= G2). <=) 5 = cos(a)cos(b) cos (c)+ sin (a)sin (b)sin (c) x = cos(a) sin (b) cos (c}+ sin (a)cos (b)sin (c) w(2, sin (a) cos(b) cos (c)- cos (a)sin (b)sin (c) cos (a) cos (b)sin (c}- sin (a)sin (b)cos (c) Let's examine this compound quaternion with an illustration. For instance, give following conditions let's get a single quaternion q to represent the compound rotatio « 1 roll = 90° pitch = 180° yaw = 0° — The values of s, x, y, z are given as follows s=0 x = cos (45°) y = -sin(45*) z= Quatemion q is = [0, [cos 45°, — sin (45°), OJ) - If the point P(1, 1, 1) is subjected to this compound rotation, the rotated point is estir utilizing the standard quaternion transform : P= qPq’ - Now evaluate qPq in two stages : 10, {cos (45°), —sin (45°), 07) -[0, (11,10) [0, [= sin(45°), - cos (45°), sin (45°) + cos (45°)]] {0, [- sin (45°), ~cos (45°), sin (45°) +-c0s (45°)]] + (0, —cos (45°), sin (45°), 0) Y= (0,(-1,-1,-11) — Thus, the coordinates of the rotated point are (~1, -1, -1), which is proved Fig. 2.8.5, 2.8.7 Quaternions In Matrix Form Pe — There exist a direct relationship between quatemions and matrices. For example, fc given quaternion [s +.xi + yj + 2k) the corresponding matrix is My My Mis Mz Mz Ma My Mn Ms Game Pi ing (MU B.Sc. Comp.-Sem-V)_2-46 Transformations and Direct X Pt, -1,-1) Fig. 2.8.5 : Two stage transform for the point P which is subject to a compound roll of 90° and a pitch of 180° My = 1-2(y7+2) My = 2 (xy +82) My = 202459) My = 2(xy+s2) My = 1-2('+2)) My = 2 (yz-sx) My, = 2(x2~sy) Mz, = 2(y2+sx) My = 1-2(°+y)) (2.8.19) Substitute the following values of s, x, y, 2: s=0 x = cos (45°) y = -sin(45°) 2 = 0 ‘The matrix transformation is given as f] 0-10 fy y|=]-10 0 |y z 0 0 -1 - When you substitute (1, 1, 1) for (x, y, z) the rotated point becomes (-1,-1,-1), as dipicted in Fig. 2.8.5. 2.8.8 Frames of Reference ~ For rotating a vertex or for positioning a virtual camera a quaternion or its equivalent matrix is used. If you use the unit quaternions, it means associated matrix is orthogonal and its transpose is equal to rotating the frame of reference in the opposite direction. Som-V)_2-47 ~ For example, if the virtual camera is oriented with a yaw rotation of 180, i. looking along the negative z axis, the orientation quaternion is (0, [0, 1, OJ]. Therefor, s=0,x=0,y=1z=0. -10 0 — The corresponding or equivalent matrix is] 9 1 0 0 0-1 ~ Wis equivalent to is transpose, Consequently, vertex (% » 2) in world spaEe hy coordinates (x,y,z) in camera space and the transform is described by ue -10 0 ]¢ y| =| 01 0 }ly z 00-1 ~ Ifthe vertex (x,y, 2) is (1, 1,0), (& 3, 2 becomes (-1, 1, 0), which is comrect. However, i is unlikely that the virtual camera will only be subjected to a simple rotation, as it wil) ‘normally be translated from the origin. — Consequently, a translation matrix will have to be introduced as described above. ‘Syllabus Topic : Transforming Vectors 2.9 Transforming Vectors So far we have seen the transforms for single points. A geometric database contains no: only pure vertices, but also vectors, which must also be put through any current transform, A general transform Q fora 3D point is represented by x = ta]? 1. You can notice that the homogeneous scaling term end to zero. This means any vect [xy 2]" can be transformed using Programming (MU B.Sc. Co ) 2.48 Transformations and Direct X : * yea]? 93) z : 0 0 — Let's inspect it by utilizing a transform from previous example. The problem concerned a change of axial system where a virtual camera was subject to the following: roll = 180° pitch = 90° yaw = 90° 4,2 a2 L=0 The transform is a o-102) eal ia | se Ons Ont ONE z -1 002) ]2 1 o oo — After the transformation the point (1,1,0) tured into (1,0,1), however, if we transform the vector [1 1 0]" it tured into [1 0 ~ 1}* by using the following transform -1 o-102771 0 o o1olfi lr ea edt 208072)(i|,0 . o colo 2.10 Determinants — Let's see the part of the determinant in transforms. Generally determinants come up in the solution of linear equations for example = axtby & = axtby (2.10.1) — Here the values of x and y are described in terms of the other constants. The values of x and y without showing the solution are given by +-o(2.10.2) W came Programming (MU B.Sc. Comp. Sem-V)_ 249 ‘Transformations and Direct x Given that the denominator a,b, ~ a, b, #0 ‘You can also write the linear equations in matrix form [*] [* bi] fx TE) em — We noticed that the denominator comes from the matrix terms aibz ~8:b.. This is calleq the determinant, and is suitable only for square matrices. A determinant is described as follows: |" b ab - By using this notation you can rewrite the original linear equations as Ey = ETE (2.10.4) ab he % by — The following are the 3 linear equations : dy = axtby tor d= at by tee d, = ax+byy +2 (210.5) — By using these 3 linear equations the value of xis given as bye, — diya + dabses — &bics + dsbiea— dabser X= “byes abies Fase, Mabe APPA ins) With same expressions for y and z Yet again, the denominator comes. from the inant of the matrix connected with the matrix formulation of the linear equations E-SHEe = aha, Where ay by a_bal = hye — aybace + Bsr — Bios + AHbiOa— APE ay by = Itcan be written as b °| by *| b “| aly gl *lh oll « ‘Transformations and Direct X Take the following example to check what creates a zero determinant. 10 = x+y (2.10.8) For x and y infinite solutions are available, so it is not viable to solve the equation. on the other hand, if we bring in a second equation relating x and y : 4 = Sx-y -+(2.109) Wecan solve for x and y using Equation (2.10.2) x = Wxe)-4x1 = 2xE1)-5x1 epee 10.10) Sox=2and y=6, which is comect. But state the second equation had been 20 = 4x+2y (2.10.11) ‘This would have created the pair of concurrent equations 10 = 2x+y (2.10.12) 20 = 4x42y 2.10.13) Now if we solve this equation for x and y then we will get 10x 2x = BADD 10 8 undefined (2.10.14) ‘We will get the undefined results. The main reason behind it is the Equation (2.10.12) is the same as equation (2.10.13). The second equation is two times of the first equation, and thus brings nothing innovative to the relationship. ‘When such situation comes the equations are said to be linearly dependent. We have seen the algebraic origins of the determinant; now let’s investigate its graphical importance. Consider the following transform [7] * iE ‘ll Li] 2.10.15) ‘The determinant of the transform is ad -cb. If we issue the vertices of a unit-square to this transform, the situation is created as shown in Fig. 2.10.1. The vertices of the unit-square can be moved as follows :

You might also like