CSC 470 Computer Graphics Three Dimensional Viewing 1
Today s Lecture Three Dimensional Viewing Developing a Camera Fly through a scene Mathematics of Projections Producing Stereo Views 2
Introduction We have already learnt how to: Create 2D and 3D solid objects Construct a 3D scene Add colors Create complex mesh surfaces So what do we do now? Moving/Flying through a scene Adding perspective 3
The Camera We previously looked at setting up a camera. glortho() and glulookat() these created parallel projections We now want to modify a scene to give us perspective projections. lines converge as they get further away 4
Viewing process and graphics pipeline parallel projection 5
OpenGL tools for modeling and viewing Set modeling transformations. Setting camera in OpenGL. Positioning and aiming the camera. 6
Set the projection matrix glmatrixmode(gl_projection); // make the projection matrix current glloadidentity(); // set it to the identity matrix glortho(left,right,bottom,top,near,far); // multiply it by the new matrix 7
glulookat(eye.x, eye.y, eye.z, look.x, look.y, look.z, up.x, up.y, up.z) It takes as parameters the eye position, eye, of the camera and the look-at point, look. It also takes an approximate up direction, up. up is most often set to (0, 1, 0) to suggest an up direction parallel to the y-axis. 8
Example 1: Set up a typical camera Show the camera with its eye situated at eye = (4,4,4) looking at the origin with lookat = (0,1,0). The up direction is set to up = (0, 1, 0). Suppose we also want the view volume to have a width of 6.4, a height of 4.8 (so its aspect ratio is 640/480), and to set near to 1 and far to 50. This camera would be established using: glmatixmode(gl_projection); // set the view volume glloadidentity(); glortho(-3.2, 3.2, -2.4, 2.4, 1, 50); glmatrixmode(gl_modelview); // place and aim the camera glloadidentity(); glulookat(2, 4, 5, 0, 0, 0, 0, 1, 0); 9
10
Example 2 11
The Camera The Perspective Camera 12
The Camera Revisited View volume: a portion of a rectangular pyramid. Viewangle (θ): the opening (vertically) of the pyramid. Viewplane: between the near plane and the far plane whose location dose not matter. Map the image (projections) on the viewplane into the viewport. Aspect ratio: to specify a view volume *With a perspective projection, the point P is determined by finding where a line from the eye to P intersects the viewplane. 13
Setting the View Volume The projection matrix (encoded with the view volume) is set up using gluperspective(). (Comparing with glortho() for parallel projection.) The height (H) of a viewplane (i.e. near plane)can be computed as: H=2*N*tg(θ/2), and its width (W) is decided by the aspect ratio (AR): W=H*AR. OpenGL commands: glmatrixmode(gl_projection); glloadidentity(); gluperspective(viewangle, aspectratio, N, F); 14
Positioning and Pointing the Camera The only difference between a parallel- and perspective-projection camera resides in the projection matrix, which determines the shape of the view volume. Code: glmatrixmode(gl_modelview); glloadidentity(); glulookat(eye, look, up); 15
The Camera Setting up the Camera instead of glortho() we now use: gluperspective() gluperspective viewangle aspectratio (W/H) near plane far plane 16
The Camera glmatrixmode(gl_projection); glloadidentity(); gluperspective(viewangle, aspectratio, N, F); 17
The Camera Positioning the Camera The camera is positioned using a combination of translations and rotations. Think if the camera being in the same location as the viewers eye. 18
The Camera up(x,y,z) eye(x,y,z) lookat(x,y,z) 19
The Camera glmatrixmode(gl_modelview); glloadidentity(); glulookat(eye.x, eye.y, eye.z, look.x, look.y, look.z, up.x, up.y, up.z); 20
The Camera The camera can have an arbitrary orientation and position. Therefore we can think of the camera having its own axes. 21
glulookat and the Camera Coordinate System The view matrix V created by glulookat is V u u u d v v v d x y z x x y z y = nx ny nz dz 0 0 0 0 where d x = -eye u, d y = -eye v, d z = -eye n V is postmultiplied by M to form the modelview matrix VM. 22
The Camera Camera Movement A camera has six degrees of freedom: 1. slid in 3 directions; and 2. rotated in 3 directions 23
The Camera Camera Movement The camera can move along its axes. 24
The Camera Camera Movement The camera can move along its axes. This is called sliding the camera. Sliding the camera: for example, move it a distance D along u-axis: eye + Du. 25
The Camera Camera Movement To move the camera along the u axis you would simply perform eye = eye + Du To move slide the camera in any direction: eye.x = du*u.x + dv*v.x + dn*n.x eye.y = du*u.y + dv*v.y + dn*n.y eye.z = du*u.z + dv*v.z + dn*n.z v n u 26
The Camera Camera Movement Besides physically moving the camera to another location the camera can be tilted in different directions to look at different parts of the scene. 27
The Camera Camera Movement We use a plane analogy to describe the cameras movement. a rotation from the horizontal along the length is called PITCH a rotation from the horizontal along the width is called ROLL a rotation around the vertical is called YAW 28
Positioning and Pointing the Camera Arbitrary orientation and position (using the aviation terms): (rotate) (change heading) pitch heading or azimuth or bearing 29
The Camera Pitch v = cos(θ)v sin(θ)n n = sin(θ)n + cos(θ)v v n 30
The Camera Roll u = cos(θ)u + sin(θ)v v = cos(θ)v - sin(θ)u v u 31
The Camera Yaw n = cos(θ)n - sin(θ)u u = sin(θ)n + cos(θ)u n u 32
The Camera A Teapot Fly By teapot_flyby.exe A Mesh Surface Fly By 33
Building a Camera in a Program The Camera class definition class Camera{ private: Point3 eye; Vector3 u,v,n; // view volume shape double viewangle, aspect, neardist, fardist; // tell OpenGL where the camera is void setmodelviewmatrix(); public: Camera(); // default constructor // like glulookat() void set(point3 eye, Point3 look, Vector3 up); void roll(float angle); // roll it void pitch(float angle); // increase pitch void yaw(float angle); // yaw it void slide(float delu, float delv, float deln); // slide it void setshape(float vang, float asp, float neard, float fard); }; 34
Building a Camera in a Program (2) The utility routine: setmodelviewmatrix(); void Camera :: setmodelviewmatrix(void) { // load modelview matrix with existing camera values float m[16]; //m is column-organized; m[0~3] = the 1st column! Vector3 evec(eye.x, eye.y, eye.z); // a vector version of eye m[0] = u.x; m[4] = u.y; m[8] = u.z; m[12] = -evec.dot(u); m[1] = v.x; m[5] = v.y; m[9] = v.z; m[13] = -evec.dot(v); m[2] = n.x; m[6] = n.y; m[10] = n.z; m[14] = -evec.dot(n); m[3] = 0; m[7] = 0; m[11] = 0; m[15] = 1.0; glmatrixmode(gl_modelview); glloadmatrixf(m); // load OpenGL s modelview matrix } void Camera:: set(point3 Eye, Point3 look, Vector3 up) { // create a modelview matrix and send it to OpenGL eye.set(eye); // store the given eye position n.set(eye.x - look.x, eye.y - look.y, eye.z - look.z); // make n u.set(up.cross(n)); // make u = up X n n.normalize(); u.normalize(); // make them unit length v.set(n.cross(u)); // make v = n X u setmodelviewmatrix(); // tell OpenGL } 35
Projections Parallel Perspective 36
Viewing process and graphics pipeline Modelview, projection, and viewport matrices 37
Projections Parallel Projection centre of projection at infinity A D A B D C B C projection plane 38
Parallel Projection Projections centre of projection at infinity A A B D D C Points on the object are projected to the viewing plane along parallel lines B C projection plane Preserves relative dimensions of the object but does not give a realistic presentation 39
40 40 Projections Projections Parallel Projection A D C B projection plane D C B A centre of projection at infinity = 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 z y x z y x p p p
Projections Parallel Projection y So all we are really doing is getting rid of the z coordinate to take the coordinates from 3d to 2d!!! z x 41
Perspective Projections 42
Perspective Transformation First discovered by Donatello, Brunelleschi, and DaVinci during Renaissance Objects closer to viewer look larger Parallel lines appear to converge to single point 43
Perspective Projection In the real world, objects exhibit perspective foreshortening: distant objects appear smaller The basic situation: 44
Perspective Projection When we do 3-D graphics, we think of the screen as a 2-D window onto the 3-D world: How tall should this bunny be? 45
Perspective Projection The geometry of the situation is that of similar triangles. View from above: X View plane P (x, y, z) (0,0,0) x =? Z d What is x? 46
Perspective Projections Projecting a 3D Point We use similar triangles to project the point onto the plane in the line of the eye. (x,y,z) N z near plane 47
Perspective Projections Projecting a 3D Point x /x = N/-z (z in the negative direction) y /y = N/-z Once projected onto a 2D plane the z coordinate is not needed. (x,y,z) (x,y ) N z near plane 48
Perspective Projections Properties 1. Parallel lines in 3D will meet at a vanishing point 49
Perspective Projections Properties 2. Lines that pass behind the eye of the camera cause a catastropic passage through infinity. 50
Perspective Projections Properties 3. Perspective projections usually produce geometrically realistic pictures. 51
Setting view volume and aiming camera 52
53
Stereo Projections Faking depth in a 2D image Based on natural stereoscopic eye-brain system. Objects are not viewed with just one eye, but two eyes. Each eye looks at the object from a slightly different location. 54
Stereo Projections 55
Stereo Projections In OpenGL we can produce stereo views by creating two side-by-side viewports with slightly different viewing angles. The lookat point stays the same but the location of the eye moves. Human eyes are about 3 inches apart, therefore a good value for D is 1.5 56
Stereo Projections eye=(0,-1,2.5) eye=(0.5,-1,2.5) -stereoview.exe 57
Taxonomy of Projections 58
Taxonomy of Projections Perspective One Point horizon vanishing point 59
Taxonomy of Projections Perspective Two Point horizon vanishing point 60
Taxonomy of Projections Perspective Three Point horizon vanishing point 61
Taxonomy of Projections Perspective Three Point M.C. Escher: Ascending and Descending 62
Taxonomy of Projections Orthographic Projections Multiview Orthographic 63
Taxonomy of Projections Orthographic Projections Axonometric View Isometric (all three axes are shortened equally) Dimetric (two axes are shortended equally) Trimetric (all axes are shortended unequally) 64
Taxonomy of Projections Oblique Projections Combine the properties of Orthographic and Axonometric Preserves the object face; and Gives a better sense of the 3D nature. 65
Taxonomy of Projections Oblique Projections 66