Upload
surendra-singh-chauhan
View
218
Download
0
Embed Size (px)
Citation preview
8/8/2019 Graphics 4 (1)
1/8
HOME WORK TITLE NO: - 4 COURSE CODE: - CAP 405
COURSE INSTRUCTOR: - SANJAY SOOD COURSE TUTOR:-
DOA: - 24 -4-10 DOS: - 08-5-10
STUDENTS ROLL NO: - B40 SECTION NO: - RD3803
DECLARATION: - I declare that this assignment is my individual work. I have not
copied from any other student work or from any other source, except when dueacknowledgement is made explicitly in the text, not has any part being written for me
by another person.
STUDENTS SIGNATURE
Deepti Sharma
EVALUATORS COMMENT: -
MARKS: - OUT OF: -
Part A
Q1: - While clipping a polygon, it is said that Sutherland Hodgeman is a better method
than Weiler Atherton polygon clipping algorithm. Perform clipping on a Polygon and
justify the above statement.
Ans: - Yes, it is true that Sutherland Hodgeman is a better method than Weiler Atherton
polygon clipping algorithm because special attention is necessary in using the Sutherland-
Hodgeman algorithm in order to avoid unwanted effects.
Ex: - Sutherland Hodgeman algorithm
Left: - v1 v2 v3 v4 v5 v6 v7 v7 v1
Top: - v1 v2 v3 v4 v5 v6 v7 v7 v1
8/8/2019 Graphics 4 (1)
2/8
Right: - v2 v3 v4 v5 v6 v7 v7 v1 v1 v2
Below: - v3 v4 v5 v6 v7 v1 v1 v2 v2
Q2: - Write a procedure for area-Subdivision algorithm for visible surface.
Ans: - Algorithm for area-subdivision: -
Step 1: - Initialize the area to be the whole screen.
Step 2: - Create a PVPL with respect to an area, sorted on Zmin. Place the polygons in their
appropriate categories. Remove polygons hidden by a surrounding polygon and remove
disjoint polygons.
Step 3: - Perform the visibility decision tests:
a) If the list is empty, set all pixels to the background color.
b) If there is exactly one polygon in the list and it is a classified as intersecting or
contained, color the polygon, and color the remaining area to the background color.
c) If there is exactly one polygon on the list and it is a surrounding one, color the area
with the color of the surrounding polygon.
d) If the area is the pixel (x, y), and neither a, b, nor c applies, compute the Z co-ordinate
z (x, y) at pixel (x, y) of all polygons on the PVPL. The pixel is then set to the colorof the polygon with the smallest z co-ordinate.
Step 4: - If none of the above cases has occurred, subdivide the screen area into fourths. For
each area, go to step 2.
Q3: - 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;
8/8/2019 Graphics 4 (1)
3/8
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);
}
void showmousegraphics ()
{
in.x.ax = 1;
int86 (0X33,&in,&out);
getch ();
closegraph ();
}
void hidemouse ()
{
in.x.ax = 2;
int86 (0X33,&in,&out);
}
8/8/2019 Graphics 4 (1)
4/8
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);
}
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);
8/8/2019 Graphics 4 (1)
5/8
// 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 ()
{
coutcirrad2;
clrscr();
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "d:\\tc\\bgi");
detectmouse ();
showmousetext ();
draw ();
hidemouse ();
8/8/2019 Graphics 4 (1)
6/8
getch ();
return 0;
}
Part B
Q1: - Design the scan-line algorithm for the removal of hidden lines from a scene.
Ans: - The algorithm is given as: -
Step 1: - Initialization
a) Initialize each screen pixel to the background color.
b) Set y to the smallest Ymin value in the edge list.
Repeat steps 1 and 3 (below) until no further processing can be performed.
Step 2: - y-scan loop. Activate edges whose Ymin is equal to y. Sort active edges in order of
increasing x.
Step 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 contains the edge. Count the number
of active polygons whose IN/OUT flag is set to IN. If this number is 1, only one
polygon is visible. All pixels values from this edge and up to the next edge are set to
the color 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 color 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 color 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:
1) Remove those edges for which the value of Ymax equals the present scan line value y.
If no edges remain, the algorithm has finished.
2) 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
3) Increment y to y+1, the next scan line, and repeat step 2.
8/8/2019 Graphics 4 (1)
7/8
Q2: - Suppose you are given an image. How will you detect the presence of Hidden
surfaces and remove hindrance from the image?
Ans: -Hidden surface removal algorithms: -
Considering the rendering pipeline, the projection, the clipping, and the rasterization steps arehandled differently by the following algorithms:
Z-buffering : - During rasterization the depth/Z value of each pixel is checked against
an existing depth value. If the current pixel is behind the pixel in the Z-buffer, the
pixel is rejected, otherwise it is shaded and its depth value replaces the one in the Z-
buffer. Z-buffering supports dynamic scenes easily, and is currently implemented
efficiently in graphics hardware. This is the current standard. The cost of using Z-
buffering is that it uses up to 4 bytes per pixel, and that the rasterization algorithm
needs to check each rasterized sample against the z-buffer. The z-buffer can also
suffer from artefacts due to precision errors although this is far less common now thatcommodity hardware supports 24-bit and higher precision buffers.
Coverage buffers and Surface buffer : - faster than z-buffers and commonly used in
games in the Quake I era. Instead of storing the Z value per pixel, they store list of
already displayed segments per line of the screen. New polygons are then cut against
already displayed segments that would hide them. A S-Buffer can display unsorted
polygons, while a C-Buffer require polygons to be displayed from the nearest to the
furthest. C-buffer having no overdrawn, they will make the rendering a bit faster.
They were commonly used with BSP trees which would give the polygon sorting.
Sorted Active Edge List : - used in Quake 1, this was storing a list of the edges of
already displayed polygons. Polygons are displayed from the nearest to the furthest.
New polygons are clipped against already displayed polygons' edges, creating newpolygons to display then storing the additional edges. It's much harder to implement
than S/C/Z buffers, but it will scale much better with the increase in resolution.
Painter's algorithm : - sorts polygons by their barycentre and draws them back to
front. This produces few artefacts when applied to scenes with polygons of similar
size forming smooth meshes and back face culling turned on. The cost here is the
sorting step and the fact that visual artefacts can occur.
Binary space partitioning : - (BSP) divides a scene along planes corresponding to
polygon boundaries. The subdivision is constructed in such a way as to provide an
unambiguous depth ordering from any point in the scene when the BSP tree is
traversed. The disadvantage here is that the BSP tree is created with an expensive pre-
process. This means that it is less suitable for scenes consisting of dynamic geometry.The advantage is that the data is pre-sorted and error free, ready for the previously
mentioned algorithms. Note that the BSP is not a solution to HSR, only a help.
Ray tracing : - attempts to model the path of light rays to a viewpoint by tracing rays
from the viewpoint into the scene. Although not a hidden surface removal algorithm
as such, it implicitly solves the hidden surface removal problem by finding the nearest
surface along each view-ray. Effectively this is equivalent to sorting all the geometry
on a per pixel basis.
The Warnock algorithm : - divides the screen into smaller areas and sorts triangles
within these. If there is ambiguity (i.e., polygons overlap in depth extent within these
areas), then further subdivision occurs. At the limit, subdivision may occur down to
the pixel level.
8/8/2019 Graphics 4 (1)
8/8
Q3: - Is Z-buffer better than other hidden surface algorithm? Give reasons.
Ans: - Z-buffer algorithm requires Z-buffer memory storage proportional to the number of
pixels on the screen; it does not require additional memory for storing all the objectscomprising the scene. In fact, since the algorithm processes polygons one at a time, the total
number of objects in a scene can be arbitrarily large. So it is better than other hidden surface
algorithm.