Computer Graphics Labs



Similar documents
Computer Graphics Labs

CS 4204 Computer Graphics

Graphics Pipeline in a Nutshell

2D Geometric Transformations

Vector Notation: AB represents the vector from point A to point B on a graph. The vector can be computed by B A.

Computer Graphics (Basic OpenGL, Input and Interaction)

Lecture 3: Coordinate Systems and Transformations

Given a point cloud, polygon, or sampled parametric curve, we can use transformations for several purposes:

An Introduction to. Graphics Programming

Geometric Transformations

OpenGL & Delphi. Max Kleiner. 1/22

Graphics Input Primitives. 5. Input Devices Introduction to OpenGL. String Choice/Selection Valuator

Section 1.1. Introduction to R n

2D Geometrical Transformations. Foley & Van Dam, Chapter 5

Exam 1 Sample Question SOLUTIONS. y = 2x

Solving Simultaneous Equations and Matrices

Section 1.4. Lines, Planes, and Hyperplanes. The Calculus of Functions of Several Variables

Thnkwell s Homeschool Precalculus Course Lesson Plan: 36 weeks

11.1. Objectives. Component Form of a Vector. Component Form of a Vector. Component Form of a Vector. Vectors and the Geometry of Space

TWO-DIMENSIONAL TRANSFORMATION

9 MATRICES AND TRANSFORMATIONS

Geometric Transformation CS 211A

Monash University Clayton s School of Information Technology CSE3313 Computer Graphics Sample Exam Questions 2007

Linear algebra and the geometry of quadratic equations. Similarity transformations and orthogonal matrices

Geometric Transformations

Geometry: Unit 1 Vocabulary TERM DEFINITION GEOMETRIC FIGURE. Cannot be defined by using other figures.

GeoGebra. 10 lessons. Gerrit Stols

Cabri Geometry Application User Guide

Geometric Camera Parameters

The mouse callback. Positioning. Working with Callbacks. Obtaining the window size. Objectives

Geometry Course Summary Department: Math. Semester 1

Gerrit Stols

Lecture Notes. Fundamentals of Computer Graphics. Prof. Michael Langer School of Computer Science McGill University

Methodology for Lecture. Review of Last Demo

Aston University. School of Engineering & Applied Science

North Carolina Math 2

THREE DIMENSIONAL GEOMETRY

G.H. Raisoni College of Engineering, Nagpur. Department of Information Technology

LINEAR ALGEBRA W W L CHEN

SOLUTIONS. f x = 6x 2 6xy 24x, f y = 3x 2 6y. To find the critical points, we solve

4BA6 - Topic 4 Dr. Steven Collins. Chap. 5 3D Viewing and Projections

CMSC 427 Computer Graphics 1

Orthogonal Projections

Problem Set 5 Due: In class Thursday, Oct. 18 Late papers will be accepted until 1:00 PM Friday.

Rotation Matrices and Homogeneous Transformations

521493S Computer Graphics. Exercise 2 & course schedule change

Understand the Sketcher workbench of CATIA V5.

What's the Spin? - Discover Properties of Geometric Rotations

Equations Involving Lines and Planes Standard equations for lines in space

Essential Mathematics for Computer Graphics fast

Lectures notes on orthogonal matrices (with exercises) Linear Algebra II - Spring 2004 by D. Klain

Circle Name: Radius: Diameter: Chord: Secant:

Two vectors are equal if they have the same length and direction. They do not


Week 1 Chapter 1: Fundamentals of Geometry. Week 2 Chapter 1: Fundamentals of Geometry. Week 3 Chapter 1: Fundamentals of Geometry Chapter 1 Test

CMSC 427 Computer Graphics 1

SpaceClaim Introduction Training Session. A SpaceClaim Support Document

Lecture L3 - Vectors, Matrices and Coordinate Transformations

A Correlation of Pearson Texas Geometry Digital, 2015

New York State Student Learning Objective: Regents Geometry

Chapter 1 Introduction to OpenGL

