51
CAP 4730 CAP 4730 Computer Graphic Computer Graphic Methods Methods Prof. Roy Levow Prof. Roy Levow Chapter 4 Chapter 4

CAP 4730 Computer Graphic Methods

Embed Size (px)

DESCRIPTION

CAP 4730 Computer Graphic Methods. Prof. Roy Levow Chapter 4. Geometry. Basic components Point Vector Line Segment Directed Line Segment Can be added head-to-tail Similar to vector addition but location of start of first segment is start of sum. Point-Vector Operations. - PowerPoint PPT Presentation

Citation preview

Page 1: CAP 4730 Computer Graphic Methods

CAP 4730CAP 4730Computer Graphic Computer Graphic

MethodsMethodsProf. Roy LevowProf. Roy Levow

Chapter 4Chapter 4

Page 2: CAP 4730 Computer Graphic Methods

GeometryGeometry

Basic componentsBasic components– PointPoint– VectorVector– Line SegmentLine Segment– Directed Line SegmentDirected Line Segment

Can be added head-to-tailCan be added head-to-tailSimilar to vector addition but location of Similar to vector addition but location of

start of first segment is start of sumstart of first segment is start of sum

Page 3: CAP 4730 Computer Graphic Methods

Point-Vector OperationsPoint-Vector Operations

Inverse of a vectorInverse of a vector– reverses directionreverses direction

Point + VectorPoint + Vector– Gives new point at end of vector from Gives new point at end of vector from

original pointoriginal point Interpret inverse Point – PointInterpret inverse Point – Point

– to be vector from second point to firstto be vector from second point to first

Page 4: CAP 4730 Computer Graphic Methods

Scalar OperationsScalar Operations

Scalar times vector is standard Scalar times vector is standard operationoperation

Can take linear combination of points Can take linear combination of points and vectors with scalar multipliers as and vectors with scalar multipliers as long as net number of points is 1long as net number of points is 1– OK: P – 2 Q + 3vOK: P – 2 Q + 3v– Bad: 2 P – 4 QBad: 2 P – 4 Q

Page 5: CAP 4730 Computer Graphic Methods

Geometric SpaceGeometric Space

Coordinate-free systemCoordinate-free system– Objects have no locationObjects have no location

Scalar FieldScalar Field– add, subtract, multiply, divideadd, subtract, multiply, divide– associative and commutativeassociative and commutative

Vector SpaceVector Space– Vectors and scalars as usualVectors and scalars as usual

Affine SpaceAffine Space– Vector space plus pointsVector space plus points

point + vector; point - pointpoint + vector; point - point

Page 6: CAP 4730 Computer Graphic Methods

Computational FrameworkComputational Framework

Magnitude of a vectorMagnitude of a vector– ||ααv| = |v| = |αα| |v| | |v|

LinesLines– parametric formparametric form

– P(P(αα) = P) = P00 + + ααvv

– affine additionaffine addition

– P = P = αα11 R + R + αα22 Q where Q where αα1 1 + + αα22 = 1 = 1

Page 7: CAP 4730 Computer Graphic Methods

ConvexityConvexity

A polygon with vertices P1, …, PA polygon with vertices P1, …, Pnn is is convex precisely when all affine convex precisely when all affine sums of the points sums of the points

P = P = αα11 R + … + R + … + ααnn P Pnn

are inside the figure are inside the figure when when ααii‘s sum to 1‘s sum to 1

Page 8: CAP 4730 Computer Graphic Methods

Dot and Cross ProductsDot and Cross Products

Dot (inner) ProductDot (inner) Product– u∙v = sum uu∙v = sum uii v vii

– |u||u|22 = = u∙u u∙u

– cos cos θθ = u∙v / |u||v| = u∙v / |u||v| Cross ProductCross Product

– n = u n = u xx v v– orthogonal to plane of u-vorthogonal to plane of u-v– |sin |sin θθ | = |u | = |u xx v| / |u||v| v| / |u||v|– Right hand ruleRight hand rule

