Sie sind auf Seite 1von 41

EASWARI ENGINEERING COLLEGE

L A B M A N U A L

DEPARTMENT OF INFORMATION TECHNOLOGY

CS2405

COMPUTER GRAPHICS LABORATORY

LAB MANUAL

IV YEAR IT July 2013 TO October 2013

PREPARED BY Mrs. B.Arthi Mrs. A.Sathya


1|Page

CS2405

COMPUTER GRAPHICS LABORATORY

LTPC 0032

1. Implementation of Bresenhams Algorithm Line, Circle, Ellipse. 2. Implementation of Line, Circle and ellipse attributes 3. Two Dimensional transformations - Translation, Rotation, Scaling, Reflection, Shear. 4. Composite 2D Transformations 5. Cohen Sutherland 2D line clipping and Windowing 6. Sutherland Hodgeman Polygon clipping Algorithm 7. Three dimensional transformations - Translation, Rotation, Scaling 8. Composite 3D transformations 9. Drawing three dimensional objects and Scenes 10. Generating Fractal images

TOTAL: 45 PERIODS

LIST OF EQUIPMENTS:

1) Turbo C 2) Visual C++ with OPENGL 3) Any 3D animation software like 3DSMAX, Maya, Blender

List of Experiments (CS2405)


2|Page

1.

Implementation of drawing

Bresenhams algorithms for line, circle and ellipse

1.1Bresenhams Line Algorithm 1.2Bresenhams circle Algorithm 1.3Bresenhams ellipse Algorithm 2. Implementation of the basic Line, Circle and Ellipse attributes 2.1 Basic Line Attributes 2.2 Basic Circle Attributes 2.3 Basic Ellipse Attributes 3. 2D Transformations - translation, rotation, scaling, reflection and shearing. 4. 2D Composite Transformations 5. Implementation of mapping 6. Implementation of Sutherland Hodgeman Polygon clipping algorithm. 7. 3D Transformations like translation, rotation and scaling. 8. 3D Composite Transformations Cohen-Sutherland 2D clipping and window-viewport

9. Creation of three Dimensional objects and scenes. 10.Generation of fractal images using OpenGL BEYOND THE SYLLABUS 11. Generation of basic Output primitives using OpenGL 12. Conversion Between Color Models MINI PROJECT 13.Create an Animation for butterfly life cycle using C 14.Develop TajMahal using openGL

3|Page

C graphics tutorial Functions of graphics.h Graphics.h functions can be used to draw different shapes, display text in different fonts, change colors and many more. Using functions of graphics.h in turbo c compiler you can make graphics programs, animations, projects and games.You can draw circles, lines, rectangles, bars and many other geometrical figures. You can change their colors using the available functions and fill them. Following is a list of functions of graphics.h header file. Every function is discussed with the arguments it needs, its description, possible errors while using that function and a sample c graphics program with its output. 1. arc function in c Declaration :- void arc(int x, int y, int stangle, int endangle, int radius); arc function is used to draw an arc with center (x,y) and stangle specifies starting angle, endangle specifies the end angle and last parameter specifies the radius of the arc. arc function can also be used to draw a circle but for that starting angle and end angle should be 0 and 360 respectively. Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); arc(100, 100, 0, 135, 50); getch(); closegraph(); return 0; } In the above program (100,100) are coordinates of center of arc, 0 is the starting angle, 135 is the end angle and 50 specifies the radius of the arc. 2. bar function in c Declaration :- void bar(int left, int top, int right, int bottom); bar function is used to draw a 2-dimensional, rectangular filled in bar . Coordinates of left top and right bottom corner are required to draw the bar. left specifies the Xcoordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom specifies the Y-coordinate of right bottom corner.Current fill pattern and fill color is used to fill the bar. To change fill pattern and fill color use setfillstyle. Example #include<graphics.h> #include<conio.h> main() { int gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); bar(100, 100, 200, 200); getch(); closegraph(); return 0;} 4|Page

3. bar3d function in c Declaration :- void bar3d(int left, int top, int right, int bottom, int depth, int topflag); bar3d function is used to draw a 2-dimensional, rectangular filled in bar . Coordinates of left top and right bottom corner of bar are required to draw the bar. left specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom specifies the Y-coordinate of right bottom corner, depth specifies the depth of bar in pixels, topflag determines whether a 3 dimensional top is put on the bar or not ( if it is non-zero then it is put otherwise not ). Current fill pattern and fill color is used to fill the bar. To change fill pattern and fill color use setfillstyle. Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); bar3d(100, 100, 200, 200, 20, 1); getch(); closegraph(); return 0; } 4. circle function in c Declaration :- void circle(int x, int y, int radius); circle function is used to draw a circle with center (x,y) and third parameter specifies the radius of the circle. The code given below draws a circle. Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); circle(100, 100, 50); getch(); closegraph(); return 0; } In the above program (100, 100) are coordinates of center of the circle and 50 is the radius of circle. 5. cleardevice function in c Declaration :- void cleardevice(); cleardevice function clears the screen in graphics mode and sets the current position to (0,0). Clearing the screen consists of filling the screen with current background color. Example 5|Page

#include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); outtext("Press any key to clear the screen."); getch(); cleardevice(); outtext("Press any key to exit..."); getch(); closegraph(); return 0; } 6. closegraph function in c closegraph function closes the graphics mode, deallocates all memory allocated by graphics system and restores the screen to the mode it was in before you called initgraph. Declaration :- void closegraph(); Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); outtext("Press any key to close the graphics mode..."); getch(); closegraph(); return 0; } 7. drawpoly function in c drawpoly function is used to draw polygons i.e. triangle, rectangle, pentagon, hexagon etc. Declaration :- void drawpoly( int num, int *polypoints ); num indicates (n+1) number of points where n is the number of vertices in a polygon, polypoints points to a sequence of (n*2) integers . Each pair of integers gives x and y coordinates of a point on the polygon. We specify (n+1) points as first point coordinates should be equal to (n+1) th to draw a complete figure. To understand more clearly we will draw a triangle using drawpoly, consider for example the array :int points[] = { 320, 150, 420, 300, 250, 300, 320, 150}; 6|Page

