Acknowledgements To: RD TH

Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

Acknowledgements to

Donald Hearn & Pauline Baker: Computer Graphics with OpenGL

Version,3rd / 4th Edition, Pearson Education,2011

Edward Angel: Interactive Computer Graphics- A Top Down approach

with OpenGL, 5th edition. Pearson Education, 2008

M M Raiker, Computer Graphics using OpenGL, Filip learning/Elsevier


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

4 3D Viewing and Visible Surface Detection


4.1 3D viewing concepts,
4.2 3D viewing pipeline,
4.3 3D viewing coordinate parameters ,
4.4 Transformation from world to viewing coordinates,
4.5 Projection transformation,
4.6 Orthogonal projections,
4.7 Perspective projections,
4.8 The viewport transformation and 3D screen coordinates.
4.9 OpenGL 3D viewing functions.
Visible Surface Detection Methods:
4.10 Classification of visible surface Detection algorithms,
4.11Back face detection,
4.12 Depth buffer method and
4.13 OpenGL visibility detection functions.

Three-Dimensional Viewing
4.1 Overview of Three-Dimensional Viewing Concepts
 When we model a three-dimensional scene, each object in the scene is typically defined
with a set of surfaces that form a closed boundary around the object interior.
 In addition to procedures that generate views of the surface features of an object, graphics
packages sometimes provide routines for displaying internal components or cross-
sectional views of a solid object.
 Many processes in three-dimensional viewing, such as the clipping routines, are similar
to those in the two-dimensional viewing pipeline.
 But three-dimensional viewing involves some tasks that are not present in
twodimensional Viewing

Viewing a Three-Dimensional Scene


 To obtain a display of a three-dimensional world-coordinate scene, we first set up a
coordinate reference for the viewing, or “camera,” parameters.

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 1


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

 This coordinate reference defines the position and orientation for a view plane (or
projection plane) that corresponds to a camera film plane as shown in below figure.

Coordinate reference for obtaining a selected view of a three-dimensional scene.


 We can generate a view of an object on the output device in wireframe (outline) form, or
we can apply lighting and surface-rendering techniques to obtain a realistic shading of the
visible surfaces Projections
Two methods:
1. One method for getting the description of a solid object onto a view plane is to project
points on the object surface along parallel lines. This technique, called parallel projection

Three parallel-projection views of an object, showing relative proportions from different viewing
positions

2. Another method for generating a view of a three-dimensional scene is to project points to


the view plane along converging paths. This process, called a perspective projection,

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 2


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

causes objects farther from the viewing position to be displayed smaller than objects of
the same size that are nearer to the viewing position

Depth Cueing
 Depth information is important in a three-dimensional scene so that we can easily
identify, for a particular viewing direction, which is the front and which is the back of
each displayed object.
 There are several ways in which we can include depth information in the two-
dimensional representation of solid objects.
 A simple method for indicating depth with wire-frame displays is to vary the brightness
of line segments according to their distances from the viewing position which is termed
as depth cueing.

A wire-frame object displayed with depth


cueing, so that the brightness of lines decreases
from the front of the object to the back

 The lines closest to the viewing position are displayed with the highest intensity, and
lines farther away are displayed with decreasing intensities.
 Depth cueing is applied by choosing a maximum and a minimum intensity value and a
range of distances over which the intensity is to vary.
 Another application of depth cuing is modeling the effect of the atmosphere on the
perceived intensity of objects

Identifying Visible Lines and Surfaces


 One approach is simply to highlight the visible lines or to display them in a different
color. Another technique, commonly used for engineering drawings, is to display the

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 3


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

nonvisible lines as dashed lines. Or we could remove the nonvisible lines from the
display

Surface Rendering
 We set the lighting conditions by specifying the color and location of the light sources,
and we can also set background illumination effects.
 Surface properties of objects include whether a surface is transparent or opaque and
whether the surface is smooth or rough.
 We set values for parameters to model surfaces such as glass, plastic, wood-grain
patterns, and the bumpy appearance of an orange.

Exploded and Cutaway Views


 Exploded and cutaway views of such objects can then be used to show the internal
structure and relationship of the object parts.
 An alternative to exploding an object into its component parts is a cutaway view, which
removes part of the visible surfaces to show internal structure

Three-Dimensional and Stereoscopic Viewing


 Three-dimensional views can be obtained by reflecting a raster image from a vibrating,
flexible mirror.
 The vibrations of the mirror are synchronized with the display of the scene on the cathode
ray tube (CRT).
 As the mirror vibrates, the focal length varies so that each point in the scene is reflected
to a spatial position corresponding to its depth.
 Stereoscopic devices present two views of a scene: one for the left eye and the other for
the right eye.
 The viewing positions correspond to the eye positions of the viewer. These two views are
typically displayed on alternate refresh cycles of a raster monitor

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 4


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

4.2 The Three-Dimensional Viewing Pipeline


 First of all, we need to choose a viewing position corresponding to where we would place
a camera.
 We choose the viewing position according to whether we want to display a front, back,
