2. Graphics Pipeline
• The graphics pipeline or rendering pipeline refers to the sequence of steps
used to create a 2D raster representation of a 3D scene.
• Stages of the graphics pipeline:
• Modeling individual Objects using 3D geometric primitives: Traditionally done
using triangles, making a wire frame for an object.
• Modeling and transformation into world coordinates: Transform from the local
coordinate system to the 3d world coordinate system.
• Camera transformation: Transform the 3d world coordinate system into the 3d
camera coordinate system, with the camera as the origin.
• Lighting: Illuminate according to lighting and reflectance.
• Projection transformation: Transform the 3d world coordinates into the 2d view of
the camera.
• Clipping: Geometric primitives that now fall completely outside of the viewing
frustum will not be visible and are discarded at this stage.
• Scan conversion or rasterization: The process by which the 2D image space
representation of the scene is converted into raster format and the correct resulting
pixel values are determined. From now on, operations will be carried out on each
single pixel.
• Texturing, fragment shading: At this stage of the pipeline individual fragments are
assigned a color based on values interpolated from the vertices during rasterization,
from a texture in memory, or from a shader program.
3. Points
• How to draw points in processing?
• Coordinates of the screen
• top left corner is assigned as origin, going right increases in x to width-1
coordinate, and going down increases in y to the height-1
• The setup() function
• Creating a window using size(width, height)
• Drawing a point using point(x,y)
• Setting point size using strokeWeight(no. of pixels)
• Setting color using stroke(colorR, colorG, colorB)
• single color value for working with greyscale images, each argument has a
value from 0-255
• Changing background color using background()
• Once stroke or stroke weight is changed, it affects the primitives
used/written after this line of code and not to any of the previously
drawn.
• How to rasterize a line between two given points?
4. Scan Conversion
• ‘Scan conversion’/rasterization is the process of taking
geometric shapes and converting them into an array of
pixels stored in the framebuffer to be displayed
• Or converting from ‘random scan’/vector specification to a raster
scan where we specify which pixels are ‘ON’ based on a stored
array of pixels
• Takes place after clipping occurs
• All graphics packages do this at end of the rendering
pipeline
5. How does computer draw line?
• Screen made of pixels
• High-level language specifies line
• System must color pixels
• There should be
• No gaps in adjacent pixels
• Pixels close to ideal line
• Consistent choices; same pixels in same
situations
• Smooth looking
• Even brightness in all orientations
• Same line for as for P0, P1 and P1, P0
• Which pixels to choose?
6. Scan Convert a Line
• Finding the next pixel
Special cases:
• Horizontal Line:
• Draw pixel P and increment x coordinate value by 1 to get next pixel.
• Vertical Line:
• Draw pixel P and increment y coordinate value by 1 to get next pixel.
• Diagonal Line:
• Draw pixel P and increment both x and y coordinate by 1 to get next
pixel. Here |slope|=1
• What should we do in the general case?
• For |slope| < 1 (gentle slope), increment x coordinate by 1 and
choose pixel on or closest to line. For |slope| > 1 (steep slope),
increment y coordinate by 1 and choose pixel on or closest to line.
• But how do we measure “closest”?
7. Steep Slope vs. Gentle Slope
• So, choose which coordinate to increment and the point to
start from wisely
8. Strategy 1: Explicit Line Equation
• Slope-intercept form: y=mx+c, where
• Find out m and c for once
• For |m| <=1 find out no. of pixels to be shaded, for every x
find out the value for y
00 mxyb
x
y
xx
yy
m
end
end
0
0
9. Strategy 2: Parametric Form
• P(t)=(1-t)P0+tP1
where P(0)=P0 and P(1)=P1
• Putting t=0 gives the first point and putting t=1 gives the
second point, so, t varies from 0 to 1 i.e. 0<=t<=1
• The value of t represents the step size to be used.
• It is one unit of movement in the given direction
• Smaller t means smaller step.
• Smaller step means more steps are required to complete
the line
• More steps, i.e. smaller steps, is better resolution
• 1/t = number of “subdivisions” of the line
10. void DrawLine(Point p1, Point p2)
{
for(float t = 0.0; t <= 1.0; t += 0.005)
{
float newX = p1.X * (1.0-t) + p2.X * t;
float newY = p1.Y * (1.0-t) + p2.Y * t;
point(newX, newY);
}
}
Using t implies any resolution of the line is normalized between 0 and 1
Implementing Parametric Form
11. The previous two strategies
• Every value of (x,y) pair is independent of the calculations
performed for the previous pair
• Interpolate instead, do incremental calculations
• Expensive to implement since it uses floating point
multiplication for finding each value of y
• Get rid of expensive operations
12. Strategy 3: Incremental Algorithms
DDA – Digital Differential Analyzer Algorithm
(works for lines of both steep and gentle slopes)
1. Start with starting and ending coordinates of the line:
• (x0, y0) and (x1, y1)
2. Color first pixel (round to nearest integer)
3. Suppose x1-x0 > y1-y0 (gentle slope)
• numsteps=x1-x0 steps (# pixels to be colored)
• else there will be y1-y0 steps i.e. numsteps=y1-y0
4. Set x=x0, y=y0
5. Set stepX= (x1-x0)/numsteps; stepY= (y1-y0)/numsteps
6. At each step,
• increment x by stepX, and
• increment y by stepY
7. For each step, round off x and y to nearest integer, and color
pixel
13. DDA Example
• Suppose we want to
draw a line starting at
pixel (2,3) and ending
at pixel (12,8).
• What are the values of
the variables x and y at
each timestep?
• What are the pixels
colored, according to
the DDA algorithm?
numsteps = 12 – 2 = 10
xinc = 10/10 = 1.0
yinc = 5/10 = 0.5
t x y R(x) R(y)
0 2 3 2 3
1 3 3.5 3 4
2 4 4 4 4
3 5 4.5 5 5
4 6 5 6 5
5 7 5.5 7 6
6 8 6 8 6
7 9 6.5 9 7
8 10 7 10 7
9 11 7.5 11 8
10 12 8 12 8
14. DDAAlgorithm (continued)
• Advantage
• Does not calculate coordinates based on the complete equation
• Disadvantage
• Round-off errors are accumulated, thus line diverges more and
more from actual line
• Round-off operations take time and still uses floating point
additions, both of which are expensive