CS 352: Computer Graphics - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

CS 352: Computer Graphics

Description:

Affine sum: P = a Q (1-a) R. Interactive Computer Graphics. Chapter 4 - 7. Dot Product ... All affine transformations (line-preserving: translation, rotation, scale, ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 35
Provided by: HarryPl6
Category:

less

Transcript and Presenter's Notes

Title: CS 352: Computer Graphics


1
CS 352 Computer Graphics
Chapter 4 Geometric Objects andTransformations
2
Overview
  • Scalars and Vectors
  • Coordinates and frames
  • Homogeneous coordinates
  • Rotation, translation, and scaling
  • Concatenating transformations
  • Transformations in OpenGL
  • A virtual trackball

3
Perspective
  • How is it that mathematics can model the (ideal)
    world so well?

4
Where we're headed
  • Virtual trackball program
  • Similar to primer demo
  • Read in a 3D object and display it.
  • Handle virtual trackball rotations
  • Later add lighting and shading

5
Background linear algebra
  • Quick review of important concepts
  • Point location (x, y, z)
  • Vector direction and magnitudeltx, y, zgt

6
Vectors
  • Magnitude of a vector v
  • Direction of a vector, unit vector v
  • Affine sum P a Q (1-a) R


7
Dot Product
  • Def u v ux vx uy vy uz vz
  • u v u v cos ?
  • Uses
  • Angle between two vectors?
  • Are two vectors perpendicular?
  • Do two vectors formacute or obtuse angle?
  • Is a face visible?(backface culling)

8
Cross Product
  • u ? v ltuyvz - uzvy, uzvx - uxvz, uxvy - uyvxgt
  • Direction normal to plane containing u, v (using
    right-hand rule in right-handed coordinate
    system)
  • Magnitude uv sin ?
  • Uses
  • Face outward normal?
  • Angle between vectors?
  • Do two line segments intersect?

9
Face outward normals
  • How to find the outward normal of a face?
  • Assume that vertices are listed in a standard
    order when viewed from the outside --
    counter-clockwise
  • Cross product of the first two edges is outward
    normal vector
  • Note that first corner must be convex

10
Coordinate systems and frames
  • Hierarchical modeling
  • May deal with many coordinate systems viewer,
    model, world, viewport
  • Frame origin basis vectors (axes)
  • Need to transform between frames
  • E.g. reading in a world description with several
    objects

11
Transformations
  • Changes in coordinate systems usually involve
  • Translation
  • Rotation
  • Scale
  • Rotation and scale can be represented as 3x3
    matrices, but not translation
  • We're also interested in a perspective
    transformation
  • We use 4D "Homogeneous coordinates"

12
Homogeneous Coordinates
  • A point (x, y, z, w) where w is a "scale factor"
  • Converting a 3D point to homogeneous coordinates
    (x, y, z) ? (x, y, z, 1)
  • Transforming back to 3-space divide by w
  • (x, y, z, w) ? (x/w, y/w, z/w)
  • (3, 2, 1) same as (3, 2, 1, 1) (6, 4, 2,
    2) (1.5, 1, 0.5, 0.5)
  • Where is the point (3, 2, 1, 0)?
  • Point at infinity or "pure direction."
  • Used for vectors (vs. points)

13
Homogeneous transformations
  • Most important reason for using homogeneous
    coordinates
  • All affine transformations (line-preserving
    translation, rotation, scale, perspective, skew)
    can be represented as a matrix multiplication
  • You can concatenate several such transformations
    by multiplying the matrices together. Just as
    fast as a single transform!
  • Modern graphics cards implement homogeneous
    transformations in hardware

14
Translation
15
Scaling
  • Note that the scaling fixed point is the origin
  • See transformation.exe

16
Example reading in an object
  • Object description with vertices and faces How
    to transform to unit cube?
  • vertices 4
  • 10 10 10
  • 40 20 20
  • 30 40 10
  • 30 10 -10
  • faces 4
  • 1 2 3
  • 2 4 3
  • 1 4 2
  • 1 3 4

17
Point normalization
  • Find min and max values of X, Y, Z
  • Find center point, Xc, Yc, Zc
  • Translate center to origin (T1)
  • Scale (S)
  • Translate center to (½, ½, ½) (T2)
  • P' T2 S T1 P
  • Modeling matrix M T2 S T1
  • Note apparent reversed order of matrices

18
Rotation
  • General rotation about anaxis v by angle u with
    fixed point p
  • With origin as fixed point, about x, y, or
    z-axis

19
Rotating about another point
  • How can I rotate around another fixed point, e.g.
    1, 2, 3?
  • Translate 1, 2, 3 -gt 0, 0, 0 (T)
  • Rotate (R)
  • Translate back (T-1)
  • T-1 R T P P'

20
Rotating about another axis
  • How can I rotate about an arbitrary axis?
  • Can combine rotationsabout z, y, and xRx Ry Rz
    P P'
  • Note that ordermatters and anglescan be hard to
    find

21
Example spinning the object
  • How to make the object rotate by 5 degrees about
    the z axis?
  • Multiply all the points by the appropriate
    rotation matrix
  • How to animate a continuous rotation?
  • Every few ms, rotate and redisplay
  • How to animate rotation and flying around?
  • First apply rotation matrix, then translation
    matrix
  • Better, multiply the two matrices together M
    T R (note apparent reversed order)
  • P' M P
  • Always work from original points, to avoid error
    accumulation

22
Orthographic projection
  • Zeroing the z coordinate with a matrix
    multiplication is easy

23
Perspective projection
  • Perspective projection can also be done with a
    single matrix multiply using homogeneous
    coordinates
  • In the default camera settings, it's easy
  • What happens when you divide through by w?

24
Transformations in OpenGL
  • Three coordinate frames in OpenGL
  • World coordinates
  • Eye (camera) coordinates
  • Window coordinates
  • Two transformations convert between them
  • Modeling xform world coords -gt eye coords
  • Projection xform eye coords -gt window coords

25
OpenGL Transformations
  • GL has a current transformation matrix (CTM) or
    Matrix Mode
  • glMatrixMode(GL_MODELVIEW)
  • glLoadIdentity()
  • glTranslatef(4.0, 5.0, 6.0)
  • glRotatef(45.0, 1.0, 2.0, 3.0)
  • glTranslatef(-4.0, -5.0, -6.0)
  • Vertices are transformed as they flow through the
    pipeline

26
OpenGL Transformations 2
  • If p is a vertex, pipeline produces Cp
    (post-multiplication only)
  • Can concatenate transforms in CTM
  • C ? I
  • C ? T(4, 5, 6)
  • C ? R(45, 1, 2, 3)
  • C ? T(-4, -5, -6)
  • C T-1 S T
  • Note that last transform defined is first applied
    see transformation.exe

27
Setting up Viewing Transform
  • To set up an orthographic projection
    transformation for the viewing transform
  • glMatrixMode(GL_PROJECTION)
  • glLoadIdentity()
  • glOrtho(0.0, width, 0.0, height, -1, 1)

28
Manipulating matrices
  • Loading a matrix
  • GLfloat myarray16
  • glLoadMatrixf(myarray)
  • Pushing and Popping
  • glPushMatrix()
  • glTranslatef()
  • glRotatef()
  • draw something
  • glPopMatrix()

29
Putting it all together
  • Load vertices and faces of object.
  • Normalize put them in (-1, 1, -1, 1, -1, 1) cube
  • Put primitives into a display list
  • Set up viewing transform to display that cube
  • Set modeling transform to identity
  • To spin the object, every 1/60 second do
  • Add 5 degrees to current rotation amount
  • Set up modeling transform to rotate by current
    amount

30
Virtual Trackball
  • Imagine a trackball embedded in the screen
  • If I click on the screen, what point on the
    trackball am I touching?

31
Trackball Rotation Axis
  • If I move the mouse from p1 to p2, what rotation
    does that correspond to?

32
Virtual Trackball Rotations
  • Rotation about the axis n p1 x p2
  • Angle of rotation use
  • p1 p2 p1 p2 cos ?
  • Fixed point if you use the -1, 1 cube, it is
    the origin

33
Data structures needed
  • An array of vertices, old_verticesn3
  • An array of normalized vertices, verticesn3,
    in the -1, 1 cube
  • An array of faces containing vertex indexes, int
    facesnmax_sides. Use facesn0 to store
    the number of sides. Set max_sides to 12 or so.

34
Virtual Trackball Program
  • Stage 1
  • Read in, normalize object
  • Put into display list (with edges)
  • Display with rotation
  • Stage 2
  • Virtual trackball rotations
  • Stage 3
  • Perspective, zoom
  • Lighting/shading
Write a Comment
User Comments (0)
About PowerShow.com