Using right hand, u is index, v is middle, n is thumbUsing right hand, u is index, v is middle, n is thumb

Page 9: CAP 4730 Computer Graphic Methods

PlanePlane

Affine sum of three non-colinear Affine sum of three non-colinear pointspoints

or point and two vectorsor point and two vectors or solution to the equationor solution to the equation

– n ∙ (P – Pn ∙ (P – P00) = 0) = 0

Page 10: CAP 4730 Computer Graphic Methods

Three-Dimensonal PrimitivesThree-Dimensonal Primitives

CurvesCurves SurfacesSurfaces Volumetric OjbectsVolumetric Ojbects Standard treatmentStandard treatment

– Objects are described by their surfaces Objects are described by their surfaces and treated as hollowand treated as hollow

– Objects can be specifiedby verticesObjects can be specifiedby vertices– Objects can be defined directly or Objects can be defined directly or

approximated by flat convex polygonsapproximated by flat convex polygons

Page 11: CAP 4730 Computer Graphic Methods

Coordinate SystemsCoordinate Systemsand Framesand Frames

Any vector in 3-dimensions can be Any vector in 3-dimensions can be represented as a linear combination represented as a linear combination of any three lineraly independent of any three lineraly independent vectorsvectors w = w = αα11 v v11 + + αα22 v v22 + + αα33 v v33

If we fix the vectors vIf we fix the vectors v11, v, v2,2, v v33 we call we call them a basisthem a basis

we can represent a point by the we can represent a point by the coefficientscoefficients a = (a = (αα1,1, αα2,2, αα33))TT

Page 12: CAP 4730 Computer Graphic Methods

Coordinate SystemsCoordinate Systemsand Framesand Frames

ConverselyConversely w = (w = (αα1,1, αα2,2, αα33) (v) (v11, v, v2,2, v v33))TT

The basis vectors define a coordinate The basis vectors define a coordinate systemsystem

However, the basis vectors are not However, the basis vectors are not anchored and could be movedanchored and could be moved

Adding a point as the origin gives us a Adding a point as the origin gives us a frameframe

Every point can then be written as PEvery point can then be written as P00 plus plus a linear combination of the basis vectorsa linear combination of the basis vectors

Page 13: CAP 4730 Computer Graphic Methods

RepresentationsRepresentations

Any point can be represented asAny point can be represented as1.1. a vector, from the origina vector, from the origin

2.2. a 3-tuple of the coefficients for the a 3-tuple of the coefficients for the basisbasis

Page 14: CAP 4730 Computer Graphic Methods

Changes in Coordinate SystemChanges in Coordinate System

Suppose we have two basesSuppose we have two bases– {u{u11, u, u22, u, u33} and {v} and {v11, v, v22, v, v33} }

Each vector uEach vector uii is a linear combination is a linear combination of the v’sof the v’s

If M is the 3x3 matrix of coefficients, If M is the 3x3 matrix of coefficients, then (uthen (u11, u, u22, u, u33))TT =M (v =M (v11, v, v22, v, v33))TT

The inverse of M transforms from u’s The inverse of M transforms from u’s to v’sto v’s

Page 15: CAP 4730 Computer Graphic Methods

Changing Coordinate SystemChanging Coordinate System

If a is the coordinate vector for v’s If a is the coordinate vector for v’s and b is the coordinate vector for u’s and b is the coordinate vector for u’s thenthen a = Ma = MTT b b

Changing the basis can effect Changing the basis can effect rotation and scalingrotation and scaling

Changing the basis leaves the origin Changing the basis leaves the origin unchangedunchanged

Page 16: CAP 4730 Computer Graphic Methods

Homogeneous CoordinatesHomogeneous Coordinates

We can include the origin in our We can include the origin in our calculation s by adding a 4calculation s by adding a 4thth dimension for dimension for the origin, Pthe origin, P00, and agreeing to multiply , and agreeing to multiply only by 1 except for the zero vector when only by 1 except for the zero vector when we use 0we use 0

Basis is now {vBasis is now {v11, v, v22, v, v3 3 , P, P00} } Last column of transform matrix isLast column of transform matrix is

