521493S Computer Graphics Exercise 1 (Chapters 1-3) 1. Consider the clipping of a line segment defined by the latter s two endpoints (x 1, y 1 ) and (x 2, y 2 ) in two dimensions against a rectangular clipping window (x min, y min ; x max, y max ). Show that only the endpoints are required to determine whether the line segment is not clipped, is partially visible, or is clipped out completely. Suppose that the line segment is between the points P 1 = (x 1, y 1 ) and P 2 = (x 2, y 2 ). We can use the endpoints of the line segment to determine where it resides in the window (x min, y min ; x max, y max ): If we create a line using parametric form: For x-axis we can solve t x : P(t) = (1 t)p 1 + tp 2, t [0, 1] t x = P 1x x P 1x P 2x We need to deal with horizontal and vertical line segments as special cases. We can check if we are in the line segment at the borders by evaluating t x (x min ) and t x (x max ). Y- axis can be checked similarly. By checking the available unclipped range of t using information from all the dimensions, we can determine if the line is fully clipped, partially clipped or completely unclipped. 1.1 Alternative solution We can solve y(x) from the known end points: 1
y = ax + b = y x x + b = y 2 y 1 x + b The unknown offset b can be solved by using any of the known points on the line. We use (x 1, y 1 ) here: Solving b from the above equation: Assigning b back to the original equation: Similarly x(y) can be solved: y 1 = y 2 y 1 x 1 + b b = y 1 y 2 y 1 x 1 y = y 2 y 1 x + y 1 y 2 y 1 x 1 x = y 2 y 1 y + x 1 y 2 y 1 y 1 We need to deal with horizontal and vertical line segments as special cases. We can find the intersections with the sides of the window by substituting y = y max, y = y min, x = x max and x = x min (the equations for the sides of the window) into the above equations. We can check the locations of the points of intersection to determine if they are on the line segment or only on the line of which it is part. 2. To avoid flicker, the memory in a frame buffer must be fast enough to allow the display to be refreshed at a rate sufficiently high. A typical workstation display can have a resolution of 1280 x 1024 pixels. If it is refreshed 72 times per second, how fast must the memory be? That is, how much time can we take to read one pixel from memory? What is this number for a 480x640 display that operates at 60 Hz but is interlaced? We have to process 1280 x 1024 x 72 pixels/sec. If we process each successively, there is only about 10 nanoseconds to process each. For a 480 x 640 interlaced display operating at 60 Hz we must process only 480 x 640 x 30 pixels/sec which gives us about 109 nanoseconds to process each pixel. 3. A fundamental operation in graphics system is to map a point (x, y), which lies within a clipping rectangle, to a point (x s, y s ), which lies in the viewport of a window on the screen. Assume that the two rectangles are defined by OpenGL function calls. glviewport(u, v, w, h); gluortho2d(x_min, x_max, y_min, y_max); Find the mathematical equations that map (x, y) into (x s, y s ). 2
We can solve this problem separately in the x and y directions. The transformation is linear, that is x s = ax + b, y s = cy + d. We must maintain proportions, so that x s in the same relative position in the viewport as x is in the window, hence Likewise x x min = x s u x max x min w, x s = u + w x x min, x max x min y s = v + h y y min. y max y min 4. Devise a test for convexity of two-dimensional polygon. Consider the lines defined by the sides of the polygon. We can assign a direction for each of these lines by traversing the vertices in a counter-clockwise order. One very simple test is obtained by noting that any point inside the object is on the left of each of these lines. Thus, if we substitute the point into the equation for each of the lines (ax + by + c), we should always get the same sign. 5. The mapping from a point in three-dimensional world coordinates to one in screen coordinates is well defined. What problems exist for inverting the mapping? i.e. Trying to map screen coordinates to world coordinates. 3
The most significant problem is that going from three dimensions to two dimensions we lose one dimension. Therefore each point in the screen map to some kind of line in the world coordinates instead of a single coordinate. Additional problems are that multiple three-dimensional lines may map to the same screen coordinate even when the coordinates are not integers. There may also be points in the screen that do not map to any valid positions in the world coordinates. For example if the whole world is mapped to a rectangle at the center of the screen, inverse mapping will not exist for pixels outside the rectangle at all. 6. Suppose a user is running a program using a GLUT window to draw OpenGL graphics. The window size is 400 x 300 pixels. An orthogonal projection of the scene is setup as follows: glviewport(0, 0, 400, 300); gluortho2d(0, 400, 0, 300); There are four circles in the scene that user can select with a mouse. As each circle can be defined as a triplet (x, y, r) where x and y are the coordinates and r is the circle radius, the four circles are expressed by c 1 = (100, 100, 50) c 2 = (300, 100, 50), c 3 = (100, 200, 50), c 4 = (300, 200, 50). When user selects his favorite circle, the GLUT callback function for mouse pointer position reports that the mouse is at coordinates (110, 110). What circle (if any) does the user select? Please also explain your answer. glviewport and gluortho2d create an orthographic view of the scene with visible size of 400 x 300. Also the actual window is of the same size so no scaling or stretching happens. Therefore a circle c 1 centered at (100,100) with radius of (50) is drawn centered as the same OpenGL screen coordinates. However, there is a difference between GLUT coordinates and OpenGL coordinates in the screen. GLUT passes down coordinates that come from the underlying windowing system as relative coordinates to the window position where the origin is at top left, x-axis grows to the right and y- axis down. In OpenGL, using presented setup and no additional transformations, the origin is at bottom left with y-axis pointing up. This means that when reported mouse pointer is at (110, 110), the position in the scene is (110, (300 1) 110) that equals (110, 189). This point is in the area of circle c 3 = (100, 200, 50) 4
7. Double buffering is used to reduce flickering of the screen caused by simultaneously drawing objects while the screen is displayed. What is the best time to flip the buffers to minimize any visual problems caused by the screen update? And why? During vertical retraces. Suppose that we have a fast computer that can render 300 frames per second. Screen of this computer is physically updated at 60 Hz. This means that we can render a completely different scene and show it 5 times during one screen update. This can cause a visual tearing effect as a set of lines come from a different image than ones above it. Quickly horizontally moving objects have parts of them shown at different positions during the same screen update. By limiting updates to one per screen refresh and refreshing the screen during vertical retrace period, no visible artifacts can be seen. 5