points array contains coordinates of triangle which are (320, 150), (420, 300) and (250, 300). Note that last point(320, 150) in array is same as first. See the program below and then its output, it will further clear your understanding. Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; int points[] = { 320, 150, 420, 300, 250, 300, 320, 150}; initgraph(&gd, &gm, "C:\\TC\\BIN"); drawpoly(4, points); getch(); closegraph(); return 0; } 8. ellipse function in c Declarations of ellipse function :void ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius); Ellipse is used to draw an ellipse (x,y) are coordinates of center of the ellipse, stangle is the starting angle, end angle is the ending angle, and fifth and sixth parameters specifies the X and Y radius of the ellipse. To draw a complete ellipse strangles and end angle should be 0 and 360 respectively. Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BIN"); ellipse(100, 100, 0, 360, 50, 25); getch(); closegraph(); return 0; } 9. fillellipse function in c Declaration of fillellipse function :void fillellipse(int x, int y, int xradius, int yradius); x and y are coordinates of center of the ellipse, xradius and yradius are x and y radius of ellipse respectively. Example #include<graphics.h> #include<conio.h> int main() 7|Page

{ int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); fillellipse(100, 100, 50, 25); getch(); closegraph(); return 0; } 10. fillpoly function in c fillpoly function draws and fills a polygon. It require same arguments as drawpoly. Declaration :- void drawpoly( int num, int *polypoints ); For details of arguments see drawpoly. fillpoly fills using current fill pattern and color which can be changed using setfillstyle. Example #include<graphics.h> #include<conio.h> main() { int gd = DETECT, gm; int points[] = { 320, 150, 440, 340, 230, 340, 320, 150 }; initgraph(&gd, &gm, "C:\\TC\\BIN"); fillpoly(4, points); getch(); closegraph(); return 0; } 11. floodfill function Declaration :- void floodfill(int x, int y, int border); floodfill function is used to fill an enclosed area. Current fill pattern and fill color is used to fill the area.(x, y) is any point on the screen if (x,y) lies inside the area then inside will be filled otherwise outside will be filled,border specifies the color of boundary of area. To change fill pattern and fill color use setfillstyle. Code given below draws a circle and then fills it. Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); setcolor(RED); circle(100,100,50); floodfill(100,100,RED); getch(); closegraph(); return 0; } 8|Page

In the above program a circle is drawn in RED color. Point (100,100) lies inside the circle as it is the center of circle, third argument to floodfill is RED which is color of boundary of circle. So the output of above program will be a circle filled with WHITE color as it is the default fill color. 12. getmaxx function in c getmaxx function returns the maximum X coordinate for current graphics mode and driver. Declaration :- int getmaxx(); Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; int maxx; char a[100]; initgraph(&gd,&gm,"C:\\TC\\BGI"); maxx = getmaxx(); sprintf(a,"Maximum X coordinate for current graphics mode and driver = %d.",maxx); outtext(a); getch(); closegraph(); return 0; } 13. getmaxy function in c getmaxy function returns the maximum Y coordinate for current graphics mode and driver. Declaration :- int getmaxy(); Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; int maxy; char a[100]; initgraph(&gd,&gm,"C:\\TC\\BGI"); maxy = getmaxy(); sprintf(a,"Maximum Y coordinate for current graphics mode and driver is = %d.",maxy); outtext(a); getch(); closegraph(); return 0; } 14. getpixel function in c getpixel function returns the color of pixel present at location(x, y). 9|Page

Declaration :- int getpixel(int x, int y); Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; int color; char array[50]; initgraph(&gd,&gm,"C:\\TC\\BGI"); color = getpixel(0, 0); sprintf(array,"color of pixel at (0,0) = %d",color); outtext(array); getch(); closegraph(); return 0; } As we haven't drawn anything on screen and by default screen is BLACK, therefore color of pixel at (0,0) is BLACK. So output of program will be color of pixel at (0,0) is 0, as 0 indicates BLACK color. 15. getx function in c getx function returns the X coordinate of current position. Declaration :- int getx(); Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; char array[100]; initgraph(&gd, &gm, "C:\\TC\\BIN"); sprintf(array, "Current position of x = %d",getx()); outtext(array); getch(); closegraph(); return 0; } 16. gety function in c gety function returns the y coordinate of current position. Declaration :- int gety(); Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; char message[100]; initgraph(&gd, &gm, "C:\\TC\\BIN"); sprintf(message, "Current position of y = %d",gety()); 10 | P a g e

outtext(message); getch(); closegraph(); return 0; } 17. line function in c line function is used to draw a line from a point(x1,y1) to point(x2,y2) i.e. (x1,y1) and (x2,y2) are end points of the line.The code given below draws a line. Declaration :- void line(int x1, int y1, int x2, int y2); Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); line(100, 100, 200, 200); getch(); closegraph(); return 0; } Above program draws a line from (100, 100) to (200, 200). 18. lineto function in c lineto function draws a line from current position(CP) to the point(x,y), you can get current position using getx and gety function. Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); moveto(100, 100); lineto(200, 200); getch(); closegraph(); return 0; } 19. linerel function in c linerel function draws a line from the current position(CP) to a point that is a relative distance (x, y) from the CP, then advances the CP by (x, y). You can use getx and gety to find the current position. Example #include<graphics.h> #include<conio.h> main() { 11 | P a g e

int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BIN"); moveto(250, 250); linerel(100, -100); getch(); closegraph(); return 0; } 20. moveto function in c moveto function changes the current position (CP) to (x, y) Declaration :- void moveto(int x, int y); Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; char msg[100]; initgraph(&gd, &gm, "C:\\TC\\BIN"); sprintf(msg, "X = %d, Y = %d",getx(),gety()); outtext(msg); moveto(50, 50); sprintf(msg, "X = %d, Y = %d", getx(), gety()); outtext(msg); getch(); closegraph(); return 0; } 21. moverel function in c moverel function moves the current position to a relative distance. Declaration :- void moverel(int x, int y); Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; int x, y; char message[100]; initgraph(&gd, &gm, "C:\\TC\\BIN"); moveto(100, 100); moverel(100, -100); x = getx(); y = gety(); sprintf(message, "Current x position = %d and y position = %d", x, y); outtextxy(10, 10, message); getch(); closegraph(); return 0; 12 | P a g e

} 22. outtext function outtext function displays text at current position. Declaration :- void outtext(char *string); Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); outtext("To display text at a particular position on the screen use outtextxy"); getch(); closegraph(); return 0; } 23. outtextxy function in c outtextxy function display text or string at a specified point(x,y) on the screen. Declaration :- void outtextxy(int x, int y, char *string); x, y are coordinates of the point and third argument contains the address of string to be displayed. Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); outtextxy(100, 100, "Outtextxy function"); getch(); closegraph(); return 0; } 24. putpixel function in c putpixel function plots a pixel at location (x, y) of specified color. Declaration :- void putpixel(int x, int y, int color); For example if we want to draw a GREEN color pixel at (35, 45) then we will write putpixel(35, 35, GREEN); in our c program, putpixel function can be used to draw circles, lines and ellipses using various algorithms. Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); putpixel(25, 25, RED); 13 | P a g e

