├── OUTPUT ├── PROG1.JPG ├── PROG2.JPG ├── PROG3.JPG ├── PROG4.JPG ├── PROG5.JPG ├── PROG6.JPG ├── PROG7.JPG ├── PROG8.JPG ├── PROG9.JPG ├── OUTPUT3.gif ├── OUTPUT4.gif └── OUTPUT9.gif ├── LICENSE ├── LAB8.cpp ├── README.md ├── LAB1.cpp ├── LAB9.cpp ├── LAB3.cpp ├── LAB7.cpp ├── LAB6.cpp ├── LAB4.cpp ├── LAB5.cpp └── LAB2.cpp /OUTPUT/PROG1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbhishekMali21/COMPUTER-GRAPHICS-LABORATORY/HEAD/OUTPUT/PROG1.JPG -------------------------------------------------------------------------------- /OUTPUT/PROG2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbhishekMali21/COMPUTER-GRAPHICS-LABORATORY/HEAD/OUTPUT/PROG2.JPG -------------------------------------------------------------------------------- /OUTPUT/PROG3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbhishekMali21/COMPUTER-GRAPHICS-LABORATORY/HEAD/OUTPUT/PROG3.JPG -------------------------------------------------------------------------------- /OUTPUT/PROG4.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbhishekMali21/COMPUTER-GRAPHICS-LABORATORY/HEAD/OUTPUT/PROG4.JPG -------------------------------------------------------------------------------- /OUTPUT/PROG5.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbhishekMali21/COMPUTER-GRAPHICS-LABORATORY/HEAD/OUTPUT/PROG5.JPG -------------------------------------------------------------------------------- /OUTPUT/PROG6.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbhishekMali21/COMPUTER-GRAPHICS-LABORATORY/HEAD/OUTPUT/PROG6.JPG -------------------------------------------------------------------------------- /OUTPUT/PROG7.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbhishekMali21/COMPUTER-GRAPHICS-LABORATORY/HEAD/OUTPUT/PROG7.JPG -------------------------------------------------------------------------------- /OUTPUT/PROG8.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbhishekMali21/COMPUTER-GRAPHICS-LABORATORY/HEAD/OUTPUT/PROG8.JPG -------------------------------------------------------------------------------- /OUTPUT/PROG9.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbhishekMali21/COMPUTER-GRAPHICS-LABORATORY/HEAD/OUTPUT/PROG9.JPG -------------------------------------------------------------------------------- /OUTPUT/OUTPUT3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbhishekMali21/COMPUTER-GRAPHICS-LABORATORY/HEAD/OUTPUT/OUTPUT3.gif -------------------------------------------------------------------------------- /OUTPUT/OUTPUT4.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbhishekMali21/COMPUTER-GRAPHICS-LABORATORY/HEAD/OUTPUT/OUTPUT4.gif -------------------------------------------------------------------------------- /OUTPUT/OUTPUT9.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbhishekMali21/COMPUTER-GRAPHICS-LABORATORY/HEAD/OUTPUT/OUTPUT9.gif -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Abhishek Mali 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LAB8.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void bezierCoefficients(int n,int *c) 6 | { 7 | int k,i; 8 | for(k=0;k<=n;k++) 9 | { 10 | c[k]=1; 11 | for(i=n;i>=k+1;i--) 12 | c[k]*=i; 13 | for(i=n-k;i>=2;i--) 14 | c[k]/=i; 15 | } 16 | } 17 | 18 | void display() 19 | { 20 | int cp[4][2]={{10,10},{100,200},{200,50},{300,300}}; 21 | int c[4],k,n=3; 22 | float x,y,u,blend; 23 | bezierCoefficients(n,c); 24 | glClear(GL_COLOR_BUFFER_BIT); 25 | glColor3f(1.0,0.0,0.0); 26 | glLineWidth(5.0); 27 | glBegin(GL_LINE_STRIP); 28 | for(u=0;u<1.0;u+=0.01) 29 | { 30 | x=0;y=0; 31 | for(k=0;k<4;k++) 32 | { 33 | blend=c[k]*pow(u,k)*pow(1-u,n-k); 34 | x+=cp[k][0]*blend; 35 | y+=cp[k][1]*blend; 36 | } 37 | glVertex2f(x,y); 38 | } 39 | glEnd(); 40 | glFlush(); 41 | } 42 | 43 | void myinit() 44 | { 45 | glClearColor(1.0,1.0,1.0,1.0); 46 | glColor3f(1.0,0.0,0.0); 47 | glPointSize(5.0); 48 | gluOrtho2D(0.0,600,0.0,600.0); 49 | } 50 | 51 | int main(int argc, char ** argv) 52 | { 53 | glutInit(&argc,argv); 54 | glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); 55 | glutInitWindowSize(600,600); 56 | glutCreateWindow("Bezier Curve"); 57 | glutDisplayFunc(display); 58 | myinit(); 59 | glutMainLoop(); 60 | return 0; 61 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Lab Experiments : 2 | ## PART A 3 | ### Design, develop, and implement the following programs using OpenGL API 4 | 1. Implement Brenham’s line drawing algorithm for all types of slope. 5 | 6 | 2. Create and rotate a triangle about the origin and a fixed point. 7 | 8 | 3. Draw a colour cube and spin it using OpenGL transformation matrices. Modelling a Coloured Cube 9 | 10 | 4. Draw a color cube and allow the user to move the camera suitably to experiment with perspective viewing. Positioning of Camera 11 | 12 | 5. Clip a lines using Cohen-Sutherland algorithm 13 | 14 | 6. To draw a simple shaded scene consisting of a tea pot on a table. Define suitably the position and properties of the light source along with the properties of the 15 | surfaces of the solid object used in the scene. Lighting and Shading 16 | 17 | 7. Design, develop and implement recursively subdivide a tetrahedron to form 3D sierpinski gasket. The number of recursive steps is to be specified by the user. 18 | sierpinski gasket 19 | 20 | 8. Develop a menu driven program to animate a flag using Bezier Curve algorithm 21 | 22 | 9. Develop a menu driven program to fill the polygon using scan line algorithm 23 | 24 | 25 | ## PART –B ( MINI-PROJECT) : 26 | Student should develop mini project on the topics mentioned below or similar applications using Open GL API. Consider all types of attributes like color, thickness, styles, font, 27 | background, speed etc., while doing mini project. 28 | 29 | (During the practical exam: the students should demonstrate and answer Viva-Voce) 30 | 31 | ### Sample Topics: 32 | Simulation of concepts of OS, Data structures, algorithms etc. 33 | -------------------------------------------------------------------------------- /LAB1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int x1, y1, x2, y2; 4 | 5 | void myInit() 6 | { 7 | glClear(GL_COLOR_BUFFER_BIT); 8 | glClearColor(0.0, 0.0, 0.0, 1.0); 9 | glMatrixMode(GL_PROJECTION); 10 | gluOrtho2D(0, 500, 0, 500); 11 | } 12 | 13 | void draw_pixel(int x, int y) 14 | { 15 | glBegin(GL_POINTS); 16 | glVertex2i(x, y); 17 | glEnd(); 18 | } 19 | 20 | void draw_line(int x1, int x2, int y1, int y2) 21 | { 22 | int dx, dy, i, e, x, y, incx, incy, inc1, inc2; 23 | dx = x2-x1; 24 | dy = y2-y1; 25 | if (dx < 0) 26 | dx = -dx; 27 | if (dy < 0) 28 | dy = -dy; 29 | 30 | incx = 1; 31 | if (x2 < x1) 32 | incx = -1; 33 | 34 | incy = 1; 35 | if (y2 < y1) 36 | incy = -1; 37 | 38 | x = x1; y = y1; 39 | 40 | if (dx > dy) 41 | { 42 | draw_pixel(x, y); 43 | e = 2 * dy-dx; 44 | inc1 = 2*(dy-dx); 45 | inc2 = 2*dy; 46 | for (i=0; i= 0) 49 | { 50 | y += incy; 51 | e += inc1; 52 | } 53 | else 54 | e += inc2; 55 | x += incx; 56 | draw_pixel(x, y); 57 | } 58 | } 59 | else 60 | { 61 | draw_pixel(x, y); 62 | e = 2*dx-dy; 63 | inc1 = 2*(dx-dy); 64 | inc2 = 2*dx; 65 | for (i=0; i= 0) 68 | { 69 | x += incx; 70 | e += inc1; 71 | } 72 | else 73 | e += inc2; 74 | y += incy; 75 | draw_pixel(x, y); 76 | } 77 | } 78 | } 79 | 80 | void myDisplay() 81 | { 82 | draw_line(x1, x2, y1, y2); 83 | glFlush(); 84 | } 85 | 86 | int main(int argc, char **argv) 87 | { 88 | printf( "Enter (x1, y1, x2, y2)\n"); 89 | scanf("%d %d %d %d", &x1, &y1, &x2, &y2); 90 | glutInit(&argc, argv); 91 | glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); 92 | glutInitWindowSize(500, 500); 93 | glutInitWindowPosition(0, 0); 94 | glutCreateWindow("Bresenham's Line Drawing"); 95 | myInit(); 96 | glutDisplayFunc(myDisplay); 97 | glutMainLoop(); 98 | return 0; 99 | } -------------------------------------------------------------------------------- /LAB9.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | float x1,x2,x3,x4,y1,y2,y3,y4; 5 | 6 | void edgedetect(float x1,float y1,float x2,float y2,int *le,int *re) 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 | x=x1; 20 | for(i=y1;i<=y2;i++) 21 | { 22 | if(x<(float)le[i]) 23 | le[i]=(int)x; 24 | if(x>(float)re[i]) 25 | re[i]=(int)x; 26 | x+=mx; 27 | } 28 | } 29 | 30 | void draw_pixel(int x,int y) 31 | { 32 | glColor3f(1.0,1.0,0.0); 33 | glBegin(GL_POINTS); 34 | glVertex2i(x,y); 35 | glEnd(); 36 | } 37 | 38 | void scanfill(float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4) 39 | { 40 | int le[500],re[500]; 41 | int i,y; 42 | for(i=0;i<500;i++) 43 | { 44 | le[i]=500; 45 | re[i]=0; 46 | } 47 | edgedetect(x1,y1,x2,y2,le,re); 48 | edgedetect(x2,y2,x3,y3,le,re); 49 | edgedetect(x3,y3,x4,y4,le,re); 50 | edgedetect(x4,y4,x1,y1,le,re); 51 | for(y=0;y<500;y++) 52 | { 53 | if(le[y]<=re[y]) 54 | for(i=(int)le[y];i<(int)re[y];i++) 55 | draw_pixel(i,y); 56 | } 57 | } 58 | 59 | void display() 60 | { 61 | x1=200.0;y1=200.0;x2=100.0;y2=300.0;x3=200.0;y3=400.0;x4=300.0;y4=300.0; 62 | glClear(GL_COLOR_BUFFER_BIT); 63 | glColor3f(0.0, 0.0, 1.0); 64 | glBegin(GL_LINE_LOOP); 65 | glVertex2f(x1,y1); 66 | glVertex2f(x2,y2); 67 | glVertex2f(x3,y3); 68 | glVertex2f(x4,y4); 69 | glEnd(); 70 | scanfill(x1,y1,x2,y2,x3,y3,x4,y4); 71 | glFlush(); 72 | } 73 | 74 | void myinit() 75 | { 76 | glClearColor(1.0,1.0,1.0,1.0); 77 | glColor3f(1.0,0.0,0.0); 78 | glPointSize(1.0); 79 | glMatrixMode(GL_PROJECTION); 80 | glLoadIdentity(); 81 | gluOrtho2D(0.0,499.0,0.0,499.0); 82 | } 83 | 84 | int main(int argc, char** argv) 85 | { 86 | glutInit(&argc,argv); 87 | glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); 88 | glutInitWindowSize(500,500); 89 | glutInitWindowPosition(0,0); 90 | glutCreateWindow("Filling a Polygon using Scan-line Algorithm"); 91 | glutDisplayFunc(display); 92 | myinit(); 93 | glutMainLoop(); 94 | } -------------------------------------------------------------------------------- /LAB3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | GLfloat vertices[]={-1.0,-1.0,-1.0,1.0,-1.0,-1.0,1.0, 5 | 1.0,-1.0,-1.0,1.0,-1.0,-1.0,-1.0,1.0, 6 | 1.0,-1.0,1.0,1.0,1.0,1.0,-1.0,1.0,1.0}; 7 | 8 | GLfloat colors[] = {0.0,0.0,0.0,1.0,0.0,0.0,1.0,1.0,0.0, 9 | 0.0,1.0,0.0,0.0,0.0,1.0,1.0,0.0, 10 | 1.0,1.0,1.0,1.0,0.0,1.0,1.0}; 11 | 12 | GLubyte cubeIndices[]={0,3,2,1,2,3,7,6,0,4,7,3,1,2,6,5,4, 5,6,7,0,1,5,4}; 13 | static GLfloat theta[]={0.0,0.0,0.0}; 14 | static GLint axis=2; 15 | 16 | void display(void) 17 | { 18 | glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 19 | glLoadIdentity(); 20 | glRotatef(theta[0],1.0,0.0,0.0); 21 | glRotatef(theta[1],0.0,1.0,0.0); 22 | glRotatef(theta[2],0.0,0.0,1.0); 23 | glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE, cubeIndices); 24 | glFlush(); 25 | glutSwapBuffers(); 26 | } 27 | 28 | void spinCube() 29 | { 30 | theta[axis]+=2.0; 31 | if(theta[axis]>360.0) theta[axis]-=360.0; 32 | glutPostRedisplay(); 33 | } 34 | 35 | void mouse(int btn, int state, int x, int y) 36 | { 37 | if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN) 38 | axis=0; 39 | if(btn==GLUT_MIDDLE_BUTTON&&state==GLUT_DOWN) 40 | axis=1; 41 | if(btn==GLUT_RIGHT_BUTTON&& state==GLUT_DOWN) 42 | axis=2; 43 | } 44 | 45 | void myReshape(int w, int h) 46 | { 47 | glViewport(0,0,w,h); 48 | glMatrixMode(GL_PROJECTION); 49 | glLoadIdentity(); 50 | if(w<=h) 51 | glOrtho(-2.0,2.0,-2.0*(GLfloat)h/(GLfloat)w, 2.0*(GLfloat)h/(GLfloat)w,-10.0,10.0); 52 | else 53 | glOrtho(-2.0*(GLfloat)w/(GLfloat)h, 2.0*(GLfloat)w/(GLfloat)h, -2.0,2.0,-10.0,10.0); 54 | glMatrixMode(GL_MODELVIEW); 55 | } 56 | 57 | void main(int argc, char **argv) 58 | { 59 | glutInit(&argc, argv); 60 | glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH); 61 | glutInitWindowSize(500,500); 62 | glutCreateWindow("Spin a color cube"); 63 | glutReshapeFunc(myReshape); 64 | glutDisplayFunc(display); 65 | glutIdleFunc(spinCube); 66 | glutMouseFunc(mouse); 67 | glEnable(GL_DEPTH_TEST); 68 | glEnableClientState(GL_COLOR_ARRAY); 69 | glEnableClientState(GL_VERTEX_ARRAY); 70 | glVertexPointer(3,GL_FLOAT, 0, vertices); 71 | glColorPointer(3, GL_FLOAT, 0, colors); 72 | glColor3f(1.0,1.0,1.0); 73 | glutMainLoop(); 74 | } -------------------------------------------------------------------------------- /LAB7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | typedef float point[3]; 6 | point v[]={ {0.0, 0.0, 1.0}, 7 | {0.0, 0.942809, -0.33333}, 8 | {-0.816497, -0.471405, -0.333333}, 9 | {0.816497, -0.471405, -0.333333}}; 10 | static GLfloat theta[] = {0.0,0.0,0.0}; 11 | int n; 12 | 13 | void triangle( point a, point b, point c) 14 | { 15 | glBegin(GL_POLYGON); 16 | glNormal3fv(a); 17 | glVertex3fv(a); 18 | glVertex3fv(b); 19 | glVertex3fv(c); 20 | glEnd(); 21 | } 22 | 23 | void divide_triangle(point a, point b, point c, int m) 24 | { 25 | point v1, v2, v3; 26 | int j; 27 | if(m>0) 28 | { 29 | for(j=0; j<3; j++) 30 | v1[j]=(a[j]+b[j])/2; 31 | for(j=0; j<3; j++) 32 | v2[j]=(a[j]+c[j])/2; 33 | for(j=0; j<3; j++) 34 | v3[j]=(b[j]+c[j])/2; 35 | divide_triangle(a, v1, v2, m-1); 36 | divide_triangle(c, v2, v3, m-1); 37 | divide_triangle(b, v3, v1, m-1); 38 | } 39 | else(triangle(a,b,c)); 40 | } 41 | 42 | void tetrahedron( int m) 43 | { 44 | glColor3f(1.0,0.0,0.0); 45 | divide_triangle(v[0], v[1], v[2], m); 46 | glColor3f(0.0,1.0,0.0); 47 | divide_triangle(v[3], v[2], v[1], m); 48 | glColor3f(0.0,0.0,1.0); 49 | divide_triangle(v[0], v[3], v[1], m); 50 | glColor3f(0.0,0.0,0.0); 51 | divide_triangle(v[0], v[2], v[3], m); 52 | } 53 | 54 | void display(void) 55 | { 56 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 57 | glLoadIdentity(); 58 | tetrahedron(n); 59 | glFlush(); 60 | } 61 | 62 | void myReshape(int w, int h) 63 | { 64 | glViewport(0, 0, w, h); 65 | glMatrixMode(GL_PROJECTION); 66 | glLoadIdentity(); 67 | if (w <= h) 68 | glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w, 2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0); 69 | else 70 | glOrtho(-2.0 * (GLfloat) w / (GLfloat) h, 2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0); 71 | glMatrixMode(GL_MODELVIEW); 72 | glutPostRedisplay(); 73 | } 74 | 75 | void main(int argc, char **argv) 76 | { 77 | printf(" No. of Divisions ? "); 78 | scanf("%d",&n); 79 | glutInit(&argc, argv); 80 | glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); 81 | glutInitWindowSize(500, 500); 82 | glutCreateWindow("3D Gasket"); 83 | glutReshapeFunc(myReshape); 84 | glutDisplayFunc(display); 85 | glEnable(GL_DEPTH_TEST); 86 | glClearColor (1.0, 1.0, 1.0, 1.0); 87 | glutMainLoop(); 88 | } -------------------------------------------------------------------------------- /LAB6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | void wall(double thickness) 3 | { 4 | glPushMatrix(); 5 | glTranslated(0.5,0.5*thickness, 0.5); 6 | glScaled(1.0,thickness, 1.0); 7 | glutSolidCube(1.0); 8 | glPopMatrix(); 9 | } 10 | 11 | void tableleg(double thick, double len) 12 | { 13 | glPushMatrix(); 14 | glTranslated(0,len/2,0); 15 | glScaled(thick, len, thick); 16 | glutSolidCube(1.0); 17 | glPopMatrix(); 18 | } 19 | 20 | void table(double topwid, double topthick, double legthick, double leglen) 21 | { 22 | glPushMatrix(); 23 | glTranslated(0,leglen,0); 24 | glScaled(topwid, topthick, topwid); 25 | glutSolidCube(1.0); 26 | glPopMatrix(); 27 | double dist=0.95*topwid/2.0-legthick/2.0; 28 | glPushMatrix(); 29 | glTranslated(dist, 0, dist); 30 | tableleg(legthick, leglen); 31 | glTranslated(0.0,0.0,-2*dist); 32 | tableleg(legthick, leglen); 33 | glTranslated(-2*dist, 0, 2*dist); 34 | tableleg(legthick, leglen); 35 | glTranslated(0,0,-2*dist); 36 | tableleg(legthick,leglen); 37 | glPopMatrix(); 38 | } 39 | 40 | void displaySolid(void) 41 | { 42 | GLfloat mat_ambient[]={0.7f,0.7f,0.7f,1.0f}; 43 | GLfloat mat_diffuse[]={0.5f,0.5f,0.5f,1.0f}; 44 | GLfloat mat_specular[]={1.0f,1.0f,1.0f,1.0f}; 45 | GLfloat mat_shininess[]={50.0f}; 46 | glMaterialfv(GL_FRONT,GL_AMBIENT, mat_ambient); 47 | glMaterialfv(GL_FRONT,GL_DIFFUSE, mat_diffuse); 48 | glMaterialfv(GL_FRONT,GL_SPECULAR, mat_specular); 49 | glMaterialfv(GL_FRONT,GL_SHININESS, mat_shininess); 50 | 51 | GLfloat lightintensity[]={0.7f,0.7f,0.7f,1.0f}; 52 | GLfloat lightposition[]={2.0f,6.0f,3.0f,0.0f}; 53 | glLightfv(GL_LIGHT0, GL_POSITION, lightposition); 54 | glLightfv(GL_LIGHT0, GL_DIFFUSE, lightintensity); 55 | 56 | glMatrixMode(GL_PROJECTION); 57 | glLoadIdentity(); 58 | double winht=1.0; 59 | glOrtho(-winht*64/48, winht*64/48, -winht, winht, 60 | 0.1, 100.0); 61 | glMatrixMode(GL_MODELVIEW); 62 | glLoadIdentity(); 63 | gluLookAt(2.3,1.3,2.0,0.0,0.25,0.0,0.0,1.0,0.0); 64 | glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 65 | glPushMatrix(); 66 | glTranslated(0.6,0.38,0.5); 67 | glRotated(30,0,1,0); 68 | glutSolidTeapot(0.08); 69 | glPopMatrix(); 70 | glPushMatrix(); 71 | glTranslated(0.4,0,0.4); 72 | table(0.6,0.02,0.02,0.3); 73 | glPopMatrix(); 74 | wall(0.02); 75 | glPushMatrix(); 76 | glRotated(90.0,0.0,0.0,1.0); 77 | wall(0.02); 78 | glPopMatrix(); 79 | glPushMatrix(); 80 | glRotated(-90.0,1.0,0.0,0.0); 81 | wall(0.02); 82 | glPopMatrix(); 83 | glFlush(); 84 | } 85 | 86 | 87 | void main(int argc, char **argv) 88 | { 89 | glutInit(&argc, argv); 90 | glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH); 91 | glutInitWindowPosition(50,50); 92 | glutInitWindowSize(400,300); 93 | glutCreateWindow("Shaded Scene"); 94 | glutDisplayFunc(displaySolid); 95 | glEnable(GL_LIGHTING); 96 | glEnable(GL_LIGHT0); 97 | glShadeModel(GL_SMOOTH); 98 | glEnable(GL_DEPTH_TEST); 99 | glEnable(GL_NORMALIZE); 100 | glClearColor(0.1,0.1,0.1,0.0); 101 | glViewport(0,0,640,480); 102 | glutMainLoop(); 103 | } -------------------------------------------------------------------------------- /LAB4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | GLfloat vertices[ ]={ -1.0,-1.0,-1.0, 6 | 1.0,-1.0,-1.0, 7 | 1.0, 1.0,-1.0, 8 | -1.0, 1.0,-1.0, 9 | -1.0,-1.0, 1.0, 10 | 1.0,-1.0, 1.0, 11 | 1.0, 1.0, 1.0, 12 | -1.0, 1.0, 1.0 }; 13 | 14 | GLfloat normals[ ] ={ -1.0,-1.0,-1.0, 15 | 1.0,-1.0,-1.0, 16 | 1.0, 1.0,-1.0, 17 | -1.0, 1.0,-1.0, 18 | -1.0,-1.0, 1.0, 19 | 1.0,-1.0, 1.0, 20 | 1.0, 1.0, 1.0, 21 | -1.0, 1.0, 1.0 }; 22 | 23 | GLfloat colors[ ]={ 0.0, 0.0, 0.0, 24 | 1.0, 0.0, 0.0, 25 | 1.0, 1.0, 0.0, 26 | 0.0, 1.0, 0.0, 27 | 0.0, 0.0, 1.0, 28 | 1.0, 0.0, 1.0, 29 | 1.0, 1.0, 1.0, 30 | 0.0, 1.0, 1.0}; 31 | 32 | GLubyte cubeIndices[]={ 0,3,2,1, 33 | 2,3,7,6, 34 | 0,4,7,3, 35 | 1,2,6,5, 36 | 4,5,6,7, 37 | 0,1,5,4}; 38 | 39 | static GLfloat theta[] = {0.0,0.0,0.0}; 40 | static GLint axis = 2; 41 | static GLdouble viewer[] = {0.0,0.0,5.0}; 42 | 43 | void display(void) 44 | { 45 | glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 46 | glLoadIdentity(); 47 | gluLookAt(viewer[0],viewer[1],viewer[2],0.0,0.0,0.0,0.0,1.0,0.0); 48 | glRotatef(theta[0],1.0,0.0,0.0); 49 | glRotatef(theta[1],0.0,1.0,0.0); 50 | glRotatef(theta[2],0.0,0.0,1.0); 51 | glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,cubeIndices); 52 | glFlush(); 53 | glutSwapBuffers(); 54 | } 55 | 56 | void mouse(int btn, int state, int x, int y) 57 | { 58 | if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN) 59 | axis=0; 60 | if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) 61 | axis=1; 62 | if(btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN) 63 | axis=2; 64 | theta[axis]+=2.0; 65 | 66 | if(theta[axis]>360.0) 67 | theta[axis]-=360.0; 68 | glutPostRedisplay(); 69 | } 70 | 71 | void keys(unsigned char key, int x, int y) 72 | { 73 | if(key=='x') viewer[0]-=1.0; 74 | if(key=='X') viewer[0]+=1.0; 75 | if(key=='y') viewer[1]-=1.0; 76 | if(key=='Y') viewer[1]+=1.0; 77 | if(key=='z') viewer[2]-=1.0; 78 | if(key=='Z') viewer[2]+=1.0; 79 | glutPostRedisplay(); 80 | } 81 | 82 | void myReshape(int w, int h) 83 | { 84 | glViewport(0,0,w,h); 85 | glMatrixMode(GL_PROJECTION); 86 | glLoadIdentity(); 87 | if(w<=h) 88 | glFrustum(-2.0,2.0,-2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,2.0,20.0); 89 | else 90 | glFrustum(-2.0,2.0,-2.0*(GLfloat)w/(GLfloat)h,2.0*(GLfloat)w/(GLfloat)h,2.0,20.0); 91 | glMatrixMode(GL_MODELVIEW); 92 | } 93 | 94 | int main(int argc, char **argv) 95 | { 96 | glutInit(&argc,argv); 97 | glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH); 98 | glutInitWindowSize(500,500); 99 | glutCreateWindow("color cuce"); 100 | glutReshapeFunc(myReshape); 101 | glutDisplayFunc(display); 102 | glutKeyboardFunc(keys); 103 | glutMouseFunc(mouse); 104 | glEnable(GL_DEPTH_TEST); 105 | glEnableClientState(GL_COLOR_ARRAY); 106 | glEnableClientState(GL_VERTEX_ARRAY); 107 | glEnableClientState(GL_NORMAL_ARRAY); 108 | glVertexPointer(3,GL_FLOAT,0,vertices); 109 | glColorPointer(3,GL_FLOAT,0,colors); 110 | glNormalPointer(GL_FLOAT,0,normals); 111 | glColor3f(1.0,1.0,1.0); 112 | glutMainLoop(); 113 | } -------------------------------------------------------------------------------- /LAB5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define outcode int 4 | 5 | double xmin=50,ymin=50,xmax=100,ymax=100; 6 | double xvmin=200,yvmin=200,xvmax=300,yvmax=300; 7 | double x0,y0,x1,y1; 8 | const int RIGHT=8; 9 | const int LEFT=2; 10 | const int TOP=4; 11 | const int BOTTOM=1; 12 | outcode ComputeOutCode(double x, double y); 13 | 14 | void CohenSutherland(double x0, double y0, double x1, double y1) 15 | { 16 | outcode outcode0, outcode1, outcodeOut; 17 | bool accept=false, done=false; 18 | outcode0=ComputeOutCode(x0,y0); 19 | outcode1=ComputeOutCode(x1,y1); 20 | do 21 | { 22 | if(!(outcode0 | outcode1)) 23 | { 24 | accept=true; 25 | done=true; 26 | } 27 | else if(outcode0 & outcode1) 28 | done=true; 29 | else 30 | { 31 | double x, y; 32 | outcodeOut=outcode0?outcode0:outcode1; 33 | if(outcodeOut & TOP) 34 | { 35 | x=x0+(x1-x0)*(ymax-y0)/(y1-y0); 36 | y=ymax; 37 | } 38 | else if(outcodeOut & BOTTOM) 39 | { 40 | x=x0+(x1-x0)*(ymin-y0)/(y1-y0); 41 | y=ymin; 42 | } 43 | else if(outcodeOut & RIGHT) 44 | { 45 | y=y0+(y1-y0)*(xmax-x0)/(x1-x0); 46 | x=xmax; 47 | } 48 | else 49 | { 50 | y=y0+(y1-y0)*(xmin-x0)/(x1-x0); 51 | x=xmin; 52 | } 53 | if(outcodeOut==outcode0) 54 | { 55 | x0=x; 56 | y0=y; 57 | outcode0=ComputeOutCode(x0,y0); 58 | } 59 | else 60 | { 61 | x1=x; 62 | y1=y; 63 | outcode1=ComputeOutCode(x1,y1); 64 | } 65 | } 66 | }while(!done); 67 | 68 | if(accept) 69 | { 70 | double sx=(xvmax-xvmin)/(xmax-xmin); 71 | double sy=(yvmax-yvmin)/(ymax-ymin); 72 | double vx0=xvmin+(x0-xmin)*sx; 73 | double vy0=yvmin+(y0-ymin)*sy; 74 | double vx1=xvmin+(x1-xmin)*sx; 75 | double vy1=yvmin+(y1-ymin)*sy; 76 | glColor3f(1.0,0.0,0.0); 77 | glBegin(GL_LINE_LOOP); 78 | glVertex2f(xvmin, yvmin); 79 | glVertex2f(xvmax, yvmin); 80 | glVertex2f(xvmax, yvmax); 81 | glVertex2f(xvmin, yvmax); 82 | glEnd(); 83 | glColor3f(0.0,0.0,1.0); 84 | glBegin(GL_LINES); 85 | glVertex2d(vx0,vy0); 86 | glVertex2d(vx1,vy1); 87 | glEnd(); 88 | } 89 | } 90 | 91 | outcode ComputeOutCode(double x, double y) 92 | { 93 | outcode code=0; 94 | if(y > ymax) 95 | code = TOP; 96 | else if(y < ymin) 97 | code = BOTTOM; 98 | if(x > xmax) 99 | code = RIGHT; 100 | else if(x < xmin) 101 | code = LEFT; 102 | return code; 103 | } 104 | 105 | void display() 106 | { 107 | glClear(GL_COLOR_BUFFER_BIT); 108 | glColor3f(1.0,0.0,0.0); 109 | glBegin(GL_LINES); 110 | glVertex2d(x0,y0); 111 | glVertex2d(x1,y1); 112 | glEnd(); 113 | glColor3f(0.0,0.0,1.0); 114 | glBegin(GL_LINE_LOOP); 115 | glVertex2f(xmin, ymin); 116 | glVertex2f(xmax, ymin); 117 | glVertex2f(xmax, ymax); 118 | glVertex2f(xmin, ymax); 119 | glEnd(); 120 | CohenSutherland(x0,y0,x1,y1); 121 | glFlush(); 122 | } 123 | 124 | void myinit() 125 | { 126 | glClearColor(1.0,1.0,1.0,1.0); 127 | glMatrixMode(GL_PROJECTION); 128 | glLoadIdentity(); 129 | gluOrtho2D(0.0,499.0,0.0,499.0); 130 | } 131 | 132 | void main(int argc, char** argv) 133 | { 134 | printf("Enter the end points of the line: "); 135 | scanf("%lf%lf%lf%lf", &x0,&y0,&x1,&y1); 136 | glutInit(&argc, argv); 137 | glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); 138 | glutInitWindowSize(500,500); 139 | glutInitWindowPosition(0,0); 140 | glutCreateWindow("Cohen-Sutherland Line Clipping"); 141 | glutDisplayFunc(display); 142 | myinit(); 143 | glutMainLoop(); 144 | } -------------------------------------------------------------------------------- /LAB2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | GLsizei winWidth = 600, winHeight = 600; 6 | GLfloat xwcMin = 0.0, xwcMax = 225.0; 7 | GLfloat ywcMin = 0.0, ywcMax = 225.0; 8 | 9 | class wcPt2D 10 | { 11 | public: GLfloat x, y; 12 | }; 13 | 14 | typedef GLfloat Matrix3x3 [3][3]; 15 | Matrix3x3 matComposite; 16 | const GLdouble pi = 3.14159; 17 | 18 | void init (void) 19 | { 20 | glClearColor (1.0, 1.0, 1.0, 0.0); 21 | } 22 | 23 | void matrix3x3SetIdentity (Matrix3x3 matIdent3x3) 24 | { 25 | GLint row, col; 26 | for (row = 0; row < 3; row++) 27 | for (col = 0; col < 3; col++) 28 | matIdent3x3 [row][col] = (row == col); 29 | } 30 | 31 | void matrix3x3PreMultiply (Matrix3x3 m1, Matrix3x3 m2) 32 | { 33 | GLint row, col; 34 | Matrix3x3 matTemp; 35 | for (row = 0; row < 3; row++) 36 | for (col = 0; col < 3 ; col++) 37 | matTemp [row][col] = m1 [row][0] * m2 [0][col] + m1 [row][1] * m2 [1][col] + m1 [row][2] * m2 [2][col]; 38 | for (row = 0; row < 3; row++) 39 | for (col = 0; col < 3; col++) 40 | m2 [row][col] = matTemp [row][col]; 41 | } 42 | 43 | void translate2D (GLfloat tx, GLfloat ty) 44 | { 45 | Matrix3x3 matTransl; 46 | matrix3x3SetIdentity (matTransl); 47 | matTransl [0][2] = tx; 48 | matTransl [1][2] = ty; 49 | matrix3x3PreMultiply (matTransl, matComposite); 50 | } 51 | 52 | void rotate2D (wcPt2D pivotPt, GLfloat theta) 53 | { 54 | Matrix3x3 matRot; 55 | matrix3x3SetIdentity (matRot); 56 | matRot [0][0] = cos (theta); 57 | matRot [0][1] = -sin (theta); 58 | matRot [0][2] = pivotPt.x * (1 - cos (theta)) + pivotPt.y * sin (theta); 59 | matRot [1][0] = sin (theta); 60 | matRot [1][1] = cos (theta); 61 | matRot [1][2] = pivotPt.y * (1 - cos (theta)) - pivotPt.x * sin (theta); 62 | matrix3x3PreMultiply (matRot, matComposite); 63 | } 64 | 65 | void scale2D (GLfloat sx, GLfloat sy, wcPt2D fixedPt) 66 | { 67 | Matrix3x3 matScale; 68 | matrix3x3SetIdentity (matScale); 69 | matScale [0][0] = sx; 70 | matScale [0][2] = (1 - sx) * fixedPt.x; 71 | matScale [1][1] = sy; 72 | matScale [1][2] = (1 - sy) * fixedPt.y; 73 | matrix3x3PreMultiply (matScale, matComposite); 74 | } 75 | 76 | void transformVerts2D (GLint nVerts, wcPt2D * verts) 77 | { 78 | GLint k; 79 | GLfloat temp; 80 | for (k = 0; k < nVerts; k++) 81 | { 82 | temp = matComposite [0][0] * verts [k].x + matComposite [0][1] * verts [k].y + matComposite [0][2]; 83 | verts [k].y = matComposite [1][0] * verts [k].x + matComposite [1][1] * verts [k].y + matComposite [1][2]; 84 | verts [k].x = temp; 85 | } 86 | } 87 | 88 | void triangle (wcPt2D *verts) 89 | { 90 | GLint k; 91 | glBegin (GL_TRIANGLES); 92 | for (k = 0; k < 3; k++) 93 | glVertex2f (verts [k].x, verts [k].y); 94 | glEnd ( ); 95 | } 96 | 97 | void displayFcn (void) 98 | { 99 | GLint nVerts = 3; 100 | wcPt2D verts [3] = { {50.0, 25.0}, {150.0, 25.0}, {100.0, 100.0} }; 101 | wcPt2D centroidPt; 102 | GLint k, xSum = 0, ySum = 0; 103 | for (k = 0; k < nVerts; k++) 104 | { 105 | xSum += verts [k].x; 106 | ySum += verts [k].y; 107 | } 108 | centroidPt.x = GLfloat (xSum) / GLfloat (nVerts); 109 | centroidPt.y = GLfloat (ySum) / GLfloat (nVerts); 110 | wcPt2D pivPt,fixedPt; 111 | pivPt = centroidPt; 112 | fixedPt = centroidPt; 113 | GLfloat tx = 0.0, ty = 100.0; 114 | GLfloat sx = 0.5, sy = 0.5; 115 | GLdouble theta = pi/2.0; 116 | glClear (GL_COLOR_BUFFER_BIT); 117 | glColor3f (0.0, 0.0, 1.0); 118 | triangle (verts); 119 | matrix3x3SetIdentity (matComposite); 120 | scale2D (sx, sy, fixedPt); 121 | rotate2D (pivPt, theta); 122 | translate2D (tx, ty); 123 | transformVerts2D (nVerts, verts); 124 | glColor3f (1.0, 0.0, 0.0); 125 | triangle (verts); 126 | glFlush ( ); 127 | } 128 | 129 | void winReshapeFcn (GLint newWidth, GLint newHeight) 130 | { 131 | glMatrixMode (GL_PROJECTION); 132 | glLoadIdentity ( ); 133 | gluOrtho2D (xwcMin, xwcMax, ywcMin, ywcMax); 134 | glClear (GL_COLOR_BUFFER_BIT); 135 | } 136 | 137 | int main (int argc, char ** argv) 138 | { 139 | glutInit (&argc, argv); 140 | glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); 141 | glutInitWindowPosition (50, 50); 142 | glutInitWindowSize (winWidth, winHeight); 143 | glutCreateWindow ("Geometric Transformation Sequence"); 144 | init ( ); 145 | glutDisplayFunc (displayFcn); 146 | glutReshapeFunc (winReshapeFcn); 147 | glutMainLoop ( ); 148 | return 0; 149 | } --------------------------------------------------------------------------------