Common Core Unit Summary Grades 6 to 8

Questions. Strategies August/September Number Theory. What is meant by a number being evenly divisible by another number?

the points are called control points approximating curve

Figure 1.1 Vector A and Vector F

Content. Chapter 4 Functions Basic concepts on real functions 62. Credits 11

Elasticity Theory Basics

MA 323 Geometric Modelling Course Notes: Day 02 Model Construction Problem

discuss how to describe points, lines and planes in 3 space.

Number Sense and Operations

Math 241, Exam 1 Information.

Example SECTION X-AXIS - the horizontal number line. Y-AXIS - the vertical number line ORIGIN - the point where the x-axis and y-axis cross

Lecture 2: Homogeneous Coordinates, Lines and Conics

SDC. Schroff Development Corporation PUBLICATIONS. MultiMedia CD by Jack Zecher

Additional Topics in Math

Algebra I Credit Recovery

Affine Transformations

Teacher Page. 1. Reflect a figure with vertices across the x-axis. Find the coordinates of the new image.

A vector is a directed line segment used to represent a vector quantity.

Section 1: How will you be tested? This section will give you information about the different types of examination papers that are available.

Basic Understandings

Univers Virtuels. OpenGL : Modélisation / Visualisation. Alexis NEDELEC. Centre Européen de Réalité Virtuelle Ecole Nationale d Ingénieurs de Brest

Vector Math Computer Graphics Scott D. Anderson

Introduction to Matrices

Algebra 2 Chapter 1 Vocabulary. identity - A statement that equates two equivalent expressions.

Unified Lecture # 4 Vectors

THEORETICAL MECHANICS

SolidWorks Implementation Guides. Sketching Concepts

Conjectures. Chapter 2. Chapter 3

SOLIDWORKS: SKETCH RELATIONS

MOVIES, GAMBLING, SECRET CODES, JUST MATRIX MAGIC

Part I. Basic Maths for Game Design

Practice Final Math 122 Spring 12 Instructor: Jeff Lang

Angle - a figure formed by two rays or two line segments with a common endpoint called the vertex of the angle; angles are measured in degrees

Visualizing Triangle Centers Using Geogebra

PHYSICS 151 Notes for Online Lecture #6

Algebra and Geometry Review (61 topics, no due date)

Transcription:

Computer Graphics Labs Abel J. P. Gomes LAB. 2 Department of Computer Science and Engineering University of Beira Interior Portugal 2011 Copyright 2009-2011 All rights reserved.

LAB. 2 1. Learning goals 2. Getting started 3. Euclidean transformations 4. Affine transformations 5. 3D transformations in OpenGL 6. Example 7. Programming exercises 8. Final remarks GEOMETRIC TRANSFORMATIONS

Lab. 2 GEOMETRIC TRANSFORMATIONS In this lecture we are going to deal with geometric transformations in 2D as their generalization in 3D is straightforward. These geometric transformations are also called affine transformations. 1. Learning Goals At the end of this chapter you should be able to: 1. Explain what transformations are and why we use them in computer graphics. 2. List the three main transformations we use in computer graphics and describe what each one does. 3. Understand how to rotate a point around an arbitrary point. 4. Understand what homogenous coordinates are and why we use them in computer graphics. 5. Understand the importance of the order of operations in a matrix multiplication expression. 6. Understand what a CTM (Combined Transformation Matrix) is and understand what order the transformations must be in to achieve the desired CTM. 7. Be aware of the default facilities of OpenGL; for example, the default 2D domain is OpenGL is [-1,1]x[-1,1]. 2. Getting Started Geometric transformations are used to fulfill two main requirements in computer graphics: 1. To model and construct scenes. 2. To navigate our way around 2 and 3 dimensional space. For example, when a street building has n identical windows, we proceed as follows: 1. To construct a single window by means of graphics primitives; 2. To replicate n times the window. 3. To put each window at a desirable location using translations and rotations. This shows that transformations such as translations and rotations can be used as scene modeling operations. These transformations can be also used to move a bot or an avatar in the virtual environment of a First- Person Shooter (FPS) game.

