0% found this document useful (0 votes)
54 views

Program No

The document describes an algorithm to clip lines using the Cohen-Sutherland algorithm. It defines constants for the bit codes representing the four sides of the clipping window. It also defines functions to compute the outcode of a point and perform the line clipping. The main line clipping function computes the outcodes of the end points, and if one or both are outside the window, it calculates the intersection point with the window boundary and clips the line segment to that point.

Uploaded by

Kp Kayum
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Program No

The document describes an algorithm to clip lines using the Cohen-Sutherland algorithm. It defines constants for the bit codes representing the four sides of the clipping window. It also defines functions to compute the outcode of a point and perform the line clipping. The main line clipping function computes the outcodes of the end points, and if one or both are outside the window, it calculates the intersection point with the window boundary and clips the line segment to that point.

Uploaded by

Kp Kayum
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 59

[Type the document title]

Program No.1
Implement Brenham`s line drawing algorithm for all types of slope

#include<GL/glut.h>

#include <stdlib.h>

#include<math.h>

void init(void)

//set display-window background color to white

glClearColor(1.0,1.0,1.0,0.0);

//set projection paramaters

glMatrixMode(GL_PROJECTION);

gluOrtho2D(0.0,300.0,0.0,300.0);

void setPixel(GLint xCoordinate, GLint yCoordinate)

glBegin(GL_POINTS);

glVertex2i(xCoordinate,yCoordinate);

glEnd();

glFlush(); //executes all OpenGL functions as quickl y as possible

//Bresenham line-drawing procedure for |m| < 1.0

void lineBres(GLint x0, GLint y0, GLint xEnd, GLint

yEnd)
[Type the document title]

GLint dx = fabs(xEnd - x0);

GLint dy = fabs(yEnd - y0);

GLint p = 2 * dy - dx;

GLint twoDy = 2 * dy;

GLint twoDyMinusDx = 2 * (dy-dx);

GLint x,y;

// determine which endpoint to use as start position

if (x0 > xEnd){

x = xEnd;

y = yEnd;

xEnd = x;

}else{

x = x0;

y = y0;

setPixel(x,y);

while(x<xEnd){

x++;

if(p<0)

p += twoDy;

else{

y++;
[Type the document title]

p += twoDyMinusDx;

setPixel(x,y);

void drawMyLine(void)

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0,0.0,0.0);

glPointSize(4.0);

GLint x0 = 100;

GLint y0 = 150;

GLint xEnd = 200;

GLint yEnd = 200;

lineBres(x0,y0,xEnd,yEnd);

void main(int argc, char**argv)

//initialize GLUT

glutInit(&argc,argv);

//initialize display mode

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

//set display-window width & height


[Type the document title]

glutInitWindowSize(400,400);

//set display-window upper-left position

glutInitWindowPosition(0,0);

//create display-window with a title

glutCreateWindow("Bresenham");

//initialze OpenGL

init();

//call graphics to be displayed on the window

glutDisplayFunc(drawMyLine);

//display everything and wait

glutMainLoop();

}
[Type the document title]

OUTPUT:
[Type the document title]

Program No.2
Create and rotate a triangle about origin and fixed point

#include<GL/glut.h>

#include<stdio.h>

float v[]={0,0.6,0.25,0.9,0.5,0.6};

GLubyte list[]={0,1,2};

int g=0;

void dis()

glClear(GL_COLOR_BUFFER_BIT);

glClearColor(0,0,0,0);

glColor3f(1,0,1);

//glViewport(0,0,700,700);

glDrawElements(GL_LINE_LOOP,3,GL_UNSIGNED_BYTE,list);

glRotated(g,0,0,1);

glDrawElements(GL_LINE_LOOP,3,GL_UNSIGNED_BYTE,list);

glFlush();

glLoadIdentity();
[Type the document title]

int main(int argc, char **argv)

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize(500,500);

printf("Enter Rotation Angle:");

scanf("%d",&g);

glutCreateWindow("Triangle Rotation");

glutDisplayFunc(dis);

glEnableClientState(GL_VERTEX_ARRAY);

glVertexPointer(2,GL_FLOAT,0,v);

glutMainLoop();

}
[Type the document title]

OUTPUT:
[Type the document title]

Program No.3
Draw a color cube and spin it using OpenGL transformation matrices.

#include<GL/glut.h>

float v[]={-0.5,-0.5,-0.5, -0.5,0.5,-0.5, 0.5,0.5,-0.5, 0.5,-0.5,-0.5,


-0.5,-0.5,0.5, -0.5,0.5,0.5, 0.5,0.5,0.5, 0.5,-0.5,0.5};

float c[]={0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1,};

GLubyte d[]={0,1,2,3, 2,3,7,6, 4,5,6,7, 4,5,1,0, 5,6,2,1, 0,3,7,4};

int gx=0,gy=0,gz=1.0;

void display()

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glRotated(0.2,gx,gy,gz);

glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,d);

glFlush();

}
[Type the document title]

void mouse(int btn,int state,int x,int y)

if(btn==GLUT_LEFT_BUTTON&&state==GLUT_DOWN) {gx=1; gy=0;


gz=0;}

if(btn==GLUT_MIDDLE_BUTTON&&state==GLUT_DOWN) {gx=0;
gy=1; gz=0;}

if(btn==GLUT_RIGHT_BUTTON&&state==GLUT_DOWN) {gx=0;
gy=0; gz=1;}

int main(int argc, char **argv)

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);

glutCreateWindow("cube");

glutDisplayFunc(display);

glutIdleFunc(display);

glutMouseFunc(mouse);

glEnableClientState(GL_VERTEX_ARRAY);

glEnableClientState(GL_COLOR_ARRAY);

glVertexPointer(3,GL_FLOAT,0,v);

glColorPointer(3,GL_FLOAT,0,c);

glEnable(GL_DEPTH_TEST);

glutMainLoop();

}
[Type the document title]

OUTPUT:
[Type the document title]

Program No.4
Draw a color cube and allow the user to move the camera suitably to
experiment with perspective viewing

#include <GL/glut.h>

float v[]={-0.5,-0.5,-0.5, -0.5,0.5,-0.5, 0.5,0.5,-0.5, 0.5, -0.5,-0.5,


-0.5,-0.5,0.5, -0.5,0.5,0.5, 0.5,0.5,0.5, 0.5,-0.5,0.5};

float c[]={0,0,0, 0,1,0, 1,1,0, 1,0,0, 0,0,1, 0,1,1, 1,1,1, 1,0,1,};

GLubyte list[]={0,1,2,3, 6,7,3,2, 4,7,6,5, 4,5,1,0, 5,6,2,1, 0,3,7,4};

int gx=0,gy=0,gz=1;

static GLfloat theta[] = {0.0,0.0,0.0};

static GLint axis = 2;


[Type the document title]

static GLdouble viewer[]= {0.0, 0.0, 5.0};

void display(void)

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

gluLookAt(viewer[0],viewer[1],viewer[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

glRotatef(theta[0], 1.0, 0.0, 0.0);

glRotatef(theta[1], 0.0, 1.0, 0.0);

glRotatef(theta[2], 0.0, 0.0, 1.0);

glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,list);

glFlush();

void mouse(int btn, int state, int x, int y)

{ if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;

if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1;

if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;

theta[axis] += 2.0;

if( theta[axis] > 360.0 ) theta[axis] -= 360.0;

display();

void keys(unsigned char key, int x, int y)

{ if(key == 'x') viewer[0]-= 1.0;


[Type the document title]

if(key == 'X') viewer[0]+= 1.0;

if(key == 'y') viewer[1]-= 1.0;

if(key == 'Y') viewer[1]+= 1.0;

if(key == 'z') viewer[2]-= 1.0;

if(key == 'Z') viewer[2]+= 1.0;

display();

int main(int argc, char **argv)

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);

glutInitWindowSize(700, 700);

glutCreateWindow("Colorcube Viewer");

glMatrixMode(GL_PROJECTION);

glFrustum(-2.0, 2.0, -2.0, 2.0, 2.0, 20.0);

glMatrixMode(GL_MODELVIEW);

glutDisplayFunc(display);

glEnableClientState(GL_VERTEX_ARRAY);

glEnableClientState(GL_COLOR_ARRAY);

glVertexPointer(3,GL_FLOAT,0,v);

glColorPointer(3,GL_FLOAT,0,c);

glutMouseFunc(mouse);
[Type the document title]

glutKeyboardFunc(keys);

glEnable(GL_DEPTH_TEST);

glutMainLoop();

OUTPUT:
[Type the document title]

Program No.5

Clip a lines using Cohen-Sutherland algorithm

#include <GL/glut.h>

double xmin=50,ymin=50, xmax=100,ymax=100; // Window boundaries

double xvmin=200,yvmin=200,xvmax=300,yvmax=300; // Viewport boundaries


[Type the document title]

//bit codes for the right, left, top, & bottom

const int RIGHT = 8;

const int LEFT = 2;

const int TOP = 4;

const int BOTTOM = 1;

//function to compute bit codes of a point

//Compute the bit code for a point (x, y) using the clip rectangle bounded
diagonally by (xmin, ymin), and (xmax, ymax)

int ComputeOutCode (double x, double y)

int code = 0;

if (y > ymax) //above the clip window

code |= TOP;

else if (y < ymin) //below the clip window

code |= BOTTOM;

if (x > xmax) //to the right of clip window

code |= RIGHT;

else if (x < xmin) //to the left of clip window

code |= LEFT;

return code;

}
[Type the document title]

/*Cohen-Sutherland clipping algorithm clips a line from P0 = (x0, y0) to P1 = (x1,


y1) against a rectangle */

void CohenSutherlandLineClipAndDraw (double x0, double y0,double x1, double


y1)

//Outcodes for P0, P1 and the point that lies outside the clip rectangle

int outcode0, outcode1, outcodeOut;

_Bool accept =0, done =0;

//compute outcodes

outcode0 = ComputeOutCode (x0, y0);

outcode1 = ComputeOutCode (x1, y1);

do{

if (!(outcode0 | outcode1)) //logical OR is 0: Trivially accept & exit

accept =1;

done =1;

else if (outcode0 & outcode1) //logical AND is not 0: Trivially reject


and exit

done =1;
[Type the document title]

else

//calculate the line segment to clip from an outside point to an


intersection with clip edge

double x, y;

//At least one endpoint is outside the clip rectangle; pick it.

outcodeOut = outcode0? outcode0: outcode1;

//Now find the intersection point with formula: y = y0 + slope *


(x - x0), x = x0 + (1/slope)* (y - y0)

if (outcodeOut & TOP) //point is above the clip rectangle

x = x0 + (x1 - x0) * (ymax - y0)/(y1 - y0);

y = ymax;

else if (outcodeOut & BOTTOM) //point is below the clip


rectangle

x = x0 + (x1 - x0) * (ymin - y0)/(y1 - y0);

y = ymin;

else if (outcodeOut & RIGHT) //point is to the right of clip


rectangle
[Type the document title]

y = y0 + (y1 - y0) * (xmax - x0)/(x1 - x0);

x = xmax;

else //point is to the left of clip rectangle

y = y0 + (y1 - y0) * (xmin - x0)/(x1 - x0);

x = xmin;

//Now we move outside point to intersection point to clip

//and get ready for next pass.

if (outcodeOut == outcode0)

x0 = x;

y0 = y;

outcode0 = ComputeOutCode (x0, y0);

else

x1 = x;

y1 = y;

outcode1 = ComputeOutCode (x1, y1);


[Type the document title]

}while (!done);

if (accept)

{ // Window to viewport mappings

double sx=(xvmax-xvmin)/(xmax-xmin); // Scale parameters

double sy=(yvmax-yvmin)/(ymax-ymin);

double vx0=xvmin+(x0-xmin)*sx;

double vy0=yvmin+(y0-ymin)*sy;

double vx1=xvmin+(x1-xmin)*sx;

double vy1=yvmin+(y1-ymin)*sy;

//draw a red colored viewport

glColor3f(1.0, 0.0, 0.0);

glBegin(GL_LINE_LOOP);

glVertex2f(xvmin, yvmin);

glVertex2f(xvmax, yvmin);

glVertex2f(xvmax, yvmax);

glVertex2f(xvmin, yvmax);

glEnd();
[Type the document title]

glColor3f(0.0,0.0,1.0); // draw blue colored clipped line

glBegin(GL_LINES);

glVertex2d (vx0, vy0);

glVertex2d (vx1, vy1);

glEnd();

void display()

double x0=60,y0=20,x1=80,y1=120;

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0,0.0,0.0);

glBegin(GL_LINES);

glVertex2d (x0, y0);

glVertex2d (x1, y1);

glEnd();

glColor3f(0.0, 0.0, 1.0);

glBegin(GL_LINE_LOOP);

glVertex2f(xmin, ymin);
[Type the document title]

glVertex2f(xmax, ymin);

glVertex2f(xmax, ymax);

glVertex2f(xmin, ymax);

glEnd();

CohenSutherlandLineClipAndDraw(x0,y0,x1,y1);

glFlush();

void myinit()

glClearColor(1.0,1.0,1.0,1.0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0,499.0,0.0,499.0);

glMatrixMode(GL_MODELVIEW);

int main(int argc, char** argv)

//int x1, x2, y1, y2;

//printf("Enter End points:");


[Type the document title]

//scanf("%d%d%d%d", &x1,&x2,&y1,&y2);

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize(500,500);

glutInitWindowPosition(0,0);

glutCreateWindow("Cohen Suderland Line Clipping Algorithm");

glutDisplayFunc(display);

myinit();

glutMainLoop();

}
[Type the document title]

OUTPUT:
[Type the document title]

Program No.6
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.

#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(-10,0,0,1);

glTranslated(tx,ty,tz);

glScaled(sx,sy,sz);

glutSolidCube(1);

glLoadIdentity();

void display()

//glViewport(0,0,700,700);

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
[Type the document title]

obj(0,0,0.5,1,1,0.05); // right wall

obj(0,-0.5,0,1,0.05,1); // bottom wall

obj(-0.5,0,0,0.05,1,1); // left wall

obj(0,-0.3,0,0.02,0.2,0.02); // four table legs

obj(0,-0.3,-0.4,0.02,0.2,0.02);

obj(0.4,-0.3,0,0.02,0.2,0.02);

obj(0.4,-0.3,-0.4,0.02,0.2,0.02);

obj(0.2,-0.18,-0.2,0.6,0.02,0.6); // table top

glRotated(50,0,1,0);

glRotated(-10,1,0,0);

glRotated(-10,0,0,1);

glTranslated(0.3,-0.1,-0.3);

glutSolidTeapot(0.1);

glFlush();

glLoadIdentity();

int main(int argc, char **argv)

{
[Type the document title]

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);

float ambient[]={1,1,1,1};

float light_pos[]={27,80,2,0};

glutInitWindowSize(700,700);

glutCreateWindow("scene");

glutDisplayFunc(display);

glEnable(GL_LIGHTING);

glEnable(GL_LIGHT0);

glMaterialfv(GL_FRONT,GL_AMBIENT,ambient);

glLightfv(GL_LIGHT0,GL_POSITION,light_pos);

glEnable(GL_DEPTH_TEST);

glutMainLoop();

}
[Type the document title]
[Type the document title]

OUTPUT:

Program No.1
Implement Brenham`s line drawing algorithm for all types of slope

