Presentation is loading. Please wait.

Presentation is loading. Please wait.

Yingcai Xiao Yingcai Xiao Chapter 3 Computer Graphics Primer.

Similar presentations


Presentation on theme: "Yingcai Xiao Yingcai Xiao Chapter 3 Computer Graphics Primer."— Presentation transcript:

1 Yingcai Xiao Yingcai Xiao Chapter 3 Computer Graphics Primer

2 Graphics Using VTK

3

4 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. 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.

5 VTK Classes for 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

6 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

7 GEOMETRIC TRANSFORMATIONS and Animation

8 GEOMETRIC TRANSFORMATIONS Review of Mathematical Preliminaries Points: Vectors : directional lines P 1 (x 1,y 1 ) P 0 (x 0,y 0 )

9 1. A vector has a direction and a length: Length: |V|=(x 2 + y 2 ) 1/2 A unit vector: |V| = 1 e.g.: Normalize a vector: 2. Add two vectors 3. Scalar Multiplication

10 4. Dot product of two vectors  scalar, V W  V  W if  = 90  => cos  = 0 => V  W = 0. if  cos  > 0 => V  W > 0. if  > 90  => cos  V  W < 0. 5.Normal: a unit vector perpendicular to a surface

11 . Lines  y=mx+b e.g.: y = x;  ax+by+c=0 Let f(x,y) = ax + by + c A point p(x p,y p ) is on the line if f(x p,y p ) = 0. When b < 0: p(x p,y p ) is above the line if f(x p,y p ) < 0. p(x p,y p ) is below the line if f(x p,y p ) > 0. When b > 0: p(x p,y p ) is above the line if f(x p,y p ) > 0. p(x p,y p ) is below the line if f(x p,y p ) < 0.

12  Parametric Form: P(t)=P 0 +t(P 1 -P 0 ); 0 <= t <= 1 P 1 (x 1,y 1 ) P 0 (x 0,y 0 ) P(t)=P 0 +t(P 1 -P 0 )

13 Where,  x(t) = x 0 + t * (x 1 – x 0 )  y(t) = y 0 + t * (y 1 – y 0 )  0 <= t <= 1  x(t) = (1-t) * x 0 + t * x 1  y(t) = (1-t) * y 0 + t * y 1  0 <= t <= 1

14 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)

15 To move a shape: translate every vertex of the shape 0 510 5 X Y Before Translation (4,5)(7,5) After Translation 0 510 5 X Y (7,1) (10,1)

16 Scaling (relative to the origin) Scale a point P(x,y) by S(s x, s y ) Algebraic: x’=s x * x y’=s y * y Matrix: Logic: P’ = SP or P’ = S(s x, s y )P Scale a line P 0 P 1 (scale each point) P 0 ’=SP 0 P 1 ’=SP 1

17 Scale a shape: scale every vertex of the shape. Visual representation 0 510 5 X Y Before Scaling (4,5)(7,5) 0 510 5 X Y After Scaling (2,5/4 ) (7/2,5/4 ) Uniform Scaling: s x =s y

18 Visual representation 0 510 5 X Y 0 5 5 X Y Rotate a shape: rotate every vertex of the shape AfterBefore 0 510 5 X Y (2.1, 4.9) (4.9, 7.8) 0 510 5 X Y (5,2) (9,2) Before Rotation After Rotation

19 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)

20 y’= y + dy w’=1 x’= x + dx P’’ = T(dx 2, dy 2 )P’ = T(dx 2, dy 2 ) T(dx 1, dy 1 ) P = T(dx 2 +dx 1, dy 2 +dy 1 ) P

21 Scaling P’=S(S x, S y )  P P’’ = S(S x2, S y2 )P’ = S(S x2, S y2 )S(S x1, S y1 ) P = S(S x2  S x1, S y2  S y1 )P

22 P’’=R(  2 )R(  1 )P  P”=R(  2 +  1 )P Rotation P’=R(  )  P

23 Shear Transformation: SH x (a) and SH y (b) P’=SH x (a)  P Shear in x against y by a (or an angle). x’ = x+ay y’ = y w’=1

24 P’=SH y (b)  P Shear in y against x by b (or an angle). x’=x y’=y + bx w’=1.

25 0 510 5 X Y 0 5 5 X Y Sheared in x Sheared in y 0 510 5 X Y Before Shear

26 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.

27 Composition of 2D Transformation Rotate the house around P1 For every vertex (P) on the object: P’ = T(P 1 )R(  )T(-P 1 )P yy x After translation of P 1 to origin y x After rotation  y x P1P1 After translation to original P 1

28 Scale and rotate the house around P1 and move it to P2 y x P1P1 Original house y x P1P1 Translate P1 to origin y x Scale y x Rotate y x P2P2 Translate to final position P2

