Anda di halaman 1dari 2

Outline: From world to screen

Window to viewport transformations

Window to viewport transformations.

The objects and primitives represented in the application model


will be stored in world coordinates.

How OpenGL does it.


Usually in terms of logical units for whatever the objects
represent.

Introduction to rasterisation or scan conversion.


Scan converting lines.

To display the appropriate images on the screen (or other


devices) it is necessary to map from world coordinates to screen
or device coordinates.
This transformation is known as the window to viewport
transformation.

Window to viewport transformations

Viewport

Window to viewport transformations

Window

World coordinates

Screen coordinates

World coordinates

Think of it as the window (on the world) to viewport (on


screen) transformation.

Window to viewport transformations


Non uniform scalings result in the world coordinate window and
viewport having different aspect ratios.

World coordinates

translation

World coordinates

The transformation will be given by:


a translation;

1 0 xmin

Tx = 0 1 ymin ;
0 0
1
a scaling:
u
max umin
xmaxxmin
Sxu =
0

translation

Screen coordinates

scaling

Screen coordinates

Screen coordinates

translation

Window to viewport transformations

World coordinates

Screen coordinates

In general the window to viewport transformation will involve a


scaling and translation.

The screen window (that is the viewport) typically covers only


part of the screen.
Generally the region will be clipped in world coordinates and
then transformed.

x
scaling

0
0

vmax vmin
ymax ymin

0
;
1

and finally another translation;

translation

1 0 umin

Tu = 0 1 vmin .
0 0
1

Window to viewport transformations


Combination, Mxu = TuSxuTx =
u
max umin
xmaxxmin
Mxu =
0

0
vmax vmin
ymax ymin

3D Viewport transformation
This is similar to the 2D case.

max umin
xmin uxmax
xmin + umin
.
max vmin
ymin yvmax
ymin + vmin

Note the inverted order of transformation Tx will be applied


first.
Use of homogeneous coordinates has allowed a single
transformation matrix to be written.

The objects transformed by the projection will then be


transformed into the viewport coordinates using the following
matrices:

1 0 0 xvmin
0 1 0 yvmin

0 0 0 z
vmin
0 0 0
1

vmax

xvmin
2

0
0
0

0
yvmax yvmin
2

0
0

0
0
zvmax zvmin
1


0
1
0
0

0 0
0
1

0
1
0
0

0
0
1
0

1
1
.
1
1

To plot the resulting 2D object we divide by w and then simply


ignore the z coordinate and plot the x and y coordinates.

Window to viewport transformations OpenGL


In OpenGL GLUT sets up the viewport (i.e. screen window)
position and size, OpenGL defines the mapping:

Basic Raster Algorithms for 2D Graphics


The conversion: primitives pixmap is scan conversion.

glutInitWindowSize(400,200);
glutInitWindowPosition(100,150);
glViewport(0, 0, 400, 200);
The window (on the world) is set by the projection matrix in
2D we have:

The most simple scan conversion task, is scan converting a line.


Theoretically, any scan conversion algorithm should produce a line with

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,400.0,0.0,200.0);

constant brightness (independent of orientation and length) and do it quickly.


It should also be able to cope with lines greater than one pixel in width and
be able to display different styles and attributes.
It is also necessary to minimise the jaggies on the line, by using anti-aliasing
methods to set pixel intensities.

Scan converting lines

Scan converting lines

Assume that pixels are disjoint circles on an integer, (x, y) grid.

Note that yi+1 = mxi+1 + c = m(xi + x) + c = yi + mx.

Line starts and ends at integer coordinates (x0, y0) and (xe, ye).

x = 1 gives:

The simplest algorithm to scan convert the line is an


incremental one:
Compute the slope m = y/x,

xi+1 = xi + 1 ,
yi+1 = yi + m .
More efficient algorithm, which works so long as |m| < 1.

Start at the leftmost point and increment x by 1,


Calculate yi = mxi + c,

If this is not the case, it is necessary to swap x and y, which will


give a slope of 1/m.

Set the intensity at the pixel value (xi, Round(yi)).


Inefficient, works only for |m| < 1.

Scan converting lines


void Line ( int x0, int xe, int y0, int ye, int value)
{
/* Assumes -1 <= m <= 1, x0 < xe */
int x;
float y,dx,dy,m;
dx = xe - x0;
dy = ye - y0;
m = dy / dx;
y = y0;
for (x = x0; x <= xe; x++){
WritePixel(x,( int) floor(y + 0.5),value);
y += m;
}
}

The algorithm is referred to as the digital differential analyser.


One potential problem with the method arises because the float
m has a finite precision.

It is also necessary to check for the special conditions of


horizontal, vertical and diagonal lines.

Summary
Having finished this lecture you should:
be able to write down the transformation matrices for the
window to viewport transformation;
know how OpenGL implements the window to viewport
transformation;
know what scan conversion means;
be able to describe two methods for scan converting lines and
their relative efficiency.

Anda mungkin juga menyukai