#include<GL/glut.h>

#include <stdlib.h>

#include<math.h>

void init(void)

//set display-window background color to white

glClearColor(1.0,1.0,1.0,0.0);

//set projection paramaters

glMatrixMode(GL_PROJECTION);

gluOrtho2D(0.0,300.0,0.0,300.0);

void setPixel(GLint xCoordinate, GLint yCoordinate)

glBegin(GL_POINTS);

glVertex2i(xCoordinate,yCoordinate);

glEnd();

glFlush(); //executes all OpenGL functions as quickl y as possible

//Bresenham line-drawing procedure for |m| < 1.0

void lineBres(GLint x0, GLint y0, GLint xEnd, GLint


[Type the document title]

yEnd)

GLint dx = fabs(xEnd - x0);

GLint dy = fabs(yEnd - y0);

GLint p = 2 * dy - dx;

GLint twoDy = 2 * dy;

GLint twoDyMinusDx = 2 * (dy-dx);

GLint x,y;

// determine which endpoint to use as start position

if (x0 > xEnd){

x = xEnd;

y = yEnd;

xEnd = x;

}else{

x = x0;

y = y0;

setPixel(x,y);

while(x<xEnd){

x++;

if(p<0)

p += twoDy;

else{
[Type the document title]

y++;

p += twoDyMinusDx;

setPixel(x,y);

void drawMyLine(void)

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0,0.0,0.0);

glPointSize(4.0);

GLint x0 = 100;

GLint y0 = 150;

GLint xEnd = 200;

GLint yEnd = 200;

lineBres(x0,y0,xEnd,yEnd);

void main(int argc, char**argv)

//initialize GLUT

glutInit(&argc,argv);

//initialize display mode

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
[Type the document title]

//set display-window width & height

glutInitWindowSize(400,400);

//set display-window upper-left position

glutInitWindowPosition(0,0);

//create display-window with a title

glutCreateWindow("Bresenham");

//initialze OpenGL

init();

//call graphics to be displayed on the window

glutDisplayFunc(drawMyLine);

//display everything and wait

glutMainLoop();

}
[Type the document title]

OUTPUT:
[Type the document title]

Program No.2
Create and rotate a triangle about origin and fixed point

#include<GL/glut.h>

#include<stdio.h>

float v[]={0,0.6,0.25,0.9,0.5,0.6};

GLubyte list[]={0,1,2};

int g=0;

void dis()

glClear(GL_COLOR_BUFFER_BIT);

glClearColor(0,0,0,0);

glColor3f(1,0,1);

//glViewport(0,0,700,700);

glDrawElements(GL_LINE_LOOP,3,GL_UNSIGNED_BYTE,list);

glRotated(g,0,0,1);

glDrawElements(GL_LINE_LOOP,3,GL_UNSIGNED_BYTE,list);

glFlush();
[Type the document title]

glLoadIdentity();

int main(int argc, char **argv)

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize(500,500);

printf("Enter Rotation Angle:");

scanf("%d",&g);

glutCreateWindow("Triangle Rotation");

glutDisplayFunc(dis);

glEnableClientState(GL_VERTEX_ARRAY);

glVertexPointer(2,GL_FLOAT,0,v);

glutMainLoop();

}
[Type the document title]