side, top, or bottom view of the scene.
 We could also pick a position in the middle of a group of objects or even inside a single
object, such as a building or a molecule.
 Then we must decide on the camera orientation.
 Finally, when we snap the shutter, the scene is cropped to the size of a selected clipping
window, which corresponds to the aperture or lens type of a camera, and light from the
visible surfaces is projected onto the camera film.
 Some of the viewing operations for a three-dimensional scene are the same as, or similar
to, those used in the two-dimensional viewing pipeline.
 A two-dimensional viewport is used to position a projected view of the three dimensional
scene on the output device, and a two-dimensional clipping window is used to select a
view that is to be mapped to the viewport.
 Clipping windows, viewports, and display windows are usually specified as rectangles
with their edges parallel to the coordinate axes.
 The viewing position, view plane, clipping window, and clipping planes are all specified
within the viewing-coordinate reference frame.

 Figure above shows the general processing steps for creating and transforming a three-
dimensional scene to device coordinates.
 Once the scene has been modeled in world coordinates, a viewing-coordinate system is
selected and the description of the scene is converted to viewing coordinates

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 5


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

 A two-dimensional clipping window, corresponding to a selected camera lens, is defined


on the projection plane, and a three-dimensional clipping region is established.
 This clipping region is called the view volume.
 Projection operations are performed to convert the viewing-coordinate description of the
scene to coordinate positions on the projection plane.
 Objects are mapped to normalized coordinates, and all parts of the scene outside the view
volume are clipped off.
 The clipping operations can be applied after all device-independent coordinate
transformations.
 We will assume that the viewport is to be specified in device coordinates and that
normalized coordinates are transferred to viewport coordinates, following the clipping
operations.
 The final step is to map viewport coordinates to device coordinates within a selected
display window

4.3 Three-Dimensional Viewing-Coordinate Parameters


 Select a world-coordinate position P0 =(x0, y0, z0) for the viewing origin, which is called
the view point or viewing position and we specify a view-up vector V, which defines the
yview direction.
 Figure below illustrates the positioning of a three-dimensional viewing-coordinate frame
within a world system.

A right-handed viewing-coordinate system, with axes x view, y view, and z view, relative to a right-handed world-
coordinate frame.

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 6


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

The View-Plane Normal Vector


 Because the viewing direction is usually along the zview axis, the view plane, also called
the projection plane, is normally assumed to be perpendicular to this axis.
 Thus, the orientation of the view plane, as well as the direction for the positive zview
axis, can be definedwith a view-plane normal vector N,

 An additional scalar parameter is used to set the position of the view plane at some
coordinate value zvp along the zview axis,

 This parameter value is usually specified as a distance from the viewing origin along the
direction of viewing, which is often taken to be in the negative zview direction.
 Vector N can be specified in various ways. In some graphics systems, the direction for N
is defined to be along the line from the world-coordinate origin to a selected point
position.
 Other systems take N to be in the direction from a reference point Pref to the viewing
origin P0,

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 7


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

Specifying the view-plane normal vector N as the direction from a selected reference point Pref to the viewing-
coordinate origin P0.

The View-Up Vector


 Once we have chosen a view-plane normal vector N, we can set the direction for the
view-up vector V.
 This vector is used to establish the positive direction for the yview axis.
 Usually, V is defined by selecting a position relative to the world-coordinate origin, so
that the direction for the view-up vector is from the world origin to this selected position

 Because the view-plane normal vector N defines the direction for the zview axis, vector V
should be perpendicular to N.
 But, in general, it can be difficult to determine a direction for V that is precisely
perpendicular to N.
 Therefore, viewing routines typically adjust the user-defined orientation of vector V,

The uvn Viewing-Coordinate Reference Frame


 Left-handed viewing coordinates are sometimes used in graphics packages, with the
viewing direction in the positive zview direction.

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 8


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

 With a left-handed system, increasing zview values are interpreted as being farther from
the viewing position along the line of sight.
 But right-handed viewing systems are more common, because they have the same
orientation as the world-reference frame.
 Because the view-plane normal N defines the direction for the zview axis and the view-up
vector V is used to obtain the direction for the yview axis, we need only determine the
direction for the xview axis.
 Using the input values for N and V,we can compute a third vector, U, that is
perpendicular to both N and V.
 Vector U then defines the direction for the positive xview axis.
 We determine the correct direction for U by taking the vector cross product of V and N
so as to form a right-handed viewing frame.
 The vector cross product of N and U also produces the adjusted value for V,
perpendicular to both N and U, along the positive yview axis.
 Following these procedures, we obtain the following set of unit axis vectors for a right-
handed viewing coordinate system.

 The coordinate system formed with these unit vectors is often described as a uvn
viewing-coordinate reference frame

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 9


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

Generating Three-Dimensional Viewing Effects


 By varying the viewing parameters, we can obtain different views of objects in a scene.
 we could change the direction of N to display objects at positions around the viewing-
coordinate origin.
 We could also vary N to create a composite display consisting of multiple views from a
