Graphics Programming I

Similar documents
Linear Algebra V = T = ( 4 3 ).

Lecture 3: Matrix and Matrix Operations

CSC 470 Introduction to Computer Graphics. Mathematical Foundations Part 2

CS 112 Transformations II. Aditi Majumder, CS 112 Slide 1

Review of linear algebra

Lecture 2: Vector-Vector Operations

Mathematics 13: Lecture 10

MATH.2720 Introduction to Programming with MATLAB Vector and Matrix Algebra

L3: Review of linear algebra and MATLAB

A VERY BRIEF LINEAR ALGEBRA REVIEW for MAP 5485 Introduction to Mathematical Biophysics Fall 2010

Vectors and Matrices

03 - Basic Linear Algebra and 2D Transformations

Vectors are used to represent quantities such as force and velocity which have both. and. The magnitude of a vector corresponds to its.

2 Sequence of transformations

Vector Operations. Vector Operations. Graphical Operations. Component Operations. ( ) ˆk

STAT 350: Geometry of Least Squares

Vectors Coordinate frames 2D implicit curves 2D parametric curves. Graphics 2008/2009, period 1. Lecture 2: vectors, curves, and surfaces

Review of Linear Algebra

A = 3 B = A 1 1 matrix is the same as a number or scalar, 3 = [3].

Review of Coordinate Systems

Lecture 3 Linear Algebra Background

MATH 320, WEEK 7: Matrices, Matrix Operations

2D Geometry. Tuesday September 2 rd, 2014.

CS123 INTRODUCTION TO COMPUTER GRAPHICS. Linear Algebra 1/33

GEOMETRY AND VECTORS

Basic Linear Algebra in MATLAB

Linear Algebra & Geometry why is linear algebra useful in computer vision?

Matrices. Math 240 Calculus III. Wednesday, July 10, Summer 2013, Session II. Matrices. Math 240. Definitions and Notation.

Dot Products. K. Behrend. April 3, Abstract A short review of some basic facts on the dot product. Projections. The spectral theorem.

CONVERSION OF COORDINATES BETWEEN FRAMES

Vectors. The standard geometric definition of vector is as something which has direction and magnitude but not position.

Arrays: Vectors and Matrices

3 a 21 a a 2N. 3 a 21 a a 2M

Phys 201. Matrices and Determinants

Complex Matrix Transformations

Linear Algebra Basics

22A-2 SUMMER 2014 LECTURE Agenda

Linear Algebra (Review) Volker Tresp 2017

Matrix & Linear Algebra

CS123 INTRODUCTION TO COMPUTER GRAPHICS. Linear Algebra /34

10. Linear Systems of ODEs, Matrix multiplication, superposition principle (parts of sections )

CHAPTER 4 VECTORS. Before we go any further, we must talk about vectors. They are such a useful tool for

Linear Algebra Review Part I: Geometry

Matrix Multiplication

Intro Vectors 2D implicit curves 2D parametric curves. Graphics 2011/2012, 4th quarter. Lecture 2: vectors, curves, and surfaces

Basic Math Matrices & Transformations

Linear Algebra (Review) Volker Tresp 2018

Lecture 02: 2D Geometry. August 24, 2017

Vector and Affine Math

Elementary maths for GMT

MTAEA Vectors in Euclidean Spaces

Review of Linear Algebra

Elementary maths for GMT

Linear Algebra & Geometry why is linear algebra useful in computer vision?

Notes on vectors and matrices

Matrix Dimensions(orders)

This appendix provides a very basic introduction to linear algebra concepts.

Graphics (INFOGR), , Block IV, lecture 8 Deb Panja. Today: Matrices. Welcome

v = v 1 2 +v 2 2. Two successive applications of this idea give the length of the vector v R 3 :

k is a product of elementary matrices.

Vector, Matrix, and Tensor Derivatives

Math Review -- Conceptual Solutions

Worksheet 1.3: Introduction to the Dot and Cross Products

Physics 342 Lecture 2. Linear Algebra I. Lecture 2. Physics 342 Quantum Mechanics I

Section 6.1. Inner Product, Length, and Orthogonality