OUTPUT:
[Type the document title]

Program No.3
Draw a color cube and spin it using OpenGL transformation matrices.

#include<GL/glut.h>

float v[]={-0.5,-0.5,-0.5, -0.5,0.5,-0.5, 0.5,0.5,-0.5, 0.5,-0.5,-0.5,


-0.5,-0.5,0.5, -0.5,0.5,0.5, 0.5,0.5,0.5, 0.5,-0.5,0.5};

float c[]={0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1,};

GLubyte d[]={0,1,2,3, 2,3,7,6, 4,5,6,7, 4,5,1,0, 5,6,2,1, 0,3,7,4};

int gx=0,gy=0,gz=1.0;

void display()

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glRotated(0.2,gx,gy,gz);

glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,d);

glFlush();
[Type the document title]

void mouse(int btn,int state,int x,int y)

if(btn==GLUT_LEFT_BUTTON&&state==GLUT_DOWN) {gx=1; gy=0;


gz=0;}

if(btn==GLUT_MIDDLE_BUTTON&&state==GLUT_DOWN) {gx=0;
gy=1; gz=0;}

if(btn==GLUT_RIGHT_BUTTON&&state==GLUT_DOWN) {gx=0;
gy=0; gz=1;}

int main(int argc, char **argv)

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);