fixed camera position.
 In interactive applications, the normal vectorNis the viewing parameter that is most often
changed. Of course,when we change the direction for N,we also have to change the other
axis vectors to maintain a right-handed viewing-coordinate system.
 If we want to simulate an animation panning effect, as when a camera moves through a
scene or follows an object that is moving through a scene, we can keep the direction for
N fixed as we move the view point,

 Alternatively, different views of an object or group of objects can be generated using


geometric transformations without changing the viewing parameters

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 10


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

4.4 Transformation from World to Viewing Coordinates


 In the three-dimensional viewing pipeline, the first step after a scene has been constructed
is to transfer object descriptions to the viewing-coordinate reference frame.
 This conversion of object descriptions is equivalent to a sequence of transformations that
superimposes the viewing reference frame onto the world frame
1. Translate the viewing-coordinate origin to the origin of the worldcoordinate
system.
2. Apply rotations to align the xview, yview, and zview axes with the world xw, yw,
and zw axes, respectively.
 The viewing-coordinate origin is at world position P0 = (x0, y0, z0). Therefore, the matrix
for translating the viewing origin to the world origin is

 For the rotation transformation, we can use the unit vectors u, v, and n to form the
composite rotation matrix that superimposes the viewing axes onto the world frame. This
transformation matrix is

where the elements of matrix R are the components of the uvn axis vectors.
 The coordinate transformation matrix is then obtained as the product of the preceding
translation and rotation matrices:

 Translation factors in this matrix are calculated as the vector dot product of each of the u,
v, and n unit vectors with P0, which represents a vector from the world origin to the
viewing origin.

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 11


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

 These matrix elements are evaluated as

4.5 Projection Transformations


 Graphics packages generally support both parallel and perspective projections.
 In a parallel projection, coordinate positions are transferred to the view plane along
parallel lines.
 A parallel projection preserves relative proportions of objects, and this is the method used
in computeraided drafting and design to produce scale drawings of three-dimensional
objects.
 All parallel lines in a scene are displayed as parallel when viewed with a parallel
projection.
 There are two general methods for obtaining a parallel-projection view of an object: We
can project along lines that are perpendicular to the view plane, or we can project at an
oblique angle to the view plane

 For a perspective projection, object positions are transformed to projection coordinates


along lines that converge to a point behind the view plane.
 Unlike a parallel projection, a perspective projection does not preserve relative
proportions of objects.
 But perspective views of a scene are more realistic because distant objects in the
projected display are reduced in size.

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 12


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

4.6 Orthogonal Projections


 A transformation of object descriptions to a view plane along lines that are all parallel to
the view-plane normal vector N is called an orthogonal projection also termed as
orthographic projection.
 This produces a parallel-projection transformation in which the projection lines are
perpendicular to the view plane.
 Orthogonal projections are most often used to produce the front, side, and top views of an
object

 Front, side, and rear orthogonal projections of an object are called elevations; and a top
orthogonal projection is called a plan view

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 13


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

Axonometric and Isometric Orthogonal Projections


 We can also form orthogonal projections that display more than one face of an object.
Such views are called axonometric orthogonal projections.
 The most commonly used axonometric projection is the isometric projection, which is
generated by aligning the projection plane (or the object) so that the plane intersects each
coordinate axis in which the object is defined, called the principal axes, at the same
distance from the origin

Orthogonal Projection Coordinates


 With the projection direction parallel to the zview axis, the transformation equations for an
orthogonal projection are trivial. For any position (x, y, z) in viewing coordinates, as in
Figure below, the projection coordinates are xp = x, yp = y

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 14


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

Clipping Window and Orthogonal-Projection View Volume


 In OpenGL, we set up a clipping window for three-dimensional viewing just as we did
for two-dimensional viewing, by choosing two-dimensional coordinate positions for its
lower-left and upper-right corners.
 For three-dimensional viewing, the clipping window is positioned on the view plane with
its edges parallel to the xview and yview axes, as shown in Figure below . If we want to use
some other shape or orientation for the clipping window, we must develop our own
viewing procedures

 The edges of the clipping window specify the x and y limits for the part of the scene that
we want to display.
 These limits are used to form the top, bottom, and two sides of a clipping region called
the orthogonal-projection view volume.
 Because projection lines are perpendicular to the view plane, these four boundaries are
planes that are also perpendicular to the view plane and that pass through the edges of the
clipping window to form an infinite clipping region, as in Figure below.

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 15


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

 These two planes are called the near-far clipping planes, or the front-back clipping
planes.
 The near and far planes allow us to exclude objects that are in front of or behind the part
of the scene that we want to display.
 When the near and far planes are specified, we obtain a finite orthogonal view volume
that is a rectangular parallelepiped, as shown in Figure below along with one possible
placement for the view plane

Normalization Transformation for an Orthogonal Projection


 Once we have established the limits for the view volume, coordinate descriptions inside
this rectangular parallelepiped are the projection coordinates, and they can be mapped
into a normalized view volume without any further projection processing.
 Some graphics packages use a unit cube for this normalized view volume, with each of
