2 Dviewing

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 50

2D VIEWING

 The Two-Dimensional Viewing Pipeline


 The Clipping Window

 Normalization and Viewport Transformations

 Clipping Algorithms
 Procedures for displaying two dimensional views of a
picture.
 Any Cartesian coordinate system can be used to define the
picture.
 A single or multiple areas of a picture can be selected for
display by a user.
 The selected parts of the picture are mapped to device
coordinates for display.
 Transformation from world to device coordinates involve
translation, rotation, scaling operations, and procedures for
deleting parts of the picture (clipping).
TWO-DIMENSIONAL VIEWING PIPELINE
Clipping Window
 A section of a two dimensional scene that is selected for
display.
Viewport
 Used to control the placement of the clipping window
within the display window.
 The clipping window selects what we want to see, the
viewport indicates where it is to be viewed on the output
device.
Two-Dimensional Viewing Transformation
 The mapping of a two-dimensional, world-coordinate
scene description to device coordinates.
 Changing the viewport position allows for viewing
objects at different locations on the display device.
 By changing clipping window size or position, zooming,
overview, or panning effects can be achieved.
1. Construct World Coordinate Scene using Model-
Coordinate Transformations
2. Convert World Coordinates to Viewing Coordinates
(clipping window)
3. Transform Viewing Coordinates to Normalized
Coordinates
4. Map Normalized Coordinates to Device Coordinates
THE CLIPPING WINDOW
 Most applications allow only a rectangular clipping
window aligned with the x and y axes.
 Rectangular clipping windows in standard positions are
easily defined by giving the coordinates of two opposite
corners of each rectangle.
 We could design our own clipping window with any
shape, size, or orientation.
 A concave polygon clipping window or one with
nonlinear boundaries, requires more processing.
 If we like to get a rotated view of a scene, we could
either define a rectangular clipping window in a rotated
viewing-coordinate frame or, equivalently, we could
rotate the world-coordinate scene. Some systems provide
options for selecting a rotated, two-dimensional viewing
frame, but usually the clipping window must be
specified in world coordinates.

Two general types of clipping window:


1. Viewing-Coordinate clipping Window
2. World-Coordinate clipping Window
VIEWING-COORDINATE CLIPPING
WINDOW
 A viewing-coordinate system is setup within the world-
coordinate frame to define the clipping window.
 A clipping window in any orientation and position can be
defined.
 We choose an origin for a 2D viewing-coordinate frame
at some world position, P0 = (x0, y0), and we can
establish the orientation using a world vector V that
defines the yview
direction. Vector V is called the
two-dimensional view up vector.
 An alternative method for specifying the orientation of
the viewing frame is to give a rotation angle relative to
either the x or y axis in the world frame.
 From this rotation angle, we can then obtain the view up
vector.
 Once we have established the parameters that define the
viewing-coordinate frame, we transform the scene
description to the viewing system.
 This involves a sequence of transformations to convert
the viewing frame into the world frame.
1. Translate viewing coordinate origin to the world
coordinate origin.
2. Rotate the viewing coordinate system to align it with the
world frame.
3. Convert object positions in world coordinates to viewing
coordinates, with the composite 2D transformation
matrix, and it is as shown in fig.
Mwc,vc = R.T
WORLD-COORDINATE CLIPPING
WINDOW
 A routine for defining a standard, rectangular clipping
window in world coordinates is provided in a graphics
programming library.
 World-coordinate positions are used to define the
clipping window.
 Once the clipping window has been established, the
screen description is processed to the output device.
NORMALIZATION AND VIEWPORT
TRANSFORMATIONS
 Many applications combine normalization and window-
to-viewpoint transformations.
 The coordinates of the viewport are given in the range
[0,1] so the viewport is positioned within a unit square.
 After clipping, the unit square viewport is mapped to the
output device.
 In other systems, the normalization and clipping is
performed before viewport transformation.
 The viewport boundaries are given in screen coordinates