glutCreateWindow("cube");

glutDisplayFunc(display);

glutIdleFunc(display);

glutMouseFunc(mouse);

glEnableClientState(GL_VERTEX_ARRAY);

glEnableClientState(GL_COLOR_ARRAY);

glVertexPointer(3,GL_FLOAT,0,v);

glColorPointer(3,GL_FLOAT,0,c);

glEnable(GL_DEPTH_TEST);

glutMainLoop();
[Type the document title]

OUTPUT:
[Type the document title]

Program No.4
Draw a color cube and allow the user to move the camera suitably to
experiment with perspective viewing

#include <GL/glut.h>

float v[]={-0.5,-0.5,-0.5, -0.5,0.5,-0.5, 0.5,0.5,-0.5, 0.5, -0.5,-0.5,


-0.5,-0.5,0.5, -0.5,0.5,0.5, 0.5,0.5,0.5, 0.5,-0.5,0.5};

float c[]={0,0,0, 0,1,0, 1,1,0, 1,0,0, 0,0,1, 0,1,1, 1,1,1, 1,0,1,};

GLubyte list[]={0,1,2,3, 6,7,3,2, 4,7,6,5, 4,5,1,0, 5,6,2,1, 0,3,7,4};

int gx=0,gy=0,gz=1;

static GLfloat theta[] = {0.0,0.0,0.0};