getch(); closegraph(); return 0; } Output of this program will be a RED pixel on screen at (25, 25) . Try to spot that pixel with your eyes at left top portion of your computer screen. 25. rectangle function in c Declaration :- void rectangle(int left, int top, int right, int bottom); rectangle function is used to draw a rectangle. Coordinates of left top and right bottom corner are required to draw the rectangle. left specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the Xcoordinate of right bottom corner, bottom specifies the Y-coordinate of right bottom corner. The code given below draws a rectangle. Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); rectangle(100,100,200,200); getch(); closegraph(); return 0; } 26. sector function in c sector function draws and fills an elliptical pie slice. Declaration :- void sector( int x, int y, int stangle, int endangle, int xradius, int yradius); Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BIN"); sector(100, 100, 0, 135, 25, 35); getch(); closegraph(); return 0; } 27. setbkcolor function in c Declaration :- void setbkcolor(int color); setbkcolor function changes current background color e.g. setbkcolor(YELLLOW) changes the current background color to YELLOW. Remember that default drawing color is WHITE and background color is BLACK. Example #include<graphics.h> #include<conio.h> 14 | P a g e

int main() { int gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); outtext("Press any key to change the background color to GREEN"); getch(); setbkcolor(GREEN); getch(); closegraph(); return 0; } 28. setfillstyle function in c setfillstyle function sets the current fill pattern and fill color. Declaration :- void setfillstyle( int pattern, int color); Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); setfillstyle(XHATCH_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); closegraph(); return 0; } 29. setviewport function in c setviewport function sets the current viewport for graphics output. Declaration :- void setviewport(int left, int top, int right, int bottom, int clip); setviewport function is used to restrict drawing to a particular portion on the screen. For example setviewport(100 , 100, 200, 200, 1); will restrict our drawing activity inside the rectangle(100,100, 200, 200). left, top, right, bottom are the coordinates of main diagonal of rectangle in which we wish to restrict our drawing. Also note that the point (left, top) becomes the new origin. Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; int midx, midy; initgraph(&gd,&gm,"C:\\TC\\BGI"); midx = getmaxx()/2; midy = getmaxy()/2; setviewport(midx - 50, midy - 50, midx + 50, midy + 50, 1); circle(50, 50, 55); getch(); 15 | P a g e

closegraph(); return 0; } 30. initgraph function in c To initialize graphics mode we use initgraph function in our program. initgraph function is present in "graphics.h" header file, so your every graphics program should include "graphics.h" header file. EXAMPLE #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI); getch(); closegraph(); return 0; } This program initializes graphics mode and then closes it after a key is pressed. To begin with we have declared two variables of int type gd and gm for graphics driver and graphics mode respectively. DETECT is a macro defined in "graphics.h" header file, then we have passed three arguments to initgraph function first is the address of gd, second is the address of gm and third is the path where your BGI files are present ( you have to adjust this accordingly where you turbo compiler is installed). Initgraph function automatically decides an appropriate graphics driver and mode such that maximum screen resolution is set, getch helps us to wait until a key is pressed, closegraph function closes the graphics mode and finally return statement returns a value 0 to main indicating successful execution of your program EXAMPLE C-GRAPHICS PROGRAMS 1. Draw shapes using c graphics This c graphics program draws basic shapes such as circle, line, rectangle, ellipse and display text on screen using c graphics. This can be a first graphics program for a beginner. Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; int left = 100,top = 100, right = 200, bottom = 200; int x = 300, y = 150, radius = 50; initgraph(&gd, &gm, "C:\\TC\\BGI"); rectangle(left, top, right, bottom); circle(x, y, radius); bar(left + 300, top, right + 300, bottom); line(left - 10, top + 150, left + 410, top + 150); ellipse(x, y + 200, 0, 360, 100, 50); outtextxy(left + 100, top + 325, "My First C Graphics Program"); 16 | P a g e

getch(); closegraph(); return 0; } 2. c program draw bar chart This program draws bar chart using c graphics. Chart is drawn using bars filled with different styles and in different colors. Example #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); setcolor(YELLOW); rectangle(0,30,639,450); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); setcolor(WHITE); outtextxy(275,0,"Bar Chart"); setlinestyle(SOLID_LINE,0,2); line(100,420,100,60); line(100,420,600,420); line(90,70,100,60); line(110,70,100,60); line(590,410,600,420); line(590,430,600,420); outtextxy(95,35,"Y"); outtextxy(610,405,"X"); outtextxy(85,415,"O"); setfillstyle(LINE_FILL,BLUE); bar(150,100,200,419); setfillstyle(XHATCH_FILL,RED); bar(225,150,275,419); setfillstyle(WIDE_DOT_FILL,GREEN); bar(300,200,350,419); setfillstyle(INTERLEAVE_FILL,MAGENTA); bar(375,125,425,419); setfillstyle(HATCH_FILL,BROWN); bar(450,175,500,419); getch(); return 0; } 3. C program to move a car Program in c using graphics move a car. A car is made using two rectangles and two circles which act as tyres of car. A for loop is used to move the car forward by changing the rectangle and circle coordinates and erasing the previous contents on screen using clearviewport, you can also use cleardevice. Speed of car can be adjusted using delay function, more the delay lesser will be the speed or lesser the delay your car will move fast. In this program color of the car also keeps on 17 | P a g e

