Chapter Four- Windows and Viewports and Clipping Algorithms
Chapter Four- Windows and Viewports and Clipping Algorithms
• In the last chapter we learned how we can use matrices to our advantage by
transforming all vertices with transformation matrices.
• What we usually do, is specify the coordinates in a range (or space) we determine
• World space
• Clip space
• Screen space
Those are all a different state at which our vertices will be transformed in before
finally ending up as fragments.
• To transform the coordinates from one space to the next coordinate space we'll use
several transformation matrices of which the most important are the model, view
and projection matrix.
• Our vertex coordinates first start in local space as local coordinates and are then
further processed to world coordinates, view coordinates, clip coordinates and
eventually end up as screen coordinates.
• The following image displays the process and shows what each transformation
does:
For example, when modifying your object it makes most sense to do this in local
space, while calculating certain operations on the object with respect to the
position of other objects makes most sense in world coordinates and so on.
1. Local coordinates are the coordinates of your object relative to its local origin;
they're the coordinates your object begins in.
• Local space is the coordinate space that is local to your object, i.e. where your
object begins in.
• The origin of your cube is probably at (0,0,0) even though your cube may end
up at a different location in your final application.
• Probably all the models you've created all have (0,0,0) as their initial position.
• All the vertices of your model are therefore in local space: they are all local to
your object.
World space
• If we would import all our objects directly in the application they would probably
all be somewhere positioned inside each other at the world's origin of (0,0,0)
which is not what we want.
• We want to define a position for each object to position them inside a larger
world.
• The coordinates in world space are exactly what they sound like: the coordinates
of all your vertices relative to a (game) world.
World space
• This is the coordinate space where you want your objects transformed to in such a
way that they're all scattered around the place (preferably in a realistic fashion).
• The coordinates of your object are transformed from local to world space; this is
accomplished with the model matrix.
• The model matrix is a transformation matrix that translates, scales
and/or rotates your object to place it in the world at a
location/orientation they belong to.
• Think of it as transforming a house by scaling it down (it was a bit too large in
local space), translating it to a suburbia town and rotating it a bit to the left on
the y-axis so that it neatly fits with the neighboring houses.
• You could think of the matrix in the previous chapter to position the container
all over the scene as a sort of model matrix as well; we transformed the local
coordinates of the container to some different place in the scene/world.
View space
• The view space is what people usually refer to as the camera of
• The view space is thus the space as seen from the camera's point of
view.
View space
• This is usually accomplished with a combination of translations and
rotations to translate/rotate the scene so that certain items are
transformed to the front of the camera.
• At the end of each vertex shader run, OpenGL expects the coordinates to be
within a specific range and any coordinate that falls outside this range is
clipped.
• Coordinates that are clipped are discarded, so the remaining coordinates will
• Because specifying all the visible coordinates to be within the range -1.0 and
1.0 isn't really intuitive, we specify our own coordinate set to work in and
• All coordinates outside this range will not be mapped between -1.0
and 1.0 and therefore be clipped.
• With this range we specified in the projection matrix, a
coordinate of (1250, 500, 750) would not be visible, since the
x coordinate is out of range and thus gets converted to a
coordinate higher than 1.0 in NDC and is therefore clipped.
• Note that if only a part of a primitive e.g. a triangle is outside
the clipping volume OpenGL will reconstruct the triangle as one
or more triangles to fit inside the clipping range.
• The projection matrix to transform view coordinates to clip
coordinates usually takes two different forms, where each form defines
its own unique frustum.
• All the coordinates inside this frustum will end up within the NDC
range after transformed by its matrix and thus won't be clipped.
• This is exactly the effect perspective projection tries to mimic and it does so using
a perspective projection matrix.
• The projection matrix maps a given frustum range to clip space, but also
manipulates the w value of each vertex coordinate in such a way that the further
away a vertex coordinate is from the viewer, the higher this w component
becomes.
• Once the coordinates are transformed to clip space they are in the range -w to w
(anything outside this range is clipped).
• OpenGL requires that the visible coordinates fall between the range -1.0 and 1.0
as the final vertex shader output, thus once the coordinates are in clip space,
• Viewport: Viewport refers to the display area on the window (screen), which is
measured in pixels in screen coordinates (excluding the title bar).
• The clipping area is mapped to the viewport. We can use glViewport function to
configure the viewport.
• The x and y parameters specify the lower-left corner of the viewport within the
window, and the width and height parameters specify these dimensions in pixels.
Example: the following code will divide the screen into
three viewports each with its own clipping area.
#include<iostream.h> glColor3f( 0, 0, 1 );
#include <GL/glut.h> glPushMatrix(); // Sets up the PROJECTION matrix
float angle =-20; glRotatef(-30, 0.0f, 0.0f, 1.0f); glMatrixMode(GL_PROJECTION);
void timer(int value) glRectf(0.0,0.0,10.0,30.0); glLoadIdentity();
{ glPopMatrix(); gluOrtho2D(0.0,50.0,-10.0,40.0); // also sets up world window
angle-=10; glColor3f(0,0,0); // Draw RED rectangle
if(angle>180) glColor3f(0,0,0);
glLineWidth(10);
angle-=20; glLineWidth(10);
glutPostRedisplay(); glBegin(GL_LINES);
glVertex2f(0,40); glBegin(GL_LINES);
glutTimerFunc(1000,timer,0); glVertex2f(0,40);
} glVertex2f(50,40);
void draw(){ glVertex2f(50,40); glVertex2f(50,40);
// Make background colour yellow glVertex2f(50,-10); glVertex2f(50,40);
glClearColor( 100, 100, 0, 0 ); glVertex2f(50,-10); glVertex2f(50,-10);
glClear (GL_COLOR_BUFFER_BIT ); glVertex2f(0,-10); glVertex2f(50,-10);
// Sets up FIRST viewport spanning the left-bottom quarter of the interface window glVertex2f(0,-10);
glVertex2f(0,-10);
glViewport(0,0,250,250); glVertex2f(0,-10);
glVertex2f(0,40);
// Sets up the PROJECTION matrix glVertex2f(0,40);
glMatrixMode(GL_PROJECTION);
glEnd();
glEnd();
glLoadIdentity(); glViewport(250,250,250,250);
glColor3f( 1, 0, 0 );
gluOrtho2D(0.0,50.0,-10.0,40.0); // also sets up world window glRectf(0.0,0.0,10.0,30.0);
// Draw BLUE rectangle
// display rectangles
glViewport(500,500,250,250);
glMatrixMode(GL_PROJECTION);
int main(int argc, char ** argv)
glLoadIdentity(); {
gluOrtho2D(0.0,50.0,-10.0,40.0); glutInit(&argc, argv);
glColor3f(0,0,0);
glPushMatrix(); // Set window size
glRotatef(angle, 0.0f, 0.0f, 1.0f); glutInitWindowSize( 750,750 );
glRectf(0.0,0.0,10.0,30.0);
glPopMatrix();
glutCreateWindow("Three viewports ");
glLineWidth(10); glutDisplayFunc(draw);
glBegin(GL_LINES); glutTimerFunc(1000,timer,0);
glVertex2f(0,40);
glVertex2f(50,40); glutMainLoop();
glVertex2f(50,40); return 0;
glVertex2f(50,-10); }
glVertex2f(50,-10);
glVertex2f(0,-10);
glVertex2f(0,-10);
glVertex2f(0,40);
glEnd();
glFlush();
}
Zooming and panning effects with
windows
#include<iostream>
and viewports
#include<GL/glut.h>
float zoomFactor = 1.1;
float l,w,x,y; if( button == GLUT_RIGHT_BUTTON )
void mouse( int button, int state, int mx, int my ) {
{ zoomFactor-=0.02;
if( button == GLUT_LEFT_BUTTON) }
{ glutPostRedisplay();
zoomFactor+=0.02;} }
//if(state == GLUT_DOWN && button ==
GLUT_LEFT_BUTTON)
void display()
{
void keyPress(int key,int x,int y) glClear( GL_COLOR_BUFFER_BIT );
{ void myinit() glMatrixMode(GL_MODELVIEW );
{ glPushMatrix();
if(key==27) glClearColor(0.0,0.0,0.0,0.0); glScalef(zoomFactor, zoomFactor, 0.0f);
exit(0); glColor3f(1.0,0.0,0.0); glColor3f( 1, 0, 0 );
if (key == GLUT_KEY_UP) } glBegin( GL_TRIANGLES );
zoomFactor += .05; glVertex2f( 0.5, 0 );
if (key == GLUT_KEY_DOWN) glVertex2f( 0.0, 0.5 );
zoomFactor -= .05; glVertex2f( 0.0, 0 );
glVertex2f( 0, 0 );
glutPostRedisplay(); glVertex2f( -0.5, 0.0);
glVertex2f( 0.0, -0.5 );
} glEnd();
glPopMatrix();
glFlush();
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitWindowSize( 600, 600 );
glutCreateWindow( "GLUT" );
glutDisplayFunc( display );
myinit();
glutMouseFunc( mouse );
glutSpecialFunc(keyPress);
glutMainLoop();
return 0;
}
2D Clipping
1. Introduction:
A scene is made up of a collection of objects specified in world coordinates
World Coordinates
Window
wymax
wymin
Window
wymax
wymin
wxmin wxmax
• Examples
• A multi view window system
• The design of page layouts in advertising or publishing applications or for adding labels or
design patterns to picture.
• Combining graphs, maps o schematics
Window
wymax
wymin
Window P2
wymax
P6
P3
P1
P7 P5
P9
P8
wymin
P10
a. Analytical clipping
• Clip it before you scan convert it
• used mostly for lines, rectangles, and polygons, where clipping algorithms are
simple and efficient
b. Scissoring
• Clip it during scan conversion
• a brute force technique
• scan convert the primitive, only write pixels if inside the clipping region
• easy for thick and filled primitives as part of scan line fill
• if primitive is not much larger than clip region, most pixels will fall inside
• can be more efficient than analytical clipping.
c. Raster Clipping
• Clip it after scan conversion
• render everything onto a temporary canvas and copy the clipping region
• wasteful, but simple and easy,
• often used for text
Window P2
wxmin ≤ x ≤ wxmax wymax
Clipped
P5
& P7
P1
Points Within the Window are Not Clipped
wymin ≤ y ≤ wymax P9
P8
wymin
wxmin wxmax
window or not
• Both endpoints inside, line trivially
accepted
• One in and one out, line is partially
inside ymin
xmin xmax
display.
0000
3 2 1 0
0001 0010
above below right left Window
P11 [1010]
P4 [1000]
Window
wymax
P6 [0000]
P3 [0001]
P7 [0001]
P9 [0000]
P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110]
wxmin wxmax
P11 [1010]
P4 [1000]
Window
wymax
P6 [0000]
P3 [0001]
P7 [0001]
P9 [0000]
P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110]
Window
wymax
P6 [0000]
P3 [0001]
P7 [0001]
P9 [0000]
P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110]
P11 [1010]
P4 [1000]
Window
wymax
P6 [0000]
P3 [0001]
P7 [0001]
P9 [0000]
P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110]
P9 [0000] P8 [0010]
wymin
bit 0 = 1 if x - wxmin ≤ 0 P10 [0100]
P13 [0101] P14 [0110]
wxmin wxmax
boundary wymin
P10’ [0000]
P8’ [0000]
P8 [0010]
calculate the
intersection point to
generate P7’ wxmin wxmax
P8’ [0000]
the window so is
retained
wxmin wxmax
Window
wymax
wymin
wxmin wxmax
May 28, 2025 Computer Graphics 73
Cohen-Sutherland Line Clipping
Mid-Point Subdivision Method
• Integer Version
• Fast as Division by 2 can be performed by simple shift right operation
• For NxN max dimension of line number of subdivisions required log2 N.
• Thus a 1024x1024 raster display require just 10 subdivisions………
NOTE!
Hodgman
turns up
again. This
by comparing it against each Gary Hodgman with whom he worked at the first
Original Area Clip Left Clip Right Clip Top Clip Bottom
1. Basic Concept:
• Simplify via separation
• Clip whole polygon against one edge
• Repeat with output for other 3 edges
• Similar for 3D
• You can create intermediate vertices that get thrown out
• Example
Note that the point one of the points added when clipping
2. Algorithm:
Let (P1, P2,…. PN) be the vertex list of the Polygon to be clipped and E be the edge
of +vely oriented, convex clipping window.
We clip each edge of the polygon in turn against each window edge E, forming a
new polygon whose vertices are determined as follows:
Four cases
1. Inside: If both Pi-1 and Pi are to the left of window edge vertex then Pi is placed on the
output vertex list.
2. Entering: If Pi-1 is to the right of window edge and Pi is to the left of window edge
vertex then intersection (I) of Pi-1 Pi with edge E and Pi are placed on the output vertex
list.
3. Leaving: If Pi-1 is to the left of window edge and Pi is to the right of window edge
vertex then only intersection (I) of Pi-1 Pi with edge E is placed on the output vertex list.
4. Outside: If both Pi-1 and Pi are to the right of window edge nothing is placed on the
output vertex list.
Pi-1
Pi
Pi
Pi-1
Pi Pi-1
Pi
Pi-1
in in
out in in out out out
save ending vert save new clip vert save new clip vert save nothing
(2 outputs)
May 28, 2025 Computer Graphics 85
Sutherland-Hodgman Polygon Clipping
I P
FOR i =1 TO (N-1) DO
YES IF PiPi+1 NO
INTERSECT E ?
COMPUTE I
YES IF Pi TO LEFT OF NO
E?
COMPUTE I
END
YOU CAN ALSO APPEND AN ADDITIONAL VERTEX PN+1 = P1 AND AVOID SPECIAL CASE FOR FIRST VERTEX
y y1 x x1
0
y 2 y1 x2 x1
or C = (x2 – x1) (y – y1) – (y2 – y1)(x – x1) > 0
me?
add clip pt. add end pt. add clip pt. follow clip edge until
found
added
May 28, 2025 Computer Graphics 91
Weiler-Atherton Polygon Clipping
• Example (cont)
continue from add clip pt. add clip pt. follow clip edge until
found
added
May 28, 2025 Computer Graphics 92
Weiler-Atherton Polygon Clipping
• Example (concluded)
polygons
TYPES
1. All or None String Clipping
2. All or None Character Clipping
3. Component Character Clipping
STRING 5
STRI NG 1
G 2
RI N
ST
STRING 3
STRING 4 STRING 4
STRING 5
STRI NG 1 NG 1
G2
RI N RI
ST ST
STRING 3 TRING 3
STRING 4 STRING 4
STRING 5
STRI NG 1 NG 1
G2
RI N
ST RI N
ST
STRING 3 STRING 3
STRING 4 STRING 4
Above
Viewing Window
Below
• The algorithm has 2 parts.
• The first part determines whether the line lies entirely on the screen, then it
will be accepted and if not whether the line can be rejected if lying entirely
off the screen.
• If it satisfies none of these 2 tests, then the line is divided in to 2 parts and
these 2 tests are applied to each part.
• The algorithm depends on the fact that every line is entirely on the screen.
Or can be divided so that one part can be rejected.
• We are extending the edges of the screen so that they divide the space
occupied by the unclipped picture in to 9 regions.
• Each of these regions has a 4 bit code.
Consider line AB, the 4 bit out code for A is 0001
B is 0000
CD C is 0000
D is 0000
EF E is 0101
F is 0010
GH G is 0101
H is 0100
• If the logical AND of the 2 codes is not zero, then the line will be
entirely off the screen.
• For example, for line GH, the logical AND of the 2 end points is 0100. it is not
equal to zero, so the line is completely out of the screen.
• If the 4 bit codes for both endpoints are zero, the line lies entirely on
the screen.
• For example, for line CD, the 4 bit codes for both endpoints C, D are 0000. So
the line lies entirely inside the screen.
• If both these tests are not successful, it means that a part of a line is
inside and a part is outside the screen.
• For example line AB. Then such lines are subdivided. A simple method
of sub division is to find the point of intersection of the line with one
edge of the screen and to discard that part of the line that lies off the
screen.
• The line AB could be subdivided at C and the portion AC is discarded.
We now have the line BC.
• For line BC, we apply the same tests. The line cannot be rejected, so
we again subdivide it at point D. the resulting line BD is entirely on the
screen.
• The algorithm works as follows.
• We compute the out codes of both endpoints and check for trivial acceptance
and rejection.
• If both tests are not successful, we find an end point that lies outside and test
the out code to find the edge that is crossed and to determine the a
corresponding intersection point.
• We can clip off the line segment from the outside end point to the
intersection point by replacing the outside end point with the intersection
point and compute the outside of this new end point to prepare for the next
iteration.
• For example consider the line segment AD
• Point A has out code 0000 and point D has out code 1001.
• The line AD cannot be accepted and cannot be rejected.
• Therefore the algorithm chooses D as the outside point, whose out
code shows that the line crosses the top edge and left edge.
• In our algorithm we choose the top edge of the screen to clip and we
clip AD to AB.
• We compute B‘s out code as 0000.
• In our next iteration, we apply the trivial acceptance/ rejection tests
to AB, and the line is accepted and displayed
• Consider another line EI in the following figure.
• EI requires more iteration.
• The first end point E has out code 0100.
• The algorithm chooses E as the outside point and tests the out code
to find the first edge against which the line is cut is the bottom edge,
where EI is clipped top FI.
• In the second iteration, FI cannot be completely accepted or rejected.
The out code of the first end point, F is 0000, so the algorithm
chooses the outside point I that has out code 1010.
• The first edge clipped against is the top edge, yielding FH.
• H has the out code 0010.
• Then the next iteration results in a clip against the right edge to
FG.
• This is accepted in the final iteration and is displayed.
POLYGON CLIPPING
SUTHERLAND AND HODGMAN POLYGON CLIPPING ALGORITHM