relative to the display-window position.
MAPPING THE CLIPPING WINDOW INTO
NORMALIZED VIEWPORT
 Viewport is defined with normalized coordinate values
between 0 and 1.
 Object descriptions are transferred to this normalized
space.
 The transformation used maintains the same relative
placement of a point in the viewport as it had in
the clipping window.
 Relative proportions are only maintained if the aspect
ratio of the viewport is the same as the clipping window.
Fig illustrates window to viewport mapping.
 Position (xw, yw) in the clipping window is mapped to
position (xv, yv) in the associated viewport.
 To transform the world-coordinate point into the same
relative position within the viewport, we require that:

Solving this we get,


 Scaling factors and translating factors are,
 we could obtain the transformation from world
coordinates to viewport coordinates with the following
sequence:
1. Scale the clipping window to the size of the viewport using
a fixed-point position of (xwmin, ywmin).

2. Translate (xwmin, ywmin) to (xvmin, yvmin).


 The composite matrix representation is:
 MAPPING THE CLIPPING WINDOW TO A
NORMALIZED SQUARE

 Another approach to two-dimensional viewing is to


transform the clipping window into a normalized square,
clip in normalized coordinates, and then transfer the
scene description to a viewport specified in screen
coordinates.
 This transformation is illustrated in Figure with
normalized coordinates in the range from −1 to 1.
 Substitute values xvmin=yvmin=-1 and
xvmax=yvmax= 1
in the composite matrix,
 After the clipping algorithms have been applied, the
normalized square with edge length equal to 2 is
transformed into a specified viewport.
 This time, we get the transformation matrix by
substituting for xwmin, ywmin= -1 and substituting for
xwmax and ywmax = +1
 The last step in the viewing process is to position the
viewport area in the display window. Typically, the
lower-left corner of the viewport is placed at a
coordinate position specified relative to the lower-left
corner of the display window.
 Figure demonstrates the positioning of a viewport within
a display window.
Display of Character Strings
 Character strings can be handled in one of two ways
when they are mapped through the viewing pipeline to a
viewport.
 The simplest mapping maintains a constant character
size. This method could be employed with bitmap
character patterns.
 We just need to transform the defining positions for the
line segments in the outline character shapes.
 Algorithms for determining the pixel patterns for the
transformed characters are then applied.
Split-Screen Effects and Multiple Output Devices
 By selecting different clipping windows and associated
viewports for a scene, we can provide simultaneous
display of two or more objects, multiple picture parts, or
different views of a single scene.
 And we can position these views in different parts of a
single display window or in multiple display windows on
the screen.
 In a design application, for example, we can display a
wire-frame view of an object in one viewport while also
displaying a fully rendered view of the object in another
viewport.
 It is also possible that two or more output devices could
be operating concurrently on a particular system, and we
can set up a clipping-window/viewport pair for each
output device.
 A mapping to a selected output device is referred to as a
workstation transformation.
 In this case, viewports could be specified in the
coordinates of a particular display device, or each
viewport could be specified within a unit square, which
is then mapped to a chosen output device.
OPENGL TWO-DIMENSIONAL
VIEWING FUNCTIONS
OpenGL Projection Mode
 Before we select a clipping window and a viewport in
OpenGL, we need to establish the appropriate mode for
constructing the matrix to transform from world
coordinates to screen coordinates.
 We must set the parameters for the clipping window as
part of the projection transformation.
glMatrixMode (GL_PROJECTION);
 This designates the projection matrix as the current
matrix, which is originally set to the identity matrix.
However, if we are going to loop back through this
statement for other views of a scene, we can also set the
initialization as
glLoadIdentity ( );
 This ensures that each time we enter the projection
mode, the matrix will be reset to the identity matrix so
that the new viewing parameters are not combined with
the previous ones.
GLU Clipping-Window Function
 To define a two-dimensional clipping window, we can
use the GLU function:
gluOrtho2D(xwmin, xwmax, ywmin, ywmax);
 This function specifies an orthogonal projection for