29 For every vertex (P) on the object: P’ = T(P 2 ) R(  )S(Sx,Sy)T(-P 1 )P. Swap the order of operations: not permitted, except uniform scaling (s x =s y ) can be swapped with rotation.

30 5.4 Window-to-Viewport Transformation world-coordinate: inches, feet etc & screen-coordinate: pixels y World coordinates Window x y Screen coordinates Window x y World coordinates Window x y Screen coordinates Viewport 1 x Viewport 2 Window in world coordinates Window translated to origin Window scale to size of viewport Translated by (u,v) to final position Maximum range of screen coordinates

31 Matrix Representation of 3D Transformation x y z Translation: Scaling:

32 Rotation, around Z-axis: Rotation, around X-axis: Rotation, around Y-axis:

33 Projection: Project 3D objects on to a 2D surface (0,0,0) d z z P( x, y, z ) x P’(x p, y p, z p ) xpxp Projection Plane (View Plane)

34 x p = x’ / w’ = x / (z/d + 1) y p = y’ / w’ = y / (z/d + 1)

35 0 Parallel Projection: d , lines of sight become parallel View Plan z P1P1 P2P2 (0,0,0) 0

36 Color

37 Color RGB For screen display All other colors are combination of the three primitives Additive

38 Color YMCK For printing All other colors are combination of the three primitives Subtractive

39 Color HSV: For human interaction Hue (Tint) Saturation (Color) Value (Brightness) Can interpolate hue and saturation

40 HSV

41 Color Conversions

42 Lighting/Shading

43 Shading Shading: determining light reflection from objects at each pixel. Basic Reflection Model: Phong Reflection Model (most commonly used) I= k a I a + k d I d (l · n) + k s I s (v · r ) α I : reflected-light intensity

44 Ambient Reflection: Direction independent k a I a I a : incident ambient-light intensity k a : object ambient-reflection coefficient part of the object material properties

45 Lambertian / Diffusive Reflection: Lighting-direction dependent I d k d (l  n) = I d k d cos(  ) I d : incident difussive-light intensity k d : object diffusive-reflection coefficient  : angle between light direction (l) and surface normal (n). Both l and n are unit vectors.

46 Specular Reflection: Viewing-direction dependent I s k s (v  r) α = I s k s cos α (Ф). I s : incident specular-light intensity k s : object specular-reflection coefficient Ф : angle between reflection direction (r) and viewing direction (v).  : specular-reflection exponent, shaniness coefficient. 1/  : roughness.

47 Attenuation: Distance dependent, no impact on ambient light f att = 1/(a + bd + cd 2 ) d : distance from the light to the surface point. a,b,c: constant, linear, quadratic attenuation coefficients. I = k a I a + f att k d I d (l · n) + f att k s I s (v · r ) α I = k a I a + I d k d cos(  ) / (a + bd + cd 2 ) + I s k s cos α (Ф) / (a + bd + cd 2 )

48 Summary: I = k a I a + f att k d I d (l · n) + f att k s I s (v · r ) α I = k a I a + I d k d cos(  ) / (a + bd + cd 2 ) + I s k s cos α (Ф) / (a + bd + cd 2 )

49 Colored Lights and Surfaces : I = (I r, I g, I b ) = { I, = r, g, b} : color channel ->Colored lights: I a, I d, I s, ->Colored objects: k a, k d, k s, I = I a k a + f att I d k d (l  n) + f att I s k s (v  r) α with = r, g, b. I r = I ar k ar + f att I dr k dr (l  n) + f att I sr k sr (v  r) α I g = I ag k ag + f att I dg k dg (l  n) + f att I sg k sg (v  r) α I b = I ab k ab + f att I db k db (l  n) + f att I sb k sb (v  r) α

50 Multiple Lights: I = I a k a + f atti [I d i k d (l i  n) + I s i k s (v  r i ) α ] with = r, g, b. m: number of lights. OpenGL support ambient component for individual light. I = [ I a i k a + f atti [I d i k d (l i  n) + I s i k s (v  r i ) α ]]

51 How to get to each pixel? Two approaches: object order and image order

52 http://en.wikipedia.org/wiki/Phong_shading

53 Image Order Shading: (Image created by Russell Yuncker) (Image created by Jian He)