changing, this is accomplished by incrementing the color value by one each time in for loop, you can also use random function for this purpose. Before you see a car moving you will be asked to press a key. Example #include<graphics.h> #include<dos.h> int main() { int i, j = 0, gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); settextstyle(DEFAULT_FONT,HORIZ_DIR,2); outtextxy(25,240,"Press any key to view the moving car"); getch(); setviewport(0,0,639,440,1); for( i = 0 ; i <= 420 ; i = i + 10, j++ ) { rectangle(50+i,275,150+i,400); rectangle(150+i,350,200+i,400); circle(75+i,410,10); circle(175+i,410,10); setcolor(j); delay(100); if( i == 420 ) break; clearviewport(); } getch(); closegraph(); return 0; } 4. c smiling face animation This animation using c draws a smiling face which appears at random position on screen. Example #include<graphics.h> #include<conio.h> #include<stdlib.h> int main() { int gd = DETECT,gm; int area, temp1, temp2; int left = 25, top = 75; char *p; initgraph(&gd,&gm,"C:\\TC\\BGI"); setcolor(YELLOW); circle(50,100,25); setfillstyle(SOLID_FILL,YELLOW); floodfill(50,100,YELLOW); setcolor(BLACK); setfillstyle(SOLID_FILL,BLACK); fillellipse(44,85,2,6); 18 | P a g e

fillellipse(56,85,2,6); ellipse(50,100,205,335,20,9); ellipse(50,100,205,335,20,10); ellipse(50,100,205,335,20,11); area = imagesize(left, top, left + 50, top + 50); p = malloc(area); setcolor(WHITE); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); outtextxy(155,451,"Smiling Face Animation"); setcolor(BLUE); rectangle(0,0,639,449); while(!kbhit()) { temp1 = 1 + random ( 588 ); temp2 = 1 + random ( 380 ); getimage(left, top, left + 50, top + 50, p); putimage(left, top, p, XOR_PUT); putimage(temp1 , temp2, p, XOR_PUT); delay(100); left = temp1; top = temp2; } getch(); closegraph(); return 0; } 5. c program countdown This c graphics program performs countdown for 30 seconds. Example #include<graphics.h> #include<dos.h> #include<conio.h> int main() { int gd = DETECT, gm; int i; char a[5]; initgraph( &gd, &gm,"C:\\TC\\BGI"); settextjustify( CENTER_TEXT, CENTER_TEXT ); settextstyle(DEFAULT_FONT,HORIZ_DIR,3); setcolor(RED); for(i = 30 ; i >=0 ; i--) { sprintf(a,"%d",i); outtextxy(getmaxx()/2, getmaxy()/2, a); delay(1000); if ( i == 0 ) break; cleardevice(); 19 | P a g e

} getch(); closegraph(); return 0; }

EX.NO:1 BRESENHAMS LINE, CIRCLE, ELLIPSE DRAWING Ex. No: 1.1 Aim: To implement the bresenhams algorithm for line Algorithm: Bresenhams Line Drawing Algorithm : 1. Input the two line end points and store the left end point in (x 0,y0) 2. Load (x0,y0) into the frame buffer,(i.e) plot the first point. 3. Calculate constrants x, y, 2y and 2y-2x and obtain the starting value for the decision parameters P0=2y-x x and y are the horizontal and vertical separations of the endpoint positions. 4. At each x k along the line, starting at k=0,perform the following test. i) Pk <0, the next point to plot is (xk+1,yk ) and Pk+1 = Pk+2y ii) Otherwise, the next point to plot is (xk+1,yk+1) and Pk+1 = Pk+2y-2x 5. repeat step 4 x times 20 | P a g e

Ex. No: 1.2 Aim: To implement the bresenhams algorithm for Circle Algorithm: Bresenhams Midpoint circle Alogirthm: 1. 2. 3. Input radius r and circle center (x1,y1) and obtain the first point on the circumference of a circle centered on the origin as (x 0,y0)=(0,r); Calculate the initial value of the decision parameter as P0=5/4 r.

At position, xk starting at k=0. perform the following test. 3.1) If Pk <0 , the next point along the circle centered on (0,0) is (x k+1,yk) and P k+1= P k+2x k+1+1 3.2) otherwise,the next point along the circle is (x k+1,yk-1) and P k+1= P k+2x k+1+1-2y k+1 Where 2x k+1= 2x k+2 and 2y k-1 = 2y k-2 4. Determine symentry points in the other search octants. 5. More each calculated pixel position (X,Y) onto the circular path centered on (x2,y2) and plot the co-ordinate values. x = x + x 2 , y = y + y x2. Repeat step 3 through 5 until X >= Y

6.

7. Plot the generated pixel positions on the screen. Ex. No: 1.3 Aim: To implement the bresenhams algorithm for Ellipse Algorithm: Bresenhams Midpoint Ellipse Alogirthm: 1. Input rx,ry and ellipse center (x2,y2) and obtain the first point on a ellipse centered on the origin as (x0,y0) = (0,ry). 2. Calculate the initial value of the decision parameter in region 1 as P01= ry2 rx2ry + rx2 3. At each Xk position in region 1,starting at k = 0,perform the following test. 3.1)

If Pk1 < 0 the next point along the ellipse centered on (0,0) is (x k+1,yk) and Pk1+1= P k1+ 2ry2x k+1+ry2 3.2) Otherwise, the next point along the circle is (X k+1,Yk-1) and Pk1+1 = Pk1 + 2ry2x k+1-2r2xyk+1+ry2 with 2ry2x k+1 = 2ry2xk +2r2y 2rx2x k-1 = 2rx2yk -2rx2 4. Calculate the initial value of the decision parameter in region 2 using the last point (x0,y0) calculated in region 1 as P 02 = ry2 (x0+ 1/2) 2 + rx2 (y0-1) 2-rx2ry2 21 | P a g e

5. At eachYk position is region 2,starting at K = 0, perform the following text 5.1) If the Pk2 > 0 the next point along the ellipse centered on (0,0) is (xk,yk-1) and P2k+1 = P k2-2r x1y k+1+r x2 5.2) Otherwise, the next point along the circle is (Xk+1,Yk-1) and P2k+1 = P k2+2r y2x k+1-2rx2yk+1+r x2 Using the same incremented calculations for x and y as in region

