Implementation strategies
Implementation is a black box: whose inputs are
set of vertices and set of primitives.
Any implementation program must contain
atleast two loops that iterate over these sets .
The two strategies are
• image oriented approach
• object oriented approach
Object oriented approach
In object oriented approach, the outer loop is over
objects.
For (each_object) render(object);
In past with old memory device -Major limitations of this
approach were:
• large amount of memory required
• high cost of processing each object independently
Today with new memory device this object – based
system is fast and relatively inexpensive.
• The main limitation is they cannot handle most global
calculations like Shading effects and hidden surface
removal .
Image-Oriented approach
Image-Oriented approach loops over pixels or rows of
pixels called scanlines, that constitute the framebuffer.
For(each_pixel) assign_a_color(pixel);
For each pixel, we work backward, determining which
geometric primitives can contribute to its color.
The advantages are :
• we need only limited display memory at any time
• the refresh rate
• the order required to refresh
The main disadvantage is that unless we first build a
data structure from the geometric data, we will not know
which primitive affects which pixel.
Four Major Tasks
1. Modeling
2. Geometry processing
3. Rasterization
4. Fragment Processing
Modelling:
• The results of modelling process are the sets of
vertices that specify a group of geometric objects
supported by the rest of the system.
• The modeler may perform other tasks like clipping.
Four Major Tasks contd..
Geometry Processing:
The goals of the geometry processor are
• to determine which geometric objects can
appear on the display.
• to assign shades or colors to the vertices of
theses objects.
Four processes are required:
1. projection
2. primitive assembly
3. clipping
4. shading
Four Major Tasks contd..
Rasterization:
• rasterizer generates fragments with the process called
scan conversion or rasterization.
• The rasterizer starts with vertices in normalized
device coordinates but outputs fragments whose
locations are in uints of display—window coordinates.
Fragment processing:
Fragment processor processes the fragments generated
by the rasterizer in order to perform the following
• texturing
• translucent effects
• anti-aliasing
Clipping
It is the process of deciding which primitives or parts of
primitives fit within the clipping or view volume defined by the
application program.
Line-Segment Clipping:
• the primitives that fit within the specified view volume pass
through the clipper are called accepted primitives.
• Primitives that cannot appear on the display are eliminated
are called rejected or culled primitives.
Cohen -Sutherland Clipping
Algorithm
Cohen -Sutherland Clipping Algorithm
Cohen -Sutherland Clipping Algorithm
Cohen -Sutherland Clipping Algorithm
Cohen -Sutherland Clipping Algorithm
Cohen -Sutherland Clipping Algorithm
Cohen -Sutherland Clipping Algorithm
Cohen -Sutherland Clipping Algorithm
Cohen -Sutherland Clipping Algorithm
Liang-Barsky Clipping
⚫ Depending on the way we express line segments, we can usedifferent approaches for line segment clipping.
⚫ Suppose the end points of line segment are
p1 = [x1,y1]T and p2 = [x2, y2]T
⚫ Line in parametric (matrix) form
p(α)=(1-α)p1+α P
2
⚫ Line as two scalar equations
x(α)=(1-α)x1+α x
2
y(α)=(1-α)y1+α y
2
⚫ α varies from 0 to 1.
P1 P2
⚫ When α value is negative, we can move to other side of P1. When
α value is greater than 1, we will move to other side of P2.
Liang-Barsky Clipping
⚫ Here there are only 2 cases are discussed.
⚫ Case 1 : 1>4 > 3 > 2 > 1>0
Line segment between 3 and 2 need to be retained.
⚫ Case 2: 1>4 > 2 > 3 > 1 >0
Complete line segment need to be removed.
⚫ Number of intersection calculations will be minimized.
⚫ Advantages:
1. Can be easily extended to 3D
2. Using values of , we do not have to use algorithm recursively as with Cohen Sutherland
Liang-Basky Clipping in 3D
⚫ In 3D we need to add one more equation to scalarrepresentation of line
z(α)=(1-α)z1+α z
2
⚫ The major difference between 2D and 3D clipping is, in 2D wewill clip line segment against line of a window. In 3D we will clipline segment against plane of a window.
⚫ Intersection point can be calculated as
a=n⋅ ( po− p1)
n⋅ ( p2− p1)
Where n is the normal to the plane and p0 is a point on the plane.
Clipping in 3D
Polygon clipping
⚫ A polygon is a two-dimensional object with an interior.
⚫ Not as simple as line segment clipping.
⚫ Clipping a line segment yields at most one line segment
⚫ Clipping a polygon can yield multiple polygons
⚫ However, clipping a convex polygon can yield at most one otherpolygon
⚫ One strategy is to replace nonconvex (concave) polygons with aset of triangular polygons (a tessellation)
⚫ Line segment clipper as a black box
⚫ Clipping line segment against top side of the window
⚫ Inputs: two endpoints and ymax
⚫ Clipping against each side of window is independent of
other sides
⚫ Can use four independent clippers in a pipeline
⚫ Pipeline clipping of polygon
⚫ Three dimensions: add front and back clippers
Clipping of other primitives
⚫ Bounding Boxes
⚫ Rather than doing clipping on a complex polygon, we can use an axis-aligned bounding box or extent
⚫ Smallest rectangle aligned with axes that encloses the polygon
⚫ Simple to compute: max and min of x and y
⚫ Can usually determine accept/reject based only on bounding box
reject
accept
requires detailed
clipping
Curves, Surfaces and Text
⚫ For simple curves, we can find intersection points and perform clipping similar to line segments.
⚫ For complex curves, finding how many number of intersections to be calculated is also difficult. So complex curves need to be approximated as line segments and surfaces for clipping.
⚫ For clipping text need to represented either as bit pattern or as any other geometric object.
⚫ Clipping can be done in frame buffer through a technique
called scissoring.
Rasterization
⚫ DDA algorithm – Digital Differential Analyzer
⚫ Simplest scan conversion algorithm for line segments
⚫ In this algorithm represent the line segment in the form of differential equation
dy/dx = m
where m is the slope.
⚫ Suppose we have line segment defined by end points (x1,y1) and (x2,y2). Then slope m can be calculated as
m=y2-y1/x2-x1 = δy/δx
⚫ Assumption : 0<=m<=1
⚫
⚫ The algorithm is based on updating each pixel as we move
from x1 to x2.
⚫ If we are on the line segment, for any change in x equal to δ x,
the corresponding change in y must be
δ y = m δ x
⚫ As we move from x1 to x2, we increase x by 1 each iteration,
thus, we must increase y by
δ y = m.
⚫ Although each x is an integer, each y is not, because m is a
floating point number and we must round it to find the
appropriate pixel. The corresponding pseudocode is
for(ix=x1; ix <= x2; ix++)
{
y+=m;
write_pixel(x, round(y) , line_color);
}
⚫ Where round is a function that rounds a real number to an integer.
⚫ For slopes greater than 1, the roles of x and y need to be swapped. Then the algorithm is
For each y, find the best x
Bresenham’s algorithm
⚫ The DDA algorithm is easy to code but requires floating point addition for each pixel generated.
⚫ Bresenham’s algorithm avoids all floating point calculations.
⚫ Assumption : 0<=m <=1
⚫ Suppose that we are somewhere in the middle of the scanconversion I.e at (i+1/2,j+1/2).
⚫ To identify next pixel we have two options depending on valueof slope they are (i+3/2,j+1/2) and (i+3/2, j+3/2).
⚫ To consider one, we will use d=b-a.
⚫ If d is negative, the line passes closer to the lower pixel, so we
choose the pixel at (i + 3/2, j + ½). otherwise, we choose the
pixel at (i+3/2,j+3/2).
Polygon Rasterization
1. Inside outside testing
a. odd – even test :
odd
even
2. Winding number
⚫ Traversing the edges of the polygon from any starting vertexand going around the edge in a particular direction (whichdirection does not matter) until we reach the starting point.
⚫ The winding number for this point is the number of times it isencircled by the edges of the polygon.
⚫ A point is inside the polygon if its winding number is not zero.
Rasterization of concave polygons
⚫ For rasterization concave polygon need to converted to convexpolygon called tesselation.
⚫ GLU library has tesselator
Fill and Sort
⚫ The basic rule for filling a polygon is, If a point is inside thepolygon, color it with the inside (fill) color.
⚫ This conceptual algorithm indicates that polygon fill is a sortingproblem, where we sort all the pixels in the frame buffer intothose that are inside the polygon and those that are not.
⚫ From this perspective, we have different polygon-fill algorithmsusing different ways of sorting the points.
1. Flood fill
2. Scan line fill
3. odd – even fill
Flood fill
⚫ Here we will consider two colors, foreground color andbackground color.
⚫ We can use foreground color to raster polygon edges usingBresenham’s algorithm.
⚫ Then we need to find an initial point (x, y) inside the polygon
called a seed point then we can look at its neighbors
recursively, coloring them with the foreground color if they are
not edge points.
⚫ The corresponding pseudocode is
Singularities
⚫ Count the intersection of the scanline with the vertex as
either zero or two edge crossings.
⚫ The vertex-scanline intersection must be counted as one
edge crossing.
Hidden Surface Removal
object space and image space approaches:
⚫ In object space approach, consider two polygons, then there are four possibilities.
⚫ Consider k objects, compare them pairwise and render visiblepart.
⚫ This need to be repeated until all objects are compared with allother k-1 objects.
⚫ The complexity of this calculation is O(k2).
⚫ In image space approach consider a ray emanating fromcenter of projection,passes through a pixel and intersects with
any of the objects in consideration.
⚫ Find the intersection closer to center of projection.
⚫ The fundamental operation is intersection of rays with polygon.
⚫ For an n x m display, we have to carry out this operation nmktimes, giving 0(k) complexity.
Sorting and Hidden surface removal
⚫ Any method that involves brute-force comparison of objects bypairs has 0(k2) complexity.
⚫ If we could organize objects by their distances from thecamera, we should be able to come up with a direct method ofrendering them.
1. Scanline algorithms
Y-x algorithm
Back Face Removal
⚫ In back face removal, all back faces of a polygon which are notvisible to user are removed. This process is also known asculling.
⚫ We can see the face of a polygon if the normal, which comesout of the polygon face, is pointed toward the viewer. If theta isthe angle between the normal and the viewer, then the polygonis facing forward if and only if
-90<theta<90
or
cos theta>0
⚫ The other way to check second condition is
n-v>0
⚫ If we assume that the polygon is on a surface
ax+by+cz+d=0
Then depending on sign of c, we can determine whether face of apolygon is back face or front face.
⚫ If the sign of c is positive, then corresponding polygon face is
front face.
⚫ In OpenGL we have a built-in function called glCullFace toeliminate back face of a polygon.
⚫ The algorithm is based on computing the area of the polygon.Consider the polygon with n vertices. Its area is given by
1/2 Σ(yi+1
+yi)(x
i+1-x
i)
⚫ A negative area indicates back face of a polygon.
Z buffer algorithm
⚫ It is the most widely used hidden surface removal algorithm
⚫ Advantages
1. It is easy to implement in both hardware and software
2. It is compatible with all graphics pipeline architectures
⚫ It is based on image space approach.
⚫ Initialize depth buffer to maximum z coordinate value and colorbuffer to background color.
⚫ Generate a ray from center of projection through pixel to thepolygons.
⚫ When the ray intersects with the polygon, update the colorbuffer pixel to the color of the polygon and depth buffer pixel tothe depth of the polygon. Repeat the same process for eachpolygon and pixel.
⚫ Compare the depth of the polygon to the value in the z- buffercorresponding to this fragment. If this depth is greater than thedepth in the z-buffer, then we have already processed apolygon with a corresponding fragment closer to the viewer,and this fragment is not visible.
⚫ If the depth is less than the depth in the z-buffer, then we havefound a fragment closer to the viewer.
⚫ We update the depth in the z-buffer and place the shadecomputed for this fragment at the corresponding location in thecolor buffer.
Depth sort and painter’s algorithm
⚫ It is based on object space approach.
⚫ Depth sort is a variant of painter’s algorithm.
⚫ For example, in the above figure (a), we have two polygons. To a viewer, they appear as shown in (b), with the polygon in front partially obscuring the other.
⚫ There are two approaches to render these polygons.
1. We could find the part of the rear polygon that is visible, and could render that part into the frame buffer - a calculation that requires clipping one polygon against the other.
2. We could paint the rear polygon in its entirety, and then the front polygon, painting over the part of the rear polygon not visible to the viewer in the process.
⚫ We can also use painter’s algorithm to render these polygons.
⚫ The first step of this algorithm is t o order all the polygons byhow far away from the viewer their maximum z-value is. Thisstep gives the algorithm the name depthsort.
⚫ If the minimum depth—the z-value—of a given polygon isgreater than the maximum depth of the polygon behind theone of interest, we can paint the polygons back to front and weare done. For example in the above figure, polygon A is behindall other polygons.
⚫ If the z-extents of two polygons overlap, we still may be able tofind an order to paint (render) the polygons individually andyield the correct image.
⚫ Consider a pair of polygons whose z-extents overlap. Thesimplest test is to check their x and y-extents. If either of the xor y extents do not overlap, neither polygon can obscure theother, and they can be painted in either order.
⚫ Rasterized line segments and edges of polygons sometimes look jagged. Itis known as aliasing error.
⚫ There are mainly three reasons for aliasing errors
1. First, if we have an n x m frame buffer, the number of pixels is fixed, and wecan generate only certain patterns to approximate a line segment.
2. Pixel locations are fixed on a uniform grid.
3. Pixels have a fixed size and shape.These aliasing errors can be removed by aprocess called antialiasing.
⚫ Mathematical lines are one-dimensional entities that have, length but notwidth, rasterized lines must have a width in order to be visible.
⚫ Suppose that each pixel is displayed as a square of width 1 unit, and canoccupy a box of 1 unit height and width on the display.
Antialiasing