54 Simple Ray Tracing: for (each scan line in image ) { for (each pixel in scan line ) { determine ray from eye through pixel; for(each object in scene) { if(object is intersected and is closest considered thus far) record intersection point and object id. } set pixel’s color to that at closest object intersection point (using the I formula given above.) }

55 Recursive Ray Tracing: Set pixel’s color to that at closest object intersection point using the I formular given below. I = (1- k r - k t )I regular + k r I r + k t I t I regular : regular reflection of lights from light source. Computed by the formula above. k r : reflection coefficient. I r : illumination from other objects (to be reflected). k t : transmission coefficient. I t : illumination from other objects (to be transmitted).

56 Object-order Rendering

57 Object-based rendering Each object is defined by its enclosure surfaces. Each surface is composed of triangle patches (Objects  Primitives). Project each triangle on to the view plane. Render the projected triangle (compute the shade colors in the triangle). No consideration of reflection light from other objects. H.W.: Faster: Open GL  VTK Using Marching Cubes to create isosurfaces (geometrical objects). Each object is defined by its enclosure surfaces. Each surface is composed of triangle patches (Objects  Primitives). Project each triangle on to the view plane. Render the projected triangle (compute the shade colors in the triangle). No consideration of reflection light from other objects. H.W.: Faster: Open GL  VTK Using Marching Cubes to create isosurfaces (geometrical objects).

58 Frame Buffer: a buffer of memory to store the colors of the screen, one memory cell per pixel. http://www.webopedia.com/TERM/F/frame_buffer.html http://en.wikipedia.org/wiki/Framebuffer http://en.wikipedia.org/wiki/Linux_framebuffer

59 Z Buffer (depth buffer): a buffer of memory to store the z values of the screen, one memory cell per pixel. http://en.wikipedia.org/wiki/Z-buffering line of sight Frame buff Z buff

60 Shading: Rendering of a projected triangle. Constant shading (flat shading)  compute one shaded color for all pixels within the triangle. Use one normal and one object color for the whole triangle. Couraud (smooth) shading  calculate shaded colors at the vertexes by the R c formula using the vertex normals and the vertex colors. Then interpolated the shaded vertex colors into each pixel. Phong shading  interpolate vertex normals and vertex colors to each pixel and then use the R c formula to compute the shaded color at the pixel. Constant shading (flat shading)  compute one shaded color for all pixels within the triangle. Use one normal and one object color for the whole triangle. Couraud (smooth) shading  calculate shaded colors at the vertexes by the R c formula using the vertex normals and the vertex colors. Then interpolated the shaded vertex colors into each pixel. Phong shading  interpolate vertex normals and vertex colors to each pixel and then use the R c formula to compute the shaded color at the pixel.

61 Flat/Constant Shading: http://www.yourdictionary.com/computer/flat-shading http://www.yourdictionary.com/computer/flat-shading for (each object) for(each triangle of the object) compute its reflection using color and normal of the triangle for(each pixel in the triangle) if(closer to the viewer than the current z buffer value) { update z buffer with the new z update pixel color with the triangle reflection }

62 Gouraud/Smooth Shading http://en.wikipedia.org/wiki/Gouraud_shading http://en.wikipedia.org/wiki/Gouraud_shading http://en.wikipedia.org/wiki/Gouraud_shading for (each object) for(each triangle of the object) {for(each vertex of in the triangle) compute the vertex reflection using the color and the normal of the vertex for(each pixel in the triangle) if(closer to the viewer than the current z buffer value) { update z buffer with the new z interpolate the pixel color from the vertex reflections. }

63 Phong Shading: http://www.yourdictionary.com/computer/phong-shading http://en.wikipedia.org/wiki/Phong_shading http://www.yourdictionary.com/computer/phong-shading http://en.wikipedia.org/wiki/Phong_shading http://www.yourdictionary.com/computer/phong-shading http://en.wikipedia.org/wiki/Phong_shading for (each object) for(each triangle of the object) for(each pixel in the triangle) if(closer to the viewer than the current z buffer value) {update z buffer with the new z interpolate the pixel normal from the vertex normals compute the pixel color/relection using Phong reflection model using pixel normals and the properties of the object. }

64 Phong Shading: http://www.yourdictionary.com/computer/phong-shading http://en.wikipedia.org/wiki/Phong_shading http://www.yourdictionary.com/computer/phong-shading http://en.wikipedia.org/wiki/Phong_shading http://www.yourdictionary.com/computer/phong-shading http://en.wikipedia.org/wiki/Phong_shading for (each object) for(each triangle of the object) for(each pixel in the triangle) if(closer to the viewer than the current z buffer value) {update z buffer with the new z interpolate the pixel normal from the vertex normals compute the pixel color/relection using Phong reflection model using pixel normals and the properties of the object. }

65 Summary: Computer Graphics => Visualization 3D Data Grid  Marching Cubes  Iso-surfaces Visualization (surface-based) : transforming data into graphics primitives with geometry and attributes (color, textures, patterns, …) Rendering: transforming graphics primitives into images


Download ppt "Yingcai Xiao Yingcai Xiao Chapter 3 Computer Graphics Primer."

Similar presentations


Ads by Google