cgass4

Embed Size (px)

Citation preview

  • 8/8/2019 cgass4

    1/12

    Homework Title / No. : 4 Course Code :-CAP 405

    Course Instructor :- Sandeep Sharma Course Tutor (if applicable) : ____________

    Students Roll No.- A15 Section No. : RD3804

    Declaration:

    I declare that this assignment is my individual work. I have not copied from any other

    students work or from any other source except where due acknowledgment is made

    explicitly in the text, nor has any part been written for me by another person.

    Students Signature :-

    Surendra

  • 8/8/2019 cgass4

    2/12

    1) While clipping a polygon, it is said that Sutherland Hodge man is a better method

    than Wailer Atherton polygon clipping algorithm. Perform clipping on a Polygon

    and justify the above statement.

    Ans:-

    1. Sutherland Hodge man algorithm:-

    The algorithm begins with an inputlist of all vertices in the subject polygon. Next, one side

    of the clip polygon is extended infinitely in both directions, and the path of the subject

    polygon is traversed. Vertices from the input list are inserted into an output list if they lie on

    the visible side of the extended clip polygon line, and new vertices are added to the output

    list where the subject polygon path crosses the extended clip polygon line.

    This process is repeated iteratively for each clip polygon side, using the output list from one

    stage as the input list for the next. Once all sides of the clip polygon have been processed, thefinal generated list of vertices defines a new single polygon that is entirely visible. Note that

    if the subject polygon wasconcave at vertices outside the clipping polygon, the new polygon

    may have coincident (i.e. overlapping) edges this is acceptable for rendering, but not for

    other applications such as computing shadows.

    EXAMPLE:-

    All steps of clipping concave polygon 'W' by 5-sided convex polygon

    TheWeilerAtherton algorithm overcomes this by returning a set of divided polygons,

    but is more complex and computationally more expensive, so SutherlandHodge man isused for many rendering applications. SutherlandHodge man can also be extended

    http://en.wikipedia.org/wiki/List_(computing)http://en.wikipedia.org/wiki/List_(computing)http://en.wikipedia.org/wiki/Concave_polygonhttp://en.wikipedia.org/wiki/Concave_polygonhttp://en.wikipedia.org/wiki/Weiler%E2%80%93Athertonhttp://en.wikipedia.org/wiki/Weiler%E2%80%93Athertonhttp://en.wikipedia.org/wiki/Weiler%E2%80%93Athertonhttp://en.wikipedia.org/wiki/File:Sutherland-Hodgman_clipping_sample.svghttp://en.wikipedia.org/wiki/Concave_polygonhttp://en.wikipedia.org/wiki/Weiler%E2%80%93Athertonhttp://en.wikipedia.org/wiki/List_(computing)
  • 8/8/2019 cgass4

    3/12

    into 3D space by clipping the polygon paths based on the boundaries of planes defined

    by the viewing space

    .

    Wilier Atherton algorithm:-

    The algorithm requires polygons to be clockwise and not reentrant (self intersecting). The

    algorithm can support holes (as counter-clockwise polygons wholly inside their parent

    polygon), but requires additional algorithms to decide which polygons are holes. Merging of

    polygons can also be performed by a variant of the algorithm.

    Two lists are created from the coordinates of each polygons A and B, where A is the clip

    region and B is the polygon to be clipped.

    The list entries are labeled as either inside or outside the other polygon. Various strategies

    can be used to improve the speed of this labeling, and to avoid needing to proceed further.

    All the polygon intersections are then found and are inserted into both lists, linking the lists

    at the intersections. Care will be needed where the polygons share an edge.

    If there are no intersections then one of three situations exist:

    1. A is inside B - return A for clipping, B for merging.

    2. B is inside A - return B for clipping, A for merging.3. A and B do not overlap - return None for clipping or A & B for merging.

    A list of inbound intersections is then generated. Each intersection in the list is then followed

    clockwise around the linked lists until the start position is found. One or more concave

    polygons may produce more than one intersecting polygon. Convex polygons will only have

    one intersecting polygon.

    The same algorithm can be used for merging two polygons by starting at the outbound

    intersections rather than the inbound ones. However this can produce counter-clockwise

    holes.

    Some polygon combinations may be difficult to resolve, especially when holes are allowed.

    Points very close to the edge of the other polygon may be considered as both in and out until

    their status can be confirmed after all the intersections have been found and verified, however

    this increases the complexity.

    EXAMPLE :-

  • 8/8/2019 cgass4

    4/12

    After clipping:-

    2) Write a procedure for area-Subdivision algorithm for visible surface.

    Ans:- The area-subdivision method takes advantage of area coherence in a scene by locating thoseview areas that represent part of a single surface. The total viewing area is successively divided into

    smaller and smaller rectangles until each small area is simple, ie. it is a single pixel, or is covered

    wholly by a part of a single visible surface or no surface at all.

  • 8/8/2019 cgass4

    5/12

    Procedure:-

    1. Initialize the area to be the whole screen.

    2. Create a PVPL with respect to an area, sorted on Zmin the smallest z coordinateof the polygon with in area). Place the polygon in their appropriate categories.

    Remove polygons hidden by a surrounding polygon and remove disjoint

    polygons.

    3. Perform the visibility decision tests:

    a) If the test is empty, set all pixels to the background colour.

    b) If there is exactly one polygon in the list and it is a classified as

    intersecting or contained , colour the polygon and colour the remaining

    area to the background colour.

    c) If there is exactly one polygon on the list and it is surroundings one,

    colour the area with the colour of the surroundings polygon .

    d) If the area is the pixel(x,y) and neither a,b nor c applies, compute the z

    coordinate z(x,y) at pixel (x,y) of all polygons on the PVPL. The pixel is

    then set to the colour pf the polygon with the smallest z coordinate.

    4. If none of the above cases has occurred, subdivide the screen area in to fourths.

    For each area go to step2.

  • 8/8/2019 cgass4

    6/12

    3) Write a program that allows a user to design a picture from a menu of

    basic shapes by dragging each selected shape into position with a pick

    device.

    Ans:-

    #include

    #include

    #include

    #include

    #include

    union REGS in, out;

    int cirrad1=0,cirrad2;

    void detectmouse ()

    {

    in.x.ax = 0;

    int86 (0X33,&in,&out);

    if (out.x.ax == 0)

    printf ("\nMouse Fail To Initialize");

    else

    printf ("\nMouse Succesfully Initialize");

    }

    void showmousetext ()

    {

    in.x.ax = 1;

    int86 (0X33,&in,&out);

  • 8/8/2019 cgass4

    7/12

    }

    void showmousegraphics ()

    {

    in.x.ax = 1;

    int86 (0X33,&in,&out);

    getch ();

    closegraph ();

    }

    void hidemouse ()

    {

    in.x.ax = 2;

    int86 (0X33,&in,&out);

    }

    void draw ()

    { while(out.x.bx!=2)

    {

    int x,y,x1,y1;

    in.x.ax = 3;

    int86 (0X33,&in,&out);

    cleardevice();

    if (out.x.bx == 1)

    {

    x = out.x.cx;

    y = out.x.dx;

    setcolor(10);

    circle(x,y,cirrad1);

  • 8/8/2019 cgass4

    8/12

    }

    if (out.x.bx == 1)

    {

    x = out.x.cx;

    y = out.x.dx;

    setcolor(10);

    circle(x,y,cirrad2);

    }

    if (out.x.bx == 1)

    {

    x = out.x.cx;

    y = out.x.dx;

    //setcolor(10);

    // circle(x,y,cirrad2);

    }

    if (out.x.bx == 1)

    {

    x1 = out.x.cx;

    y1 = out.x.dx;

    }

    line(x,y,x+34,y+23);

    line(x,y,x-90,y-0);

    delay (10);

    }

    getch();

    }

    int main ()

  • 8/8/2019 cgass4

    9/12

    {

    coutcirrad2;

    clrscr();

    int gdriver = DETECT, gmode, errorcode;

    initgraph(&gdriver, &gmode, "d:\\tc\\bgi");

    detectmouse ();

    showmousetext ();

    draw ();

    hidemouse ();

    getch ();

    return 0;

    }

    Part B

    4) Design the scan-line algorithm for the removal of hidden lines from a

    scene.

    Ans:-

    1. The edge list contains all non horizontal edges of the projection of the polygon in the

    scene. The edges are sorted by the edge smaller y coordinate Y min. Each edge entry

    in the edge list also contain

    a) The x coordinate of the end of the edge with the smaller y coordinate.

    b) The y coordinate of the edge other end (Y max).

    c) The increment dx =1/m;

    d) A pointer indicate the polygon to which the edge belongs.

    2. The polygon list for each polygon contains

  • 8/8/2019 cgass4

    10/12

    a) The equation of the plane within which the polygon lies used for depth

    determines to find the value Z value pixel(x,y).

    b) An IN/OUT flag initialized to OUT.

    c) Colour information for the polygon.

    The algorithm proceeds as follows

    1. Initialization

    a) Initialize each screen pixel to the background colour.

    b) Set y to the smallest Y min value in the edge.

    Repeat the step 2 and 3 until no further processing can be performed.

    2. Y-scan loop .active edge whose Ymin is equal to y. Sort active edges in order of

    increasing x.

    3. X-scan loop. Process from left to right, each active edge as follows

    a) Invert the IN/OUT flag of the polygon list which contain the edge . count

    the number of active polygon whose IN/OUT flag is et to IN. If this

    number is one, only one polygon is visible. All the pixel values from this

    edge and up to the next edge are set to the colour of the polygon. If this

    number is greater than 1, determine the visible polygon by the smallest Z

    value of each polygon at the pixel under consideration. These z values are

    found from the equation of the plane containing the polygon. The pixels

    from this edge and up to the next edge are set to the colour of this

    polygon, unless the polygon becomes obscured by another before the next

    edge is reached , in which case we set the remaining pixels to the colour of

    the obscuring polygon. If this number is 0,pixels from this edge and up to

    the next one are left unchanged.

    b). when the last active edge is processed , we then proceed as follows:

    i. Remove those edges for which the value of Y max equals thepresent scan line value Y . if no edges remain, the algorithm has

    finished.

    ii. For each remaining active edge , in order ,replace x by x+1/m. This

    is the edge intersection with the next scan line Y+1.

    iii. Increment Y to Y+1,th next scan line, and repeat step 2.

  • 8/8/2019 cgass4

    11/12

    5) Suppose you are given an image. How will you detect the presence of

    Hidden surfaces and remove hindrance from the image?

    Ans:-

    1. Object-space MethodsCompare objects and parts of objects to each other within the scene definition to determine which

    surfaces, as a whole, we should label as visible:

    For each object in the scene do

    Begin

    1. Determine those part of the object whose view is unobstructed by other parts of it or

    any other object with respect to the viewing specification.

    2. Draw those parts in the object color.

    End

    o Compare each object with all other objects to determine the visibility of the objectparts.

    o If there are n objects in the scene, complexity = O(n2)

    o Calculations are performed at the resolution in which the objects are defined (only

    limited by the computation hardware).

    o Process is unrelated to display resolution or the individual pixel in the image and the

    result of the process is applicable to different display resolutions.

    o Display is more accurate but computationally more expensive as compared to image

    space methods because step 1 is typically more complex, eg. Due to the possibility

    of intersection between surfaces.

    o Suitable for scene with small number of objects and objects with simple relationship

    with each other.

    2. Image-space Methods :-

    Visibility is determined point by point at each pixel position on the projection plane.

    For each pixel in the image do

    Begin

    1. Determine the object closest to the viewer that is pierced by the projector through the pixel

    2. Draw the pixel in the object color .

    End

    o For each pixel, examine all n objects to determine the one closest to the viewer.

    o If there are p pixels in the image, complexity depends on n and p ( O(np) ).

    o Accuracy of the calculation is bounded by the display resolution.

    o A change of display resolution requires re-calculation

    Application of Coherence in Visible Surface Detection Methods: -

    o Making use of the results calculated for one part of the scene or image for other

    nearby parts.

    o Coherence is the result of local similarity

    o As objects have continuous spatial extent, object properties vary smoothly within a

    small local region in the scene. Calculations can then be made incremental.

  • 8/8/2019 cgass4

    12/12

    6) Is Z-buffer better than other hidden surface algorithm? Give reasons.

    Ans:- YES, Z buffering is better than other hidden surface algorithm because

    1. z buffering (or depth buffering) is one of the simplest hidden surface algorithms

    2. via hardware or software, an extra "z" buffer is maintained along with frame buffer. It

    keeps track of the nearest object at each pixel location.

    3. initialized to minimum z value (eg. most negative)

    4. then, when object being drawn, if its z coordinate at a point is greater (more positive,

    less distance to viewer) than z buffer value, it is drawn, and new z coord is saved;

    otherwise, it is not drawn

    5. if a line segment in 3D is being drawn, then the intermediate z values between

    endpoint z coords are interpolated: linear interpolation for polygons, and can computez for more complex surfaces

    6. some problems:

    o aliasing: limited to size of z buffer word per pixel; can have imprecision at

    extremely far distances --> important to have as small a viewing volume as

    possible

    7. possible to write into and manipulate z buffer memory directly, eg. cursors