Lecture 10: Eigenvectors and eigenvalues (Numerical Recipes, Chapter 11)

7.5 Operations with Matrices. Copyright Cengage Learning. All rights reserved.

Dot product. The dot product is an inner product on a coordinate vector space (Definition 1, Theorem

Physics 342 Lecture 2. Linear Algebra I. Lecture 2. Physics 342 Quantum Mechanics I

Mobile Robotics 1. A Compact Course on Linear Algebra. Giorgio Grisetti

Sometimes the domains X and Z will be the same, so this might be written:

Introduction. Introduction (2) Easy Problems for Vectors 7/13/2011. Chapter 4. Vector Tools for Graphics

We could express the left side as a sum of vectors and obtain the Vector Form of a Linear System: a 12 a x n. a m2

Contravariant and Covariant as Transforms

Unit 3: Matrices. Juan Luis Melero and Eduardo Eyras. September 2018

Introduction - Motivation. Many phenomena (physical, chemical, biological, etc.) are model by differential equations. f f(x + h) f(x) (x) = lim

Math 129: Linear Algebra

Game Engineering: 2D

Image Registration Lecture 2: Vectors and Matrices

Dealing with Rotating Coordinate Systems Physics 321. (Eq.1)

Linear Algebra Review. Fei-Fei Li

On-Line Geometric Modeling Notes FRAMES

Math 360 Linear Algebra Fall Class Notes. a a a a a a. a a a

CSE4030 Introduction to Computer Graphics

Matrices: 2.1 Operations with Matrices

Basic Linear Algebra. Florida State University. Acknowledgements: Daniele Panozzo. CAP Computer Graphics - Fall 18 Xifeng Gao

Matrix Arithmetic. j=1

Mathematics for 3D Graphics

Mon Feb Matrix algebra and matrix inverses. Announcements: Warm-up Exercise:

MITOCW ocw f99-lec17_300k

INSTITIÚID TEICNEOLAÍOCHTA CHEATHARLACH INSTITUTE OF TECHNOLOGY CARLOW MATRICES

Chapter 2. Linear Algebra. rather simple and learning them will eventually allow us to explain the strange results of

Usually, when we first formulate a problem in mathematics, we use the most familiar

1 Matrices and matrix algebra

Mathematics for Graphics and Vision

Matrix Arithmetic. a 11 a. A + B = + a m1 a mn. + b. a 11 + b 11 a 1n + b 1n = a m1. b m1 b mn. and scalar multiplication for matrices via.

POLI270 - Linear Algebra

v = ( 2)

A Review of Linear Algebra

Notes: Vectors and Scalars

Transcription:

Graphics Programming I Agenda: Linear algebra primer Transformations in OpenGL Timing for animation Begin first programming assignment

Linear algebra primer Three important data types: Scalar values Row / column vectors 1x4 and 4x1 are the sizes we'll most often encounter Square matrices 4x4 is the size we'll most often encounter

Scalars These are the numbers you know! Example: 3.14, 5.0, 99.9, 2, etc.

Row vectors These are special matrices that have multiple columns but only one row. Example: Add and subtract the way you would expect. Example: [5.0 3.14 37 ] [1 2 3] [9 10 11]=[10 12 14 ] Both vectors must be the same size. Operate with scalars the way you would expect. Example: 3.2 [1 2 3]=[3.2 6.4 9.6 ] Notice that vector multiplication is missing...

Column vectors These are special matrices that have multiple rows but only one column. Example: [ 1 2 3] Work just like row vectors.

Vector Operations There are only a few operations specific to vectors that are really important for us.

Dot Product Noted as a dot between two vectors (e.g., A B ) Also known as inner product. Multiply matching elements, sum all the results. Example: [2.3 1.2 ] [1.7 6.5 ]= 2.3 1.7 1.2 6.5 =11.71

Vector Magnitude Noted by vertical bars, like absolute value. Take the square root of the dot product of the vector with itself...like absolute value. Result is the magnitude (a.k.a. length) of the vector. Example: [ 2 2 2 ] 2 = [ 2 2 2 2 2 2 2 2 2 ] 2 [ 2 2 = 2 4 2 4 =1 2 2 ] =

Normalize Noted by dividing a vector by its magnitude. Example: A A Results in a vector with the same direction, but a magnitude of 1.0. Works the same as with scalars.

Why is the dot product so interesting? In 3-space, the dot of two unit vectors is the cosine of the angle between the two vectors. If the vectors are not already normalized (unit length), we can divide the dot product by the magnitudes. Example: a b a b =cos

Cross Product Noted as an X between two vectors (e.g., a b) Derivation of the cross product is not important. The math is: a b=[a y b z a z b y a z b x a x b z a x b y a y b x ] Only valid in 3-dimensions.

Why is the cross product so interesting? Two really useful properties. The result of the cross product between two vectors is a new vector that is perpendicular (also called normal) to both vectors. If the source vectors are normalized: a b =sin

Matrices Like vectors, but have multiple rows and columns. Example: [1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0] Add and subtract like you would expect. Like vectors, both matrices must be the same size...in both dimensions.

Matrix / vector multiplication Special rules apply that make it different from scalar multiplication. Not commutative! e.g., Is associative. e.g., M N N M NM P=N MP Column count of first matrix must match row count of second matrix. If M is a 4-by-3 matrix and N is a 3-by-1 matrix, we can do M N, but not N M. If the source matrices are n-by-m and m-by-p, the resulting matrix will be n-by-p.

Matrix / vector multiplication (cont.) To calculate an element of the matrix, C, resulting from AB: C ij = r=1 a ir b rj What does this look like? n

Matrix / vector multiplication (cont.) To calculate an element of the matrix, C, resulting from AB: What does this look like? n C ij = r=1 a ir b rj The dot product of a row of A with a column of B. This is why the column count of A must match the row count of B...otherwise the dot product wouldn't work.

Multiplicative Identity There is an identity for matrix multiplication. 0 0 0 1 I=[1 1] 0 0 0 Just like any other multiplicative identity,. If you pretend that a scalar is a 1x1 matrix, this should make sense. AI= A

Transpose Noted by a T in the exponent position (e.g., ). M T The rows become the columns, and the columns become the rows. Example: [ 2 3 4 5 6 7]T =[ 2 4 6 3 5 7]

References http://en.wikipedia.org/wiki/matrix_multiplication http://en.wikipedia.org/wiki/dot_product http://en.wikipedia.org/wiki/cross_product

Rotation using matrices Rotation around the Z-axis. If ϴ is 0, this is the identity matrix. Rotations around the Y-axis. [cos sin 0 0 1] sin cos 0 0 0 0 1 0 0 0 0 [ cos 0 sin 0 1] 0 1 0 0 sin 0 cos 0 0 0 0

Question! Looking at the previous equations, we can do a rotation using 4 multiplies and 2 adds, but the matrix multiply requires 16 multiplies and 12 adds. x' = x cos ϴ + y sin ϴ y' = -x sin ϴ + y cos ϴ z' = z Why use the matrix method?

Matrices are more expressive! If we want to do a series of rotations with matrices we could do: Which is the same as: Look familiar? v'=m 1 v v ' '=M 2 v ' v' ' '=M 3 v ' ' M 3 M 2 M 1 v

Matrices are more expressive! If we want to do a series of rotations with matrices we could do: Which is the same as: Look familiar? v '=M 1 v v ' '=M 2 v ' v ' ' '=M 3 v ' ' M 3 M 2 M 1 v Matrix multiplication is associative! M 3 M 2 M 1 v

Translations with Matrices Points are stored as p = [ x y z 1 ] Remember the definition of matrix multiplication: p x '= p x M 11 p y M 12 p z M 13 p w M 14 p y '=p x M 21 p y M 22 p z M 23 p w M 24 p z '=p x M 31 p y M 32 p z M 33 p w M 34 p w '=p x M 41 p y M 42 p z M 43 p w M 44 Since p w is always 1, the 4 th column of the matrix acts as a translation.

Scaling with Matrices To scale, we just want to multiply each component by a scale factor. Piece of cake! M=[S x 0 0 0 0 S y 0 0 0 0 S z 0 0 0 0 1]

References http://en.wikipedia.org/wiki/rotation_matrix

Break

OpenGL Transformation Matrices OpenGL has several matrix stacks. Each stack has a specific purpose. Active stack selected with glmatrixmode(). OpenGL is modal, so ones a matrix stack is selected, all matrix commands will affact that stack. For object transformations, use the GL_MODELVIEW matrix.

Loading Matrices Initialize to the identity matrix with glloadidentity(). Load the matrix from your program with glloadmatrix[fd](). GL matrices are column major, but C stores 2- dimensional arrays row major. Watch out! We'll use glloadtransposematrix[fd]() later. Can read matrix back with glgetfloatv().

Matrix Operations Multiply a matrix with the top of the stack using glmultmatrix[fd](). This is a post-multiply. M top =M top M glpushmatrix() and glpopmatrix() save and restore the current top of stack. This means you can push, make changes, then pop to get back the previous matrix. The stack has a limited depth, so don't go crazy!

Built in Transformation Operators Several routines to create and concatenate common transformations: glrotate[fd]() - Create a rotation around an abritrary vector. gltranslate[fd]() glscale[fd]()

Orthonormal Basis Yes, it's a mouthful...but what does it mean? A vector space where all of the components are orthogonal to each other, and each is normal. Normal meaning unit length. Orthogonal meaning at right angles to each other. All pure rotation matrices (i.e., no scaling) are orthonormal bases. As is the identity matrix! But how is this useful?

Question! Q: Given a world position for a camera, a world position to point the camera at, and an up direction, how can we construct a transformation using just rotations and translations?

Question! Q: Given a world position for a camera, a world position to point the camera at, and an up direction, how can we construct a transformation using just rotations and translations? A: We can't. We can construct an orthonormal basis from those 3 vectors.

Camera Look At Matrix E is the eye position V is the point being viewed U is the up direction The function glulookat does this. F=V E f = F F, u= U U s=u f t=s f s 0 s 1 s 2 E 0 t M=[ 0 t 1 t 2 E 1 ] f 0 f 1 f 2 E 2 0 0 0 1

Example U=[0 1 0 ], E=[0 0 0 ], V =[ sin 2 0 cos 2 ] f =V s=f U=[ ]=[ f y U z f z U y f z U x f x U z f x U y f y U cos x 2 0 sin 2 ] t=s f =[ s y f z s z f y s z f x s x f z s x f y s y f x ]=[ cos 0 sin 2 M=[ 0 2 0 1 0 0 sin 0 cos 0 2 2 0 0 0 1 ] 0 sin 2 2 cos 2 2 0 ]

Look familiar? cos 0 sin 2 M=[ 0 2 0 1 0 0 sin 0 cos 0 2 2 1] 0 0 0 cos 0 sin 0 0 1 0 0 sin 0 cos 0 R=[ 1] 0 0 0

References http://www.wikipedia.org/orthonormal_basis

Break

Timing for Animation Animations should run at a consistent speed regardless of the rendering speed If someone starts ripping a CD or compiling a C program in the background, the animation should not slow down. What do we need in order to accomplish this?

Timing for Animation Animations should run at a consistent speed regardless of the rendering speed If someone starts ripping a CD or compiling a C program in the background, the animation should not slow down. What do we need in order to accomplish this? We need to know how much time has elapsed since the last frame. SDL_GetTicks() gives us this information. We also need to think of animation in terms of how long it takes to do something.

Example static Uint32 last_t = ~0; // Calculate time elapsed since last frame. // SDL_GetTicks returns the time in milliseconds. const Uint32 t = SDL_GetTicks(); if (last_t!= (Uint32) ~0) { float dt = (float)(t last_t) / 1000.0; } // One complete revolution every 3 seconds. rotation_angle += dt * ((2.0 * M_PI) / 3.0); // Update last_t. last_t = t;

Next week... Lighting and materials Second programming will be assigned. First programming assignment is due.

Legal Statement This work represents the view of the authors and does not necessarily represent the view of IBM or the Art Institute of Portland. OpenGL is a trademark of Silicon Graphics, Inc. in the United States, other countries, or both. Khronos and OpenGL ES are trademarks of the Khronos Group. Other company, product, and service names may be trademarks or service marks of others.