0, 0, 0, 10, 0, 0, 1

Can convert to new basis {uCan convert to new basis {u11, u, u22, u, u33, Q, Q00} }

Page 17: CAP 4730 Computer Graphic Methods

ExamplesExamples

Change of basis 4.3.3, p. 159Change of basis 4.3.3, p. 159

Change of frame 4.3.5, p. 163Change of frame 4.3.5, p. 163

Page 18: CAP 4730 Computer Graphic Methods

Working with RepresentationsWorking with Representations

Problem: How to obtain the Problem: How to obtain the transformation matrix given two transformation matrix given two representations representations

Solution: The representation of one Solution: The representation of one frame in terms of another, a = Cb, frame in terms of another, a = Cb, gives the inverse of the matrix, gives the inverse of the matrix, D = CD = C-1-1

(see p. 165)(see p. 165)

Page 19: CAP 4730 Computer Graphic Methods

Frames in OpenGLFrames in OpenGL

Camera us at origin of its frame, Camera us at origin of its frame, pointing in –z direction; y is the up pointing in –z direction; y is the up direction and x completes a right-direction and x completes a right-handed coordinate system (see next handed coordinate system (see next slide)slide)

To view objects near origin, camera To view objects near origin, camera must be moved back, say a distance must be moved back, say a distance d (see next slide)d (see next slide)

Page 20: CAP 4730 Computer Graphic Methods
Page 21: CAP 4730 Computer Graphic Methods

Camera Model-View MatrixCamera Model-View Matrix

Model-View matrix isModel-View matrix is

11 00 00 00

00 11 00 00

00 00 11 -d-d

00 00 00 11

moves point (x,y,z) in world frame to moves point (x,y,z) in world frame to (x,y,z-d)(x,y,z-d)

Page 22: CAP 4730 Computer Graphic Methods

Camera at (1,0,1)Camera at (1,0,1)Pointing at OriginPointing at Origin

Camera is centered at (1,0,1,1)Camera is centered at (1,0,1,1)TT

Orthogonal to back is n=(-1,0,-1,1)Orthogonal to back is n=(-1,0,-1,1)TT

Up is same as world coord (0,1,0,0)Up is same as world coord (0,1,0,0)TT

Third orthogonal is (1,0,-1,0)Third orthogonal is (1,0,-1,0)TT

Inverse ofInverse of 1 0 -1 11 0 -1 1 .5 0 -.5 0 .5 0 -.5 0 0 1 0 0 is 0 1 0 0 is 0 1 0 0 0 1 0 0-1 0 -1 1-1 0 -1 1 -.5 0 -.5 0-.5 0 -.5 0 0 0 0 10 0 0 1 0 0 0 1 0 0 0 1

Page 23: CAP 4730 Computer Graphic Methods

Modeling a Colored CubeModeling a Colored Cube

StepsSteps1.1. ModelingModeling

2.2. Converting to camera frameConverting to camera frame

3.3. ClippingClipping

4.4. ProjectingProjecting

5.5. Removing Hidden SurfacesRemoving Hidden Surfaces

6.6. RasterizingRasterizing

Code is cube.cCode is cube.c

Page 24: CAP 4730 Computer Graphic Methods

Modeling CubeModeling Cube

Model as 6 faces defined by verticesModel as 6 faces defined by vertices– Vertices at 1, -1Vertices at 1, -1– Each face is a square (polygon)Each face is a square (polygon)

Polygons have inside and outside facePolygons have inside and outside face– Face is outward if vertices occur in Face is outward if vertices occur in

counterclockwise ordercounterclockwise order– Use right-hand rotation through vertices, Use right-hand rotation through vertices,

thumb points outthumb points out Outward face must be to outside of cubeOutward face must be to outside of cube

Page 25: CAP 4730 Computer Graphic Methods

Data StructuresData Structures

Polygons or QuadsPolygons or Quads Vertex list representationVertex list representation

Page 26: CAP 4730 Computer Graphic Methods

Cube CodeCube Code