the x, y, and z coordinates normalized in the range from 0 to 1.
 Another normalization-transformation approach is to use a symmetric cube, with
coordinates in the range from −1 to 1

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 16


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

 We can convert projection coordinates into positions within a left-handed normalized-


coordinate reference frame, and these coordinate positions will then be transferred to
lefthanded screen coordinates by the viewport transformation.
 To illustrate the normalization transformation, we assume that the orthogonal-projection
view volume is to be mapped into the symmetric normalization cube within a left-handed
reference frame.
 Also, z-coordinate positions for the near and far planes are denoted as znear and zfar,
respectively. Figure below illustrates this normalization transformation

 The normalization transformation for the orthogonal view volume is

4.7 Perspective Projections


 We can approximate this geometric-optics effect by projecting objects to the view plane
along converging paths to a position called the projection reference point (or center of
projection).

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 17


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

 Objects are then displayed with foreshortening effects, and projections of distant objects
are smaller than the projections of objects of the same size that are closer to the view
plane

Perspective-Projection Transformation Coordinates


 Figure below shows the projection path of a spatial position (x, y, z) to a general
projection reference point at (xprp, yprp, zprp).

 The projection line intersects the view plane at the coordinate position (xp, yp, zvp), where
zvp is some selected position for the view plane on the zview axis.
 We can write equations describing coordinate positions along this perspective-projection
line in parametric form as

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 18


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

 On the view plane, z’ = zvp and we can solve the z’ equation for parameter u at this
position along the projection line:

 Substituting this value of u into the equations for x’ and y’, we obtain the general
perspective-transformation equations

Perspective-Projection Equations: Special Cases


Case 1:
 To simplify the perspective calculations, the projection reference point could be limited
to positions along the zview axis, then
xprp = yprp = 0:

Case 2:
 Sometimes the projection reference point is fixed at the coordinate origin, and
(xprp, yprp, zprp) = (0, 0, 0) :

Case 3:
 If the view plane is the uv plane and there are no restrictions on the placement of the
projection reference point, then we have
zvp = 0:

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 19


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

Case 4:
 With the uv plane as the view plane and the projection reference point on the zview axis,
the perspective equations are
xprp = yprp = zvp = 0:

 The view plane is usually placed between the projection reference point and the scene,
but, in general, the view plane could be placed anywhere except at the projection point.
 If the projection reference point is between the view plane and the scene, objects are
inverted on the view plane (refer below figure)

 Perspective effects also depend on the distance between the projection reference point
and the view plane, as illustrated in Figure below.

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 20


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

 If the projection reference point is close to the view plane, perspective effects are
emphasized; that is, closer objects will appearmuchlarger thanmore distant objects of the
same size.
 Similarly, as the projection reference point moves farther from the view plane, the
difference in the size of near and far objects decreases

Vanishing Points for Perspective Projections


 The point at which a set of projected parallel lines appears to converge is called a
vanishing point.
 Each set of projected parallel lines has a separate vanishing point.
 For a set of lines that are parallel to one of the principal axes of an object, the vanishing
point is referred to as a principal vanishing point.
 We control the number of principal vanishing points (one, two, or three) with the
orientation of the projection plane, and perspective projections are accordingly classified
as one-point, two-point, or three-point projections

Principal vanishing points for


perspective-projection views of a cube.
When the cube in (a) is projected to a
view plane that intersects only the
z axis, a single vanishing point in the z
direction (b) is generated. When the
cube is projected to a view plane that
intersects both the z and x axes, two
vanishing points (c) are produced.
Perspective-Projection View Volume

 A perspective-projection view volume is often referred to as a pyramid of vision because


it approximates the cone of vision of our eyes or a camera.

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 21


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

 The displayed view of a scene includes only those objects within the pyramid, just as we
cannot see objects beyond our peripheral vision, which are outside the cone of vision.
 By adding near and far clipping planes that are perpendicular to the zview axis (and
parallel to the view plane), we chop off parts of the infinite, perspectiveprojection view
volume to form a truncated pyramid, or frustum, view volume

 But with a perspective projection, we could also use the near clipping plane to take out
large objects close to the view plane that could project into unrecognizable shapes within
the clipping window.
 Similarly, the far clipping plane could be used to cut out objects far from the projection
reference point that might project to small blots on the view plane.

Perspective-Projection Transformation Matrix


 We can use a three-dimensional, homogeneous-coordinate representation to express the
perspective-projection equations in the form

where the homogeneous parameter has the value


h = zprp – z

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 22


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

 The perspective-projection transformation of a viewing-coordinate position is then


accomplished in two steps.
 First, we calculate the homogeneous coordinates using the perspective-transformation
matrix:

Where,
Ph is the column-matrix representation of the homogeneous point (xh, yh, zh, h) and
P is the column-matrix representation of the coordinate position (x, y, z, 1).
 Second, after other processes have been applied, such as the normalization transformation
