Program No
Program No
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)
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,300.0,0.0,300.0);
glBegin(GL_POINTS);
glVertex2i(xCoordinate,yCoordinate);
glEnd();
yEnd)
[Type the document title]
GLint p = 2 * dy - dx;
GLint x,y;
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;
lineBres(x0,y0,xEnd,yEnd);
//initialize GLUT
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400,400);
glutInitWindowPosition(0,0);
glutCreateWindow("Bresenham");
//initialze OpenGL
init();
glutDisplayFunc(drawMyLine);
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]
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
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>
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]
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;}
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>
int gx=0,gy=0,gz=1;
void display(void)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,list);
glFlush();
theta[axis] += 2.0;
display();
display();
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(700, 700);
glutCreateWindow("Colorcube Viewer");
glMatrixMode(GL_PROJECTION);
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
#include <GL/glut.h>
//Compute the bit code for a point (x, y) using the clip rectangle bounded
diagonally by (xmin, ymin), and (xmax, ymax)
int code = 0;
code |= TOP;
code |= BOTTOM;
code |= RIGHT;
code |= LEFT;
return code;
}
[Type the document title]
//Outcodes for P0, P1 and the point that lies outside the clip rectangle
//compute outcodes
do{
accept =1;
done =1;
done =1;
[Type the document title]
else
double x, y;
//At least one endpoint is outside the clip rectangle; pick it.
y = ymax;
y = ymin;
x = xmax;
x = xmin;
if (outcodeOut == outcode0)
x0 = x;
y0 = y;
else
x1 = x;
y1 = y;
}while (!done);
if (accept)
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;
glBegin(GL_LINE_LOOP);
glVertex2f(xvmin, yvmin);
glVertex2f(xvmax, yvmin);
glVertex2f(xvmax, yvmax);
glVertex2f(xvmin, yvmax);
glEnd();
[Type the document title]
glBegin(GL_LINES);
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);
glEnd();
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);
//scanf("%d%d%d%d", &x1,&x2,&y1,&y2);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
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>
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.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);
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]
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)
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,300.0,0.0,300.0);
glBegin(GL_POINTS);
glVertex2i(xCoordinate,yCoordinate);
glEnd();
yEnd)
GLint p = 2 * dy - dx;
GLint x,y;
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;
lineBres(x0,y0,xEnd,yEnd);
//initialize GLUT
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
[Type the document title]
glutInitWindowSize(400,400);
glutInitWindowPosition(0,0);
glutCreateWindow("Bresenham");
//initialze OpenGL
init();
glutDisplayFunc(drawMyLine);
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();
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
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>
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]
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;}
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>
int gx=0,gy=0,gz=1;
void display(void)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,list);
glFlush();
theta[axis] += 2.0;
display();
display();
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(700, 700);
glutCreateWindow("Colorcube Viewer");
glMatrixMode(GL_PROJECTION);
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
#include <GL/glut.h>
//Compute the bit code for a point (x, y) using the clip rectangle bounded
diagonally by (xmin, ymin), and (xmax, ymax)
int code = 0;
code |= TOP;
code |= BOTTOM;
code |= RIGHT;
code |= LEFT;
return code;
[Type the document title]
//Outcodes for P0, P1 and the point that lies outside the clip rectangle
//compute outcodes
do{
accept =1;
done =1;
done =1;
else
double x, y;
//At least one endpoint is outside the clip rectangle; pick it.
y = ymax;
y = ymin;
}
[Type the document title]
x = xmax;
x = xmin;
if (outcodeOut == outcode0)
x0 = x;
y0 = y;
else
x1 = x;
[Type the document title]
y1 = y;
}while (!done);
if (accept)
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;
glBegin(GL_LINE_LOOP);
glVertex2f(xvmin, yvmin);
glVertex2f(xvmax, yvmin);
glVertex2f(xvmax, yvmax);
glVertex2f(xvmin, yvmax);
[Type the document title]
glEnd();
glBegin(GL_LINES);
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);
glEnd();
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);
{
[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);
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>
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.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);
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]
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: