You are on page 1of 55

WUCS307

Computer Graphics

Lecture 4
Graphics Rendering Pipeline
There are three stages
• Application Stage
• Geometry Stage
• Rasterization Stage
Application Stage
• The main work in this stage include :
• Modeling
• Read data
• Interaction

11/24/2022 Lecture 1 3
Geometry Stage
 The geometry stage is responsible for the
majority of the per-polygon operations or per-
vertex operations. This stage is further divided
into the following functional stages
Modeling
Lighting Viewing
Transform

Screen
Clipping Projection
Mapping

To Rasterization Stage
4
( 1 ) Modeling Transformation
 Each model has a modeling coordinate. Originally, a
model resides in its own model space.
 Each model can be associated with a modeling
transform so that it can be positioned and oriented
(To construct a satisfied scene with models, some
times we have to do some adjustments, such as
translation, rotation and scaling, to these models).
 If we place a model into a position of the scene, all
11/24/2022 Lecture 1 5
the vertex coordinate of the model need to be
transformed into world coordinates (position relative
to coordinate origin of world
11/24/2022 Lecture 1 coordinates) 5
( 2 ) Viewing transformation
 Only the models that the camera (or observer) sees are
rendered.
 All models are transformed with the viewing
transformation.

 In the left, the camera is located and oriented as the user wants it
to be. The view transform relocates the camera at the origin
looking along the negative z-axis. This is done to make the clipping
and projection operations simpler and faster.
6
( 3 ) Projection Transformation
 We want to create a picture of the scene viewed from the
camera
 We project the vertices of objects to the projection plane

7
( 4 ) Clipping
 Only the primitives wholly or partially inside the view
volume need to be passed on to the rasterizer stage.

8
( 5 ) Screen Mapping
 The x- and y-coordinates of each primitive are
transformed to form screen coordinates.

11/24/2022 9
Lecture 1
Rasterization Stage
Converting the vertex information output by the
geometry pipeline into pixel information needed by
the video display. (the intensity information is
stored in frame buffer)
The electron beam moves across each row from
top to bottom of the screen. At each pixel, access
frame buffer, achieve the colour information of
current pixel, draw the colour.
11/24/2022 Lecture 1 10
Vector Images
• We can describe a picture as a set of complex objects,
positioned at specified coordinate locations within the
scene.
• A vector image is described by storing descriptions of
shapes, i.e. areas of color bounded by lines or curves with
no reference to any particular pixel grid.
• Vector images store the instructions for displaying the
image rather than the pixels needed to display it.
• The main advantage of vector images is that they are
resolution independent and display well on very high
resolution devices.
Vector Images
• The disadvantage is that they must be rasterized before
they can be displayed
• Vector images are often used for:
• text, diagrams, mechanical drawings, and other
applications where crispness and precision are
important and photographic images and complex
shading aren’t needed.
Chapter 4. Output Primitives
3.1 Concept of output primitives

Object can be described with set of basic geometric structures. Then the
scene is displayed by scan converting the geometric-structure specification
into pixel patterns (geometric objects are scan converted into a set of discrete
intensity points)
3.1 Concept of output primitives
• Graphics programming packages provide
functions to describe a scene in terms of these
basic geometric structures (including
coordinate data and other information),
referred to as output primitives.
• Points and line segments are the simplest
primitives. They can construct more complex
primitives, such as circles or surfaces.
Output Primitives
• The basic objects from which graphics display is created are
called primitives.
• Graphics programming packages provide functions to
describe a scene in terms of basic geometric structures,
referred to as output primitives
• Examples include: points, line segments, spline curves,
quadric surfaces.
• Computer display devices need special procedures for
displaying any graphic object: line, circle, and curves.
Output Primitives
• The process in which the object is represented as the
collection of discrete pixels is called scan conversion.
• Almost any model can be reproduced with a sufficiently
dense matrix of dots (pointillism)
• Many algorithms have been developed to provide users
with fast and efficient routines to generate higher-level
from the primitives.
• However, regardless of what routines are developed,
the computer can produce images on raster devices
only by turning the appropriate pixels on or off.
Output Primitives: Point
• A pixel (picture element) is the basic unit of
programmable color on the computer display/image
• Each pixel on the display surface has a finite size
depending on the screen resolution, therefore a pixel is
represented as a coordinate
• Since a pixel is collection of number of points, it does not
represent any mathematical point.
• This means that we wish to represent a point, the pixel at
that position is illuminated, i.e.
Output Primitives: Lines
• A straight line may be defined by two endpoints and an
equation.
• It is obtained by tracing the path between two
coordinates along a given point.
• The equation of the line is used to describe the (x, y)
coordinates of all the points that lie between these two
endpoints:
Output Primitives: Lines
• Line drawing is accomplished by calculating intermediate
positions along the line path between two specified
endpoint positions.
• An output device is then directed to fill in these
positions between the endpoints.
Line Drawing Algorithms
• The Cartesian slope-intercept equation for a straight line
is
• Given a set of endpointsand the slope can be computed
as:
• The y-intercept:
• Algorithms for displaying straight lines are based on the
line equation and the calculations.
3.1 Concept of output primitives
Primitives of OpenGL

GL_POINTS GL_LINES GL_LINE_STRIP GL_LINE_LOOP

GL_TRIANGLES GL_QUADS GL_POLYGON GL_TRIANGLE_FAN

GL_TRIANGLE_STRIP GL_QUAD_STRIP
3.2 Point Plotting
• Idea: addressing the memory unit in frame buffer according
to point coordinate(x, y) , then complete reading/writing
operation.
• Pixel addressing
• Display resolution
• Color depth
• Coordinates
• Pixels storage orders
Figure 6-10 Pixel screen positions stored linearly in row-major order within the
frame buffer.

addr(x,y) = addr(0,0)+ (y*(Xmax+1)+x)*color_depth


Shadow Mask
01001011 D/A
N
8

10101100 D/A

8
绿
00001010 D/A
寄存器 红
彩色电子枪
帧缓冲存储器 CRT 光

3.2 Point Plotting

• The plotting of other output primitives is based on point


Point Plotting with OpenGL
glBegin (GL_POINTS); glPointSize (size)

glVertex2f (0.0, 0.0);

glVertex2f (0.0, 3.0);

glEnd ();
3.3 Line-Drawing Algorithm

• Problem

• DDA Algorithm

• Bresenham Algorithm

• Line Drawing with OpenGL


3.3.1 Problem of Line Drawing
Any object can be scan
converted into a set of
discrete intensity points

Eg. (10,21.51) -> (10,22)

 The
Scankey point ofa line
converting linesegment
drawing algorithm
means we haveistohow tothe
locate
calculate
pixel theclosest
positions intermediate
to the linepoints between
path and store thetwo
intensity
endpoints
of each position and reduce
in frame stairstep appearing
buffer.
 Digital devices display a straight line segment by plotting
discrete points between the two endpoints.
 Screen locations are referenced with integer values. However,
the calculation results are not always are. The rounding of
coordinate values may lead to a stairstep appearance.
• A straight line can be expressed by slope-intercept
equation,
According to equation (2), we know
y = y=
m*x +m*b x (1)
where m represents
x = y/mthe slope of the line and b is the
y intercept
These equations form the basis for determining
deflection voltages in analog devices
 Assume two endpoints are P1(x1,y1) , P2(x2,y2)

m = (y2 - y1)/(x2 - x1) (2)


b = y1 - m*x1 (3)
• if |m|<1 , △ x can be set proportional to a small horizontal
deflection voltage and the corresponding vertical deflection
is set proportional to △y as calculated from
y= m* x (6-4)
• if |m|>1 , △ y can be set proportional to a small vertical
deflection voltage and the corresponding horizontal
deflection is set proportional to △x, calculated from
x = y/m (6-5)
Figure 6-3 Straight-line segment with five sampling positions along the x axis
between x0 and xend.

 on raster display systems,


lines are plotted with
pixels, and step size in
horizontal and vertical
directions are constrained
by pixel separation.
 That means we must
sample a line at discrete
positions and determine
the nearest pixel to the line
at each sampled position.
Direct use of straight line equation
• Given the gradient and any
interval, presume , we can compute
the corresponding interval as:
• Incrementing x simply solves y.
• While this method of displaying a
straight line is adequate for simple
graphics, graphics systems require a
much faster response than this.
Digital Differential Analyzer (DDA)
• DDA is a scan-conversion line algorithm base on
calculating either ∆x or ∆y.
• Where,
• The algorithm works on the principle of obtaining the
successive pixel values based on the differential equation
governing the line.
• Based on calculating either dy or dx.
DDA - Idea
1. Go to the starting end point
2. Increment x and y values by
constants proportional to x and
y such that one of them is 1.
3. Round to the closest raster
position
Line Drawing Algorithms: DDA
• As the slope of the line is a crucial factor in its
construction, there will be cases where depending on the
slope of the line the is >1 or <1.
• Case 1: slope (m) of line is <1 (less the one)
• In this case to plot the line we have to move the
direction of pixel in x by 1 unit every time and then
hunt for the pixel value of the y direction which best
suits the line and illuminate that pixel in order to plot
the line.
Line Drawing Algorithms: DDA
• Case 2: slope (m) of line is >1 (greater than one)
• The most appropriate strategy is to move towards the y
direction by 1 unit every time and determine the pixel
in x direction which best suits the line and get that pixel
illuminated to plot the line.
3.3.2 DDA Algorithm
Digital differential analyzer (DDA)
• Idea : sample the line at unit intervals in one
coordinate and determine corresponding integer
values nearest the line path for the other
coordinate by using y= m* x or x = y/m
Sample at unit x intervals
y
|m| ≤ 1
y2

y1

x1 x2 x
Straight line segment with seven sampling positions
along the x axis between x1 and x2.
if |m|≤1, we sample at unit x intervals (x=1) and
compute successive y values.
When the starting endpoint is at left
xk+1=xk+1
yk+1=yk+m
When the starting endpoint is at right
xk+1=xk-1
yk+1=yk-m
Sample at unit y intervals
y
|m|>1
y2

y1

x1 x2 x
if |m|>1, we sample at unit y intervals (y=1) and
compute consecutive x values
When the starting endpoint is at left
yk+1=yk+1
xk+1=xk+1/m
When the starting endpoint is at right
yk+1=yk-1
xk+1=xk-1/m
DDA Summary
• The DDA algorithm can be summarized as
if |m|≤1:xk+1=xk+1,yk+1=yk+m ;xa<xb
or : xk+1=xk-1,yk+1=yk-m ;xa>xb

if |m|≥1:yk+1=yk+1,xk+1=xk+1/m ;ya<yb
or :yk+1=yk-1,xk+1=xk-1/m ;ya>yb
Program Implementation
Program Implementation
#define ROUND(a) ((INT)(a+0.5))
void lineDDA(int xa, int ya, int xb, int yb)
{
int dx = xb-xa; dy = yb-ya, steps, k;
float x_in, y_in, x =xa, y=ya;
if (abs(dx) > abs(dy)) steps = abs(dx);
else steps = abs (dy);
x_in = dx / (float)steps;
Program Implementation

y_in = dy / (float)steps;
setpixel(ROUND(x), ROUND(y));
for (k=0; k < steps; k++) {
x += x_in;
y += y_in;
setPixel(ROUND(x), ROUND(y));
}
}
Example
• Assuming two endpoints P1(20,10), P2(30,18), calculate
the intermediate points by using DDA algorithm
x = x2-x1=30-20=10;
y = y2-y1=18-10=8;
m= y / x =0.8<1;
xk+1=xk+1
yk+1=yk+m
DDA example

Step k (x,y) (xi,yi)


20,10 20,10
0 21,10.8 21,11
1 22,11.6 22,12
2 23,12.4 23,12
3 24,13.2 24,13
4 25,14 25,14
DDA example

Step k (x,y) (xi,yi)


5 26,14.8 26,15
6 27,15.6 27,16
7 28,16.4 28,16
8 29,17.2 29,17
9 30,18 30,18
Example

18
17
16
15
14
13
12
11
10
20 21 22 23 24 25 26 27 28 29 30
Evaluation of DDA
• DDA is faster than the direct use of equation y=m*x+b. It
eliminates the multiplication
• The rounding operations and floating arithmetic are time-
consuming
• The accumulation of roundoff error can cause the calculated
pixel positions to drift away from the true line path
Exercise 1
• Assuming two endpoints P1(30,20), P2(40,27), calculate
the intermediate points by using DDA algorithm
x = x2-x1=40-30=10;
y = y2-y1=27-20=7;
m= y / x =0.7<1;
xk+1=xk+1
yk+1=yk+m
step k (x , y) pixel
(30, 20) (30, 20)
0 (31, 20.7) (31, 21)
1 (32, 21.4) (32, 21)
2 (33, 22.1) (33, 22)
3 (34, 22.8) (34, 23)
4 (35, 23.5) (35, 24)
5 (36, 24.2) (36, 24)
6 (37, 24.9) (37, 25)
7 (38, 25.6) (38, 26)
8 (39, 26.3) (39, 26)
9 (40, 27.0) (40, 27)
DDA Algorithm
• Advantages of DDA Algorithm
• It is a simple algorithm and it does not require special
skills for implementation.
• It is the fastest method for calculating pixel positions
than the equation of a pixel position.
• It makes use of raster characteristics
• Disadvantages of DDA algorithm
• Floating point arithmetic in DDA is time consuming
• The algorithm is orientation dependent, hence end-
point accuracy is poor.

You might also like