and clipping routines, homogeneous coordinates are divided by parameter h to obtain the
true transformation-coordinate positions.
 The following matrix gives one possible way to formulate a perspective-projection
matrix.

 Parameters sz and tz are the scaling and translation factors for normalizing the projected
values of z-coordinates.
 Specific values for sz and tz depend on the normalization range we select.

Symmetric Perspective-Projection Frustum


 The line from the projection reference point through the center of the clipping window
and on through the view volume is the centerline for a perspectiveprojection frustum.
 If this centerline is perpendicular to the view plane, we have a symmetric frustum (with
respect to its centerline)

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 23


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

 Because the frustum centerline intersects the view plane at the coordinate location (xprp,
yprp, zvp), we can express the corner positions for the clipping window in terms of the
window dimensions:

 Another way to specify a symmetric perspective projection is to use parameters that


approximate the properties of a camera lens.
 A photograph is produced with a symmetric perspective projection of a scene onto the
film plane.
 Reflected light rays from the objects in a scene are collected on the film plane from
within the “cone of vision” of the camera.
 This cone of vision can be referenced with a field-of-view angle, which is a measure of
the size of the camera lens.
 A large field-of-view angle, for example, corresponds to a wide-angle lens.
 In computer graphics, the cone of vision is approximated with a symmetric frustum, and
we can use a field-of-view angle to specify an angular size for the frustum.

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 24


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

 For a given projection reference point and view-plane position, the field-of view angle
determines the height of the clipping window from the right triangles in the diagram of
Figure below, we see that

 so that the clipping-window height can be calculated as

 Therefore, the diagonal elements with the value zprp −zvp could be replaced by either of
the following two expressions

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 25


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

Oblique Perspective-Projection Frustum


 If the centerline of a perspective-projection view volume is not perpendicular to the view
plane, we have an oblique frustum

 In this case, we can first transform the view volume to a symmetric frustum and then to a
normalized view volume.
 An oblique perspective-projection view volume can be converted to a sym metric frustum
by applying a z-axis shearing-transformation matrix.
 This transformation shifts all positions on any plane that is perpendicular to the z axis by
an amount that is proportional to the distance of the plane from a specified z- axis
reference position.
 The computations for the shearing transformation, as well as for the perspective and
normalization transformations, are greatly reduced if we take the projection reference
point to be the viewing-coordinate origin.

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 26


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

 Taking the projection reference point as (xprp, yprp, zprp) = (0, 0, 0), we obtain the elements
of the required shearing matrix as

 We need to choose values for the shearing parameters such that

 Therefore, the parameters for this shearing transformation are

 Similarly, with the projection reference point at the viewing-coordinate origin and with
the near clipping plane as the view plane, the perspective-projection matrix is simplified
to

 Concatenating the simplified perspective-projection matrix with the shear matrix we have

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 27


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

Normalized Perspective-Projection Transformation Coordinates


 When we divide the homogeneous coordinates by the homogeneous parameter h, we
obtain the actual projection coordinates, which are orthogonal-projection coordinates
 The final step in the perspective transformation process is to map this parallelepiped to a
normalized view volume.
 The transformed frustum view volume, which is a rectangular parallelepiped, is mapped
to a symmetric normalized cube within a left-handed reference frame

 Because the centerline of the rectangular parallelepiped view volume is now the zview
axis, no translation is needed in the x and y normalization transformations: We require
only the x and y scaling parameters relative to the coordinate origin.
 The scaling matrix for accomplishing the xy normalization is

 Concatenating the xy-scalingmatrix produces the following normalization matrix for a


perspective-projection transformation.

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 28


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

 From this transformation, we obtain the homogeneous coordinates:

And the projection coordinates are

 To normalize this perspective transformation, we want the projection coordinates to be


(xp, yp, zp) = (−1, −1, −1) when the input coordinates are (x, y, z) = (xwmin, ywmin, znear), and
we want the projection coordinates to be (xp, yp, zp) = (1, 1, 1) when the input coordinates
are (x, y, z) = (xwmax, ywmax, zfar).

 And the elements of the normalized transformation matrix for a general perspective-
projection are

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 29


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

4.8 The Viewport Transformation and Three-Dimensional Screen


Coordinates
 Once we have completed the transformation to normalized projection coordinates,
clipping can be applied efficiently to the symmetric cube then the contents of the
normalized view volume can be transferred to screen coordinates.
 Positions throughout the three-dimensional view volume also have a depth (z coordinate),
and we need to retain this depth information for the visibility testing and surface-
rendering algorithms
 If we include this z renormalization, the transformation from the normalized view volume
to three dimensional screen coordinates is

 In normalized coordinates, the znorm = −1 face of the symmetric cube corresponds to the
clipping-window area. And this face of the normalized cube is mapped to the rectangular
viewport, which is now referenced at zscreen = 0.
 Thus, the lower-left corner of the viewport screen area is at position (xvmin, yvmin, 0) and
the upper-right corner is at position (xvmax, yvmax, 0).

4.9 OpenGL Three-Dimensional Viewing Functions


