1. Trang chủ
  2. » Giáo Dục - Đào Tạo

Using transformations in OpenGL (đồ họa máy TÍNH SLIDE)

87 45 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 87
Dung lượng 6,31 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Camera Analogy and Transformations• Viewing transformations – tripod–define position and orientation of the viewing volume in the world... Coordinate Systems and Transformations• Steps

Trang 1

Transformations

Trang 2

What is a Transformation?

• Maps points (x, y) in one coordinate system to

points (x', y') in another coordinate system

x' = ax+ by + c y' = dx+ ey+ f

• Simple Transformations

– can be combined

Trang 3

Transformations are used

• Position objects in a scene (modeling)

• Change the shape of objects

• Create multiple copies of objects

• Projection for virtual cameras

• Animations

Trang 4

How are Transforms Represented?

Biểu diễn dưới dạng ma trận

p' = M p + t

Trang 5

2D transformations

Trang 6

Affine Transformations

• Want transformations which preserve geometry

– lines, polygons, quadrics

• Affine = line preserving

– Rotation, translation, scaling

– Projection

– Concatenation (composition)

Trang 8

(Nonuniform) Scale

• Scale

S = S-1 =

Trang 9

Trang 11

Composing Transforms

• Often want to combine transforms

• E.g first scale by 2, then rotate by 45 degrees

• Advantage of matrix formulation: All still a matrix

• Not commutative!! Order matters

– X2 = SX1

– X3 = RX2

– X3 = R(SX1) = (RS)X1

– X  (SR)X

Trang 12

Inverting Composite Transforms

• Say I want to invert a combination of 3

transforms

• Option 1: Find composite matrix, invert

• Option 2: Invert each transform and swap order

• Obvious from properties of matrices

Trang 13

3D rotations

Trang 15

Rotations in 3D

• Rotations about coordinate axes simple

• Always linear, orthogonal

Trang 16

Geometric Interpretation 3D Rotations

• Rows of matrix are 3 unit vectors of new coord frame

• Can construct rotation matrix from 3 orthonormal

Trang 17

Geometric Interpretation 3D Rotations

• Rows of matrix are 3 unit vectors of new coord frame

• Can construct rotation matrix from 3 orthonormal vectors

• Effectively, projections of point into new coord frame

• New coord frame uvw taken to cartesian components xyz

• Inverse or transpose takes xyz cartesian to uvw

Trang 18

• Not Commutative (unlike in 2D)!!

• Rotate by x, then y is not same as y then x

• Order of applying rotations does matter

• Follows from matrix multiplication not

commutative

– R1 * R2 is not the same as R2 * R1

• Demo: HW1, order of right or up will matter

Trang 19

Homogeneous Coordinates

Trang 20

• E.g move x by + 5 units, leave y, z unchanged

• We need appropriate matrix What is it?

Trang 21

Homogeneous Coordinates

• Add a fourth homogeneous coordinate (w=1)

• 4x4 matrices very common in graphics, hardware

• Last row always 0 0 0 1 (until next lecture)

Trang 22

Representation of Points (4-Vectors)

For w > 0, normal finite point

