Download presentation
Presentation is loading. Please wait.
1
o عَلَّمَهُ الْبَيَانَ
Allah says o عَلَّمَهُ الْبَيَانَ He has taught him speech (and intelligence). Al-Qur'an, (Ar-Rahman)
2
Bouchahma Maged boumaged@unizwa.edu.om
Lecture-6 Viewing in 2D Bouchahma Maged
3
Contents In this lecture we’ll cover the following:
Concepts from Linear Algebra 2D Viewing Viewing Pipeline Window to Viewport Transform Clipping Line Clipping Polygon Clipping Curve Clipping Viewing in OpenGL
4
2D Viewing pipeline MC WC PVC CVC DC VC NVC
Construct World-Coordinate Scene using modeling coordinates Convert World Coordinates to Viewing Coordinates Map normalized viewing coordinates to Device coordinates MC WC PVC CVC DC Projection Transformation Clipping Window to Viewport Mapping (Conversion of VC to Normalized VC) VC NVC Modeling Transformation: gltranslatef, … Viewing Transformation: glulookat Projection Transformation: gluortho2d, glperspective… Clipping: automatic (uses viewing coordinates specified in gluortho2d for 2d points) Window to viewport mapping: automatic, rotated viewports not allowed
5
2D Viewing pipeline… World Coordinate Scene Description
Objects are first modeled For example: A star can be modeled by a set of connected points in an axes specific to the star shape Different objects can have different object specific axes A scene is typically composed of different modeled objects in a world coordinate system Thus transformations of each object model to the world coordinate system are used to draw the object in the world as required ymodel ymodel xmodel xmodel xworld yworld
6
Modeling Transformation in OpenGL
MODE = GL_MODELVIEW glTranslate glRotate glScale
7
2D Viewing pipeline… Conversion of world coordinates to viewing coordinates The viewing coordinate system describes how the world is viewed We may want to choose the origin of the viewing coordinates to be different from that of the world coordinates We choose the origin of the viewing coordinate system at a point P(x0,y0) in the world coordinates We may want to have the viewing coordinates oriented at an angle to the world coordinates We also choose the orientation of the view-up vector V in the world coordinates
8
2D Viewing pipeline: Translation
yworld yview yview xview xworld xview
9
2D Viewing pipeline: Viewing Orientation
View-up vector
10
Converting world coordinates to viewing coordinates
Thus a transformation between axis will be involved View up vector is used to calculate the orientation matrix xview yview (0view,0view) xworld yworld (0world,0world) θ (x0,y0)
11
Viewing Transformation in OpenGL
MODE = GL_MODELVIEW Glulookat If gluLookAt is not used then the world coordinates are used hence forth
12
Projection Transformation
The display of a scene requires projection onto the 2D screen Types of Projection Transformations Perspective Projections Parallel Projections Projection matrix is used to convert the viewing coordinates into projected Viewing Coordinates 12
13
Parallel Orthographic Projection
In 2D viewing only orthographic projections are used
14
Perspective Projection
15
Projection Transformation in OpenGL
MODE = GL_PROJECTION gluOrtho2D Produces a one to one mapping from the world coordinates to the projected viewing coordinates Projected viewing coordinates = world coordinates if gluLookAt is not used
16
Projected Viewing Coordinates
Clipping Because drawing things to a display takes time we clip everything outside the window The extremities of the window are specified in viewing coordinates yview Window wymax wymin wxmin wxmax xview Projected Viewing Coordinates
17
Clipping Area Definition in OpenGL
gluOrtho2D specifies the extremities of the window clipping area in projected viewing coordinates which are the same as world coordinates if glulookat is not used
18
Window to View-Port Transformation
Once the viewing coordinates have been obtained, these are mapped onto normalized device coordinates Done by specifying a view-port in normalized device coordinates and then mapping the window contents onto the view-port viewport window yview 1 yw_max (xw,yw) Window is specified in viewing coordinates (0,0) 1 yw_min xw_min xw_max xview
19
Window to view-port transformation
yDevice yv_min (xv,yv) yv_min xDevice xv_min xv_min This may require scaling and translation
20
Defining the View Port in OpenGL
glViewPort Defines the extremities of the view port but in device coordinates
21
Workstation Transformation
Mapping from the normalized device coordinates to device coordinates yDevice yv_min yv_min xDevice xv_min xv_min
22
Clipping For the image below consider which lines and points should be kept and which ones should be clipped P4 Window P2 wymax P6 P3 P1 P7 P5 P9 P8 wymin P10 wxmin wxmax
23
Points Within the Window are Not Clipped
Point Clipping Easy - a point (x,y) is not clipped if: wxmin ≤ x ≤ wxmax AND wymin ≤ y ≤ wymax otherwise it is clipped P4 Clipped Clipped Window P2 wymax Clipped P5 P1 P7 Points Within the Window are Not Clipped P9 P8 wymin Clipped P10 wxmin wxmax
24
Line Clipping Harder - examine the end-points of each line to see if they are in the window or not Situation Solution Example Both end-points inside the window Don’t clip One end-point inside the window, one outside Must clip Both end-points outside the window Don’t know!
25
Brute Force Line Clipping
Brute force line clipping can be performed as follows: Don’t clip lines with both end-points within the window For lines with one end- point inside the window and one end-point outside, calculate the intersection point (using the equation of the line) and clip from this point out
26
Brute Force Line Clipping (cont…)
For lines with both end-points outside the window test the line for intersection with all of the window boundaries, and clip appropriately However, calculating line intersections is computationally expensive Because a scene can contain so many lines, the brute force approach to clipping is much too slow
27
Cohen-Sutherland Clipping Algorithm
An efficient line clipping algorithm The key advantage of the algorithm is that it vastly reduces the number of line intersections that must be calculated
28
Cohen-Sutherland: World Division
World space is divided into regions based on the window boundaries Each region has a unique four bit region code Region codes indicate the position of the regions with respect to the window 1001 1000 1010 0001 0000 Window 0010 0101 0100 0110 above below right left 3 2 1 Region Code Legend
29
Cohen-Sutherland: Labelling
Every end-point is labelled with the appropriate region code wymax wymin wxmin wxmax Window P3 [0001] P6 [0000] P5 [0000] P7 [0001] P10 [0100] P9 [0000] P4 [1000] P8 [0010] P12 [0010] P11 [1010] P13 [0101] P14 [0110]
30
Cohen-Sutherland: Lines In The Window
Lines completely contained within the window boundaries have region code [0000] for both end-points so are not clipped wymax wymin wxmin wxmax Window P3 [0001] P6 [0000] P5 [0000] P7 [0001] P10 [0100] P9 [0000] P4 [1000] P8 [0010] P12 [0010] P11 [1010] P13 [0101] P14 [0110]
31
Cohen-Sutherland: Lines Outside The Window
Any lines with a common set bit in the region codes of both end-points can be clipped The AND operation can efficiently check this wymax wymin wxmin wxmax Window P3 [0001] P6 [0000] P5 [0000] P7 [0001] P10 [0100] P9 [0000] P4 [1000] P8 [0010] P12 [0010] P11 [1010] P13 [0101] P14 [0110]
32
Cohen-Sutherland Examples
Consider the line P9 to P10 below Start at a point not lying within the window e.g. P10 From the region codes of the two end-points we know the line doesn’t cross the left or right boundary Calculate the intersection of the line with the bottom boundary to generate point P10’ The line P9 to P10’ is completely inside the window so is retained wymax wymin wxmin wxmax Window P10 [0100] P9 [0000] P10’ [0000] We have started at a point not lying within the window because the code for that point would automatically indicate which vertices to consider for intersection whereas the code for a point inisde the window (0000) provides no such information
33
Cohen-Sutherland Examples (cont…)
Consider the line P3 to P4 below Start at (point outside the boundary) P4 From the region code of P4 we know the line crosses the top boundary Calculate the intersection point to generate P4’ with code (0001) Discard the line from P4 to P4’ as it lies completely outside (codes!) Now the line from P4’ to P3 can be completely clipped wymax wymin wxmin wxmax Window P4’ [0001] P3 [0001] P4 [1000]
34
Cohen-Sutherland Examples (cont…)
Consider the line P7 to P8 below Start at P7 P7 is to the left so we find its intersection with the left boundary giving P7’ with code (0000) Now the outside point is P8 with code (0010) so we find the intersection with the right boundary P8’ with code (0000) Now as both points P7’ & P8’ are inside the window, a line is drawn between them wymax wymin wxmin wxmax Window P7’ [0000] P7 [0001] P8 [0010] P8’ [0000]
35
Calculating Line Intersections
Intersection points with the window boundaries are calculated using the line-equation parameters Consider a line with the end-points (x1, y1) and (x2, y2) The y-coordinate of an intersection with a vertical window boundary can be calculated using: y = y1 + m (xboundary - x1) where xboundary can be set to either wxmin or wxmax
36
Calculating Line Intersections (cont…)
The x-coordinate of an intersection with a horizontal window boundary can be calculated using: x = x1 + (yboundary - y1) / m where yboundary can be set to either wymin or wymax m is the slope of the line in question and can be calculated as m = (y2 - y1) / (x2 - x1)
37
Cohen-Sutherland Clipping
Input: End points of the line P1 and P2 Window boundaries Output Draw Flag: 1 indciates the line (or a part of it) is to be drawn In this case the algorithm also changes P1 and P2 to be the end points of the part of the line to be drawn 0 indicates that the line need not be drawn The Clipping algorithm is applied on all lines to be drawn one by one
38
Cohen-Sutherland Clipping Pseudo Code
Procedure Set done=0, draw=0 While (NOT(done)) Encode the points P1 and P2 as C1 and C2 If the codes of P1 and P2 are both 0000, then (Trivial Accept) done=1, draw=1 and output P1 and P2 as the end points Else If AND(C1,C2) is non-zero then (Trivial Reject) done=1, draw=0 Else If P1 is inside the window then swap the points and their codes (so that P1 is the point which is always outside the window) Find an intersection of the line from P1 to P2 with the window boundary on the basis of the code of C1 Set P1= intersection point End While
39
Area Clipping Similarly to lines, areas must be clipped to a window boundary Consideration must be taken as to which portions of the area must be clipped
40
Sutherland-Hodgman Area Clipping Algorithm
A technique for clipping areas developed by Sutherland & Hodgman Put simply the polygon is clipped by comparing it against each boundary in turn Original Area Clip Left Clip Right Clip Top Clip Bottom
41
Sutherland-Hodgman Area Clipping Algorithm (cont…)
To clip an area against an individual boundary: Consider each vertex in turn against the boundary Vertices inside the boundary are saved for clipping against the next boundary Vertices outside the boundary are clipped If we proceed from a point inside the boundary to one outside, the intersection of the line with the boundary is saved If we cross from the outside to the inside intersection point and the vertex are saved
42
Sutherland-Hodgman Example
Each example shows the point being processed (P) and the previous point (S) Saved points define area clipped to the boundary in question S P Save Point P S P Save Point I I P S No Points Saved S P Save Points I & P I
43
Other Area Clipping Concerns
Clipping concave areas can be a little more tricky as often superfluous lines must be removed Clipping curves requires more work For circles we must find the two intersection points on the window boundary Window Window
44
Summary Objects within a scene must be clipped to display the scene in a window Because there are can be so many objects clipping must be extremely efficient The Cohen-Sutherland algorithm can be used for line clipping The Sutherland-Hodgman algorithm can be used for area clipping
45
Many of the brightly colored tile-covered walls and floors of the Alhambra in Spain show us that the Moors were masters in the art of filling a plane with similar inter-locking figures, bordering each other without gaps. What a pity that their religion forbade them to make images! M.C. Esclier End of Lecture-6
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.