Viewing Transformation Previous readings have described how to transform objects from one position and orientation to another. One application for such transformations is to create complex models from simple primitives. For example, a robot arm may be composed of cylinders, spheres, and polygons of different sizes and orientations. Transformations may be used as part of the modeling process to describe how the primitive elements are transformed and combined to form the more complex object. Modeling will be described in a later reading. Another application of transformations is to modify the entire scene so that it appears different on the screen to the viewer. For example, if the scene is rendered from the viewer s position and orientation, and the viewer position changes, then the scene needs to be transformed accordingly before being drawn to the screen. This is best described by considering two separate coordinate systems. The world coordinate system, or world space, is where objects are defined. This space may be very application specific. For example, if modeling subatomic particles, coordinates may be specified in angstroms while if modeling the universe, light years may be more appropriate. The position of the viewer may be some arbitrary location in world space. However, angstroms and light years do not fit on the screen very well, so a different space is necessary to define where to put things on the screen. This is known as screen space. The typical measurements for screen space are in pixels. Although we think of the screen as two dimensional, screen space can be defined as three dimensional, in which the Z axis is perpendicular to the screen and defines what objects are in front of other objects for display purposes. The process of converting from world space to screen space is known as the viewing transformation. Various pieces of information are combined to transform objects defined in world space to places on the screen to draw graphic primitives. Examples of the necessary information are: Viewer position and orientation Point being looked at Distance from viewer to projection window Location and orientation of viewing window (viewport) on the screen This reading will focus on the concepts and process of the 2 D viewing transformation while the next reading will expand these to the 3 D case. Windows and Viewports As described, world coordinates are specified in whatever units make sense for the application. An architect drawing a 2 D set of plans for a building may use feet or meters for defining the drawing while a cartographer creating a map of a country may use miles or kilometers. Regardless of the unit, it is necessary to specify what portion of the world is going to be displayed to the screen. This is done by specifying a window in the world that will get mapped to the screen. The window can be specified by the corners of the rectangle in world space coordinates. This is shown in Figure 1. Whatever is visible in 1
the window in world coordinates gets mapped to the screen coordinates. Note that if the window is not the same size or aspect ratio as the screen display area, distortion of the displayed image can result. Also note that there is no requirement for the window to be aligned with the world axes, and may be rotated by some amount. This might indicate the viewer has tilted his head. Figure 1. Viewport in world space mapped to screen. In screen space, rather than map the window to the entire screen, the user can select an area of the screen to display the window in, known as the viewport. A single screen can contain several viewports of the same scene, or different scenes. The viewport can be sized differently than the window or rotated to different orientations. Figure 2 shows a screen containing different viewports of the world space window shown in Figure 1. Note how the differently shaped and oriented viewports change the image. Although different systems use different terminology, we will use the term window to refer to the rectangular area in world space that represents what we want to view. It is, literally, the window into the world through which we are looking. The viewport is the porthole on the screen that we are looking through to see the objects in the window. Figure 2. Different viewports of same window. 2
Mapping objects from the window to the viewport can be accomplished by defining a viewing transformation and applying it to every object visible in the window. Logically, if you defined a transformation that successfully transforms the corners of the window to the corners of the viewport, that transformation will work properly for all objects contained in the window. To accomplish this, we will develop the transformation in a two step process. First, transform the window to a normalized or canonical window of unit size centered about the world origin. Next, transform the canonical window to the correct location and orientation for the viewport. A window can be defined by:,, center of the window in world coordinates width and height of the window in world coordinates angle of rotation of the window about its center To transform the window, we will translate the center to the world origin, rotate by to align it with the axes, and scale by 1/,1/. Using the standard transformation matrices previously described. This sequence can be annotated by:,, (1) Applying this transformation to all points in the window will result in the coordinates lying between 0.5 and 0.5 in both the and directions. The next step is to transform this canonical window to the viewport. Similar to a window, a viewport can be defined by:,, center of the viewport in screen coordinates width and height of the viewport in screen coordinates angle of rotation of the viewport about its center The proper sequence to transform the canonical coordinates is scale to the appropriate size, rotate about the origin, and translate the origin to the viewport center. In matrix notation,,, (2) These two sequences, (1) and (2), can be combined into a single viewing transformation matrix. When each point in the window is multiplied by this matrix, the appropriate screen coordinates result. Clipping If all of the objects in world space are transformed with the viewing transformation, there will be objects both inside and outside of the viewing area. The process of removing parts of the scene that are not visible in the window/viewport is known as clipping. In Figure 1, the pentagon in the lower right portion of world space was clipped totally out of the final scene and the circle was partially clipped. Clipping can be done at various points along the viewing process. For example, all objects can be clipped to the edges of the original viewing window, and only visible objects need to be transformed to the final screen coordinates. Alternatively, all objects can be transformed, and the transformed objects can be clipped to the viewport. To simplify the clipping operation, we will assume clipping occurs after 3
the first part of the viewing transformation (1). Thus, all objects are transformed to the canonical coordinates in world space before being clipped. The advantage of this is that the clipping window is a well defined rectangle centered about the origin. After clipping, only the visible points remain and are transformed with the second part of the viewing transformation, (2), for final display. The process of clipping depends on the type of object being clipped. Clipping Points The simplest drawing primitive to clip is the point. The, coordinates of the point simply need to be checked against the boundaries of the clipping window to determine if the point is inside or outside the window. Here the advantage of deferring clipping until after the first transformation can be seen. If the window were rotated in world space, the determination of inside or outside is not as simple. The disadvantage of waiting until after the first transformation is that all points need to transformed, even if the viewing window is a relatively small part of the overall scene. If there is a performance issue for the particular application, the clipping operation may be accomplished before the transformation. Clipping Lines Since we are conducting clipping to a rectangle, we can identify four discrete cases as shown in Figure 3. If both ends of the line are interior to the rectangle, as in A, the entire line is visible. If both ends are exterior the line may be totally invisible, as in B, or partially visible as in D, in which case the end line segments would be clipped and removed. Finally, if only one end of the line is interior, as in C, then the line needs to be clipped to the appropriate boundary. Figure 3. Possible line clipping configurations: A totally interior; B totally exterior; C one end interior; D ends exterior, crosses boundary An easy way to accomplish the line clipping cases shown above is to successively clip each line against the half spaces defined by the edges of the window. The half space is defined by extending the boundary line infinitely in each direction, with one side designated as being 4
outside the window. Figure 4 shows the progressive clipping of the lines to each of the four half spaces defined by the window. For the half space defined by the top edge of the window, both lines B and C were intersected with the half space line, the portion of both lines lying in the outside part of the half space were discarded. Lines A and D had both ends on the inside of the half space and were kept. For the right edge half space, line D was clipped, and the remainder of line B was discarded as both ends were on the outside of the half space. The bottom edge clipped line D, and the left edge did not clip or remove any lines. The remaining parts of the lines are all contained in the window and can be transformed to the viewport accordingly. Clipping Polygons Figure 4. Clipping lines to half spaces defined by window boundaries. Clipping polygons will follow a similar strategy as clipping lines. Each edge of each polygon can be classified similar to Figure 3. An additional complication in clipping polygons is that a corner of the window may become a corner of the clipped polygon as shown in Figure 5. Note the star in the lower left corner, when clipped, will consist of two edges formed from part of the left and bottom edge of the window along with parts of the original polygon as shown on the right. 5
Figure 5. Clipping polygons to window. The described polygon clipping algorithm is known as the Sutherland Hodgeman algorithm after its inventors. The vertices of the polygon to be clipped are ordered from 1 to n in clockwise order and added to a vertex list. Figure 6 shows the initial configuration of a five vertex polygon to be clipped at the top. Each edge, in order of vertices, is clipped to the half space defined by the top of the window, as shown in part A of Figure 6. Notice that when line 2 3 was clipped, a new point at the window top edge was added to the list. When line 3 4 was clipped, another point was added and the old point 3 discarded. The vertex list now represents a polygon with six vertices that has been clipped to the top edge. The process is repeated for the right, bottom, and left edge as shown in B, C, and D. When finished, the clipped polygon has 8 vertices. Figure 6. Step by step polygon clipping. 6
This algorithm works for every convex polygon, polygons with every internal angle less than 180 degrees. Note that it is possible for a concave polygon to result in two separate pieces after clipping as shown in Figure 7. Using the algorithm described, there are seven vertices after clipping as shown in the middle of the Figure. However, if you simply treat the set of vertices as a polygon and connect them together, you end up with the extra line going from vertex 7 to vertex 1 as shown on the right in the Figure. Special consideration needs to be taken to avoid this error. One solution is to convert any concave polygons to convex polygons prior to clipping. For example, any polygon can be converted to triangles which are guaranteed to be convex. Another solution, known as the Weiler Atherton algorithm traverses the vertices of a polygon in order looking at whether edges to be clipped are traversing from outside to inside the window or vice versa. If it is going from inside to outside, the window edge is followed as the next line versus the polygon edge. With this approach, the correct two clipped polygons, as shown in the middle of Figure 7, are generated. Clipping other primitives Figure 7. Concave polygon resulting in error. In addition to points, lines, and polygons, the scene to be clipped may consist of other geometric shapes, such as ellipses or circles, or different types of curves. Special clipping algorithms can be developed for each type of object. Or, more commonly, such primitives are converted to one of the three cases described and clipped accordingly. For example, a filled circle can be represented as a polygon with many edges and clipped using the polygon clipping routine. For text, a simpler approach would be to check the bounding box of each character and only display characters whose bounding box is completely interior to the viewing window. This is shown in the middle image of Figure 8. Note that characters straddling the window boundary are lost. Alternatively, each character can be converted to a polygon and clipped as shown on the right. Figure 8. Clipping text. 7
LAGNIAPPE Pac Man While Pong was one of the earliest computer games created, arguably Pac Man, created by Namco in 1980, is perhaps the most well known. According to the Davie Brown Celebrity Index, Pac Man has the highest brand awareness of any video game character among American consumers, recognized by 94 percent of them. The game was developed primarily by Namco employee Tōru Iwatani over eighteen months. The original title was pronounced pakku man where paku paku describes the sound of the mouth movement when widely opened and then closed in succession. The game was picked up for manufacture in the United States by Bally under the altered title Pac Man. The name was changed from Puck Man to Pac Man, as it was thought that vandals would be likely to change the P in "puck" to an F, forming a common expletive. Puck Man machines can still be found throughout Europe. Guinness World Records has awarded the Pac Man series eight records in Guinness World Records: Gamer's Edition 2008, including "First Perfect Pac Man Game" for Billy Mitchell's July 3, 1999 score; "Most Successful Coin Operated Game"; and "Largest Pac Man Game", when, in 2004, students from New York University created Pac Manhattan, a real life reenactment of the game, in which people dressed as Pac Man and the four ghosts chased each other around Manhattan city blocks. Each player was teamed with a controller who communicated the player's positions using cellular phones. 8