3. Euclidean Transformations There are two Euclidean transformations: 1. Translation 2. Rotation 3.1. Translation Translation can be thought of as moving something. In translation, a point is moved a distance in a given direction. For example, when the point A(x, y) is translated dx units in the x direction and dy units in the y direction, it becomes: or, equivalently, Representing points as column matrices, we obtain A = x, y A'(x + dx, y + dy) x'= x + dx y'= y + dy A'= x' y' so that the translation can be expressed as follows: A'= A + T In general, translating an object means to translate its vertices (i.e. corners or endpoints) in such a manner that lines or polygons can then be drawn using the transformed vertices. 3.2. Rotation about the origin and T = dx dy By default, rotating an object by the angle θ means rotating it around the origin by θ. Using polar coordinates (r,φ), a given point in the plane is given by the following equations: x = rcosφ y = rsinφ After rotating this point by the angle θ around the origin, the get the following transformed point: or x'= rcos(φ + θ) y'= rsin(φ + θ)

that is In matrix notation, we then obtain or where R is the 2x2 rotation matrix. x'= r.cosφ.cosθ r.sinφ.sinθ y'= r.cosφ.sinθ + r.sinφ.cosθ x'= x.cosθ y.sinθ y'= x.sinθ + y.cosθ x' y' = cosθ sinθ 3.3. Homogeneous Coordinates We ve seen the following matrix transformations: Translation: Rotation: A'= R.A A'= A + T sinθ cosθ. x y A'= R.A Translation is achieved through matrix addition while rotation is achieved by matrix product. This means that we can combine any number of translation matrices through addition, and any number of rotation matrices through multiplication. However, we cannot combine translation and rotation matrices into a single matrix through the product operation. It would be very useful if we could do this because that would enable the composition of geometric transformations through a single matrix operation, say matrix product. Besides, it would be less computationally expensive, as explained below. Homogenous Coordinates are just a way to overcome this problem. With homogenous coordinates, a series of geometric transformations can be applied in a sequence using matrix product. The result is usually called combined transformation matrix or CTM. Therefore, translations and rotations expressed in homogenous coordinates are given by: Translation: Rotation: A'= T.A A'= R.A In homogenous coordinates a point P(x, y) is represented by the homogenous point where: P(X,Y,W )

X = x W and Y = y W, where W usually equals 1 in computer graphics for simplicity. Using homogenous coordinates, the Euclidean transformation matrices are expressed as 3x3 matrices as follows: Translation: 1 0 dx T(dx,dy) = 0 1 dy 0 0 1 Rotation: cosθ sinθ 0 R(θ) = sinθ cosθ 0 0 0 1 3.4. Rotation about an arbitrary point The rotation matrix (above) works well if we intend to rotate a point around the origin. But, what about rotating the point (x,y) around the arbitrary point (x a, y a )? The answer lies in the following procedure of three steps: - Translate (x a,y a ) to the origin, i.e. translate by T( x a, y a ) - Perform the rotation R(θ). - Translate so that point at the origin returns to the original location, i.e.. translate by T(x a,y a ). Therefore, to rotate an object made up of 5 vertices, each geometric transformation would need to be done 5 times. Overall, we have what is computationally expensive. 3 Transformations x 5 Vertices = 15 calculations The computational cost can be reduced using the CTM (Combined Transformation Matrix), i.e. by combining the transformations into a single CTM: so that the total number of calculations is equal to 8. 3 Transformations x Identity Matrix = 3 calculations 1 Transformation (CTM) x 5 Vertices = 5 calculations 3.5. Order and composition of transformations The order of geometric transformations of the CTM is relevant because the matrix product is not