Define colors for the vertices Define colors for the vertices

see codesee code

Page 27: CAP 4730 Computer Graphic Methods

Coloring FacesColoring Faces

Faces will be filled with colors by Faces will be filled with colors by intepolation, intepolation,

bilinearbilinear scan-line interpolation scan-line interpolation

from modelfrom model avoids need for flatavoids need for flat

Page 28: CAP 4730 Computer Graphic Methods

Vertex ArraysVertex Arrays

Avoid recomputationAvoid recomputation OpenGL ArraysOpenGL Arrays

1.1. ColorColor

2.2. VertexVertex

3.3. Color IndexColor Index

4.4. NormalNormal

5.5. Texture CoordinateTexture Coordinate

6.6. Edge FlagEdge Flag

Page 29: CAP 4730 Computer Graphic Methods

Application to CubeApplication to Cube

glEnableClientState(array_type);glEnableClientState(array_type);– array types:array types:

GL_COLOR_ARRAYGL_COLOR_ARRAY

GL_VERTEX_ARRAYGL_VERTEX_ARRAY Create global arrays of values as Create global arrays of values as

beforebefore

Page 30: CAP 4730 Computer Graphic Methods

Application to Cube (cont)Application to Cube (cont)

Register data arrays with OpenGLRegister data arrays with OpenGL

glVertexPointer (dim, glType, glVertexPointer (dim, glType,

gap, array_var);gap, array_var);

glColorPointe (…)glColorPointe (…) For exampleFor example

glVertexPointer (3, GL_FLOAT, 0,glVertexPointer (3, GL_FLOAT, 0,

vertices);vertices);

Page 31: CAP 4730 Computer Graphic Methods

Application to Cube (cont)Application to Cube (cont)

Then draw the elements withThen draw the elements withglDrawElements(type, n, fmt, ptr);glDrawElements(type, n, fmt, ptr);

For exampleFor example for (1=0; i<6; i++)for (1=0; i<6; i++)

glDrawElements(GL_POLYGON, 4glDrawElements(GL_POLYGON, 4GL_UNSIGNED_BYTE,GL_UNSIGNED_BYTE,&cubeIndex[4*i]);&cubeIndex[4*i]);

or could do quads in one batch of 24or could do quads in one batch of 24

Page 32: CAP 4730 Computer Graphic Methods

Affine TransformationsAffine Transformations

A transformation maps a point (or A transformation maps a point (or vector) into another point (or vector)vector) into another point (or vector)

Q = T(P)Q = T(P) With homogeneousWith homogeneous

coordinates we can coordinates we can use the same use the same function for both function for both points and vectors, points and vectors, q=f(p) or v=f(u)q=f(p) or v=f(u)

Page 33: CAP 4730 Computer Graphic Methods

Linear TransformationsLinear Transformations

Limit consideration to functions satisfyingLimit consideration to functions satisfyingf(ap+bq) = af(p) + bf(q)f(ap+bq) = af(p) + bf(q)

called linear transformationscalled linear transformations Linear transformations can always be Linear transformations can always be

written as matrix multiplications by a 4x4 written as matrix multiplications by a 4x4 matrixmatrix

For homogeneous coordinates, the last For homogeneous coordinates, the last row is 0 0 0 1 but other 12 entries are row is 0 0 0 1 but other 12 entries are arbitrary, but only 9 effect vectors.arbitrary, but only 9 effect vectors.

Page 34: CAP 4730 Computer Graphic Methods

Linear Transformations (cont)Linear Transformations (cont)

ProperitesProperites– Linear transformations can be viewed Linear transformations can be viewed

as eitheras either1.1. change in representation (frame)change in representation (frame)2.2. transformation on vertices within transformation on vertices within

frameframe– Lines and planes are preservedLines and planes are preserved– Transformed line segment is generated Transformed line segment is generated

by transformed end pointsby transformed end points This simplifies design of graphics pipelineThis simplifies design of graphics pipeline

Page 35: CAP 4730 Computer Graphic Methods

Translation, Rotation, ScalingTranslation, Rotation, Scaling