mapping the scene to the screen.
 For a three-dimensional scene, this means that objects
would be projected along parallel lines that are
perpendicular to the two dimensional xy display screen
and convert object positions to normalized coordinates.
 Normalized coordinates in the range from −1 to 1 are used in
the OpenGL clipping routines. And the gluOrtho2D function
sets up a three-dimensional version of transformation matrix for
mapping objects within the clipping window to normalized
coordinates.
 Objects outside the normalized square (and outside the clipping
window) are eliminated from the scene to be displayed.
 If we do not specify a clipping window in an application
program, the default coordinates are (xwmin, ywmin) = (−1.0,
−1.0) and
(xwmax, ywmax) = (1.0, 1.0).
 Thus the default clipping window is the normalized square
centred on the coordinate origin with a side length of 2.0.
OpenGL Viewport Function
 We specify the viewport parameters with the OpenGL
function
glViewport (xvmin, yvmin, vpWidth, vpHeight);
where all parameter values are given in integer screen
coordinates relative to the display window, parameters
xvmin and yvmin specify the position of the lower left
corner of the viewport relative to the lower-left corner of
the display window, and the pixel width and height of the
viewport are set with parameters vpWidth and vpHeight.
 After the clipping routines have been applied, positions
within the normalized square are transformed into the
viewport rectangle.
 Coordinates for the upper-right corner of the viewport
are calculated for this transformation matrix in terms of
the viewport width and height:
xvmax = xvmin + vpWidth,
yvmax = yvmin + vpHeight
 For the final transformation, pixel colors for the
primitives within the viewport are loaded into the refresh
buffer at the specified screen locations.
 Multiple viewports can be created in OpenGL for a
variety of applications. We can obtain the parameters for
the currently active viewport using the query function
glGetIntegerv (GL_VIEWPORT, vpArray);
where vpArray is a single-subscript, four-element array.
 This Get function returns the parameters for the current
viewport to vpArray in the order xvmin, yvmin,
vpWidth, and vpHeight.
CREATING A GLUT DISPLAY WINDOW
 We use the GLUT routines for creating and manipulating
display . To access these routines, we first need to initialize
GLUT with the following function:
glutInit (&argc, argv);
 We have three functions in GLUT for defining a display
window and choosing its dimensions and position:
glutInitWindowPosition (xTopLeft, yTopLeft);
glutInitWindowSize (dwWidth, dwHeight);
glutCreateWindow ("Title of Display Window");
 If we do not use these two functions to specify a size and
position, the default size is 300 by 300 and the default
position is (−1, −1), which leaves the positioning of the
display window to the window-management system.
GLUT Display-Window Identifier
 Multiple display windows can be created for an
application, and each is assigned a positive-integer
display-window identifier, starting with the value 1 for
the first window that is created. At the time that we
initiate a display window, we can record its identifier
with the statement ,
windowID = glutCreateWindow("A Display Window");
Deleting a GLUT Display Window
 The GLUT library also includes a function for deleting a
display window that we have created. If we know the
display window’s identifier, we can eliminate it with the
statement
glutDestroyWindow (windowID);
Current GLUT Display Window
 When we specify any display-window operation, it is
applied to the current display window, which is either the
last display window that we created or the one we select
with the following command:
glutSetWindow (windowID);
 At any time, we can query the system to determine
which window is the current display window:
currentWindowID = glutGetWindow ( );
 A value of 0 is returned by this function if there are no
display windows or if the current display window was
destroyed.
Relocating and Resizing a GLUT Display Window
 We can reset the screen location for the current display window
with
glutPositionWindow(xNewTopLeft, yNewTopLeft);
 where the coordinates specify the new position for the upper-
left display-window corner, relative to the upper-left corner of
the screen.
 Similarly, the following function resets the size of the current
display window:
glutReshapeWindow(dwNewWidth, dwNewHeight);
 With the following command, we can expand the current