1. 6. Determine symmetry points in the other three quadrants. 7. More each calculated pixel position (x,y) onto the elliptical path centered on (x2,y2) and plot the co-ordinate values x = x+x c , y=y+yc 8. Repeat the steps for region 1 until 2ry2x >= 2rx2y 9. Plot the generated pixel positions

EX.NO:2 IMPLEMENTATION OF LINE, CIRCLE & ELLIPSE ATTRIBUTES Ex.No.2.1 Aim: To implement the basic line attributes to three different lines drawn using the line function Algorithm: Line,Circle & Ellipse attributes Step 1. Input line coordinates, and draw three different lines Step 2. Apply different line style, line width and line color for each line Ex.No.2.2 Aim: To implement the basic circle attributes to a circle drawn using Bresenhams circle algorithm Algorithm: Step 1. Draw a circle using bresenhams circle drawing algorithm Step 2. Apply a selected fill style and fill color Ex.No.2.3 Aim: To implement the basic ellipse attributes to a ellipse drawn using Bresenhams ellipse algorithm Algorithm: Step 1. Draw an ellipse using bresenhams ellipse drawing algorithm Step 2. Apply a selected fill style and fill color

22 | P a g e

EX.NO:3 2D TRANSFORMATION Aim: To perform 2D transformation such as translation, rotation, scaling, reflection and shear Algorithm: To perform 2D transformation on a triangle Translation: 1. Get 3 coordinates (x1,y1) (x2,y2) and (x3, y3) of the triangle. 2. Draw the sides of the triangle using the line() function. 3. Get the translation value (tx, ty) 4. Add the translation value to all the three co-ordinate values of the triangle x = x +tx y = y + ty. Where x = x1, x2, x3 and y = y1, y2, y3 5. Plot all the three (x, y) values and observe the translation of the triangle to the new position. .Rotation: 1. Get 3 coordinates (x1,y1) (x2,y2) and (x3, y3) of the triangle. 2. Draw the sides of the triangle using the line() function. 3. Get the rotation angle and rotation point (pivot point) (xr, yr) 4. If pivot point is origin Rotate the triangle about its angle by changing the and obtain three new coordinates (x, y) Where x = xcos - ysin y = xsin + y cos pivot point is an arbitrary position Obtain three new coordinates (x, y) using x= xr + (x xr) cos (y yr) sin y= yr + (x xr) sin +( y yr) cos 23 | P a g e

5. Plot all the three new (x, y) values and observe the rotation of the triangle to the new position. Scaling: Ordinary scaling: Object is scaled and repositioned 1. Get 3 coordinates (x1,y1) (x2,y2) and (x3, y3) of the triangle. 2. Draw the sides of the triangle using the line() function. 3. Get the scaling factor (sx, sy) 4. Calculate the new three transformed coordinates by multiplying each vertex by scaling factor (sx, sy) x= x. sx y= y. sy 5. plot all the three new (x, y) values and observe the increase/ decrease in size of the object. Fixed Point Scaling: Location of the object is fixed. Fixed point(xf, yf) can be one of the vertices 1. Get 3 coordinates (x1,y1) (x2,y2) and (x3, y3) of the triangle. 2. Draw the sides of the triangle using the line() function. 3. choose one of the coordinate as the (xf, yf) 4. Get the scaling factor (sx, sy) 5. Calculate the new three transformed coordinates by multiplying each vertex by scaling factor (sx, sy) x=x.sx + xf(1-sx) y=y.sy+yf(1-sy) where (x, y) vertex of triangle and (1-sx) , (1-sy)constant for all points in the object 6. plot all the three new(x, y) calculated vertices to form a transformed triangle. Note: (sx, sy) greater than 1 increases the size of the object (sx, xy) less than 1 decreases the size of the object (sx, sy) assigned same value results in uniform scaling (sx, sy) assigned different values results in differential scaling Reflection 1. Get 3 coordinates (x1,y1) (x2,y2) and (x3, y3) of the triangle. 2. Draw the sides of the triangle using the line() function. 3. Calculate the new three transformed coordinates by multiplying each vertex with the reflection matrix 4. plot all the three new(x, y) calculated vertices to form a transformed triangle Note: Reflection about line y=0(x-axis)

100 0 10 001
Reflection about line x=0(y-axis) 24 | P a g e

100 010 001


Reflection about line y=x

010 100 001


Shear 1. Get 3 coordinates (x1,y1) (x2,y2) and (x3, y3) of the triangle. 2. Draw the sides of the triangle using the line() function. 3. Get any real number as the shear value 3. Calculate the new three transformed coordinates by multiplying each vertex with expression corresponding to the specified shear and obtain three new coordinates 4. Plot all the three new(x, y) calculated vertices to form a transformed triangle Note: x- direction shear x= x + shx .y y = y y- direction shear x= x y = y +shy. X EX.NO:4 2D COMPOSITE TRANSFORMATION Aim: To build complex geometric and coordinate transformations from basic transformations Algorithm: Perform Translation, scaling and inverse translation 1. Take translation, scaling , inverse translation matrices, perform matrix multiplication and form a new composite matrix 2. Get 3 coordinates (x1,y1) (x2,y2) and (x3, y3) of the triangle. 3. Draw the sides of the triangle using the line() function. 25 | P a g e

4. Multiply the vertices of the triangle with new composite matrix to get three new coordinates of the triangle 5. plot the three new coordinates to get a transformed, scaled and inverse translated triangle. Note: Matrix multiplication is associative A. B. C = (A. B).C = A. (B. C) Additivity of successive translations We want to translate a point P to P by T(dx1, dy1) and then to P by another T(dx2, dy2) On the other hand, we can define T21= T(dx1, dy1) T(dx2, dy2) first, then apply T21 to P: where T21 = T (d x 2 , d y 2 )T (d x1 , d y1 )

1 0 d x 2 1 0 d x1 1 0 d x1 + d x 2 = 0 1 d y 2 0 1 d y1 = 0 1 d y1 + d y 2 1 0 0 1 0 0 1 0 0 P ' ' = S ( s x 2 , s y 2 )[ S ( s x1 , s y1 ) P ] Multiplicativity of successive scaling =[ S ( s x 2 , s y 2 ) S ( s x1 , s y1 )]P


where = S 21 P