TranslationTranslationP’ = P + dP’ = P + d

Rotation in two coordinates about Rotation in two coordinates about originorigin

cos th -sin thcos th -sin th

sin thsin th cos th cos th

Page 36: CAP 4730 Computer Graphic Methods

Rotation (cont)Rotation (cont)

Properties of rotationsProperties of rotations– one fixed pointone fixed point– can be composed from two-dimensional can be composed from two-dimensional

rotation in different coordinatesrotation in different coordinates– define positive rotation in plane if define positive rotation in plane if

rotation is counterclockwise when rotation is counterclockwise when viewed down normal to planeviewed down normal to plane

– is a rigid-body transformationis a rigid-body transformationshape is not changedshape is not changed

Page 37: CAP 4730 Computer Graphic Methods

ScalingScaling

An affine non-rigid body An affine non-rigid body transformationtransformation

Multiply coordinate by some factorMultiply coordinate by some factor Negative multiplication produces Negative multiplication produces

reflectionreflection

Page 38: CAP 4730 Computer Graphic Methods

Affine Transformations in Affine Transformations in Homogeneous CooridnatesHomogeneous Cooridnates

All represented by linear All represented by linear transformationstransformations

4x4 matrix with last row 0 0 0 14x4 matrix with last row 0 0 0 1 Translation by a b c, and inverseTranslation by a b c, and inverse

1 0 0 a1 0 0 a 1 0 0 -a1 0 0 -a0 1 0 b0 1 0 b 0 1 0 –b0 1 0 –b0 0 1 c0 0 1 c 0 0 1 -c0 0 1 -c0 0 0 1 0 0 0 1 0 0 0 10 0 0 1

Page 39: CAP 4730 Computer Graphic Methods

ScalingScaling

Multiply axes by a, b, c respectivelyMultiply axes by a, b, c respectively

a 0 0 0a 0 0 0

0 b 0 00 b 0 0

0 0 c 00 0 c 0

0 0 0 10 0 0 1 Inverse comes from 1/a, 1/b, 1/cInverse comes from 1/a, 1/b, 1/c

Page 40: CAP 4730 Computer Graphic Methods

RotationRotation

Can compose z, x, and y rotations by Can compose z, x, and y rotations by thz, thx, thythz, thx, thy

Inverse is rotation by –thz, -thx, -thyInverse is rotation by –thz, -thx, -thy It can be shown that RIt can be shown that R-1-1 = R = RTT

– Matrices satisfying this property are said Matrices satisfying this property are said to be orthogonalto be orthogonal

Page 41: CAP 4730 Computer Graphic Methods

ShearShear

Shear in the x axis is defined byShear in the x axis is defined byx’ = x + y cot thx’ = x + y cot th

Inverse users -thInverse users -th

Page 42: CAP 4730 Computer Graphic Methods

Concatenation of TransformsConcatenation of Transforms

A sequence of transformations with A sequence of transformations with TT11 followed by T followed by T22 followed by T followed by T33 can can be composed as Tbe composed as T33(T(T22(T(T11(p)))(p)))

If the corrersponding matrices are A, If the corrersponding matrices are A, B, C this yields M = CBAB, C this yields M = CBA

Thus a concatenation of Thus a concatenation of transformations can be represented transformations can be represented by a single matrixby a single matrix

Page 43: CAP 4730 Computer Graphic Methods

Composed TransformaitonsComposed Transformaitons

Rotation in z about a fixed pointRotation in z about a fixed pointT(p)RT(p)Rzz(th)T(-p)(th)T(-p)

General rotationGeneral rotationRRxx(thx) R(thx) Ryy(thy) R(thy) Rzz(thz)(thz)

Page 44: CAP 4730 Computer Graphic Methods

Rotation about given vectorRotation about given vector

First change coordinates so given First change coordinates so given vector is on a coordinate axisvector is on a coordinate axis– Use unit vector to avoid scalingUse unit vector to avoid scaling

Then rotate about that coordinate Then rotate about that coordinate axisaxis

