Category : CG LAB

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 surfaces of the solid object used in the scene. Lighting and Shading#include<GL/glut.h> void obj(double tx,double ty,double tz,double sx,double sy,double sz){ glRotated(50,0,1,0); glRotated(10,-1,0,0); glRotated(11.7,0,0,-1); glTranslated(tx,ty,tz); glScaled(sx,sy,sz); glutSolidCube(1); ..

Read more

8. Develop a menu driven program to animate a flag using the Bezier Curve algorithm#include<GL/glut.h>#include<math.h>#include<stdio.h> void bezierCoefficients(int n,int *c){ int k,i; for(k=0;k<=n;k++) { c[k]=1; for(i=n;i>=k+1;i–) c[k]*=i; for(i=n-k;i>=2;i–) c[k]/=i; }} void display(){ int cp[4][2]={{10,10},{100,200},{200,50},{300,300}}; int c[4],k,n=3; float x,y,u,blend; bezierCoefficients(n,c); glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,0.0,0.0); glLineWidth(5.0); glBegin(GL_LINE_STRIP); for(u=0;u<1.0;u+=0.01) { x=0;y=0; for(k=0;k<4;k++) { blend=c[k]*pow(u,k)*pow(1-u,n-k); x+=cp[k][0]*blend; y+=cp[k][1]*blend; } glVertex2f(x,y); } glEnd(); glFlush();} ..

Read more

2. Create and rotate a triangle about the origin and a fixed point.#include <stdio.h>#include <GL/glut.h> void display(){    glClear(GL_COLOR_BUFFER_BIT);    glLoadIdentity();    glTranslatef( -0.14 , 0.62 , 0.0);    glRotatef(90 , 0 , 0 , 1);    glBegin( GL_TRIANGLES );    glColor3f( 0 , 1 , 0);    glVertex3f( -0.3 , -0.2 , 0 );  ..

Read more

4. Draw a color cube and allow the user to move the camera suitably to experiment with perspective viewing. Positioning of Camera #include<stdio.h>#include<math.h>#include<iostream>#include<GL/glut.h>using namespace std; float v[8][3] = {{-1,-1,-1},{-1,1,-1},{1,1,-1},{1,-1,-1},{-1,-1,1},{-1,1,1},{1,1,1},{1,-1,1}};float t[] ={0,0,0};int ax = 2;float viewer[]={5,0,0}; void init(){ glMatrixMode(GL_PROJECTION); glFrustum(-2,2,-2,2,2,10); glMatrixMode(GL_MODELVIEW);} void polygon(int a, int b, int c, int d){ glBegin(GL_QUADS); glVertex3fv(v[a]); glVertex3fv(v[b]); glVertex3fv(v[c]); glVertex3fv(v[d]); glEnd();} void colorcube(){ glColor3f(0,0,1); polygon(0,1,2,3); glColor3f(0,1,0); polygon(4,5,6,7); glColor3f(1,0,0); polygon(0,1,5,4); glColor3f(0,0,0); polygon(3,2,6,7); glColor3f(0,1,1); polygon(0,4,7,3); glColor3f(1,0,1); polygon(1,5,6,2);} void spincube(){ t[ax] += 1; if (t[ax] == 360)  ..

Read more

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. Sierpinski gasket#include<stdio.h>#include<math.h>#include<iostream>#include<GL/glut.h>using namespace std;float v[4][3] = { { 0.0,0.0,1.0 },{ 0,1,-1 },{ -0.8,-0.4,-1 },{ 0.8,-0.4,-1 } };int n; void triangle(float a[], float b[], float c[]){ glBegin(GL_POLYGON); glVertex3fv(a); glVertex3fv(b); glVertex3fv(c); ..

Read more

1. Implement Bresenham’s line drawing algorithm for all types of slope.#include<stdio.h>#include<math.h>#include<iostream>#include<GL/glut.h> int xstart, ystart, xend, yend;void init(){ gluOrtho2D(0, 500, 0, 500);} void draw_pixel(int x, int y){ glColor3f(1, 0, 0); glBegin(GL_POINTS); glVertex2i(x, y); glEnd(); glFlush();} void LineBres(int xstart, int ystart, int xend, int yend){ int dx = abs(xend – xstart); int dy = abs(yend – ystart); ..

Read more

5. Clip lines using Cohen-Sutherland algorithm#include<stdio.h>#include<GL/glut.h> #define true 1;#define false 0;#define bool int;double x,y;int xmin=50,xmax=100,ymin=50,ymax=100;const int RIGHT=8,LEFT=2,TOP=4,BOTTOM=1;int outcode0,outcode1,outcodeout,done,accept; int computeoutcode(double x,double y){  int code=0;  if(y>ymax)    code|=TOP;  else if(y<ymin)    code|=BOTTOM;  if(x>xmax)    code|=RIGHT;  else if(x<xmin)    code|=LEFT;  return code;} void LineClip(double x0,double y0,double x1,double y1){  int accept=false;  int done=false;  outcode0=computeoutcode(x0,y0);  outcode1=computeoutcode(x1,y1);  do{    if(!(outcode0|outcode1))  ..

Read more

9. Develop a menu driven program to fill the polygon using the Scan line algorithm#include<stdio.h>#include<math.h>#include<iostream>#include<GL/glut.h>int le[500], re[500], flag=0 ,m; void init(){ gluOrtho2D(0, 500, 0, 500);} void edge(int x0, int y0, int x1, int y1){ if (y1<y0) { int tmp; tmp = y1; y1 = y0; y0 = tmp; tmp = x1; x1 = x0; x0 ..

Read more

3. Draw a color cube and spin it using OpenGL transformation matrices. Modeling a Coloured Cube#include<stdio.h>#include<math.h>#include<iostream>#include<GL/glut.h> float v[8][3] = { { -1,-1,-1 } ,{ -1,1,-1 } ,{ 1,1,-1 } ,{ 1,-1,-1 } , { -1,-1,1 } ,{ -1,1,1 } ,{ 1,1,1 } ,{ 1,-1,1 } };int t[] = { 0,0,0 };int ax = 2; void ..

Read more