commutative. In fact, Matrix product is associative: When multiplying matrices, the order we carry out the multiplications is not relevant, that is Matrix multiplication is not commutative: A. B. C = (A. B). C = A. (B. C) When multiplying matrices together, we carry out the multiplications is relevant, that is A. B B. A The question is then how do we work out the order of our matrices when creating the CTM? Turning back to the steps to rotate the point around the point, let us rewrite the corresponding procedure: - Translate (x a,y a ) to the origin, i.e. translate by T( x a, y a ) - Perform the rotation R(θ). - Translate so that point at the origin returns to the original location, i.e.. translate by T(x a,y a ). Thus, the order of the CTM is: CTM = T(x a, y a ). R(θ). T( x a, y a ) When we multiply the CTM by the point P we have CTM. P = T(x a,y a ). R(θ).T( x a, y a ). P An important fact to bear in mind is that the transformation closest to the point P in the expression is the first transformation to be applied to P. 4. Affine Transformations Euclidean transformations preserve the distance between points, and because of that they are then called rigid transformations. Affine transformations generalize Euclidean transformations in the sense that they don not preserve distance but parallelism instead. This means that two parallel lines remain parallel after applying a affine transformation. As a consequence of this principal invariant, other properties are preserved. For example, an affine transformation also preserves collinearity (i.e., all points of a line remain on a line after transformation) and ratios of distances or proportions (e.g., the midpoint of a line segment remains the midpoint after transformation). An affine transformation is also called an affinity. Examples of affine transformations are contraction, expansion, dilation, reflection, rotation, shear, similarity transformations, spiral similarities, and translation, as are their combinations. In general, an affine transformation is the result of a composition of rotations, translations, dilations, and shears. As seen above, rotations and translations are Euclidean transformations. Let us then see the other two basic affine transformations. Dilation or Scaling: In scaling, we change the size of an object. Scaling makes an object bigger or smaller in the x and/or y

direction. Scaling a point (x,y) by a factor s x along the x axis and s y along the y axis requires we multiply each coordinate by the corresponding scaling factor: or, using the matrix notation, Shearing: x'= s x. x y'= s y. y x' s x 0 0 x y' = 0 s y 0. y 1 0 0 1 1 Shearing enjoys the property that all points along a given line l remain fixed, while other points are shifted parallel to l by a distance that is proportional to their perpendicular distance from l. Note that shearing an object in the plane does not change its area at all. As a margin note, let us say that shearing can easily be generalized to three dimensions, where planes are translated instead of lines. Shearing a point (x,y) by a factor h x along the x axis and h y along the y axis is given by the following equations: or, using the matrix notation, x'= x + h x.y y'= y + h y.x x' 1 h x 0 x y' = h y 1 0. y 1 0 0 1 1 The effect of a shearing looks like pushing an object in a direction that is parallel to a coordinate axis in 2D (or coordinate plane in 3D). Note that we can do this only in the x-direction as follows or in the y-direction x'= x + h x.y y'= y x'= x y'= y + h y.x

5. 3D Transformations in OpenGL Translation: The gltranslated and gltranslatef functions multiply the current matrix by a translation matrix. Their prototypes are: void gltranslated(gldouble x, GLdouble y, GLdouble z); void gltranslatef(glfloat x, GLfloat y, GLfloat z); Rotation: The glrotated and glrotatef functions multiply the current matrix by a rotation matrix. Their prototypes are: void glrotated(gldouble angle, GLdouble x, GLdouble y, GLdouble z); void glrotatef(glfloat angle, GLfloat x, GLfloat y, GLfloat z); The glrotate function computes a matrix that performs a counterclockwise rotation of angle degrees about the vector from the origin through the point (x, y, z). Scaling: The glscaled and glscalef functions multiply the current matrix by a scaling matrix. Their prototypes are: void glscaled (GLdouble x, GLdouble y, GLdouble z); void glscalef(glfloat x, GLfloat y, GLfloat z); All these transformations 6. Example The following program starts by drawing a line segment between two points, P(5.0,1.5) and Q(9.3,7.2) in the 2-dimensional Euclidean space. Then, such a line segment is translated 5 units along the x-axis and -2 units along the y-axis. 1 #include <GLUT/glut.h> // Header file for GLUT and OpenGL 2 #include <stdlib.h> 3 4 //-------------------------------------------------------------- 5 // DRAWING CALLBACK FUNCTION 6 //-------------------------------------------------------------- 7 8 void draw(){ 9 // background colour: yellow 10 glclearcolor( 100, 100, 0, 0 ); 11 glclear ( GL_COLOR_BUFFER_BIT ); 12 13 // Sets up the DOMAIN (xmin,xmax,ymin,ymax) in R 2.