Finally restore to original coordinate Finally restore to original coordinate systemsystem

Can translate for fixed point as Can translate for fixed point as beforebefore

Page 45: CAP 4730 Computer Graphic Methods

Instance TransformationsInstance Transformations

When a particular kind of figure is When a particular kind of figure is used many times in a scene, it is used many times in a scene, it is convenient to construct one convenient to construct one prototype and draw the objects by prototype and draw the objects by transforming that prototypetransforming that prototype

Convention is to center prototype on Convention is to center prototype on center of mass and then scale, center of mass and then scale, rotate, and translate: TRSrotate, and translate: TRS

Page 46: CAP 4730 Computer Graphic Methods

Open GL Transformation MatricesOpen GL Transformation Matrices

Current Transformation Matrix (CTM) Current Transformation Matrix (CTM) is part of the pipelineis part of the pipeline

It can be set or modifiedIt can be set or modified After setting matrix modeAfter setting matrix mode

glLoadMatrixf(*matrix); //load matrixglLoadMatrixf(*matrix); //load matrix

glLoadIdentity();glLoadIdentity();

Page 47: CAP 4730 Computer Graphic Methods

OpenGL TransformationsOpenGL Transformations

OpenGL has functions to perform OpenGL has functions to perform basic operationsbasic operations

glRotatef(angle, vx, vy, vz);glRotatef(angle, vx, vy, vz);

// rotate about vector v// rotate about vector v

glTranslatef(dx, dy, dz);glTranslatef(dx, dy, dz);

glScalef(sx, sy, sz);glScalef(sx, sy, sz); Applied in order calledApplied in order called

Page 48: CAP 4730 Computer Graphic Methods

OpenGL Rotation about PointOpenGL Rotation about Point

Rotate 45Rotate 45oo about line through origin about line through origin and (1, 2, 3) with fixed point (4, 5, 6)and (1, 2, 3) with fixed point (4, 5, 6)

glMatrixMode(GL_MODELVIEW);glMatrixMode(GL_MODELVIEW);

glLoadIdentity();glLoadIdentity();

glTranslatef(4.0, 5.0, 6.0);glTranslatef(4.0, 5.0, 6.0);

glRotatef(45.0, 1.0, 2.0, 3.0);glRotatef(45.0, 1.0, 2.0, 3.0);

glTranslatef(-4.0, -5.0, -6.0);glTranslatef(-4.0, -5.0, -6.0);

Page 49: CAP 4730 Computer Graphic Methods

Spinning the CubeSpinning the Cube

Mouse button click sets rotation axisMouse button click sets rotation axis Idle callback adjusts rotation theta on Idle callback adjusts rotation theta on

selected axisselected axis Display uses glRotatef() to set cube Display uses glRotatef() to set cube

at desired angles in 3-dat desired angles in 3-d

Page 50: CAP 4730 Computer Graphic Methods

Loading, Pushing, PoppingLoading, Pushing, Popping

Can push and pop save and restore Can push and pop save and restore matrixmatrix– Projection stack is often limited to 2 Projection stack is often limited to 2

levels; don’t usually transform itlevels; don’t usually transform it Can load matrix from array withCan load matrix from array with

glLoadMatrix(myArray);glLoadMatrix(myArray); And multiply by our own matrix withAnd multiply by our own matrix with

glMultMatrix(myArray);glMultMatrix(myArray); Can’t get OpenGL matrix into arrayCan’t get OpenGL matrix into array

Page 51: CAP 4730 Computer Graphic Methods

Smooth RotationSmooth Rotation

Rotation is an arbitrary direction is Rotation is an arbitrary direction is equivalent to rotation about a great circle equivalent to rotation about a great circle connecting the start and end positions of a connecting the start and end positions of a pointpoint

Can recompute this incrementallyCan recompute this incrementally– but requires much computationbut requires much computation

Altenatively can compute one incremental Altenatively can compute one incremental step, save it, and then glMultMatrixf() for step, save it, and then glMultMatrixf() for each stepeach step– Saves much computationSaves much computation