├── README.md ├── lab1.c ├── lab2.c ├── lab3.c ├── lab4.c ├── lab5.c ├── lab6.c ├── lab7.c ├── lab8.cpp └── lab9.cpp /README.md: -------------------------------------------------------------------------------- 1 | # VTU-Computer-Graphics-Lab 2 | Lab programs of CG lab 3 | -------------------------------------------------------------------------------- /lab1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | GLint X1,Y1,X2,Y2; 5 | void LineBres(void) 6 | { 7 | glClear(GL_COLOR_BUFFER_BIT); 8 | int dx=abs(X2-X1),dy=abs(Y2-Y1); 9 | int p=2*dy-dx; 10 | int twoDy=2*dy,twoDyDx=2*(dy-dx); 11 | int x,y; 12 | if(X1>X2) 13 | { 14 | x=X2; 15 | y=Y2; 16 | X2=X1; 17 | } 18 | else 19 | { 20 | x=X1; 21 | y=Y1; 22 | X2=X2; 23 | } 24 | glBegin(GL_POINTS); 25 | glVertex2i(x,y); 26 | while(x 2 | #include 3 | float house[5][2]={{200,100},{400,100},{300,300}}; 4 | float h=200,k=100,theta; 5 | void drawtriangle() 6 | { 7 | glBegin(GL_LINE_LOOP); 8 | glVertex2fv(house[0]); 9 | glVertex2fv(house[1]); 10 | glVertex2fv(house[2]); 11 | glEnd(); 12 | } 13 | void display() 14 | { 15 | glClear(GL_COLOR_BUFFER_BIT); 16 | drawtriangle(); 17 | glTranslatef(h,k,0); 18 | glRotatef(theta,0,0,1); 19 | glTranslatef(-h,-k,0); 20 | drawtriangle(); 21 | glFlush(); 22 | } 23 | void init() 24 | { 25 | gluOrtho2D(-700,700,-700,700); 26 | } 27 | void main(int argc,char** argv) 28 | { 29 | printf("Enter the rotation angle.\n"); 30 | scanf("%f",&theta); 31 | glutInit(&argc,argv); 32 | glutInitDisplayMode(GLUT_SINGLE); 33 | glutInitWindowSize(350,350); 34 | glutCreateWindow("house:C Tathva"); 35 | glutDisplayFunc(display); 36 | init(); 37 | glutMainLoop(); 38 | } 39 | 40 | -------------------------------------------------------------------------------- /lab3.c: -------------------------------------------------------------------------------- 1 | #include 2 | float ver[][3]={{0,0,0},{1,0,0},{1,1,0},{0,1,0},{0,0,1},{1,0,1},{1,1,1},{0,1,1}}; 3 | float theta[]={0,0,0}; 4 | int axis=0; 5 | void polygon(int a,int b,int c,int d) 6 | { 7 | glBegin(GL_POLYGON); 8 | glColor3fv(ver[a]); 9 | glVertex3fv(ver[a]); 10 | glColor3fv(ver[b]); 11 | glVertex3fv(ver[b]); 12 | glColor3fv(ver[c]); 13 | glVertex3fv(ver[c]); 14 | glColor3fv(ver[d]); 15 | glVertex3fv(ver[d]); 16 | glEnd(); 17 | } 18 | void ColorCube(void) 19 | { 20 | polygon(0,3,2,1); 21 | polygon(2,3,7,6); 22 | polygon(0,4,7,3); 23 | polygon(1,2,6,5); 24 | polygon(4,5,6,7); 25 | polygon(0,1,5,4); 26 | } 27 | void display() 28 | { 29 | glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 30 | glLoadIdentity(); 31 | glRotatef(theta[0],1,0,0); 32 | glRotatef(theta[1],0,1,0); 33 | glRotatef(theta[2],0,0,1); 34 | ColorCube(); 35 | glFlush(); 36 | glutSwapBuffers(); 37 | } 38 | void SpinCube() 39 | { 40 | theta[axis]+=0.5; 41 | if(theta[axis]>360) 42 | theta[axis]=0; 43 | display(); 44 | } 45 | void mouse(int btn,int state,int x,int y) 46 | { 47 | if(btn=GLUT_LEFT_BUTTON && state==GLUT_DOWN) 48 | axis=0; 49 | if(btn=GLUT_MIDDLE_BUTTON && state==GLUT_DOWN) 50 | axis=1; 51 | if(btn=GLUT_RIGHT_BUTTON && state==GLUT_DOWN) 52 | axis=2; 53 | } 54 | void myReshape(int w,int h) 55 | { 56 | glViewport(0,0,w,h); 57 | glMatrixMode(GL_PROJECTION); 58 | glLoadIdentity(); 59 | if(w<=h) 60 | glOrtho(-2,2,-2*h/w,2*h/w,-10,10); 61 | else 62 | glOrtho(-2*h/w,2*h/w,-2,2,-10,10); 63 | glMatrixMode(GL_MODELVIEW); 64 | } 65 | void main(int argc,char **argv) 66 | { 67 | glutInit(&argc,argv); 68 | glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH); 69 | glutInitWindowSize(500,500); 70 | glutCreateWindow("ROTATING COLOR CUBE: C Tathva"); 71 | glutReshapeFunc(myReshape); 72 | glutDisplayFunc(display); 73 | glutIdleFunc(SpinCube); 74 | glutMouseFunc(mouse); 75 | glEnable(GL_DEPTH_TEST); 76 | glutMainLoop(); 77 | } 78 | -------------------------------------------------------------------------------- /lab4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | float ver[8][3]={{0,0,0},{1,0,0},{1,1,0},{0,1,0},{0,0,1},{1,0,1},{1,1,1},{0,1,1}}; 5 | float v1[3]={0,0,5}; 6 | void polygon(int a,int b,int c,int d); 7 | void polygon1(); 8 | void display() 9 | { 10 | glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 11 | glLoadIdentity(); 12 | gluLookAt(v1[0],v1[1],v1[2],0,0,0,0,1,0); 13 | polygon1(); 14 | glFlush(); 15 | } 16 | void polygon1() 17 | { 18 | polygon(0,1,2,3); 19 | polygon(4,5,6,7); 20 | polygon(5,1,2,6); 21 | polygon(4,0,3,7); 22 | polygon(4,5,1,0); 23 | polygon(7,6,2,3); 24 | } 25 | void polygon(int a,int b,int c,int d) 26 | { 27 | glBegin(GL_POLYGON); 28 | glColor3fv(ver[a]); 29 | glVertex3fv(ver[a]); 30 | glColor3fv(ver[b]); 31 | glVertex3fv(ver[b]); 32 | glColor3fv(ver[c]); 33 | glVertex3fv(ver[c]); 34 | glColor3fv(ver[d]); 35 | glVertex3fv(ver[d]); 36 | glEnd(); 37 | } 38 | void key(char f,int x,int y) 39 | { 40 | if(f=='x')v1[0]-=.10; 41 | if(f=='X')v1[0]+=.10; 42 | if(f=='y')v1[1]-=.10; 43 | if(f=='Y')v1[1]+=.10; 44 | if(f=='z')v1[2]-=.10; 45 | if(f=='Z')v1[2]+=.10; 46 | display(); 47 | } 48 | void Reshape(int w,int h) 49 | { 50 | glViewport(0,0,w,h); 51 | glMatrixMode(GL_PROJECTION); 52 | glLoadIdentity(); 53 | if(w<=h) 54 | glFrustum(-2.0,2.0,-2.0*h/w,0*h/w,2.0,20); 55 | else 56 | glFrustum(-2.0*w/h,2.0*w/h,-2.0,2.0,2.0,20); 57 | glMatrixMode(GL_MODELVIEW); 58 | } 59 | void main(int argc,char ** argv) 60 | { 61 | glutInit(&argc,argv); 62 | glutInitDisplayMode(GLUT_SINGLE|GLUT_DEPTH); 63 | glutInitWindowSize(500,500); 64 | glutInitWindowPosition(10,10); 65 | glutCreateWindow("Spinning Color Cube : C Tathva"); 66 | glutDisplayFunc(display); 67 | glutKeyboardFunc(key); 68 | glEnable(GL_DEPTH_TEST); 69 | glutReshapeFunc(Reshape); 70 | glutMainLoop(); 71 | } 72 | -------------------------------------------------------------------------------- /lab5.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | float xmin=50,ymin=50,xmax=100,ymax=100; 4 | float xvmin=200,yvmin=200,xvmax=400,yvmax=400; 5 | int RIGHT=8,LEFT=2,TOP=4,BOTTOM=1; 6 | float sx,sy,vx1,vy1,vx2,vy2; 7 | float X1,Y1,X2,Y2; 8 | int compute(float x,float y) 9 | { 10 | int code=0; 11 | if(y > ymax) 12 | code = TOP; 13 | 14 | else if(y < ymin) 15 | code = BOTTOM; 16 | 17 | if(x > xmax) 18 | code = RIGHT; 19 | 20 | else if(x < xmin) 21 | code = LEFT; 22 | return code; 23 | } 24 | void cohen(float X1,float Y1,float X2,float Y2) 25 | { 26 | float x,y; 27 | int accept=0,done=0,code_p,code_q,code; 28 | 29 | code_p=compute(X1,Y1); 30 | code_q=compute(X2,Y2); 31 | do 32 | { 33 | if(!(code_p | code_q)) 34 | { 35 | accept=1; 36 | done=1; 37 | } 38 | else if(code_p & code_q) 39 | done=1; 40 | else 41 | { 42 | code=code_p ? code_p : code_q; 43 | if(code & TOP) 44 | { 45 | x=X1+(X2-X1)*(ymax-Y1)/(Y2-Y1); 46 | y=ymax; 47 | } 48 | else if(code & BOTTOM) 49 | { 50 | x=X1+(X2-X1)*(ymin-Y1)/(Y2-Y1); 51 | y=ymin; 52 | } 53 | else if(code & RIGHT) 54 | { 55 | y=Y1+(Y2-Y1)*(xmax-X1)/(X2-X1); 56 | x=xmax; 57 | } 58 | else 59 | { 60 | y=Y1+(Y2-Y1)*(xmin-X1)/(X2-X1); 61 | x=xmin; 62 | } 63 | if(code==code_p) 64 | { 65 | X1=x; 66 | Y1=y; 67 | code_p=compute(X1,Y1); 68 | } 69 | else 70 | { 71 | X2=x; 72 | Y2=y; 73 | code_q=compute(X2,Y2); 74 | } 75 | } 76 | }while(!done); 77 | 78 | if(accept) 79 | { 80 | sx=(xvmax-xvmin)/(xmax-xmin); 81 | sy=(yvmax-yvmin)/(ymax-ymin); 82 | vx1=xvmin+(X1-xmin)*sx; 83 | vy1=xvmin+(Y1-ymin)*sy; 84 | vx2=xvmin+(X2-xmin)*sx; 85 | vy2=xvmin+(Y2-ymin)*sy; 86 | 87 | } 88 | } 89 | void display() 90 | { 91 | glClear(GL_COLOR_BUFFER_BIT); 92 | 93 | glColor3f(1,1,1); 94 | glLineWidth(2); 95 | // The below code is used to draw a enterd lines 96 | glBegin(GL_LINES); 97 | glVertex2d(X1,Y1); 98 | glVertex2d(X2,Y2); 99 | glEnd(); 100 | 101 | glColor3f(1,1,1); 102 | // The below code is used to draw a window. 103 | glBegin(GL_LINE_LOOP); 104 | glVertex2f(xmin,ymin); 105 | glVertex2f(xmax,ymin); 106 | glVertex2f(xmax,ymax); 107 | glVertex2f(xmin,ymax); 108 | glEnd(); 109 | 110 | cohen(X1,Y1,X2,Y2); 111 | 112 | glColor3f(1,1,1); 113 | // The below code is for the view port 114 | glBegin(GL_LINE_LOOP); 115 | glVertex2f(xvmin,yvmin); 116 | glVertex2f(xvmax,yvmin); 117 | glVertex2f(xvmax,yvmax); 118 | glVertex2f(xvmin,yvmax); 119 | glEnd(); 120 | 121 | glColor3f(1,1,1); 122 | // The clipped coordinates at the viewport. 123 | glBegin(GL_LINES); 124 | glVertex2d(vx1,vy1); 125 | glVertex2d(vx2,vy2); 126 | glEnd(); 127 | glFlush(); 128 | } 129 | void myinit() 130 | { 131 | glClearColor(0,0,0,1); 132 | gluOrtho2D(0,500,0,500); 133 | } 134 | void main(int argc,char **argv) 135 | { 136 | printf("\n Enter the points\n"); 137 | scanf("%f%f%f%f",&X1,&Y1,&X2,&Y2); 138 | glutInit(&argc,argv); 139 | glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); 140 | glutInitWindowSize(500,500); 141 | glutCreateWindow("cohen sutherland: C Tathva"); 142 | glutDisplayFunc(display); 143 | myinit(); 144 | glutMainLoop(); 145 | } 146 | 147 | -------------------------------------------------------------------------------- /lab6.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void init() 4 | { 5 | glMatrixMode(GL_PROJECTION); 6 | glLoadIdentity(); 7 | glOrtho(-4,4,-4,4,-10,10); 8 | glMatrixMode(GL_MODELVIEW); 9 | void wall() 10 | { 11 | glPushMatrix(); 12 | glScalef(2,0.05,2); 13 | glutSolidCube(2); 14 | glPopMatrix(); 15 | 16 | glPushMatrix(); 17 | glTranslatef(-2,2,0); 18 | glScalef(0.05,2,2); 19 | glutSolidCube(2); 20 | glPopMatrix(); 21 | 22 | glPushMatrix(); 23 | glTranslatef(0,2,-2); 24 | glScalef(2,2,0.05); 25 | glutSolidCube(2); 26 | glPopMatrix(); 27 | } 28 | void table() 29 | { 30 | glPushMatrix(); 31 | glTranslatef(0,0.5,0); 32 | glScalef(1,0.05,1); 33 | glutSolidCube(2); 34 | glPopMatrix(); 35 | 36 | glPushMatrix(); 37 | glTranslatef(-0.8,0.2,0.8); 38 | glScalef(0.1,0.25,0.1); 39 | glutSolidCube(2); 40 | glPopMatrix(); 41 | 42 | glPushMatrix(); 43 | glTranslatef(0.8,0.2,-0.8); 44 | glScalef(0.1,0.25,0.1); 45 | glutSolidCube(2); 46 | glPopMatrix(); 47 | 48 | glPushMatrix(); 49 | glTranslatef(-0.8,0.2,-0.8); 50 | glScalef(0.1,0.25,0.1); 51 | glutSolidCube(2); 52 | glPopMatrix(); 53 | } 54 | void tea() 55 | { 56 | glPushMatrix(); 57 | glTranslatef(0,1,0); 58 | glutSolidTeapot(0.5); 59 | glPopMatrix(); 60 | } 61 | void display(void) 62 | { 63 | float amb[]={1,0,0}; 64 | float pos[]={2,4,1}; 65 | glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 66 | glMaterialfv(GL_FRONT,GL_AMBIENT,amb); 67 | glLightfv(GL_LIGHT0,GL_POSITION,pos); 68 | gluLookAt(2,1,2,0,1,0,0,1,0); 69 | wall(); 70 | table(); 71 | tea(); 72 | glFlush(); 73 | } 74 | void main(int argc,char ** argv) 75 | { 76 | glutInit(&argc,argv); 77 | glutInitDisplayMode(GLUT_SINGLE|GLUT_DEPTH); 78 | glutInitWindowSize(600,600); 79 | glutCreateWindow("Teapot : C Tathva"); 80 | init(); 81 | glutDisplayFunc(display); 82 | glEnable(GL_DEPTH_TEST); 83 | glShadeModel(GL_SMOOTH); 84 | glEnable(GL_LIGHTING); 85 | glEnable(GL_LIGHT0); 86 | glEnable(GL_NORMALIZE); 87 | glutMainLoop(); 88 | } 89 | -------------------------------------------------------------------------------- /lab7.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | float v[][3]={{-1,-0.5,0},{1,-0.5,0},{0,1,0},{0,0,1}}; 4 | int n; 5 | void triangle(float *p,float *q,float *r) 6 | { 7 | glVertex3fv(p); 8 | glVertex3fv(q); 9 | glVertex3fv(r); 10 | } 11 | void tetra(float *a,float *b,float *c,float *d) 12 | { 13 | glColor3f(1,0,0); 14 | triangle(a,b,c); 15 | glColor3f(0,1,0); 16 | triangle(a,b,d); 17 | glColor3f(0,0,1); 18 | triangle(b,c,d); 19 | glColor3f(0,0,0); 20 | triangle(a,d,c); 21 | } 22 | void divide_tetra(float *a,float *b,float *c,float *d,int m) 23 | { 24 | float mid[6][3]; 25 | int j; 26 | if(m>0) 27 | { 28 | for(j=0;j<3;j++) 29 | { 30 | mid[0][j]=(a[j]+b[j])/2; 31 | mid[1][j]=(a[j]+c[j])/2; 32 | mid[2][j]=(a[j]+d[j])/2; 33 | mid[3][j]=(b[j]+c[j])/2; 34 | mid[4][j]=(b[j]+d[j])/2; 35 | mid[5][j]=(c[j]+d[j])/2; 36 | } 37 | divide_tetra(a,mid[1],mid[0],mid[2],m-1); 38 | divide_tetra(b,mid[0],mid[3],mid[4],m-1); 39 | divide_tetra(c,mid[1],mid[3],mid[5],m-1); 40 | divide_tetra(d,mid[2],mid[4],mid[5],m-1); 41 | } 42 | else 43 | tetra(a,b,c,d); 44 | } 45 | void display() 46 | { 47 | glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 48 | glBegin(GL_TRIANGLES); 49 | divide_tetra(v[0],v[1],v[2],v[3],n); 50 | glEnd(); 51 | glFlush(); 52 | } 53 | void init(void) 54 | { 55 | glClearColor(1,1,1,1); 56 | glOrtho(-2,2,-2,2,-10,10); 57 | } 58 | int main(int argc,char* *argv) 59 | { 60 | printf("Enter the number of divisions:"); 61 | scanf("%d", &n); 62 | glutInit(&argc, argv); 63 | glutInitDisplayMode(GLUT_SINGLE | GLUT_DEPTH); 64 | glutInitWindowSize(700,700); 65 | glutCreateWindow("3D Sierpinski Gasket: C Tathva"); 66 | init(); 67 | glutDisplayFunc(display); 68 | glEnable(GL_DEPTH_TEST); 69 | glutMainLoop(); 70 | } 71 | -------------------------------------------------------------------------------- /lab8.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #define PI 3.1416 6 | static int win,val=0,CMenu; 7 | void CreateMenu(void); 8 | void Menu(int value); 9 | struct wcPt3D 10 | { 11 | GLfloat x,y,z; 12 | }; 13 | GLsizei winWidth=600,winHeight=600; 14 | GLfloat xwcMin=0.0,xwcMax=130.0; 15 | GLfloat ywcMin=0.0,ywcMax=130.0; 16 | void bino(GLint n,GLint *C) 17 | { 18 | GLint k,j; 19 | for(k=0;k<=n;k++) 20 | { 21 | C[k]=1; 22 | for(j=n;j>=k+1;j--) 23 | C[k]*=j; 24 | for(j=n-k;j>=2;j--) 25 | C[k]/=j; 26 | } 27 | } 28 | void computeBezPt(GLfloat u,struct wcPt3D *bezPt,GLint nCtrlPts,struct wcPt3D *ctrlPts,GLint *C) 29 | { 30 | GLint k,n=nCtrlPts-1; 31 | GLfloat bezBlendFcn; 32 | bezPt->x =bezPt->y =bezPt->z=0.0; 33 | for(k=0;kx += ctrlPts[k].x * bezBlendFcn; 37 | bezPt->y += ctrlPts[k].y * bezBlendFcn; 38 | bezPt->z += ctrlPts[k].z * bezBlendFcn; 39 | } 40 | } 41 | void bezier(struct wcPt3D *ctrlPts, GLint nCtrlPts,GLint nBezCurvePts) 42 | { 43 | struct wcPt3D bezCurvePt; 44 | GLfloat u; 45 | GLint *C,k; 46 | C=new GLint[nCtrlPts]; 47 | bino(nCtrlPts-1,C); 48 | glBegin(GL_LINE_STRIP); 49 | for(k=0;k<=nBezCurvePts;k++) 50 | { 51 | u=GLfloat(k)/GLfloat(nBezCurvePts); 52 | computeBezPt(u,&bezCurvePt,nCtrlPts,ctrlPts,C); 53 | glVertex2f(bezCurvePt.x, bezCurvePt.y); 54 | } 55 | glEnd(); 56 | delete[]C; 57 | } 58 | void displayFcn() 59 | { 60 | GLint nCtrlPts=4,nBezCurvePts=20; 61 | static float theta=0; 62 | struct wcPt3D ctrlPts[4]={{20,100,0},{30,110,0},{50,90,0},{60,100,0}}; 63 | ctrlPts[1].x +=10*sin(theta * PI/180.0); 64 | ctrlPts[1].y +=5*sin(theta * PI/180.0); 65 | ctrlPts[2].x -=10*sin((theta+30) * PI/180.0); 66 | ctrlPts[2].y -=10*sin((theta+30) * PI/180.0); 67 | ctrlPts[3].x -=4*sin((theta)*PI/180.0); 68 | ctrlPts[3].y += sin((theta-30)*PI/180.0); 69 | theta+=0.1; 70 | glClear(GL_COLOR_BUFFER_BIT); 71 | glColor3f(0.0,0.0,0.0); 72 | glPointSize(5); 73 | if(val==1){ 74 | glPushMatrix(); 75 | glLineWidth(5); 76 | glColor3f(1.0,0.5,0); 77 | for(int i=0;i<8;i++) 78 | { 79 | glTranslatef(0,-0.8,0); 80 | bezier(ctrlPts, nCtrlPts, nBezCurvePts); 81 | } 82 | glColor3f(1,1,1); 83 | for(int i=0;i<8;i++) 84 | { 85 | glTranslatef(0,-0.8,0); 86 | bezier(ctrlPts, nCtrlPts, nBezCurvePts); 87 | } 88 | glColor3f(0,1.0,0); 89 | for(int i=0;i<8;i++) 90 | { 91 | glTranslatef(0,-0.8,0); 92 | bezier(ctrlPts, nCtrlPts, nBezCurvePts); 93 | } 94 | glPopMatrix(); 95 | glColor3f(0.7,0.5,0.3); 96 | glLineWidth(5); 97 | glBegin(GL_LINES); 98 | glVertex2f(20,100); 99 | glVertex2f(20,40); 100 | glEnd(); 101 | glFlush(); 102 | } 103 | if(val==2){ 104 | glPushMatrix(); 105 | glLineWidth(5); 106 | glColor3f(1.0,1.0,0.0); 107 | for(int i=0;i<12;i++) 108 | { 109 | glTranslatef(0,-0.8,0); 110 | bezier(ctrlPts, nCtrlPts, nBezCurvePts); 111 | } 112 | glColor3f(1,0.0,0.0); 113 | for(int i=0;i<12;i++) 114 | { 115 | glTranslatef(0,-0.8,0); 116 | bezier(ctrlPts, nCtrlPts, nBezCurvePts); 117 | } 118 | glPopMatrix(); 119 | glColor3f(0.7,0.5,0.3); 120 | glLineWidth(5); 121 | glBegin(GL_LINES); 122 | glVertex2f(20,100); 123 | glVertex2f(20,40); 124 | glEnd(); 125 | glFlush(); 126 | } 127 | glutPostRedisplay(); 128 | glutSwapBuffers(); 129 | } 130 | void winReshapeFun(GLint newWidth,GLint newHeight) 131 | { 132 | glViewport(0,0,newWidth,newHeight); 133 | glMatrixMode(GL_PROJECTION); 134 | glLoadIdentity(); 135 | gluOrtho2D(xwcMin,xwcMax,ywcMin,ywcMax); 136 | glClear(GL_COLOR_BUFFER_BIT); 137 | } 138 | void CreateMenu(void) 139 | { 140 | CMenu=glutCreateMenu(Menu); 141 | glutAddMenuEntry("Indian flag",1); 142 | glutAddMenuEntry("Karnataka flag",2); 143 | glutAddMenuEntry("Exit",0); 144 | glutAttachMenu(GLUT_RIGHT_BUTTON); 145 | } 146 | void Menu(int value) 147 | { 148 | if(value==0) 149 | { 150 | glutDestroyWindow(win); 151 | exit(0); 152 | } 153 | else 154 | { 155 | val=value; 156 | } 157 | } 158 | int main(int argc,char **argv) 159 | { 160 | glutInit(&argc,argv); 161 | glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); 162 | glutInitWindowPosition(50,50); 163 | glutInitWindowSize(winWidth,winHeight); 164 | glutCreateWindow("prg.8 Bezier Curve: C Tathva"); 165 | CreateMenu(); 166 | glutDisplayFunc(displayFcn); 167 | glutReshapeFunc(winReshapeFun); 168 | glutMainLoop(); 169 | } 170 | -------------------------------------------------------------------------------- /lab9.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | float x1=200,y1=200,x2=100,y2=300,x3=200,y3=400,x4=300,y4=300; 5 | float le[500],re[500]; 6 | void edgedetect(float x1,float y1,float x2,float y2) 7 | { 8 | float mx,x,temp; 9 | int i; 10 | if((y2-y1)<0) 11 | { 12 | temp=y1;y1=y2;y2=temp; 13 | temp=x1;x1=x2;x2=temp; 14 | } 15 | if((y2-y1)!=0) 16 | mx=(x2-x1)/(y2-y1); 17 | else 18 | mx=x2-x1; 19 | 20 | x=x1; 21 | for(i=y1;i<=y2;i++) 22 | { 23 | if(x < le[i]) 24 | le[i] = x; 25 | if(x > re[i]) 26 | re[i] = x; 27 | x+=mx; 28 | } 29 | } 30 | void draw_pixel(int x,int y) 31 | { 32 | glColor3f(1,1,0); 33 | glPointSize(2); 34 | glBegin(GL_POINTS); 35 | glVertex2i(x,y); 36 | glEnd(); 37 | glFlush(); 38 | } 39 | void scanfill(float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4) 40 | { 41 | 42 | int i,y; 43 | for(i=0;i<500;i++) 44 | { 45 | le[i]=500; 46 | re[i]=0; 47 | } 48 | edgedetect(x1,y1,x2,y2); 49 | edgedetect(x2,y2,x3,y3); 50 | edgedetect(x3,y3,x4,y4); 51 | edgedetect(x4,y4,x1,y1); 52 | 53 | for(y=0;y<500;y++) 54 | { 55 | if(le[y]<=re[y]) 56 | for(i=le[y];i