display window to fill the screen:
glutFullScreen ( );
 Whenever the size of a display window is changed, its
aspect ratio may be distorted from their original shapes.
We can adjust for a change in display-window
dimensions using the statement
glutReshapeFunc (winReshapeFcn);
 This GLUT routine is activated when the size of a
display window is changed, and the new width and
height are passed to its argument: the function
winReshapeFcn, in this example. Thus, winReshapeFcn
is the “callback function” for the “reshape event.”
 we could also reset the clipping-window boundaries,

change the display-window color.


MANAGING MULTIPLE GLUT DISPLAY
WINDOWS
 The GLUT library also has a number of routines for
manipulating a display window in various ways. These
routines are particularly useful when we have multiple
display windows on the screen.
 We use the following routine to convert the current
display window to an icon in the form of a small picture
or symbol representing the window: glutIconifyWindow
( );
 The label on this icon will be the same name that we
assigned to the window, but we can change this with the
following command:
glutSetIconTitle ("Icon Name");
 We also can change the name of the display window with a
similar command:
glutSetWindowTitle ("New Window Name");
 With multiple display windows open on the screen, some
windows may overlap or totally obscure other display
windows. We can choose any display window to be in front of
all other windows by first designating it as the current
window, and then issuing the “pop-window” command:
glutSetWindow (windowID);
glutPopWindow ( );
 In a similar way, we can “push” the current display
window to the back so that it is behind all other display
windows. This sequence of operations is
glutSetWindow (windowID);
glutPushWindow ( );
 We can also take the current window off the screen with

glutHideWindow ( );
 In addition, we can return a “hidden” display window, or
one that has been converted to an icon, by designating it
as the current display window and then invoking the
function
glutShowWindow ( );
GLUT Subwindows
 Within a selected display window, we can set up any
number of second-level display windows, which are
called subwindows. This provides a means for
partitioning display windows into different display
sections.
 We create a subwindow with the following function

glutCreateSubWindow(windowID,xBottomLeft, yBottomLeft,
width, height);
 Parameter windowID identifies the display window in
which we want to set up the subwindow.
SELECTING A DISPLAY-WINDOW
SCREEN-CURSOR SHAPE
 We can use the following GLUT routine to request a
shape for the screen cursor that is to be used with the
current window:
glutSetCursor (shape);
 For example, we can assign the symbolic constant
GLUT_CURSOR_UP_DOWN to parameter shape to
obtain an up-down arrow. A rotating arrow is chosen
with GLUT_CURSOR_CYCLE, a wristwatch shape is
selected with GLUT_CURSOR_WAIT, and a skull and
crossbones is obtained with the constant
GLUT_CURSOR _DESTROY.
VIEWING GRAPHICS OBJECTS IN A
GLUT DISPLAY WINDOW
 we invoke the following function to assign something to that window:
glutDisplayFunc (pictureDescrip);
 The argument is a routine that describes what is to be displayed in the
currentwindow. This routine, called pictureDescrip for this example, is
referred to as a callback function because it is the routine that is to be
executed whenever GLUT determines that the display-window
contents should be renewed.
 Also, we may need to call glutDisplayFunc after the
glutPopWindow command if the display window has been damaged
during the process of redisplaying the windows. In this case, the
following function is used to indicate that the contents of the current
display window should be renewed:
glutPostRedisplay ( );
 This routine is also used when an additional object, such as a pop-up
menu, is to be shown in a display window.
 Sometimes it is convenient to designate a function that is to
be executed when there are no other events for the system to
process. We can do that with
glutIdleFunc (function);
 The parameter for this GLUT routine could reference a
background function or a procedure to update parameters for
an animation when no other processes are taking place.
 Finally, we can use the following function to query the
system about some of the current state parameters:
glutGet (stateParam);
 we can retrieve the current display-window width or the
screen width with GLUT_WINDOW_WIDTH or
GLUT _SCREEN_WIDTH.

You might also like