OpenGL Viewing-Transformation Function

glMatrixMode (GL_MODELVIEW);
 a matrix is formed and concatenated with the current modelview matrix, We set the
modelview mode with the statement above

gluLookAt (x0, y0, z0, xref, yref, zref, Vx, Vy, Vz);
 Viewing parameters are specified with the above GLU function.

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 30


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

 This function designates the origin of the viewing reference frame as the world-
coordinate position P0 = (x0, y0, z0), the reference position as Pref =(xref, yref, zref), and the
view-up vector as V = (Vx, Vy, Vz).
 If we do not invoke the gluLookAt function, the default OpenGL viewing parameters are
P0 = (0, 0, 0)
Pref = (0, 0, −1)
V = (0, 1, 0)

OpenGL Orthogonal-Projection Function


glMatrixMode (GL_PROJECTION);
 set up a projection-transformation matrix.
 Then, when we issue any transformation command, the resulting matrix will be
concatenated with the current projection matrix.

glOrtho (xwmin, xwmax, ywmin, ywmax, dnear, dfar);


 Orthogonal-projection parameters are chosen with the function
 All parameter values in this function are to be assigned double-precision, floating point
Numbers
 Function glOrtho generates a parallel projection that is perpendicular to the view plane
 Parameters dnear and dfar denote distances in the negative zview direction from the viewing-
coordinate origin
 We can assign any values (positive, negative, or zero) to these parameters, so long as
dnear<dfar.
 Exa: glOrtho (-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 31


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

OpenGL General Perspective-Projection Function


glFrustum (xwmin, xwmax, ywmin, ywmax, dnear, dfar);
 specify a perspective projection that has either a symmetric frustum view volume or an
oblique frustum view volume
 All parameters in this function are assigned double-precision, floating-point numbers.
 The first four parameters set the coordinates for the clipping window on the near plane,
and the last two parameters specify the distances from the coordinate origin to the near
and far clipping planes along the negative zview axis.

OpenGL Viewports and Display Windows


glViewport (xvmin, yvmin, vpWidth, vpHeight);
 A rectangular viewport is defined.
 The first two parameters in this function specify the integer screen position of the lower-
left corner of the viewport relative to the lower-left corner of the display window.
 And the last two parameters give the integer width and height of the viewport.
 To maintain the proportions of objects in a scene, we set the aspect ratio of the viewport
equal to the aspect ratio of the clipping window.
 Display windows are created and managed with GLUT routines. The default viewport in
OpenGL is the size and position of the current display window

OpenGL Three-Dimensional Viewing Program Example

#include <GL/glut.h>
GLint winWidth = 600, winHeight = 600; // Initial display-window size.
GLfloat x0 = 100.0, y0 = 50.0, z0 = 50.0; // Viewing-coordinate origin.
GLfloat xref = 50.0, yref = 50.0, zref = 0.0; // Look-at point.
GLfloat Vx = 0.0, Vy = 1.0, Vz = 0.0; // View-up vector.
/* Set coordinate limits for the clipping window: */
GLfloat xwMin = -40.0, ywMin = -60.0, xwMax = 40.0, ywMax = 60.0;
/* Set positions for near and far clipping planes: */
GLfloat dnear = 25.0, dfar = 125.0;

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 32


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

void init (void)


{
glClearColor (1.0, 1.0, 1.0, 0.0);
glMatrixMode (GL_MODELVIEW);
gluLookAt (x0, y0, z0, xref, yref, zref, Vx, Vy, Vz);
glMatrixMode (GL_PROJECTION);
glFrustum (xwMin, xwMax, ywMin, ywMax, dnear, dfar);
}
void displayFcn (void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 1.0, 0.0); // Set fill color to green.
glPolygonMode (GL_FRONT, GL_FILL);
glPolygonMode (GL_BACK, GL_LINE); // Wire-frame back face.
glBegin (GL_QUADS);
glVertex3f (0.0, 0.0, 0.0);
glVertex3f (100.0, 0.0, 0.0);
glVertex3f (100.0, 100.0, 0.0);
glVertex3f (0.0, 100.0, 0.0);
glEnd ( );
glFlush ( );
}
void reshapeFcn (GLint newWidth, GLint newHeight)
{
glViewport (0, 0, newWidth, newHeight);
winWidth = newWidth;
winHeight = newHeight;
}
void main (int argc, char** argv)
{
glutInit (&argc, argv);

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 33


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);


glutInitWindowPosition (50, 50);
glutInitWindowSize (winWidth, winHeight);
glutCreateWindow ("Perspective View of A Square");
init ( );
glutDisplayFunc (displayFcn);
glutReshapeFunc (reshapeFcn);
glutMainLoop ( );
}

Visible-Surface Detection Methods


4.10 Classification of Visible-Surface Detection Algorithms
 We can broadly classify visible-surface detection algorithms according to whether they
deal with the object definitions or with their projected images.
 Object-space methods: compares objects and parts of objects to each other within the