S 21 = S ( s x 2 , s y 2 ) S ( s x1 , s y1 ) 0 0 s x1 0 0 s x 2 = 0 s y 2 0 0 s y1 0 0 1 0 1 0 0 Additivity of successive rotations 0 0 s x 2 * s x1 ( )[ R ( ) P ] P ''= = 0 sy2 * s 0R 1 y1 2 = [ R ( ) R ( 0 0 1 2 1 )]P = R21 P R21 = R (2 ) R(1 )

cos 2 = sin 2 0

sin 2 cos 2 0

0 cos 1 0 sin 1 1 0

sin 1 cos 1 0

0 0 1

cos(2 + 1 ) sin(2 + 1 ) 0 = EX.NO:5 sin(2 + 1 ) cos(2 + 1 ) 0 COHEN-SUTHERLAND CLIPPING AND WINDOWING 0 2D LINE 0 1 Aim: To clip a line using Cohen Sutherland algorithm Algorithm Step 1: Start Step 2: Initialize in Graphics mode Step 3: Declare int of x1, x2, x3, x4, y1, y2, y3, and y4 Step 4: Accept the four line coordinates Step 5: Draw line (x1, y1, x2, y2) Step 6: Accept the four side window co-ordinates Step 7: Draw rectangle (x3, y3, x4, y4)
26 | P a g e

Step Step Step Step Step Step Step Step Step Step

8: if((x1<x3)||(x2<x3)then l=1. 9: if((x4<x2)||(x4<x1))then r=1. 10: if((y1<y3)||(y2<y3))then t=1. 11: if((y2>y4)||(y1>y4))then b=1. 12: if(l==1)then, y1=y1+(y2-y1)*(x3-x1)/(x2-x1), x1=x3 13: if(r==1)then, y2=y2+(y2-y1)*(x4-x1)/(x2-x1), x2=x4 14: if(t==1)then, x1=x1+(x2-x1)*(y3-y1)/(y2-y1), y1=y3 15: if(b==1)then, x2+(x2-x1)*(y4-y2)/(y2-y1), y2=y4.Draw rect(x3,y3,x4,y4) 16: if(l ! = 0)||r!=0||b!=0||t!=0)then,line(x1,y1,x2,y2) 17: stop

EX.NO:6 SUTHERLAND HODGEMAN POLYGON CLIPPING ALGORITHM Aim: To implement Sutherland Hodgeman Polygon clipping Algorithm Algorithm: 1. 2. 3. 4. 5. 6. Initialize the graphics mode Get the co-ordinates of clipping window Enter the number of vertex Get the vertex co-ordinates of clipping window Check whether the visible region falls wholly inside, if yes save the endpoints. Check whether the visible region exits the window, if yes save the intersection. 7. Check whether the visible region falls wholly outside, if yes save nothing. 8. Check whether the visible region enters the window, if yes save the intersection and endpoints 9. Perform the clean up procedure. 27 | P a g e

EX.NO:7 3D TRANSFORMATION Aim: To implement 3D transformation such as translation, scaling and rotation. Algorithm: 1.Get the three-dimensional co-ordinates (x1, y1, z1) and (x2, y2, z2). 2.Using the values (x1, y1, z1) and (x2, y2, z2) draw the 3D object by using bar 3d() function. 3.Clear the viewport or clear the device by using clear viewport() or clear device() function. Translation: 1.Get the value of translation vectors, tx, ty, tz. 2.Add the translation vector values to the corresponding 3D co-ordinates nx1=x1+tx; ny1=y1+ty; xz1=z1+tz nx2=x2+tx; ny2=y2+ty; nz2=z2+tz 3.Using the value(nx1,ny1,nz1) and (nx2,ny2,nz2) 28 | P a g e

draw the 3D object by using bar3d() function. 4.Observe the translation of 3D object. Scaling: 1.Get the value of scaling factor sx,sy,sz. 2.Multiply the scaling factor value with the corresponding 3D co-ordinates. nx1=x1*sx; ny1=y1*sy; nz1=z1*sz; nx2=x2*sx; ny2=y2*sy; nz2=z2*sz; 3.Using the value of (nx1,ny1,nz1) and (nx2,ny2,nz2) draw the 3D object by using the bar 3d() function. 4.Observe the scaling of 3D object. Rotation: 1.Get the value of angle to rotate the 3D object. 2.Rotate the 3D object about z axis by changing the value of x1 and y1 as nx1=x1*cos(angle)-x1*sin(angle); ny1=y1*sin(angle)+y1*cos(angle); 2.1 Using the value(nx1,ny1,nz1) and (nx2,ny2,nz2) draw the 3D object which is rotated with respect to z axis. 3.Rotate the 3D object about y axis by changing the value of z1 and x1 as nz1=z1*cos(angle)-z1*sin(angle); nx1=x1*sin(angle)+x1*cos(angle); 3.1 Using the value (nx1,ny1,nz1) and (nx2,ny2,nz2) draw the 3D object which is rotated 3D object y axis. 4.Rotate the 3D object about x axis by changing the value of y1 and z1 as ny1=y1*cos(angle)-y1*sin(angle); nz1=z1*sin(angle)+z1*cos(angle); 4.1 Using the value (nx1, ny1, nz1) and (nx2, ny2, nz2) draw the 3D object, which is rotated 3D object about x-axis. 5.Observe all the rotation of the 3D object.

EX.NO:8 3D- COMPOSITE TRANSFORMATION Aim: To perform 3D composite transformations such as translation and scaling, rotation and scaling, rotation and translation. Algorithm: Step 1.Draw a cube using draw cube function Step 2. Perform Translation and scaling by calculating the new points X3[i] = (x1[i]+tx)*sx Y3[i] = (y1[i]+ty)*sy Step 3. Plot the new calculated coordinates using draw cube function Step 4: Display both the images (before and after transformation) Note: Rotation and scaling X4[i] =x3 [i] +z3[i]/2 y4[i] =y3 [i] +z3[i]/2

29 | P a g e

