CS430 Computer Graphics - Winona State University
Download
Report
Transcript CS430 Computer Graphics - Winona State University
CS430
Computer Graphics
Rendering Pipeline and
Primitive Rasterization
Chi-Cheng Lin, Winona State University
Topics
Introduction
Viewport Transformation
Primitive Rasterization
Primitive Clipping
2
Introduction
Scan conversion
Convert specification of a primitive into
pixels in the frame buffer
Simple rendering pipeline
Model
Viewport
Transformation
Scan
Conversion
Pixels on
display
3
Viewport Transformation
Problem
When we define a 2D model, we want to use
whatever coordinate values and units
convenient to use in the 2D Cartesian system.
BUT, the coordinate values on the display (or
in the screen window) are always nonnegative
measured in pixels.
ALSO, we might want to show different parts
of our model in different places on the display
(or in the screen window)
Solution: viewport transformation
4
World Window and Viewport
World coordinate
Space in which objects are described
World window
A rectangle area specifies which part of the
“world” should be drawn
Viewport
A rectangle area specifies which part on
the display (or in the window) the world
window is mapped to
5
World Window and Viewport
Example
6
Window-to-Viewport Mapping
(x, y) in world window should be mapped
to (sx, sy) in viewport proportionally
distortion is possible
7
Window-to-Viewport Mapping
Every vertex used in window to define
an object should be mapped to viewport
(sx,sy)
(x,y)
x - W.l
sx - V.l
y - W.b
sy - V.b
8
Window-to-Viewport Mapping
sx V .l
x W .l
V .r V .l
V .r V .l
sx
x (V .l
W .l )
V .r V .l W .r W .l
W .r W .l
W .r W .l
Similarly,
sy V .b
y W .b
V .t V .b
V .t V .b
sy
y (V .b
W .b)
V .t V .b W .t W .b
W .t W .b
W .t W .b
T herefore,
sx A x C
and
sy B y D
where
V .r V .l
A
, C V .l A W .l
W .r W .l
V .t V .b
B
, D V .b B W .b
W .t W .b
9
How does OpenGL do it?
Set up world window
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(left, right, bottom, top);
We can write a function setWindow()
setWindow(GLdouble left, GLdouble right,
GLdouble bottom, GLdouble top)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(left, right, bottom, top);
}
10
How does OpenGL do it?
Set up viewport
glViewport(left, bottom, width, height);
or
glViewport(left, bottom, right-left, top-bottom);
Default viewport = screen window size
Parameters to set up a window are doubles
while those to set up a viewport are ints.
(Why?)
11
Example: Tiling with Motif
setWindow(0,640.0,0,440.0); // set a fixed window
for (int i=0; i<5; i++) // for each column
for (int j=0; j<5; j++) { // for each row
glViewport(i*64, j*44, 64, 44); // set the next viewport
drawPolylineFile(“dino.dat ”); // draw it again
}
12
Example: Tiling with Motif
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++) {
if ((i+j)%2 = 1) // if (i+j) is odd
setWindow(0.0, 640.0, 0.0, 440.0); // right-side-up window
else
setWindow(0.0, 640.0, 440.0, 0.0); // upside-down window
glViewport(i*64, j*44, 64, 44); // set the next viewport
drawPolylineFile("dino.dat"); // draw it again
}
13
Zooming and Panning
Zooming
Scale the world window with respect to the
center of the world window with viewport
fixed
Panning
Shift the world window with viewport fixed
14
Mapping without Distortion
To prevent from distortion, a viewport
with the same aspect ratio of world
window is required.
Aspect ratio: width/height
How do you draw the largest
undistorted picture in the screen
window?
15
Mapping without Distortion
If aspect ratio of world window = R
Two cases
glViewport(0,0,W,W/R);
glViewport(0,0,H*R,H);
16
Resize Screen Window
glutReshapeFunc(myReshape)
Prototype of myReshape
void myReshape(GLsizei W, Glsizei H);
W and H: new width and height
Making a matched viewport
void myReshape(GLsizei W,GLsizei H)
{
if (R >W/H) //use (global)window aspect ratio
setViewport(0, W, 0, W/R);
else
setViewport(0, H*R, 0, H);
}
17
Primitives Rasterization
Line
Brute force
DDA
Midpoint algorithm
Circle
Brute force
Polar coordinates
Midpoint algorithm
Polyline and polygon
18
Primitive Clipping
Problem
Not everything defined in the world will be
included in the world window
Solution
Clipping
Line clipping
Polygon clipping
OpenGL does clipping for you
19