Multimedia Signals and Systems Virtual Reality and VRML Kunio Takaya Electrical and Computer Engineering University of Saskatchewan January 16, 2008 ** Go to full-screen mode now by hitting CTRL-L 1
Contents 1 Basic Theory of 3D Graphics 4 2 Viewing Transformation 16 3 Projection to the View Plane 24 4 Equation of a Plane 29 5 Reflection Model - the Phong model 36 6 MATLAB - surface and mesh 45 7 Virtual Reality Modeling Language 48 2
R µ ν 1 2 R δµ ν = 8πG c 4 T µ ν Here T µ ν is tensor of energy momentum. black red green yellow blue magenta cyan 3
1 Basic Theory of 3D Graphics Alan Watt, Fundamentals of Three-Dimensional Computer Graphics, Addison Wesley, 1991, ISBN 0-201-15442-0 In 3D computer graphics, we use a right-handed corrdinate system to define the world coordinate system and we use a left-handed coordinate system to define the view coordinate system. In the right-handed or left-handed coordinate systems, thumb points x-axis, index finger points y-axis, middle finger points z-axis. 4
A vertex in a 3D space, V at [x, y, z] is represented by [X, Y, Z, w] in a homogeneous coordinate system. Where, w is a scale factor, which is normally 1. x = X/w, y = Y/w, and z = Z/w. 1. Transformation A vertex point V is transformed to a new point V by a transformation matrix M. V = VM 5
Translation can be treated as matrix multiplication, V = VT 1 0 0 0 0 1 0 0 = [x, y, z, 1] 0 0 1 0 T x T y T z 1 = [x + T x, y + T y, z + T z, 1] 6
2. Scaling V = VS S x 0 0 0 0 S y 0 0 = [x, y, z, 1] 0 0 S z 0 0 0 0 1 = [xs x, ys y, zs z, 1] 7
3. Rotation about x-axis V = VR x 1 0 0 0 0 cos θ x sin θ x 0 = [x, y, z, 1] 0 sin θ x cos θ x 0 0 0 0 1 = [x, y cos θ x + z sin θ x, y sin θ x z cos θ x, 1] 8
4. Rotation about y-axis V = VR y cos θ y 0 sin θ y 0 0 1 0 0 = [x, y, z, 1] sin θ y 0 cos θ y 0 0 0 0 1 = [x cos θ y + z sin θ y, y, x cos θ y + z sin θ y, 1] 9
5. Rotation about z-axis V = VR z = [x, y, z, 1] cos θ z sin θ z 0 0 sin θ z cos θ z 0 0 0 0 1 0 0 0 0 1 = [x cos θ z y sin θ z, x sin θ z + y cos θ z, z, 1] Let a current point in the (x, y) plane be x = r cos θ 0, and y = r sin θ 0. After rotation about z-axis by θ z, x = r cos(θ 0 + θ z ), y = r sin(θ 0 + θ z ) 10
Let s check that the given rotation matrix about z-axis is correct! x = r cos(θ 0 + θ z ) = r(cos θ 0 cos θ z sin θ 0 sin θ z ) = x cos θ z y sin θ z y = r sin(θ 0 + θ z ) = r(sin θ 0 cos θ z + cos θ 0 sin θ z ) = y cos θ z + x sin θ z 11
When rotaions about different axes are mixed, multiply each of respective rotation matrices in the order of applied rotaions. For example, if rotation about x-axis, rotation about y-axis, then rotation about z axis are applied, the combined rotation matrix is R = R x R y R z. If rotation about z axis is applied first, then rotation about x-axis followed by rotation about y-axis, the combined rotation matrix is R = R z R x R y. 12
6. Example Consider rotating a body about a line parallel to the z axis which passes through the point [T x, T y, 0] where one of the vertices of the object is located. A net transformation matrix can be derived as follows: 1. translate the object to the origin, 2. apply the desired rotation, and 3. translate the object back to its original position. 13
14
= T 1 R z T 2 = 1 0 0 0 cos θ z sin θ z 0 0 1 0 0 0 0 1 0 0 sin θ z cos θ z 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 1 0 T x T y 0 1 0 0 0 1 T x T y 0 1 cos θ z sin θ z 0 0 sin θ z cos θ z 0 0 0 0 1 0 T x cos θ z + T y sin θ z + T x T x cos θ z T y sin θ z + T y 0 1 15
2 Viewing Transformation The viewing transformation, T view transform points in the world coordinate system into the view corrdinate system. [x v, y v, z v, 1] = [x w, y w, z w, 1]T view We adopt a left-handed convention for the view coordinate system. World coordinates are defined as a right-handed system so that in the computation of a net transformation matrix for the viewing transformation, we would include a conversion to a left-handed coordinate system. Four steps of transformations are required to accomplish this conversion. 16
17
7. Set a View Point The viewing transformation is specified using spherical instead of cartesian coordinates. A view point is specified with a distance from the origin of the world coordinate, µ, and two angles, θ and φ. The view point s cartesian coordinates are T x = µ cos θ sin φ, T y = µ sin θ sin φ, T z = µ cos φ Since we are moving the origin of a system from [0, 0, 0, 1] to [T x, T y, T z, 1], the first transformation required is 1 0 0 0 0 1 0 0 T 1 = 0 0 1 0 T x T y T z 1 18
= 1 0 0 0 0 1 0 0 0 0 1 0 µ cos θ sin φ µ sin θ sin φ µ cos φ 1 19
8. Align y with the plane [µ, θ, φ] The next step is to rotate the coordinate system through 90 θ in a clockwise direction about the z -axis. This is to align y -axis to the plain that contains the vector [µ, θ, φ], and x -axis to become normal to this plane. A clockwise rotation of the coordinate system is the same as a counterclosckwise rotaion of a point relative to the coordinate system. Thus, sin θ cos θ 0 0 cos θ sin θ 0 0 T 2 = 0 0 1 0 0 0 0 1 since cos(90 θ) = sin θ, and sin(90 θ) = cos θ 20
9. Align z to the vector [µ, θ, φ] The next step is to rotate the coordinate system through 180 φ counterclockwise about the x -axis. This makes z -axis pass through the origin of the world coordinate system. 1 0 0 0 0 cos φ sin φ 0 T 3 = 0 sin φ cos φ 0 0 0 0 1 21
10. Convert to a left-handed system 1 0 0 0 0 1 0 0 T 4 = 0 0 1 0 0 0 0 1 22
11. The net viewing transformation Multiplying these together gives the net transformation matrix from the world coordinate system to the viewing cooridniate system. sin θ cos θ cos φ cos θ sin θ 0 cos θ sin θ cos φ sin θ sin φ 0 T view = T 1 T 2 T 3 T 4 = 0 sin φ cos φ 0 0 0 µ 1 With this transformation, T view [x v, y v, z v, 1] = [x w, y w, z w, 1]T view. 23
3 Projection to the View Plane 12. Perspective Projection A view plane is a plane perpendicular to the line [µ, θ, φ] positioned at a distance d from the view point. Perspective projection of a point in the view coordinate system, [x v, y v, z v, 1], is [x s, y s, d, 1] = [ x v z v d, y v z v d, d, 1] Taking advantage of desciptive power of the homogeneous coordinate system, we can write this as [x s, y s, d, 1] = [ x v z v d, y v z v d, d, 1] = [x v, y v, z v, z v d ] 24
= [x v, y v, z v, 1] 1 0 0 0 0 1 0 0 0 0 1 1 d 0 0 0 0 Thus, T pers = 1 0 0 0 0 1 0 0 0 0 1 1 d 0 0 0 0 25
26
13. Parallel (orthographic) Projection When the view point is set at infinity, the projection is parallel projection. 1 0 0 0 0 1 0 0 T orth = 0 0 0 0 0 0 0 1 27
28
4 Equation of a Plane 14. Plane defined by a Line Normal A plane is a two-dimensional doubly ruled surface spanned by two linearly independent vectors. The equation of a plane with nonzero normal vector n = (a, b, c) 29
through the point x 0 = (x 0, y 0, z 0 ) is n (x x 0 ) = 0 where x = (x, y, z). This gives the general equation of a plane, ax + by + cz + d = 0, where d = ax 0 by 0 cz 0 A plane specified in this form therefore has x-, y-, and z-intercepts at x = d a, y = d b, z = d c and lies at a distance D = d a2 + b 2 + c 2. It is especially convenient to specify planes in so-called Hessian normal form. This is obtained by defining the components of the 30
unit normal vector ˆn = (n x, n y, n z ) as n x = a a2 + b 2 + c 2, n y = b a2 + b 2 + c 2, n z = c a2 + b 2 + c 2 and the constant p = d. Then the Hessian normal a2 + b 2 + c2 form of the plane is ˆn x = p. The (signed) distance to a point x 0 is D = ˆn x 0 + p, and the distance from the origin is simply D = p. Refer to http://mathworld.wolfram.com/plane.html 31
15. Plane defined by Intercepts This section presents the useful equation to describe a plane of polygons that construct a wireframe 3D structure. In intercept form, a plane passing through the points (a, 0, 0), (0, b, 0) and (0, 0, c ) is given by x a + y b + z c = 1. The plane through (x 1, y 1, z 1 ) and parallel to (a 1, b 1, c 1 ) and 32
(a 2, b 2, c 2 ) (two normal lines) is x x 1 y y 1 z z 1 a 1 b 1 c 1 a 2 b 2 c 2 = 0 The plane through points (x 1, y 1, z 1 ) and (x 2, y 2, z 2 ) parallel to (a, b, c) direction is x x 1 y y 1 z z 1 x 2 x 1 y 2 y 1 z 2 z 1 = 0 a b c 33
The three-point form is x y z 1 x 1 y 1 z 1 1 x 2 y 2 z 2 1 x 3 y 3 z 3 1 = x x 1 y y 1 z z 1 x 2 x 1 y 2 y 1 z 2 z 1 x 3 x 1 y 3 y 1 z 3 z 1 = 0 A plane specified in three-point form can be given by A 1 x + A 2 y + A 3 z A = 0, where, A = det(x 1, x 2, x 3 ) and A i is and the determinant obtained by replacing x i with a column vector of 1 s. To express in Hessian normal form, note that the unit normal vector can also be immediately written as ˆn = (x 3 x 1 ) (x 2 x 1 ) j(x 3 x 1 ) (x 2 x 1 )j and the constant p giving the distance from the plane to the origin 34
is p = A A 2 1 + A 2 2 + A 2 3. The (signed) point-plane distance from a point (x 0, y 0, z 0 ) to a plane ax + by + cz + d = 0 is D = ax 0 + by 0 + cz 0 + d a2 + b 2 + c 2. The dihedral angle between the planes a 1 x + b 1 y + c 1 z + d 1 = 0 and a 2 x + b 2 y + c 2 z + d 2 = 0 which have normal vectors ˆn 1 = (a 1, b 1, c 1 ) and ˆn 2 = (a 2, b 2, c 2 ) is simply given via the dot product of the normals, cos θ = ˆn 1 ˆn 2 = a 1 a 2 + b 1 b 2 + c 1 c 2 a 2 1 + b 2 1 + c 2 1 a 2 2 + b 2 2 + c 2 2 The dihedral angle is therefore particularly simple to compute if the planes are specified in Hessian normal form. 35
5 Reflection Model - the Phong model 16. Back face elimination (culling) 36
Given a view point, we can determine whether or not a polygon is visible from the view point. Calculate the angle between the polygon surfance normal and the line-of-sight vector. The surface (polygon) is visible if and only if the angle between the two is less than 90. N V > 0 Visible 17. the Phoneg reflection model The light incident at a surface is light reflected, light scattered, light absorbed, light transmitted 37
38
Of those components, the light reflected and redirected toward the view point determines the impression of the surface (polygon). The reflected light consists of diffused reflection, ambient light, and specular reflection. diffused reflection A surface with a perfect diffuser scatters light equally in all directions. This means that the amount of reflected light seen by the viewer does not depend on the viewer s position. Such surfaces are dull and matt, and the intensity of diffused reflected light is given by Lambert s law. I d = I i k d (L N) = I i k d cos θ, where, 0 θ π 2 Where, I i is the incident light, and k d is the diffuse reflectivity. ambient light Ambient light is the result of multiple reflectio from alls and object, and incident on a surface from all directions. It is modelled as a constant term, I a k a. We combine diffused reflection and ambient light, and consider a distance from the 39
surface to a view poing r and a constant k. I = I a k a + I i k d L N r + k specular reflection When a surface is glossy (not matt, not of diffusion), the incident light (L) is reflected by the surface into the direction of (R), keeping the angle to the surface normal to be equal. Amount of reflected light observed depends on the view angle φ measured from the line of reflection. The intensity of specular reflection is given by I s = I i k s cos n φ r + k = I i k s (R V) n r + k Combining diffused light, ambient light and specular light, L N I = I a k a + I i k d r + k + I (R V) n ik s r + k In the 3D graphic model, incident light sources and a view 40
point are given and known. However, the direction of reflection R needs calculations. In practice, (R V) is replaced by (N H) using a surface that reflect the incident light to the view angle. The normal line to this surface is H. L N I = I a k a + I i k d r + k + I (N H) n ik s r + k For colored objects, the Phong model uses a different diffuse reflection coefficient for each of RGB. Specular lights is treated as white. I = I a k a r + I i [k d r L N r + k + k (N H) n s r + k ] I = I a k a g + I i [k d g L N r + k + k s I = I a k a b + I i [k d b L N r + k + k s (N H) n r + k ] (N H) n r + k ] 41
42
43
44
6 MATLAB - surface and mesh 16. 3D Rendering by the Phong model ep=pi/64; theta=[-pi/2: pi/16: pi/2]; radius=cos(theta); height=sin(theta); phi=[0: pi/16: 2*pi]; mmax=size(radius,2); nmax=size(phi,2); x=cos(phi); y=sin(phi); z=zeros(1,nmax); for k=2:mmax r=radius(k); h=height(k); x=[x;r*cos(phi)]; y=[y;r*sin(phi)]; z=[z;h*ones(1,nmax)]; end figure; subplot(1,2,1); s=surface( xda,x, yda,y, zdata,z, EdgeColor, None,... Clipping, off ); cd=ones(mmax,1)*rand(1,nmax); set(s, CData, cd); l1=light( pos,[1-2 1]); l2=light( pos,-[1-2 1]); 45
axis vis3d; set(gca, DataAspectRatio, [1 1 1]); shading interp; lighting phong; set(gca, CameraViewAngle, 7); axis([-1.2 1.2-1.2 1.2-1.2 1.2]); colormap(hsv(180)); view(130,30); material([.25.5.9]); set(l2, Position, [1 1 1]); 46
47
7 Virtual Reality Modeling Language 17. 3D Rendering by VRML VRML stands for Virtual Reality Modeling Language. It allows to specify dynamic 3D scenes through which users can navigate with the help of a VRML browser. The Virtual Reality Modeling Language (VRML) can been seen as a 3-D visual extension of the WWW. People can navigate through 3-D space and click on objects representing URLs. Often, VRML is pronounced like Vermal, not V-R-M-L. Cortona VRML Client 5.1 is a VRML interactive Web3D viewer, a plug-in for popular Internet browsers (Internet Explorer, Netscape Browser, Mozilla, Mozilla Firefox, etc.) downloadable from 48
http://www.parallelgraphics.com/products/cortona/ The Virtual Reality Modeling Language consists of two parts. Part 1 (ISO/IEC 14772-1) defines the base functionality and text encoding for VRML. Part 2 (ISO/IEC 14772-2) defines the base functionality and all bindings for the VRML External Authoring Interface. The Virtual Reality Modeling Language, International Standard ISO/IEC 14772-1:1997 is described in http://www.web3d.org/x3d/specifications/vrml/iso-iec-14772- VRML97/ Online tutorials provide a good guide for writing a code in VRML. One of the sites is http://www.lighthouse3d.com/vrml/tutorial/ Here, we just study how the world and view coordinates we studied 49
here are implemented, and also how the Phong model of ray tracing is implemented in VRML. 18. Simple VRML scripts 50
#VRML V2.0 utf8 #transform.wrl Transform{ translation 2.0 2.0 0.0 children[ Shape{ appearance Appearance{ material Material {} } geometry Cone{ bottomradius 2.0 height 4.0 } } ] } Transform{ translation -2.0 2.0 0.0 scale 1 2 1 rotation 1.0 0.0 0.0 3.14 children[ Shape{ appearance Appearance{ material Material {} } geometry Cone{ bottomradius 2.0 height 4.0 } } ] } 51
#VRML V2.0 utf8 #emissiveball.wrl Viewpoint{ position -1.0 10.0 10.0 orientation 1.0 0.0 0.0-0.78 } Transform{ translation -2.5 0.0 0.0 children[ Shape{ appearance Appearance{ material Material { diffusecolor 0.66 0.81 0.92 specularcolor 0.66 0.81 0.92 shininess 0.5 } } geometry Sphere{ radius 2.0 } } ] } 52
Transform{ translation 2.5 0.0 0.0 children[ Shape{ appearance Appearance{ material Material { emissivecolor 1.0 0.0 0.0 } } geometry Sphere{ radius 2.0 } } ] } 53
#VRML V2.0 utf8 #grid.wrl Viewpoint{ position 2.5 3.0 10.0 orientation 1.0 0.0 0.0-0.4 } #background Background{ skycolor [0.66 0.81 0.92] } # Set a view point # Rotate about x-axis # Set color #Geometry and Shape Shape{ appearance Appearance{ material Material{ diffusecolor 0 0.39 0.21 } } geometry ElevationGrid{ # Display geometrical shape xdimension 10 # number of grid point x-axis zdimension 10 # number of grid point z-axis xspacing 0.5 # distance between grids x-axis zspacing 0.5 # distance between grids z-axis height[ # Specify vertex heights 0.5, 0.5, 0.4, 0.4, 0.5, 0.6, 0.4, 0.4, 0.5, 0.2, 54
]}} 0.5, 0.5, 0.3, 0.4, 0.4, 0.5, 0.4, 0.4, 0.4, 0.2, 0.4, 0.4, 0.3, 0.2, 0.2, 0.2, 0.4, 0.4, 0.3, 0.2, 0.4, 0.3, 0.2, 0.2, 0.2, 0.2, 0.2, 0.3, 0.3, 0.2, 0.2, 0.2, 0.2, 0.0, 0.0, 0.0, 0.0, 0.1, 0.1, 0.1, 0.2, 0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.1, 0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.1, -0.1, -0.2, -0.3, -0.1, -0.1, -0.2, -0.2, -0.3, -0.4, -0.5, -0.5, -0.5, -0.5, -0.2, -0.4, -0.4, -0.4, -0.5, -0.3, -0.5, -0.5, -0.5, -0.5 55
19. VRML animation robot.wrl 56