EX.NO:9 DRAWING THREE DIMENSIONAL OBJECTS AND SCENES USING 3Ds MAX Aim: To draw a 3 Dimensional objects and scenes(Table) using 3Ds Max Algorithm: Step 1. Open the 3Ds max application and maximize the view port window Step 2. Click the create button from the toolbox and choose geometry. Step 3. Click the box button from the geometry menu and draw it on the viewport window, this will be the top of the table. Step 4. Choose the select and move button from the main toolbar Step 5. Click modify menu from the toolbox and edit the box color, length, breadth and height as required Step 6. Similarly create the other required shapes Step 7. Edit the x, y, z axis of each object to form a table Step 8. Press ctrl+A to select all the object and click Group from the menu bar and select group Step 9. Give a name for the group and click ok, now the objects are grouped into one piece as a table. Step 10. Use the auto key button to create any required animation

30 | P a g e

OpenGL Tutorial
Setting Up Compilers Windows Using MS Visual C++ Installing GLUT 1. Most of the following files (ie. OpenGL and GLU) will already be present if you have installed MS Visual C++ v5.0 or later. The following GLUT files will need to be copied into the specified directories. 2. To install: right-click each link choose Save Link As... accept the default name (just click Save) libraries (place in the lib\ subdirectory of Visual C++) opengl32.lib glu32.lib glut32.lib include files (place in the include\GL\ subdirectory of Visual C+ +) gl.h glu.h glut.h dynamically-linked libraries (place in the \ Windows\System subdirectory opengl32.dll glu32.dll glut32.dll Compiling OpenGL/GLUT Programs 1. Create a new project: o choose File | New from the File Menu o select the Projects tab o choose Win32 Console Application o fill in your Project name 2. Designate library files for the linker to use: o choose Project | Settings from the File Menu o under Oject/library modules: enter "opengl32.lib glu32.lib glut32.lib" 3. Add/Create files to the project: o choose Project | Add to Project | Files from the File menu o add the required program files 4. Build and Execute Initialization 1. glutInit() glutInit is used to initialize the GLUT library. glutInit will initialize the GLUT library and negotiate a session with the window system Usage void glutInit(int *argcp, char **argv); 31 | P a g e

argcp A pointer to the program's unmodified argc variable from main. Upon return, the value pointed to by argcp will be updated, because glutInit extracts any command line options intended for the GLUT library. argv The program's unmodified argv variable from main. Like argcp, the data for argv will be updated because glutInit extracts any command line options understood by the GLUT library. 2. glutInitWindowPosition(), glutInitWindowSize() glutInitWindowPosition and glutInitWindowSize set the initial window position and size respectively. Usage void glutInitWindowSize(int width, int height); void glutInitWindowPosition(int x, int y); width Width in pixels. height Height in pixels. x Window X location in pixels. y Window Y location in pixels. 3. glutInitDisplayMode() glutInitDisplayMode sets the initial display mode. Usage void glutInitDisplayMode(unsigned int mode); mode Display mode, normally the bitwise OR-ing of GLUT display mode bit masks. See values below: GLUT_RGBA Bit mask to select an RGBA mode window. This is the default if neither GLUT_RGBA nor GLUT_INDEX are specified. GLUT_RGB An alias for GLUT_RGBA. GLUT_INDEX Bit mask to select a color index mode window. This overrides GLUT_RGBA if it is also specified. GLUT_SINGLE Bit mask to select a single buffered window. This is the default if neither GLUT_DOUBLE or GLUT_SINGLE are specified. GLUT_DOUBLE Bit mask to select a double buffered window. This overrides GLUT_SINGLE if it is also specified. GLUT_ACCUM Bit mask to select a window with an accumulation buffer. GLUT_ALPHA Bit mask to select a window with an alpha component to the color buffer(s). GLUT_DEPTH Bit mask to select a window with a depth buffer. GLUT_STENCIL Bit mask to select a window with a stencil buffer. GLUT_MULTISAMPLE Bit mask to select a window with multisampling support. If multisampling is not available, a non-multisampling window will automatically be chosen. 32 | P a g e

Note: both the OpenGL client-side and server-side implementations must support the GLX_SAMPLE_SGIS extension for multisampling to be available. GLUT_STEREO Bit mask to select a stereo window. GLUT_LUMINANCE Bit mask to select a window with a ``luminance'' color model. This model provides the functionality of OpenGL's RGBA color model, but the green and blue components are not maintained in the frame buffer. Instead each pixel's red component is converted to an index between zero and glutGet(GLUT_WINDOW_COLORMAP_SIZE)-1 and looked up in a per-window color map to determine the color of pixels within the window. 4. glutCreateWindow glutCreateWindow creates a top-level window. Usage int glutCreateWindow(char *name); name ASCII character string for use as window name. glutCreateWindow creates a top-level window. The name will be provided to the window system as the window's name. The intent is that the window system will label the window with the name.Implicitly, the current window is set to the newly created window. 5. glutDisplayFunc glutDisplayFunc sets the display callback for the current window. Usage void glutDisplayFunc(void (*func)(void)); func The new display callback function. glutDisplayFunc sets the display callback for the current window. When GLUT determines that the normal plane for the window needs to be redisplayed, the display callback for the window is called. Before the callback, the current window is set to the window needing to be redisplayed 6. glutMainLoop glutMainLoop enters the GLUT event processing loop. Usage void glutMainLoop(void); glutMainLoop enters the GLUT event processing loop. This routine should be called at most once in a GLUT program. Once called, this routine will never return. It will call as necessary any callbacks that have been registered. Geometric Objects Point, Lines and Polygons 1. glVertex() Geometric object is described by a set of vertices. Vertices specified by the user as two-dimensional (only x- and y-coordinates) are assigned a zcoordinate equal to zero. glVertex is only effective between a glBegin() and a glEnd() pair. glBegin(GL_POINTS); 33 | P a g e

glVertex2f(6.0,1.5); glVertex2f(4.0, 0.0); glEnd(); void glVertex3d( GLdouble x, GLdouble y, GLdouble z ) void glVertex3f( GLfloat x, GLfloat y, GLfloat z ) 2. glBegin(), glEnd() delimit the vertices

of a primitive or a group of like primitives

3. glFlush() ensure if all previously issued commands are executed. glFlush() is generally called at the end of a sequence of drawing commands to ensure all objects in the scene are drawn before beginning to accept user input 4. glClear() will clear the specified buffers to their current clearing values. 5. glColor3f() To set a color, use the command glColor3f(). It takes three floating point parameters which are between 0.0 and 1.0. The parameters are, in order, the red, green, and blue components of the color glColor3f(0.0, 0.0, 0.0); //black glColor3f(1.0, 0.0, 0.0); //red glColor3f(0.0, 1.0, 0.0); //green glColor3f(1.0, 1.0, 0.0); //yellow 6. glLoadIdentity() will initialize the viewing matrix by loading it with the identity matrix 7. GL_COLOR_BUFFER_BIT. To clear the color buffer use the argument GL_COLOR_BUFFER_BIT. 8. glMatrixMode() To perform transformations on the models that we have created, we will use the same glMatrixMode() command with GL_MODELVIEW as the argument. This indicates that the succeeding transformations now affect the modelview matrix instead of the projection matrix. void glMatrixMode( GLenum mode ) The mode Specifies which matrix stack is the target for subsequent matrix operations. Three values are accepted: GL_MODELVIEW, GL_PROJECTION,and GL_TEXTURE. The default value is GL_MODELVIEW. 9. glOrtho() 34 | P a g e

left, planes. bottom, top Specify the coordinates for the bottom and top horizontal clipping planes. near, far Specify the distances to the nearer and farther depth clipping planes. These distances are negative if the plane is to be behind the viewer. glOrtho describes a perspective matrix that produces a parallel projection. ( left, bottom, -near) and (right, top, -near) specify the points on the near clipping plane that are mapped to the lower leftand upper right corners of the window, respectively, assuming that the eye is located at (0, 0, 0). -far specifies the location of the far clipping plane. Both near and far can be either positive or negative. The corresponding matrix is | 2 | |---------0 0 t | |right-left x| | | | 2 | | 0 ---------0 t | | top-bottom y| | | | | | 0 0 -2 | | -------- t | | far-near z| | | | 0 0 0 1 | where right+left t = - ---------x right-left top+bottom t = - ---------y top-bottom far+near t = - -------z far-near

the command glOrtho() to create an orthographic parallel viewing volume void glOrtho( GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far ) right Specify the coordinates for the left and right vertical clipping

35 | P a g e

EX.NO:10 GENERATING FRACTAL IMAGES Aim: To construct a Sierpinski's Triangle using OpenGL Algorithm : 1. Take 3 points in a plane to form a triangle, you need not draw it. 2. Randomly select any point inside the triangle and consider that your current position. 3. Randomly select any one of the 3 vertex points. 4. Move half the distance from your current position to the selected vertex. 5. Plot the current position. 6. Repeat from step 3.

36 | P a g e

EX:NO:11 Aim:

CONVERSION BETWEEN COLOR MODELS

To write a program to convert HSV color model to RGB and RGB color model to HSV. Algorithm: HSV to RGB color model Step1: initialize the graphics function Step2: get the value of h, s and v. Step3: for a set of TRGB values HSV point correspond to RGB color values is set for V. Parameter S is determined as relative distance from V. Parameter h is determined by calculating aa=v*(1-s). bb= v*(s*f). cc= v*(1-s*(1-f)). Step4: apply the value to the equation and determined the values r,g,b RGB to HSV color model Step1: initialize the graphics function Step2: get the value of r,g and b. Step3:set pointer v as maximum value *v=max Step4:set pointer S *s=delta/max; Step5:set pointer h as *h=NO_HUE; *h=(g-b)/delta; Step6:a).if r has maximum value *h=(g-b)/delta; b). if g has maximum value *h=2+(b-r)/delta; c). if b has maximum value *h=4+(r-g)/delta; Step7: find the r,s,b and v.

