640 likes | 790 Views
Yingcai Xiao. Chapter 3 Computer Graphics Primer. Graphics Using VTK. VTK Classes for Graphics. VTKRenderWindow : a class specifies which part of physical screen we can use to draw , in DC, manages a window . VTKRenderWindowInteractor : Enable interaction of a window .
E N D
Yingcai Xiao Chapter 3 Computer Graphics Primer
VTK Classesfor Graphics • VTKRenderWindow: a class specifies which part of physical screen we can use to draw, in DC, manages a window. • VTKRenderWindowInteractor: Enable interaction of a window. • VTKRender: in W.C., manages the scene, light, cameras and actors (objects). • VTKLight: specifies a light. • VTKCamera: specifies a camera. • VTKActor: defines an object. • VTK???Mapper: defines geometry of actors. • VTKProperty: defines the appearance of actors.
VTK Classesfor Graphics • Objects are created using Class::New(); vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor :: New() ; • vtkRenderWindow *ren= vtkRenderWindow :: New() ; iren->SetRenderWindow(ren) ; • A property can be shared by many actors
VTK Demos • T:\xiao\visualization\Examples\src\Tutorial • Step1\Cxx\Cone.cxx: basic objects • Step1\Cxx\Cone2.cxx: callbacks • Step 3\Cxx\Cone3.cxx: viewport • Step 4\Cxx\Cone4.cxx: object property • Step 5\Cxx\Cone5.cxx: track ball • Step 6\CXX\Cone6.cxx: control and manipulation of objects
GEOMETRIC TRANSFORMATIONS Review of Mathematical Preliminaries Points: • Vectors: directional lines P1(x1,y1) P0(x0,y0)
1. A vector has a direction and a length: Length: |V|=(x2 + y2)1/2 A unit vector: |V| = 1 e.g.: • Normalize a vector: 2. Add two vectors • 3. Scalar Multiplication
V • W , • 4. Dot product of two vectors scalar • V W if = 90 => cos = 0 => V W = 0. • if < 90 => cos > 0 => V W > 0. • if > 90 => cos < 0 => V W < 0. • 5.Normal: a unit vector perpendicular to a surface
. • Lines y=mx+b e.g.: y = x; ax+by+c=0 Let f(x,y) = ax + by + c A point p(xp,yp) is on the line if f(xp,yp) = 0. When b < 0: p(xp,yp) is above the line if f(xp,yp) < 0. p(xp,yp) is below the line if f(xp,yp) > 0. When b > 0: p(xp,yp) is above the line if f(xp,yp) > 0. p(xp,yp) is below the line if f(xp,yp) < 0.
Parametric Form: P(t)=P0+t(P1-P0); 0 <= t <= 1 P1(x1,y1) P0(x0,y0) • P(t)=P0+t(P1-P0)
Where, • x(t) = x0 + t * (x1 – x0) • y(t) = y0 + t * (y1 – y0) • 0 <= t <= 1 • x(t) = (1-t) * x0 + t * x1 • y(t) = (1-t) * y0 + t * y1 • 0 <= t <= 1
2D Transformations • Translate a point The algebraic representation of translation of point P(x,y) by D(dx,dy) is x’= x + dx y’= y + dy Its “matrix” representation is • Its logical representation is P’ = P + D • Its visual representation is P’ (x’,y’) P(x,y) D(dx,dy)
Y Y 10 10 5 5 X X 0 0 5 5 10 10 (4,5) (7,5) (10,1) (7,1) Before Translation After Translation • To move a shape: translate every vertex of the shape
Scaling (relative to the origin) Scale a point P(x,y) by S(sx,sy) Algebraic: x’=sx* x y’=sy* y Matrix: Logic: P’ = SP or P’ = S(sx,sy)P Scale a line P0P1 (scale each point) P0’=SP0 • P1’=SP1
Y Y 10 10 5 5 X X 0 0 5 5 10 10 (4,5) (7,5) (2,5/4) (7/2,5/4) Before Scaling After Scaling • Scale a shape: scale every vertex of the shape. Visual representation Uniform Scaling:sx=sy
(4.9, 7.8) (2.1, 4.9) (5,2) (9,2) Y Y Y Y Before Rotation After Rotation 10 10 10 10 5 5 5 5 X X X X 0 0 0 0 5 5 5 5 10 10 10 10 • Visual representation Before After Rotate a shape: rotate every vertex of the shape
Summary • Translation: P’ = P + D • Scaling: P’ = S P • Rotation: P’ = R P Homogeneous Coordinates: P(x,y,w) (x,y) to (x,y,w) : (x,y,w) to (x,y)
y’= y + dy • w’=1 • x’= x + dx • P’’ = T(dx2 , dy2)P’ • = T(dx2 , dy2) T(dx1 , dy1) P • = T(dx2+dx1, dy2+dy1) P
Scaling P’=S(Sx, Sy)P • P’’ = S(Sx2 , Sy2)P’ • = S(Sx2 , Sy2)S(Sx1 , Sy1) P • = S(Sx2Sx1, Sy2Sy1)P
Rotation • P’=R()P P’’=R(2)R(1)PP”=R(2+1)P
Shear Transformation:SHx(a) and SHy(b) P’=SHx(a)P • Shear in x against y by a (or an angle). • x’ = x+ay • y’ = y w’=1
P’=SHy(b)P • Shear in y against x by b (or an angle). • x’=x • y’=y + bx • w’=1.
Y Y Y 10 10 10 5 5 5 X X X 0 0 0 5 5 5 10 10 10 Sheared in x Before Shear Sheared in y
Rigid-body Transformation: T and R. • change: location, orientation; • not change: size, angle between elements. Affine Transformation: S, SH • change: size, location, angle; • not change: line (parallelism). • Note: uniform scaling is between the two. It changes size but not angle. • So far, our S, R, SH are all around the origin.
y x After translation of P1 to origin y y P1 x x After rotation After translation to original P1 • Composition of 2D Transformation Rotate the house around P1 For every vertex (P) on the object: P’ = T(P1)R()T(-P1)P y
y y y P1 x x x Original house P1 Translate P1 to origin Scale y y P2 x x Translate to final position P2 Rotate Scale and rotate the house around P1 and move it to P2
For every vertex (P) on the object: P’ = T(P2) R()S(Sx,Sy)T(-P1)P. • Swap the order of operations:not permitted, except uniform scaling (sx=sy) can be swapped with rotation.
y Maximum range of screen coordinates y Viewport 2 Window Viewport 1 x x World coordinates Window in world coordinates Window translated to origin Window scale to size of viewport Screen coordinates Translated by (u,v) to final position y y Window Window x x Screen coordinates World coordinates 5.4 Window-to-Viewport Transformation world-coordinate: inches, feet etc & screen-coordinate: pixels
y x z • Matrix Representation of 3D Transformation Translation: Scaling:
Rotation, around Z-axis: Rotation, around X-axis: Rotation, around Y-axis:
P( x, y, z ) x xp P’(xp, yp, zp) z d z (0,0,0) Projection Plane (View Plane) Projection: Project 3D objects on to a 2D surface
xp= x’ / w’ = x / (z/d + 1) • yp= y’ / w’ = y / (z/d + 1)
0 Parallel Projection: d , lines of sight become parallel P1 0 z P2 View Plan (0,0,0)
Color • RGB • For screen display • All other colors are combination of the three primitives • Additive
Color • YMCK • For printing • All other colors are combination of the three primitives • Subtractive
Color • HSV: • For human interaction • Hue (Tint) • Saturation (Color) • Value (Brightness) • Can interpolate hue and saturation
Shading Shading: determining light reflection from objects at each pixel. Basic Reflection Model: Phong Reflection Model (most commonly used) I= kaIa + kd Id (l · n) + ksIs(v · r )α I : reflected-light intensity
Ambient Reflection: Direction independent kaIa Ia : incident ambient-light intensity ka : object ambient-reflection coefficient part of the object material properties
Lambertian / Diffusive Reflection: Lighting-direction dependent Idkd(ln) = Idkdcos() Id : incident difussive-light intensity kd : object diffusive-reflection coefficient : angle between light direction (l) and surface normal (n). Both l and n are unit vectors.
Specular Reflection: Viewing-direction dependent Isks(vr)α = Iskscos α (Ф). Is : incident specular-light intensity ks : object specular-reflection coefficient Ф : angle between reflection direction (r) and viewing direction (v). : specular-reflection exponent, shaniness coefficient. 1/: roughness.
Attenuation: Distance dependent, no impact on ambient light fatt = 1/(a + bd + cd2) d : distance from the light to the surface point. a,b,c: constant, linear, quadratic attenuation coefficients. I = kaIa + fattkd Id (l · n) + fattksIs(v · r )α I = kaIa + Idkdcos() / (a + bd + cd2) + Iskscos α (Ф) / (a + bd + cd2)
Summary: I = kaIa + fattkd Id (l · n) + fattksIs(v · r )α I = kaIa + Idkdcos() / (a + bd + cd2) + Iskscos α (Ф) / (a + bd + cd2)
Colored Lights and Surfaces : I = (Ir, Ig, Ib) = { I,= r, g, b} : color channel ->Colored lights: Ia, Id, Is, ->Colored objects: ka, kd, ks, I = Ia ka+ fatt Idkd(ln) + fatt Isks(vr) α with = r, g, b. Ir = Iar kar + fatt Idr kdr(ln) + fatt Isr ksr(vr)α Ig = Iagkag+ fattIdgkdg(ln) + fattIsgksg(vr) α Ib = Iabkab+ fattIdbkdb(ln) + fattIsbksb(vr) α
Multiple Lights: I = Ia ka+ fatti [Idikd(lin) + Isiks(vri) α] with = r, g, b. m: number of lights. OpenGL support ambient component for individual light. I = [ Iai ka+ fatti [Idikd(lin) + Isiks(vri) α]]
How to get to each pixel? Two approaches: object order and image order