[Type the document title]

static GLint axis = 2;

static GLdouble viewer[]= {0.0, 0.0, 5.0};

void display(void)

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

gluLookAt(viewer[0],viewer[1],viewer[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

glRotatef(theta[0], 1.0, 0.0, 0.0);

glRotatef(theta[1], 0.0, 1.0, 0.0);

glRotatef(theta[2], 0.0, 0.0, 1.0);

glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,list);

glFlush();

void mouse(int btn, int state, int x, int y)

{ if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;

if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1;

if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;

theta[axis] += 2.0;

if( theta[axis] > 360.0 ) theta[axis] -= 360.0;

display();

void keys(unsigned char key, int x, int y)


[Type the document title]

{ if(key == 'x') viewer[0]-= 1.0;

if(key == 'X') viewer[0]+= 1.0;

if(key == 'y') viewer[1]-= 1.0;

if(key == 'Y') viewer[1]+= 1.0;

if(key == 'z') viewer[2]-= 1.0;

if(key == 'Z') viewer[2]+= 1.0;

display();

int main(int argc, char **argv)

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);

glutInitWindowSize(700, 700);

glutCreateWindow("Colorcube Viewer");

glMatrixMode(GL_PROJECTION);

glFrustum(-2.0, 2.0, -2.0, 2.0, 2.0, 20.0);

glMatrixMode(GL_MODELVIEW);

glutDisplayFunc(display);

glEnableClientState(GL_VERTEX_ARRAY);

glEnableClientState(GL_COLOR_ARRAY);

glVertexPointer(3,GL_FLOAT,0,v);

glColorPointer(3,GL_FLOAT,0,c);
[Type the document title]

glutMouseFunc(mouse);

glutKeyboardFunc(keys);

glEnable(GL_DEPTH_TEST);

glutMainLoop();

OUTPUT:
[Type the document title]

Program No.5

Clip a lines using Cohen-Sutherland algorithm

#include <GL/glut.h>

double xmin=50,ymin=50, xmax=100,ymax=100; // Window boundaries

double xvmin=200,yvmin=200,xvmax=300,yvmax=300; // Viewport boundaries


[Type the document title]

//bit codes for the right, left, top, & bottom

const int RIGHT = 8;

const int LEFT = 2;

const int TOP = 4;

const int BOTTOM = 1;

//function to compute bit codes of a point

//Compute the bit code for a point (x, y) using the clip rectangle bounded
diagonally by (xmin, ymin), and (xmax, ymax)

int ComputeOutCode (double x, double y)

int code = 0;

if (y > ymax) //above the clip window

code |= TOP;

else if (y < ymin) //below the clip window

code |= BOTTOM;

if (x > xmax) //to the right of clip window

code |= RIGHT;

else if (x < xmin) //to the left of clip window

code |= LEFT;

return code;
[Type the document title]

/*Cohen-Sutherland clipping algorithm clips a line from P0 = (x0, y0) to P1 = (x1,


y1) against a rectangle */

void CohenSutherlandLineClipAndDraw (double x0, double y0,double x1, double


y1)

//Outcodes for P0, P1 and the point that lies outside the clip rectangle

int outcode0, outcode1, outcodeOut;

_Bool accept =0, done =0;

//compute outcodes

outcode0 = ComputeOutCode (x0, y0);

outcode1 = ComputeOutCode (x1, y1);

do{

if (!(outcode0 | outcode1)) //logical OR is 0: Trivially accept & exit

accept =1;

done =1;

else if (outcode0 & outcode1) //logical AND is not 0: Trivially reject


and exit
[Type the document title]

done =1;

else

//calculate the line segment to clip from an outside point to an


intersection with clip edge

double x, y;

//At least one endpoint is outside the clip rectangle; pick it.

outcodeOut = outcode0? outcode0: outcode1;

//Now find the intersection point with formula: y = y0 + slope *


(x - x0), x = x0 + (1/slope)* (y - y0)

if (outcodeOut & TOP) //point is above the clip rectangle

x = x0 + (x1 - x0) * (ymax - y0)/(y1 - y0);

y = ymax;

else if (outcodeOut & BOTTOM) //point is below the clip


rectangle

x = x0 + (x1 - x0) * (ymin - y0)/(y1 - y0);

y = ymin;

}
[Type the document title]

else if (outcodeOut & RIGHT) //point is to the right of clip


rectangle

y = y0 + (y1 - y0) * (xmax - x0)/(x1 - x0);

x = xmax;

else //point is to the left of clip rectangle

y = y0 + (y1 - y0) * (xmin - x0)/(x1 - x0);

x = xmin;

//Now we move outside point to intersection point to clip

//and get ready for next pass.

if (outcodeOut == outcode0)

x0 = x;

y0 = y;

outcode0 = ComputeOutCode (x0, y0);

else

x1 = x;
[Type the document title]

y1 = y;

outcode1 = ComputeOutCode (x1, y1);

}while (!done);

if (accept)

{ // Window to viewport mappings

double sx=(xvmax-xvmin)/(xmax-xmin); // Scale parameters

double sy=(yvmax-yvmin)/(ymax-ymin);

double vx0=xvmin+(x0-xmin)*sx;

double vy0=yvmin+(y0-ymin)*sy;

double vx1=xvmin+(x1-xmin)*sx;

double vy1=yvmin+(y1-ymin)*sy;

//draw a red colored viewport

glColor3f(1.0, 0.0, 0.0);

glBegin(GL_LINE_LOOP);

glVertex2f(xvmin, yvmin);

glVertex2f(xvmax, yvmin);

glVertex2f(xvmax, yvmax);

glVertex2f(xvmin, yvmax);
[Type the document title]

glEnd();

glColor3f(0.0,0.0,1.0); // draw blue colored clipped line

glBegin(GL_LINES);

glVertex2d (vx0, vy0);

glVertex2d (vx1, vy1);

glEnd();

void display()

double x0=60,y0=20,x1=80,y1=120;

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0,0.0,0.0);

glBegin(GL_LINES);

glVertex2d (x0, y0);

glVertex2d (x1, y1);

glEnd();

glColor3f(0.0, 0.0, 1.0);


[Type the document title]

glBegin(GL_LINE_LOOP);

glVertex2f(xmin, ymin);

glVertex2f(xmax, ymin);

glVertex2f(xmax, ymax);

glVertex2f(xmin, ymax);

glEnd();

CohenSutherlandLineClipAndDraw(x0,y0,x1,y1);

glFlush();

void myinit()

glClearColor(1.0,1.0,1.0,1.0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0,499.0,0.0,499.0);

glMatrixMode(GL_MODELVIEW);

int main(int argc, char** argv)

{
[Type the document title]

//int x1, x2, y1, y2;

//printf("Enter End points:");

//scanf("%d%d%d%d", &x1,&x2,&y1,&y2);

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize(500,500);

glutInitWindowPosition(0,0);

glutCreateWindow("Cohen Suderland Line Clipping Algorithm");

glutDisplayFunc(display);

myinit();

glutMainLoop();

}
[Type the document title]

OUTPUT:
[Type the document title]

Program No.6
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.

#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(-10,0,0,1);

glTranslated(tx,ty,tz);

glScaled(sx,sy,sz);

glutSolidCube(1);

glLoadIdentity();

void display()

{
[Type the document title]

//glViewport(0,0,700,700);

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

obj(0,0,0.5,1,1,0.05); // right wall

obj(0,-0.5,0,1,0.05,1); // bottom wall

obj(-0.5,0,0,0.05,1,1); // left wall

obj(0,-0.3,0,0.02,0.2,0.02); // four table legs

obj(0,-0.3,-0.4,0.02,0.2,0.02);

obj(0.4,-0.3,0,0.02,0.2,0.02);

obj(0.4,-0.3,-0.4,0.02,0.2,0.02);

obj(0.2,-0.18,-0.2,0.6,0.02,0.6); // table top

glRotated(50,0,1,0);

glRotated(-10,1,0,0);

glRotated(-10,0,0,1);

glTranslated(0.3,-0.1,-0.3);

glutSolidTeapot(0.1);

glFlush();

glLoadIdentity();

}
[Type the document title]

int main(int argc, char **argv)

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);

float ambient[]={1,1,1,1};

float light_pos[]={27,80,2,0};

glutInitWindowSize(700,700);

glutCreateWindow("scene");

glutDisplayFunc(display);

glEnable(GL_LIGHTING);

glEnable(GL_LIGHT0);

glMaterialfv(GL_FRONT,GL_AMBIENT,ambient);

glLightfv(GL_LIGHT0,GL_POSITION,light_pos);

glEnable(GL_DEPTH_TEST);

glutMainLoop();

}
[Type the document title]
[Type the document title]

OUTPUT:

You might also like