scene definition to determine which surfaces, as a whole, we should label as visible.
 Image-space methods: visibility is decided point by point at each pixel position on the
projection plane.
 Although there are major differences in the basic approaches taken by the various visible-
surface detection algorithms, most use sorting and coherence methods to improve
performance.
 Sorting is used to facilitate depth comparisons by ordering the individual surfaces in a
scene according to their distance from the view plane.
 Coherence methods are used to take advantage of regularities in a scene.

4.11 Back-Face Detection


 A fast and simple object-space method for locating the back faces of a polyhedron is
based on front-back tests. A point (x, y, z) is behind a polygon surface if
Ax + By + Cz + D < 0
where A, B,C, and Dare the plane parameters for the polygon

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 34


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

 We can simplify the back-face test by considering the direction of the normal vector N
for a polygon surface. If Vview is a vector in the viewing direction from our camera
position, as shown in Figure below, then a polygon is a back face if
Vview . N > 0

 In a right-handed viewing system with the viewing direction along the negative zv axis
(Figure below), a polygon is a back face if the z component, C, of its normal vector N
satisfies C < 0.
 Also, we cannot see any face whose normal has z component C = 0, because our viewing
direction is grazing that polygon. Thus, in general, we can label any polygon as a back
face if its normal vector has a z component value that satisfies the inequality
C <=0

 Similar methods can be used in packages that employ a left-handed viewing system. In
these packages, plane parameters A, B, C, and D can be calculated from polygon vertex
coordinates specified in a clockwise direction.
 Inequality 1 then remains a valid test for points behind the polygon.
 By examining parameter C for the different plane surfaces describing an object, we can
immediately identify all the back faces.
 For other objects, such as the concave polyhedron in Figure below, more tests must be
carried out to determine whether there are additional faces that are totally or partially
obscured by other faces

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 35


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

 In general, back-face removal can be expected to eliminate about half of the polygon
surfaces in a scene from further visibility tests.

4.12 Depth-Buffer Method


 A commonly used image-space approach for detecting visible surfaces is the depth-buffer
method, which compares surface depth values throughout a scene for each pixel position
on the projection plane.
 The algorithm is usually applied to scenes containing only polygon surfaces, because
depth values can be computed very quickly and the method is easy to implement.
 This visibility-detectionapproach is also frequently alluded to as the z-buffer method,
because object depth is usually measured along the z axis of a viewing system

Figure above shows three surfaces at varying distances along the orthographic projection line
from position (x, y) on a view plane.

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 36


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

 These surfaces can be processed in any order.


 If a surface is closer than any previously processed surfaces, its surface color is
calculated and saved, along with its depth.
 The visible surfaces in a scene are represented by the set of surface colors that have been
saved after all surface processing is completed
 As implied by the name of this method, two buffer areas are required. A depth buffer is
used to store depth values for each (x, y) position as surfaces are processed, and the frame
buffer stores the surface-color values for each pixel position.

Depth-Buffer Algorithm
1. Initialize the depth buffer and frame buffer so that for all buffer positions (x, y),
depthBuff (x, y) = 1.0, frameBuff (x, y) = backgndColor
2. Process each polygon in a scene, one at a time, as follows:
• For each projected (x, y) pixel position of a polygon, calculate the depth z (if not already
known).
• If z < depthBuff (x, y), compute the surface color at that position and set
depthBuff (x, y) = z, frameBuff (x, y) = surfColor (x, y)
After all surfaces have been processed, the depth buffer contains depth values for the visible
surfaces and the frame buffer contains the corresponding color values for those surfaces.

 Given the depth values for the vertex positions of any polygon in a scene, we can
calculate the depth at any other point on the plane containing the polygon.
 At surface position (x, y), the depth is calculated from the plane equation as

 If the depth of position (x, y) has been determined to be z, then the depth z’ of the next
position (x + 1, y) along the scan line is obtained as

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 37


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

 The ratio −A/C is constant for each surface, so succeeding depth values across a scan line
are obtained from preceding values with a single addition.
 We can implement the depth-buffer algorithm by starting at a top vertex of the polygon.
 Then, we could recursively calculate the x-coordinate values down a left edge of the
polygon.
 The x value for the beginning position on each scan line can be calculated from the
beginning (edge) x value of the previous scan line as

where m is the slope of the edge (Figure below).

 Depth values down this edge are obtained recursively as

 If we are processing down a vertical edge, the slope is infinite and the recursive
calculations reduce to

 One slight complication with this approach is that while pixel positions are at integer (x,
y) coordinates, the actual point of intersection of a scan line with the edge of a polygon
may not be.
 As a result, it may be necessary to adjust the intersection point by rounding its fractional
part up or down, as is done in scan-line polygon fill algorithms.
 An alternative approach is to use a midpoint method or Bresenham-type algorithm for
determining the starting x values along edges for each scan line.

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 38


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

 The method can be applied to curved surfaces by determining depth and color values at
each surface projection point.
 In addition, the basic depth-buffer algorithm often performs needless calculations.
 Objects are processed in an arbitrary order, so that a color can be computed for a surface
