Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 80 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
80
Dung lượng
2,19 MB
Nội dung
Computer Graphics About the Tutorial To display a picture of any size on a computer screen is a difficult process Computer graphics are used to simplify this process Various algorithms and techniques are used to generate graphics in computers This tutorial will help you understand how all these are processed by the computer to give a rich visual experience to the user Audience This tutorial has been prepared for students who don’t know how graphics are used in computers It explains the basics of graphics and how they are implemented in computers to generate various visuals Prerequisites Before you start proceeding with this tutorial, we assume that you are already aware of the basic concepts of C programming language and basic mathematics Copyright & Disclaimer Copyright 2015 by Tutorials Point (I) Pvt Ltd All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt Ltd The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors Tutorials Point (I) Pvt Ltd provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial If you discover any errors on our website or in this tutorial, please notify us at contact@tutorialspoint.com i Computer Graphics Table of Contents About the Tutorial i Audience i Prerequisites i Copyright & Disclaimer i Table of Contents ii COMPUTER GRAPHICS – BASICS Cathode Ray Tube Raster Scan Application of Computer Graphics LINE GENERATION ALGORITHM DDA Algorithm Bresenham’s Line Generation Mid-Point Algorithm CIRCLE GENERATION ALGORITHM 11 Bresenham’s Algorithm 11 Mid Point Algorithm 13 POLYGON FILLING 16 Scan Line Algorithm 16 Flood Fill Algorithm 17 Boundary Fill Algorithm 18 4-Connected Polygon 18 8-Connected Polygon 19 Inside-outside Test 21 ii Computer Graphics VIEWING AND CLIPPING 24 Point Clipping 24 Line Clipping 24 Cohen-Sutherland Line Clippings 25 Cyrus-Beck Line Clipping Algorithm 27 Polygon Clipping (Sutherland Hodgman Algorithm) 28 Text Clipping 29 Bitmap Graphics 31 2D TRANSFORMATION 33 Homogenous Coordinates 33 Translation 33 Rotation 34 Scaling 36 Reflection 37 Shear 38 Composite Transformation 39 3D GRAPHICS 41 Parallel Projection 41 Orthographic Projection 42 Oblique Projection 43 Isometric Projections 43 Perspective Projection 44 Translation 45 Rotation 46 Scaling 47 Shear 48 iii Computer Graphics Transformation Matrices 49 CURVES 51 Types of Curves 51 Bezier Curves 52 Properties of Bezier Curves 52 B-Spline Curves 53 Properties of B-spline Curve 54 SURFACES 55 Polygon Surfaces 55 Polygon Tables 55 Plane Equations 57 Polygon Meshes 57 10 VISIBLE SURFACE DETECTION 59 Depth Buffer (Z-Buffer) Method 59 Scan-Line Method 61 Area-Subdivision Method 61 Back-Face Detection 62 A-Buffer Method 64 Depth Sorting Method 65 Binary Space Partition (BSP) Trees 66 11 FRACTALS 68 What are Fractals? 68 Generation of Fractals 68 Geometric Fractals 69 12 COMPUTER ANIMATION 71 iv Computer Graphics Animation Techniques 71 Key Framing 72 Morphing 73 v Computer Graphics COMPUTER GRAPHICS – BASICS Computer graphics is an art of drawing pictures on computer screens with the help of programming It involves computations, creation, and manipulation of data In other words, we can say that computer graphics is a rendering tool for the generation and manipulation of images Cathode Ray Tube The primary output device in a graphical system is the video monitor The main element of a video monitor is the Cathode Ray Tube (CRT), shown in the following illustration The operation of CRT is very simple: The electron gun emits a beam of electrons (cathode rays) The electron beam passes through focusing and deflection systems that direct it towards specified positions on the phosphor-coated screen When the beam hits the screen, the phosphor emits a small spot of light at each position contacted by the electron beam It redraws the picture by directing the electron beam back over the same screen points quickly Figure: Cathode Ray Tube Computer Graphics There are two ways (Random scan and Raster scan) by which we can display an object on the screen Raster Scan In a raster scan system, the electron beam is swept across the screen, one row at a time from top to bottom As the electron beam moves across each row, the beam intensity is turned on and off to create a pattern of illuminated spots Picture definition is stored in memory area called the Refresh Buffer or Frame Buffer This memory area holds the set of intensity values for all the screen points Stored intensity values are then retrieved from the refresh buffer and “painted” on the screen one row (scan line) at a time as shown in the following illustration Each screen point is referred to as a pixel (picture element) or pel At the end of each scan line, the electron beam returns to the left side of the screen to begin displaying the next scan line Figure: Raster Scan Random Scan (Vector Scan) In this technique, the electron beam is directed only to the part of the screen where the picture is to be drawn rather than scanning from left to right and top to bottom as in raster scan It is also called vector display, stroke-writing display, or calligraphic display Picture definition is stored as a set of line-drawing commands in an area of memory referred to as the refresh display file To display a specified picture, the system Computer Graphics cycles through the set of commands in the display file, drawing each component line in turn After all the line-drawing commands are processed, the system cycles back to the first line command in the list Random-scan displays are designed to draw all the component lines of a picture 30 to 60 times each second Figure: Random Scan Application of Computer Graphics Computer Graphics has numerous applications, some of which are listed below: Computer graphics user interfaces (GUIs) – A graphic, mouse-oriented paradigm which allows the user to interact with a computer Business presentation graphics - "A picture is worth a thousand words" Cartography - Drawing maps Weather Maps – Real-time mapping, symbolic representations Satellite Imaging - Geodesic images Photo Enhancement - Sharpening blurred photos Medical imaging - MRIs, CAT scans, etc - Non-invasive internal examination Computer Graphics Engineering drawings - mechanical, electrical, civil, etc - Replacing the blueprints of the past Typography - The use of character images in publishing - replacing the hard type of the past Architecture - Construction plans, exterior sketches - replacing the blueprints and hand drawings of the past Art - Computers provide a new medium for artists Training - Flight simulators, computer aided instruction, etc Entertainment - Movies and games Simulation and modeling - Replacing physical modeling and enactments Computer Graphics Algorithm Step-1: Set the buffer values: Depthbuffer (x, y) = Framebuffer (x, y) = background color Step-2: Process each polygon (One at a time) For each projected (x, y) pixel position of a polygon, calculate depth z If Z > depthbuffer (x, y) Compute surface color, set depthbuffer (x, y) = z, framebuffer (x, y) = surfacecolor (x, y) Advantages It is easy to implement It reduces the speed problem if implemented in hardware It processes one object at a time Disadvantages It requires large memory It is time consuming process 60 Computer Graphics Scan-Line Method It is an image-space method to identify visible surface This method has a depth information for only single scan-line In order to require one scan-line of depth values, we must group and process all polygons intersecting a given scan-line at the same time before processing the next scan-line Two important tables, edge table and polygon table, are maintained for this The Edge Table: It contains coordinate endpoints of each line in the scene, the inverse slope of each line, and pointers into the polygon table to connect edges to surfaces The Polygon Table: It contains the plane coefficients, surface material properties, other surface data, and may be pointers to the edge table To facilitate the search for surfaces crossing a given scan-line, an active list of edges is formed The active list stores only those edges that cross the scan-line in order of increasing x Also a flag is set for each surface to indicate whether a position along a scan-line is either inside or outside the surface Pixel positions across each scan-line are processed from left to right At the left intersection with a surface, the surface flag is turned on and at the right, the flag is turned off You only need to perform depth calculations when multiple surfaces have their flags turned on at a certain scan-line position Area-Subdivision Method The area-subdivision method takes advantage by locating those view areas that represent part of a single surface Divide the total viewing area into smaller and smaller rectangles until each small area is the projection of part of a single visible surface or no surface at all Continue this process until the subdivisions are easily analyzed as belonging to a single surface or until they are reduced to the size of a single pixel An easy way to this is 61 Computer Graphics to successively divide the area into four equal parts at each step There are four possible relationships that a surface can have with a specified area boundary Surrounding surface: One that completely encloses the area Overlapping surface: One that is partly inside and partly outside the area Inside surface: One that is completely inside the area Outside surface: One that is completely outside the area The tests for determining surface visibility within an area can be stated in terms of these four classifications No further subdivisions of a specified area are needed if one of the following conditions is true: All surfaces are outside surfaces with respect to the area Only one inside, overlapping or surrounding surface is in the area A surrounding surface obscures all other surfaces within the area boundaries Back-Face Detection A fast and simple object-space method for identifying the back faces of a polyhedron is based on the "inside-outside" tests A point (x, y, z) is "inside" a polygon surface with plane parameters A, B, C, and D if When an inside point is along the line of sight to the surface, the polygon must be a back face (we are inside that face and cannot see the front of it from our viewing position) We can simplify this test by considering the normal vector N to a polygon surface, which has Cartesian components (A, B, C) In general, if V is a vector in the viewing direction from the eye (or "camera") position, then this polygon is a back face if V.N > 62 Computer Graphics Furthermore, if object descriptions are converted to projection coordinates and your viewing direction is parallel to the viewing z-axis, then: V = (0, 0, Vz) and V.N = VZ C So that we only need to consider the sign of C the component of the normal vector N In a right-handed viewing system with viewing direction along the negative ZV axis, the polygon is a back face if C < Also, we cannot see any face whose normal has z component C = 0, since your viewing direction is towards that polygon Thus, in general, we can label any polygon as a back face if its normal vector has a z component value: C = when the viewing direction is along the positive zv axis By examining parameter C for the different planes defining an object, we can immediately identify all the back faces 63 Computer Graphics A-Buffer Method The A-buffer method is an extension of the depth-buffer method The A-buffer method is a visibility detection method developed at Lucas film Studios for the rendering system Renders Everything You Ever Saw (REYES) The A-buffer expands on the depth buffer method to allow transparencies The key data structure in the A-buffer is the accumulation buffer Each position in the A-buffer has two fields: 1) Depth field: It stores a positive or negative real number 2) Intensity field: It stores surface-intensity information or a pointer value 64 Computer Graphics If depth >= 0, the number stored at that position is the depth of a single surface overlapping the corresponding pixel area The intensity field then stores the RGB components of the surface color at that point and the percent of pixel coverage If depth < 0, it indicates multiple-surface contributions to the pixel intensity The intensity field then stores a pointer to a linked list of surface data The surface buffer in the A-buffer includes: RGB intensity components Opacity Parameter Depth Percent of area coverage Surface identifier The algorithm proceeds just like the depth buffer algorithm The depth and opacity values are used to determine the final color of a pixel Depth Sorting Method Depth sorting method uses both image space and object-space operations The depthsorting method performs two basic functions: First, the surfaces are sorted in order of decreasing depth Second, the surfaces are scan-converted in order, starting with the surface of greatest depth The scan conversion of the polygon surfaces is performed in image space This method for solving the hidden-surface problem is often referred to as the painter's algorithm The following figure shows the effect of depth sorting: 65 Computer Graphics Figure: Depth Sorting The algorithm begins by sorting by depth For example, the initial “depth” estimate of a polygon may be taken to be the closest z value of any vertex of the polygon Let us take the polygon P at the end of the list Consider all polygons Q whose z-extents overlap P’s Before drawing P, we make the following tests If any of the following tests is positive, then we can assume P can be drawn before Q Do the x-extents not overlap? Do the y-extents not overlap? Is P entirely on the opposite side of Q’s plane from the viewpoint? Is Q entirely on the same side of P’s plane as the viewpoint? Do the projections of the polygons not overlap? If all the tests fail, then we split either P or Q using the plane of the other The new cut polygons are inserting into the depth order and the process continues Theoretically, this partitioning could generate O(n2) individual polygons, but in practice, the number of polygons is much smaller Binary Space Partition (BSP) Trees Binary space partitioning is used to calculate visibility To build the BSP trees, one should start with polygons and label all the edges Dealing with only one edge at a time, extend each edge so that it splits the plane in two Place the first edge in the tree as root Add subsequent edges based on whether they are inside or outside Edges that span the extension of an edge that is already in the tree are split into two and both are added to the tree 66 Computer Graphics Figure: BSP Trees From the above figure, first take A as a root Make a list of all nodes in figure (a) Put all the nodes that are in front of root A to the left side of node A and put all those nodes that are behind the root A to the right side as shown in figure (b) Process all the front nodes first and then the nodes at the back As shown in figure (c), we will first process the node B As there is nothing in front of the node B, we have put NIL However, we have node C at back of node B, so node C will go to the right side of node B Repeat the same process for the node D 67 11 FRACTALS Computer Graphics A French/American mathematician Dr Benoit Mandelbrot discovered Fractals The word fractal was derived from a Latin word fractus which means broken What are Fractals? Fractals are very complex pictures generated by a computer from a single formula They are created using iterations This means one formula is repeated with slightly different values over and over again, taking into account the results from the previous iteration Fractals are used in many areas such as: Astronomy: For analyzing galaxies, rings of Saturn, etc Biology/Chemistry: For depicting bacteria cultures, Chemical reactions, human anatomy, molecules, plants, Others: For depicting clouds, coastline and borderlines, data compression, diffusion, economy, fractal art, fractal music, landscapes, special effect, etc Figure: Fractals Generation of Fractals Fractals can be generated by repeating the same shape over and over again as shown in the following figure In figure (a) shows an equilateral triangle In figure (b), we can see that the triangle is repeated to create a star-like shape In figure (c), we can see that the star shape in figure (b) is repeated again and again to create a new shape 68 Computer Graphics We can unlimited number of iteration to create a desired shape In programming terms, recursion is used to create such shapes (a) Zeroth Generation (b) First Generation (c) Second Generation Figure: Generation of Fractals Geometric Fractals Geometric fractals deal with shapes found in nature that have non-integer or fractal dimensions To geometrically construct a deterministic (nonrandom) self-similar fractal, we start with a given geometric shape, called the initiator Subparts of the initiator are then replaced with a pattern, called the generator Initiator Generator Figure: Initiator and Generator for fractals As an example, if we use the initiator and generator shown in the above figure, we can construct good pattern by repeating it Each straight-line segment in the initiator is replaced with four equal-length line segments at each step The scaling factor is 1/3, so the fractal dimension is D = ln 4/ln ≈ 1.2619 69 Computer Graphics Also, the length of each line segment in the initiator increases by a factor of 4/3 at each step, so that the length of the fractal curve tends to infinity as more detail is added to the curve as shown in the following figure: Segment Length = Segment Length = 1/3 Segment Length = 1/9 Length = Length = 4/3 Length = 16/9 70 Computer Graphics 12 COMPUTER ANIMATION Animation means giving life to any object in computer graphics It has the power of injecting energy and emotions into the most seemingly inanimate objects Computerassisted animation and computer-generated animation are two categories of computer animation It can be presented via film or video The basic idea behind animation is to play back the recorded images at the rates fast enough to fool the human eye into interpreting them as continuous motion Animation can make a series of dead images come alive Animation can be used in many areas like entertainment, computer aided-design, scientific visualization, training, education, e-commerce, and computer art Animation Techniques Animators have invented and used a variety of different animation techniques Basically there are six animation technique which we would discuss one by one in this section Traditional Animation (frame by frame) Traditionally most of the animation was done by hand All the frames in an animation had to be drawn by hand Since each second of animation requires 24 frames (film), the amount of efforts required to create even the shortest of movies can be tremendous Keyframing In this technique, a storyboard is laid out and then the artists draw the major frames of the animation Major frames are the ones in which prominent changes take place They are the key points of animation Keyframing requires that the animator specifies critical or key positions for the objects The computer then automatically fills in the missing frames by smoothly interpolating between those positions Procedural In a procedural animation, the objects are animated by a procedure - a set of rules not by keyframing The animator specifies rules and initial conditions and runs simulation Rules are often based on physical rules of the real world expressed by mathematical equations 71 Computer Graphics Behavioral In behavioral animation, an autonomous character determines its own actions, at least to a certain extent This gives the character some ability to improvise, and frees the animator from the need to specify each detail of every character's motion Performance Based (Motion Capture) Another technique is Motion Capture, in which magnetic or vision-based sensors record the actions of a human or animal object in three dimensions A computer then uses these data to animate the object This technology has enabled a number of famous athletes to supply the actions for characters in sports video games Motion capture is pretty popular with the animators mainly because some of the commonplace human actions can be captured with relative ease However, there can be serious discrepancies between the shapes or dimensions of the subject and the graphical character and this may lead to problems of exact execution Physically Based (Dynamics) Unlike key framing and motion picture, simulation uses the laws of physics to generate motion of pictures and other objects Simulations can be easily used to produce slightly different sequences while maintaining physical realism Secondly, real-time simulations allow a higher degree of interactivity where the real person can maneuver the actions of the simulated character In contrast the applications based on key-framing and motion select and modify motions form a pre-computed library of motions One drawback that simulation suffers from is the expertise and time required to handcraft the appropriate controls systems Key Framing A keyframe is a frame where we define changes in animation Every frame is a keyframe when we create frame by frame animation When someone creates a 3D animation on a computer, they usually don’t specify the exact position of any given object on every single frame They create keyframes Keyframes are important frames during which an object changes its size, direction, shape or other properties The computer then figures out all the in-between frames and saves an extreme amount of time for the animator The following illustrations depict the frames drawn by user and the frames generated by computer 72 Computer Graphics Figure: Frames drawn by user Figure: In-between frames generated by computer Morphing The transformation of object shapes from one form to another form is called morphing It is one of the most complicated transformations Figure: Original graphics 73 Computer Graphics Figure: A warped version of the original A morph looks as if two images melt into each other with a very fluid motion In technical terms, two images are distorted and a fade occurs between them 74