Lecture 4: Viewing Topics: 1. Classical viewing 2. Positioning the camera 3. Perspective and orthogonal projections 4. Perspective and orthogonal projections in OpenGL 5. Perspective and orthogonal projection matrices Chapter 5, Sections 5.1, 5.2, 5.3, 5.4, 5.7, 5.8. 1
Classical and computer viewing Both kinds of viewing involve: objects, a viewer, projectors, and a projection plane. The projectors meet at the centre of projection (COP. The COP is the centre of the lens or ee, or in computer graphics the origin of the camera frame. In both classical and computer viewing the viewer can be an infinite distance from the objects. As we move the COP to infinit the projectors become parallel and in the limit, the COP can be replaced b a direction of projection (DOP. Views with a finite COP are called perspective views while those with a COP at infinit are called parallel views. Projector Object Object COP Projection plane DOP Projector Projection plane 2
Classical viewing Front elevation Elevation oblique Plan oblique Isometric One-point perspective Three-point perspective In classical viewing there is an underling notion of a principal face. This is tpical in architecture where objects are often buildings having planar faces: front, back, top, bottom, right and left faces. Such objects have three orthogonal directions associated with them. 3
Parallel viewing An orthogonal projection is one in which the projectors are not onl parallel but also perpendicular to the projection plane. The classical orthographic projection is an orthogonal projection in which the projection plane is placed parallel to one of the principal faces of the object. A more general orthogonal projection is the aonometric projection, in which the projection plane can have an arbitrar orientation. Dimetric Trimetric Isometric 4
Oblique projections are parallel views in which the projection plane is not orthogonal to the projectors. The projection plane is skew. Projection plane Projection plane Projection plane (a (b (c (a construction (b top view (c side view 5
Perspective viewing In perspective viewing, the image of an object gets smaller when it is moved awa from the viewer. Therefore perspective views look more natural. However the cannot be used to make measurements, as with parallel views. (a (b (c (a Three-point perspective: three vanishing points (b Two-point perspective: two vanishing points (c One-point perspective: one vanishing point 6
Positioning the camera How do we position the camera with respect to the objects? There are two points of view: (1 we move the camera with respect to the objects or (2 we move objects relative to a fied camera. The two reciprocal movements will result in the same image. These movements are controlled b the model-view matri which represents a transformation between the world frame and the camera frame. Initiall (b default the model-view matri is set to the identit, in which case both frames are the same. The camera is at the origin pointing down the negative ais. 7
Eample 1. Objects are tpicall placed near the origin. So to view them, we need to move the camera back along the ais. The correct transformation is therefore glmatrimode(gl_modelview; glloadidentit(; gltranslatef(0,0, 0.0, -d; for some positive number d., c c, c c, c c (a (b We can think of this transformation either as a translation of the objects through the vector (0, 0, d or as a translation of the camera through the vector (0, 0, d. 8
Eample 2. We now wish to view the objects (which are again centred around the origin from the positive ais. To achieve this we need first to move the camera backwards along the positive ais, a distance d, and then rotate the camera around the ais b 90 degrees: glmatrimode(gl_modelview; glloadidentit(; gltranslatef(0,0, 0.0, -d; glrotate(-90.0, 0.0, 1.0, 0.0; The equivalent viewpoint is to think of the model-view matri being applied to the objects: the are first rotated through 90 degrees around the ais, and then translated through (0, 0, d. The model-view matri has the form M = T R, and M is applied to each object verte p, moving it to Mp. T R 9
Eample 3. To achieve more complicated orientations of the camera we can concatenate rotation matrices. However, it is easier to use a function provided in GLU: glulookat(ee, ee, ee, at, at, at, up, up, up; (at, at, at (up, up, up (ee, ee, ee This function sets the origin of the camera to be the eepoint e and sets the camera frame aes u,v,n (plaing the role of,, according to the point at and the vector up. It first sets n = e at, the normal to the projection plane. The vector v is then computed as the projection of up onto this plane. Finall u is taken to be the cross product of v and n. 10
Perspective projections Suppose the camera is at its default position at the origin, pointing along the negative ais. How do we compute a perspective projection? For simplicit, suppose the projection plane is parallel to the - plane, i.e., = d, for some negative number d. Each point (,, in space is projected towards the origin. Therefore the point where the projector hits the projection plane is ( p, p, p, where p = d/, p = d/, p = d. (,, ( p, d (, = d ( p, d (, ( p, p, p = d (a (b (c The perspective transformation (,, ( p, p, p is clearl non-affine. So how can we represent it using a 4 4 matri? 11
The answer is to etend the notion of homogeneous coordinates. Recall that the point (,, has the homogeneous representation (,,, 1. We can etend this representation b agreeing that the same point can be represented b the vector (w, w, w, w, for an w 0. To recover the representation (,,, 1, we have onl to divide the coordinates b w. With this etended homogeneous representation of points, we can represent our perspective transformation b the matri 1 0 0 0 0 1 0 0 M =. 0 0 1 0 0 0 1/d 0 The matri M transforms the point p = (,,, 1 T into the point q = (,,, /d T, and after division b the last coordinate, /d, we arrive at q = ( p, p, p, 1. This latter division is called perspective division, and can be made part of the pipeline: model-view projection perspective division 12
Orthogonal projections Orthogonal projections can be viewed as the limiting case of perspective projections as the COP goes to infinit. However, in practice it is much simpler to derive the projection equation directl. We can assume that the projection plane is = 0, in which case the point (,, is mapped to ( p, p, p where p =, p =, p = 0. ( p, p, 0 = 0 (,, In homogeneous coordinates, the transformation is p 1 0 0 0 p 0 1 0 0 =. p 0 0 0 0 1 0 0 0 1 1 Here no perspective division is necessar, but in hardware implementations we can use the same pipeline for both perspective and orthogonal projections. 13
Perspective projections in OpenGL In addition to the projections themselves, we need to specif a clipping volume or view volume, b specifing front and back clipping frames. For perspective projections, the viewing volume becomes a frustum, a truncated pramid. View volume View plane Front clipping plane Back clipping plane COP 14
In OpenGL, we can specif a perspective view b constructing and loading the matri ourselves, or b using one of two special functions. The first is glfrustum(min, ma, min, ma, near, far which sets the frustum shown in the following figure. = ma = min ( ma, ma, min ( min, min, min Here, min = near and ma = far in the figure. The near and far distances must be positive, and so min and ma are alwas negative. Note that min, ma, min, ma are the coordinates of the front clipping plane. 15
The second function for specifing a perspective projection with a clipping volume is gluperspective(fov, aspect, near, far. This function uses the angle of view fov in the direction and the aspect ratio width divided b height together with the near and far planes to completel determine the desired frustum. w h fov 16
Orthogonal projections in OpenGL To specif an orthogonal projection in OpenGL there is just one function: glortho(min, ma, min, ma, near, far whose arguments are identical to those of glfrustum. The view volume is now a parallelepiped. We have set min = near and ma = far again in the figure. ( ma, ma, ma = ma View volume = min ( min, min, min Unlike with perspective projections, the arguments near and far no longer have to be positive. The onl restriction is that near < far. 17
Orthogonal projection matrices The projection matrices used in OpenGL must also take account of the clipping volume. We begin with the projection matri for the function glortho(min, ma, min, ma, near, far Set min = near, ma = far. All OpenGL needs to do is to map the chosen view volume into the canonical one: = ±1, = ±1, = ±1. ( ma, ma, ma (1,1,1 ( min, min, min (-1,-1,-1 This can be achieved b concatenating a translation and a scaling: ( T ( min + ma /2, ( min + ma /2, ( min + ma /2, ( S 2/( ma min, 2/( ma min, 2/( ma min, giving the projection matri P = ST = 2 ma min 0 0 min+ ma ma min 0 2 ma min 0 min+ ma ma min 0 0 2 ma min min+ ma ma min 0 0 0 1. 18
Perspective projection matrices We now find the projection matri in OpenGL specified b the function gluperspective(fov, aspect, near, far. Again set min = near, ma = far. Assume for simplicit that fov = 90 and aspect = 1: (-1, -1, -1 (1, 1, -1 From our previous discussion, the simple matri 1 0 0 0 0 1 0 0 M = 0 0 1 0 0 0 1 0 provides the correct perspective projectors but sends all points into the plane = 1. 19
We need a more general matri, 1 0 0 0 0 1 0 0 N =, 0 0 α β 0 0 1 0 in order to transform the front clipping plane = min into the canonical plane = 1 and the back clipping plane = ma into = 1. The matri N clearl maps the point (,,, 1 to the point (,,, w where =, =, = α + β, w =. After dividing b w, we get = /, = /, = (α + β/, which means that the planes = ± and = ± are transformed into the planes = ±1 and = ±1, for an α, β. On the other hand, the planes = min and = ma are mapped respectivel into the planes = (α + β/ min, = (α + β/ ma. So we have to solve the simultaneous equations (α + β/ min = 1, (α + β/ ma = 1, for α, β, in order to map the front clipping plane = min into the canonical plane = 1 and the back clipping plane = ma into = 1. The solution is α = ma + min ma min, β = 2 ma min ma min. 20