point that is later replaced by a closer surface.

4.13 OpenGL Visibility-Detection Functions


OpenGL Polygon-Culling Functions
 Back-face removal is accomplished with the functions
glEnable (GL_CULL_FACE);
glCullFace (mode);
 where parameter mode is assigned the value GL_BACK, GL_FRONT,
GL_FRONT_AND_BACK
 By default, parameter mode in the glCullFace function has the value GL_BACK
 The culling routine is turned off with
glDisable (GL_CULL_FACE);

OpenGL Depth-Buffer Functions


 To use the OpenGL depth-buffer visibility-detection routines, we first need to modify the
GL Utility Toolkit (GLUT) initialization function for the display mode to include a
request for the depth buffer, as well as for the refresh buffer
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
 Depth buffer values can then be initialized with
glClear (GL_DEPTH_BUFFER_BIT);
 the preceding initialization sets all depth-buffer values to the maximum
value 1.0 by default
 The OpenGL depth-buffer visibility-detection routines are activated with the following
function:
glEnable (GL_DEPTH_TEST);
And we deactivate the depth-buffer routines with
glDisable (GL_DEPTH_TEST);

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 39


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

 We can also apply depth-buffer visibility testing using some other initial value for
themaximumdepth, and this initial value is chosen with theOpenGLfunction:
glClearDepth (maxDepth);
 Parameter maxDepth can be set to any value between 0.0 and 1.0.
 Projection coordinates in OpenGL are normalized to the range from −1.0
to 1.0, and the depth values between the near and far clipping planes are
further normalized to the range from 0.0 to 1.0.
 As an option, we can adjust these normalization values with
glDepthRange (nearNormDepth, farNormDepth);
 By default, nearNormDepth = 0.0 and farNormDepth = 1.0.
 But with the glDepthRange function, we can set these two parameters to
any values within the range from 0.0 to 1.0, including nearNormDepth >
farNormDepth
 Another option available in OpenGL is the test condition that is to be used for the depth-
buffer routines.We specify a test condition with the following function:
glDepthFunc (testCondition);
o Parameter testCondition can be assigned any one of the following eight symbolic
constants: GL_LESS, GL_GREATER, GL_EQUAL, GL_NOTEQUAL,
GL_LEQUAL, GL_GEQUAL, GL_NEVER (no points are processed), and
GL_ALWAYS.
o The default value for parameter testCondition is GL_LESS.
 We can also set the status of the depth buffer so that it is in a read-only state or in a read-
write state. This is accomplished with
glDepthMask (writeStatus);
o When writeStatus = GL_TRUE (the default value), we can both read from and
write to the depth buffer.
o With writeStatus = GL_FALSE, the write mode for the depth buffer is disabled
and we can retrieve values only for comparison in depth testing.

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 40


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

OpenGL Wire-Frame Surface-Visibility Methods


 A wire-frame display of a standard graphics object can be obtained in OpenGL by
requesting that only its edges are to be generated.
 We do this by setting the polygon-mode function as, for example:
glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
But this displays both visible and hidden edges
glEnable (GL_DEPTH_TEST);
glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
glColor3f (1.0, 1.0, 1.0);
/* Invoke the object-description routine. */
glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
glEnable (GL_POLYGON_OFFSET_FILL);
glPolygonOffset (1.0, 1.0);
glColor3f (0.0, 0.0, 0.0);
/* Invoke the object-description routine again. */
glDisable (GL_POLYGON_OFFSET_FILL);

OpenGL Depth-Cueing Function


 We can vary the brightness of an object as a function of its distance from the viewing
position with
glEnable (GL_FOG);
glFogi (GL_FOG_MODE, GL_ LINEAR);
This applies the linear depth function to object colors using dmin = 0.0 and dmax = 1.0. But we can
set different values for dmin and dmax with the following function calls:
glFogf (GL_FOG_START, minDepth);
glFogf (GL_FOG_END, maxDepth);
 In these two functions, parameters minDepth and maxDepth are assigned floating-point
values, although integer values can be used if we change the function suffix to i.
 We can use the glFog function to set an atmosphere color that is to be combined with the
color of an object after applying the linear depthcueing function

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 41


Module 4 ***SAI RAM*** 3D Viewing and Visible surface detection

Difference Between perspective projection and parallel projection


Perspective projection Parallel projection
The center of projection is at a finite distance Center of projection at infinity results with a
from the viewing plane parallel projection
Explicitly specify: center of projection Direction of projection is specified
Size of the object is inversely proportional to No change in the size of object
the distance of the object from the center of
projection
Produces realistic views but does not preserve A parallel projection oreserves relative
relative proportion of objects proportion of objects, but does not give us a
reaistic representation of the appearance of
object.
Not useful for recording exact shape and Used for exact measurement
measurements of the object
Parallel lines do not in general project as Parallel lines do remain parallel
parallel

Mr. Sukruth Gowda M A, Dept., of CSE, SVIT 42

You might also like