For w = 0, point at infinity (used for vectors to stop

Trang 23

Advantages of Homogeneous Coords

• Unified framework for translation, viewing, rot…

• Can concatenate any set of transforms to 4x4

matrix

• No division (as for perspective viewing) till end

• Simpler formulas, no special cases

• Standard in graphics software, hardware

Trang 24

General Translation Matrix

Trang 25

Combining Translations, Rotations

• Order matters!! TR is not the same as RT

• General form for rigid body transforms

• We show rotation first, then translation

(commonly used to position objects) on next

slide Slide after that works it out the other way

Trang 26

Combining Translations, Rotations

x y z

Trang 27

Combining Translations, Rotations

Trang 28

Transformations in OpenGL

Trang 31

Camera Analogy and Transformations

• Viewing transformations

– tripod–define position and

orientation of the viewing

volume in the world

Trang 32

Coordinate Systems and Transformations

• Steps in Forming an Image

– Specify geometry (world coordinates)

– Specify camera (camera coordinates)

– Project (window coordinates)

– Map to viewport (screen coordinates)

• Each step uses transformations

• Every transformation is equivalent to a change

in coordinate systems (frames)

Trang 33

Homogeneous Coordinates

• Each vertex has an extra value, w

• Most of the time w = 1, and we can ignore it

Trang 34

• As w is nonzero, the homogeneous vertex (x, y,

z, w)T corresponds to the 3D point (x/w, y/w,

z/w)T

• Directions (directed line segments) can be

represented with w = 0.0

Trang 35

w= 1

w= 2

Homogeneous Visualization

• Divide by w to normalize (homogenize)

• W = 0? Point at infinity (direction)

(0, 0, 1) = (0, 0, 2) = …

(7, 1, 1) = (14, 2, 2) = …

Trang 36

Vertex transformations

• Vertex transformations and projections can all be

represented by applying an appropriate 4 x 4 matrix to the vertex coordinates.

• All affine operations are matrix multiplications

• All matrices are stored column-major in OpenGL

• Matrices are always post-multiplied

• Product of matrix and vector is

=

Trang 38

Programming Transformations

• Prior to rendering, view, locate, and orient:

– Eye / camera position

– 3D geometry

• Manage the matrices

– Including matrix stack

• Combine (composite) transformations

Trang 39

Transformation Pipeline

• other calculations here

– material  color – shade model (flat) – polygon rendering mode – polygon culling

Projection Matrix

Perspective Division

Viewport Transform

Modelview

Modelview

Projection

l l l

object eye clip normalized

device window

Trang 40

– usually same as window size

– viewport aspect ratio should be same as projection

transformation or resulting image may be distorted

Trang 41

Viewing Transformations

Trang 43

Using gluLookAt()

• gluLookAt(eyex, eyey, eyez,

aimx, aimy, aimz,

upx, upy, upz)

– up vector determines unique orientation

– careful of degenerate positions

Trang 44

the negative z-axis,

– and has the positive

y-axis as straight up

• This is the same as calling

gluLookAt(0.0, 0.0, 0.0,

0.0, 0.0, -100.0,

0.0, 1.0, 0.0);

Trang 45

Using gluLookAt()

• gluLookAt(4.0, 2.0, 1.0,

2.0, 4.0, -3.0,

2.0, 2.0, -1.0);

Trang 46

Projection Tutorial

Trang 47

Modeling Transformations

Trang 50

Scale Transformation

• glScale*(x, y, z) generates S =

Trang 51

Rotation Transformation

• The glRotate*() command generates a

matrix for rotation about an arbitrary axis

• Rotating about the Oz axis

glRotate*(a, 0, 0, 1)

Trang 54

Rodrigues Formula

• About (kx, ky, kz), a unit

vector on an arbitrary axis

c = cos , s = sin

Trang 55

Bài t p ập

• Xác định ma trận của phép quay glRotatef(30.0f, 1.0f, 1.0f, 0.0f)

Trang 56

Order of transformations

• In general not commutative: order matters!

Rotate then translate Translate then rotate

Trang 57

Non-commutative Composition

• Scale then Translate

• Translate then Scale:

Trang 59

Transformation Tutorial

Trang 60

Compositing Modeling Transformations

• Problem 1: hierarchical objects

– one position depends upon a previous position

– robot arm or hand; sub-assemblies

• Solution: moving local coordinate system

– modeling transformations move coordinate system

– post-multiply column-major matrices

– OpenGL post-multiplies matrices

Trang 61

/* draw sun */

glut glutWireSphere(1.0, 20, 16);

/* draw smaller planet */

glRotatef((float ) year , 0.0f, 1.0f, 0.0f);

glTranslatef(2.0f, 0.0f, 0.0f);

Trang 62

Compositing Modeling Transformations

• Problem 2: objects move relative to absolute world

origin

– my object rotates around the wrong origin

• make it spin around its center or something else

• Solution: fixed coordinate system

– modeling transformations move objects around fixed

coordinate system

– pre-multiply column-major matrices

– OpenGL post-multiplies matrices

– must reverse order of operations to achieve desired effect

Trang 63

Example: object spin around its center

• You’ll adjust to reading a lot of code backwards!

• Here (x, y, z) is the fixed point

– first move it to the origin (last transformation in code)

– Then rotate about the axis (ax, ay, az)

– And finally move fixed point back.

Trang 64

Connection: Viewing and Modeling

Trang 65

Connection: Viewing and Modeling

• Moving camera is equivalent to moving every

object in the world towards a stationary camera

• Viewing transformations are equivalent to

several modeling transformations

– View tranformation matrix E places the camera within the scene

– Then we apply E -1 to

all points in the world

– Move the eye (camera)

by updating E

Trang 66

OpenGL Viewing Code

• In OpenGL, we can use the built-in transformation calls to specify the viewing transformation

• The camera is positioned in the scene with

translation by [vTx, vTy, vTz] and rotation about the X, Y, and Z axis:

Trang 67

Projection Transformation

Trang 68

Projection Transformation

• To lower dimensional space (here 3D -> 2D)

– Preserve straight lines

• Shape of viewing frustum

• Perspective projection

• Orthographic parallel projection

Trang 69

Demo

Trang 70

Orthographic projection

• The viewing volume is a rectangular parallelepiped.

• Vertexes of an object are "projected" towards infinity

• Distance from the camera doesn’t affect how large an object appears

Example: Simply

project onto xy

plane, drop z

coordinate

Trang 71

Orthographic projection

• Specify the orthographic viewing frustum by

– specifying minimum and maximum x, y coordinates – Indicating range of distances along the z-axis by

specifying near and far planes

Trang 72

Orthographic Projections matrix

• Here is the orthographic world-to-clip

transformation:

Trang 73

Orthographic Projection in OpenGL

• This matrix is constructed with the following OpenGL

call:

glOrtho(left, right, bottom, top, zNear,zFar)

• And the 2D version (another GL utility function):

gluOrtho2D(left, right, bottom, top)

– Just a call to glOrtho() with near = -1 and far = +1

• Usually, the following code is part of the initialization

Trang 74

Perspective Projections

• Artists (Donatello, Brunelleschi, and Da Vinci)

during the renaissance discovered the

importance of perspective for making images

appear realistic

• Parallel lines intersect at a point

Trang 75

Perspective projection

• Characteristic of perspective projection is

foreshortening:

– The farther an object is from the camera, the smaller

it appears in the final image

Trang 76

Perspective projection

• glFrustum(left, right, bottom, top,

zNear, zFar)

Trang 77

Perspective projection

– fovy = vertical field of view in degrees

– aspect = image width / height at near depth

– Can only specify symmetric viewing frustums where the viewing window is centered around the –z axis.

Trang 78

OpenGL Perspective Matrix

• Mapping the perspective viewing frustum in OpenGL

to clip space involves some affine transformations

• OpenGL uses a clever composition of these

transformations with the perspective projection matrix:

Trang 79

Viewport Transformation

Trang 80

Viewport Transformation

• The viewport is the rectangular region of the

window where the image is drawn

• Defining the Viewport

– glViewport(GLint x, GLint y, GLsizei

Trang 81

Mapping the Viewing Volume to the Viewport

• The aspect ratio of a viewport should equal the aspect ratio of the viewing volume

– If the two ratios are different, the projected image will

be distorted when mapped to the viewport

Trang 82

Common Transformation Usage

• 3 examples of reshape() routine

– restate projection & viewing transformations

• Usually called when window resized

Trang 83

reshape(): Perspective & LookAt

public void reshape(GLAutoDrawable drawable,

int x, int y, int width, int height) { GL2 gl = drawable.getGL().getGL2();

// Set the view port (display area) to cover the entire window

gl.glViewport(0, 0, width, height);

// Setup perspective projection,

gl.glMatrixMode(GL_PROJECTION);

gl.glLoadIdentity();

// Enable the model-view transform

gl.glMatrixMode(GL_MODELVIEW);

gl.glLoadIdentity();

glu gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0,

Trang 84

reshape(): Perspective & Translate

• Same effect as previous LookAt

public void reshape(GLAutoDrawable drawable,

int x, int y, int width, int height) { GL2 gl = drawable.getGL().getGL2();

Trang 85

reshape(): Ortho

public void reshape(GLAutoDrawable drawable,

int x, int y, int width, int height) { GL2 gl = drawable.getGL().getGL2();

double aspect = (double) width / height;

double left = -2.5, right = 2.5;

double bottom = -2.5, top = 2.5;

gl.glViewport(0, 0, width, height);

Trang 86

Additional Clipping Planes

• At least 6 more clipping planes available

• Good for cross-sections

• Modelview matrix moves clipping plane clipped

– glEnable(GL_CLIP_PLANEi)

– glClipPlane(GL_CLIP_PLANEi, Gldouble[] coeff)

Ax + By + Cz + D < 0

Trang 87

Reversing Coordinate Projection

• Screen space back to world space

glGetIntegerv(GL_VIEWPORT, int viewport[4]) glGetDoublev(GL_MODELVIEW_MATRIX,

Ngày đăng: 29/03/2021, 08:19

TỪ KHÓA LIÊN QUAN

w