EX:NO:12

BUILDING CAMERA IN A PROGRAM USING OPENGL 37 | P a g e

Aim : To build a camera using the basic trigonometric functions Algorithm: Using the function glutInit() initialize the toolkit Using the function glutInitDisplayMode() initialize the display buffer Using glutInitWindowSize() and glutInitWindowPosition() specify the window size and where It should be positioned 1. Build the camera using cube ,keyboard input with the glut keyboard controls and basic trigonometric functions yrotrad = (yrot / 180 * 3.141592654f); xrotrad = (xrot / 180 * 3.141592654f); xpos -= float(sin(yrotrad)); zpos += float(cos(yrotrad)) ; ypos += float(sin(xrotrad)); is used to create the camera 2. Specify the view port size using the function glViewport (0, 0, (GLsizei)w, (GLsizei)h); //set the viewport to the current window specifications

3.

Adjust the camera settings using the functions given below glEnable (GL_DEPTH_TEST); //enable the depth testing glEnable (GL_LIGHTING); //enable the lighting glEnable (GL_LIGHT0); //enable LIGHT0, our Diffuse Light glShadeModel (GL_SMOOTH); //set the shader to smooth shader

4.

Using the function given below rotate the camera in different axis glRotatef(xrot,1.0,0.0,0.0); //rotate our camera on teh x-axis (left and right) glRotatef(yrot,0.0,1.0,0.0); //rotate our camera on the y-axis (up and down) glTranslated(-xpos,-ypos,-zpos); //translate the screen to the position of our camera

EX:NO: 13 BUTTERFLY LIFECYCLE MODEL

38 | P a g e

Objective: To create an animation for butterfly lifecycle using openGL Description: Draw the required objects using the available basic output primitives with attractive colors. Use the required available transformations to transform the shape of the objects. To create animation use the delay() function. Describe each stage using outtext() function.

EX:NO: 14 DEVELOP TAJMAHAL USING OPENGL Objective: 39 | P a g e

To develop Tajmahal using the basic output primitives using openGL Description: Taj Mahal is created with many minars and gumbazz. Use the simple objects like spheres and cubes to build these units. First for the main part we use box or a large cube and then point small sphere over them. Then place a small cubical structure with very low height, a egg like sphere is also placed over it. Similarly, place four long minars over each four corrners.

REFERENCES 1. http://www.programmingsimplified.com/c/graphics.h 2. http://www.cs.uccs.edu/~semwal/glman.html 3. Donald Hearn, Pauline Baker, Computer Graphics C Version, second edition, Pearson Education,2004. 40 | P a g e

4. F.S. Hill, Computer Graphics using OPENGL, Second edition, Pearson Education,2003

41 | P a g e

Das könnte Ihnen auch gefallen