14 // Let (xmin,xmax,ymin,ymax)=(0.0,20.0,0.0,20.0) 15 glmatrixmode(gl_projection); 16 glloadidentity(); 17 gluortho2d(0.0,20.0,0.0,20.0); 18 19 // Let us now define the line segment in red 20 // Endpoints: (5.0,1.5) and (9.3,7.2) 21 glmatrixmode(gl_modelview); 22 glloadidentity(); 23 gltranslatef(5,-2,0); 24 glcolor3f( 1, 0, 0 ); 25 glbegin(gl_lines); 26 glvertex2f(5.0,1.5); 27 glvertex2f(9.3,7.2); 28 glend(); 29 30 // display line segment 31 glutswapbuffers(); 32 } 33 34 //-------------------------------------------------------------- 35 // KEYBOARD CALLBACK FUNCTION 36 //-------------------------------------------------------------- 37 38 void keyboard(unsigned char key,int x,int y) 39 { 40 // press ESC key to quit 41 if(key==27) exit(0); 42 } 43 44 //-------------------------------------------------------------- 45 // MAIN FUNCTION 46 //-------------------------------------------------------------- 47 48 int main(int argc, char ** argv) 49 { 50 glutinit(&argc, argv); 51 // Double Buffered RGB display 52 glutinitdisplaymode( GLUT_RGB GLUT_DOUBLE); 53 // Set window size 54 glutinitwindowsize( 500,500 ); 55 glutcreatewindow("line Segment"); 56 // Declare callback functions 57 glutdisplayfunc(draw); 58 glutkeyboardfunc(keyboard); 59 // Start the main loop of events 60 glutmainloop(); 61 return 0; 62 }

Comments: (1) The translation appears on line 23, just before the object. Why? (2) Assume that now we wish to rotate the line segment by 45 degrees. Where do you put the rotation function? Before or after the translation function? Why? 7. Programming Exercises 1. Re-write the house-building program implemented in the last class in a way that all building blocks (body, roof, windows and door) are constructed from the origin. Then, use translations to place these blocks at the desired locations. Each block is constructed in a separate function. In the particular case of the window, we need to call it twice because we are assuming that the house has two windows. 2. Add the toggle facilities to the previous program 1. in a manner that to add/remove the house body by pressing the b key, roof by pressing the r key, windows by pressing the w key, and door by pressing the d key. 3. Let us now replicate twice the house. The first copy of the original house must be reduced down to ¾ and placed side-by-side on the left of the original house. The second house copy must be scaled up to 5/4 and placed side-by-side on the right of the original house. 4. Write a program to draw a circle of a given radius r. 5. Let us now add the bright sun to the scene of the previous program, changing the position of the sun by clicking on the s key. The trajectory of the sun is a circle arc. 8. Final Remarks 1. Transformations are mathematical functions that allow us to model and to navigate within 2D and 3D spaces. 2. In computer graphics, we use three main transformations: translation, scaling and rotation. 3. Homogenous coordinates allow us to treat translation, scaling and rotation in the same manner. Consequently, all affine transformations can be combined into a CTM that substantially reduces the calculations that need to be made. 4. Matrix multiplication is associative but not commutative. 5. A CTM combines a number of transformations into a single matrix. 6. The order of drawing objects in OpenGL is the same as the one of the sequence of the C program, i.e. top-bottom order. However, the order of applying geometric transformations to objects is the opposite, and follows the logic of a stack: last in, first out. The stack here is the GL_MODELVIEW.