0% found this document useful (0 votes)
16 views28 pages

Lab Manual

Uploaded by

somnathabhagata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views28 pages

Lab Manual

Uploaded by

somnathabhagata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Program 1

Develop OpenGL program to draw a line using Bresenham’s algorithm


for all types of slopes
#include <GL/glut.h>
#include <stdio.h>
int x1, y1, x2, y2;
void draw_pixel(int x, int y)
{
glColor3f(1.0,0.0,0.0);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void bresenhams_line_draw(int x1, int y1, int x2, int y2)
{
int dx = x2 - x1; // x difference
int dy = y2 - y1; // y difference
int m = dy/dx; // slope
if (m < 1)
{
int decision_parameter = 2*dy - dx;
int x = x1; // initial x
int y = y1; // initial y
if (dx < 0) // decide the first point and second point
{
x = x2; // making second point as first point
y = y2;
x2 = x1;
}
draw_pixel (x, y); // plot a point
while (x < x2) // from 1st point to 2nd point
{
if (decision_parameter >= 0)
{
x = x+1;
y = y+1;
decision_parameter = decision_parameter + 2*dy - 2*dx * (y+1 - y);
}
else
{
x = x+1;
y = y;
decision_parameter = decision_parameter + 2*dy - 2*dx * (y - y);
}
draw_pixel (x, y);
}
}
else if (m > 1)
{
int decision_parameter = 2*dx - dy;
int x = x1; // initial x
int y = y1; // initial y
if (dy < 0)
{
x = x2;
y = y2;
y2 = y1;
}
draw_pixel (x, y);
while (y < y2)
{
if (decision_parameter >= 0)
{
x = x+1;
y = y+1;
decision_parameter = decision_parameter + 2*dx - 2*dy * (x+1 - x);
}
else
{
y = y+1;
x = x;
decision_parameter = decision_parameter + 2*dx - 2*dy * (x- x);
}
draw_pixel(x, y);
}
}
else if (m == 1)
{
int x = x1;
int y = y1;
draw_pixel (x, y);
while (x < x2)
{
x = x+1;
y = y+1;
draw_pixel (x, y);
}
}
}
void init()
{
glClearColor(1,1,1,1);
gluOrtho2D(0.0, 500.0, 0.0, 500.0); // left ->0, right ->500, bottom ->0, top ->500
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
bresenhams_line_draw(x1, y1, x2, y2);
glFlush();
}
int main(int argc, char **argv)
{
printf( "Enter Start Points (x1,y1)\n");
scanf("%d %d", &x1, &y1); // 1st point from user
printf( "Enter End Points (x2,y2)\n");
scanf("%d %d", &x2, &y2); // 2nd point from user
glutInit(&argc, argv); // initialize graphics system
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); //single buffered mode with RGB colour variants
glutInitWindowSize(500, 500); // 500 by 500 window size
glutInitWindowPosition(220, 200); // where do you wanna see your window
glutCreateWindow("Bresenham's Line Drawing"); // the title of your window
init(); // initialize the canvas
glutDisplayFunc(display); // call display function
glutMainLoop(); // run forever
}
OUTPUT:
Program 2

Develop OpenGL program to create and rotate a triangle about the


origin and a fixed point

#include<GL/glut.h>
#include<stdio.h>
int x,y;
int where_to_rotate=0; // don't rotate initially
float rotate_angle=0; // initial angle
float translate_x=0,translate_y=0; // initial translation

void draw_pixel(float x1, float y1)


{
glPointSize(5);
glBegin(GL_POINTS);
glVertex2f(x1,y1); // plot a single point
glEnd();
}

void triangle(int x, int y)


{
glColor3f(1,0,0);
glBegin(GL_POLYGON); // drawing a Triangle
glVertex2f(x,y);
glVertex2f(x+400,y+300);
glVertex2f(x+300,y+0);
glEnd();
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

glColor3f(1,1,1); // mark origin point as white dot


draw_pixel(0,0); // plot origin - white colour

if (where_to_rotate == 1) //Rotate Around origin


{
translate_x = 0; // no translation for rotation around origin
translate_y = 0;
rotate_angle += 1; // the amount of rotation angle
}

if (where_to_rotate == 2) //Rotate Around Fixed Point


{
translate_x = x; // SET the translation to wherever the customer says
translate_y = y;
rotate_angle += 1; // the amount of rotation angle
glColor3f(0,0,1); // mark the customer coordinate as blue dot
draw_pixel(x,y); // plot the customer coordinate - blue colour
}

glTranslatef(translate_x, translate_y, 0); // ACTUAL translation +ve


glRotatef(rotate_angle, 0, 0, 1); // rotate
glTranslatef(-translate_x, -translate_y, 0); // ACTUAL translation -ve
triangle(translate_x,translate_y); // what to rotate? - TRIANGLE
glutPostRedisplay(); // call display function again and again
glutSwapBuffers(); // show the output
}

void init()
{
glClearColor(0,0,0,1); //setting to black
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-800, 800, -800, 800);
glMatrixMode(GL_MODELVIEW);
}

void rotateMenu (int option)


{
if(option==1)
where_to_rotate=1; // rotate around origin

if(option==2)
where_to_rotate=2; // rotate around customer's coordinates

if(option==3)
where_to_rotate=3; // stop rotation
}

int main(int argc, char **argv)


{
printf( "Enter Fixed Points (x,y) for Rotation: \n");
scanf("%d %d", &x, &y); // getting the user's coordinates to
rotate

glutInit(&argc, argv); // initialize the graphics system


glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); // SINGLE also works
glutInitWindowSize(800, 800); // 800 by 800 size..you can change it
glutInitWindowPosition(0, 0); // where do you wanna see your window
glutCreateWindow("Create and Rotate Triangle"); // title
init(); // initialize the canvas
glutDisplayFunc(display); // call display function
glutCreateMenu(rotateMenu); // menu items
glutAddMenuEntry("Rotate around ORIGIN",1);
glutAddMenuEntry("Rotate around FIXED POINT",2);
glutAddMenuEntry("Stop Rotation",3);
glutAttachMenu(GLUT_RIGHT_BUTTON);

glutMainLoop(); // run forever


}

OUTPUT:
Program 3

Develop a OpenGL program to implement to recursively subdivide a


tetrahedron to form 3D Sierpinski gasket. The number of recursive
steps is to be specified by the user.
#include<stdlib.h>
#include<stdio.h>
#include<GL/glut.h>

typedef float point[3];


point v[]= {{0, 0, 1}, {0, 1, 0}, {-1, -0.5, 0}, {1, -0.5, 0}};
int n;

void triangle(point a,point b,point c)


{
glBegin(GL_POLYGON);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glEnd();
}

void divide_triangle(point a,point b,point c,int n)


{
point v1,v2,v3;
int j;
if(n>0)
{
for(j=0; j<3; j++)
v1[j] = (a[j]+b[j])/2;

for(j=0; j<3; j++)


v2[j] = (a[j]+c[j])/2;

for(j=0; j<3; j++)


v3[j] = (c[j]+b[j])/2;

divide_triangle(a,v1,v2,n-1);
glFlush();
divide_triangle(c,v2,v3,n-1);
glFlush();
divide_triangle(b,v3,v1,n-1);
glFlush();
}
else(triangle(a,b,c));
}

void tetrahedron(int n)
{
glColor3f(1, 0, 0);
divide_triangle(v[0], v[1], v[2], n);

glColor3f(0, 1, 0);
divide_triangle(v[3], v[2], v[1], n);

glColor3f(0, 0, 1);
divide_triangle(v[0], v[3], v[1], n);

glColor3f(0, 0, 0);
divide_triangle(v[0], v[2], v[3], n);

}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
tetrahedron(n);
glFlush();
}
void myReshape(int w,int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

if(w<=h)
glOrtho(-2, 2, -2*(GLfloat)h/(GLfloat)w, 2*(GLfloat)h/(GLfloat)w, -10,
10);
else
glOrtho(-2*(GLfloat)w/(GLfloat)h, 2*(GLfloat)w/(GLfloat)h, -2, 2, -10,
10);

glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
int main(int argc,char ** argv)
{
printf("No of Recursive steps/Division: ");
scanf("%d",&n);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutCreateWindow(" 3D Sierpinski gasket");

glutReshapeFunc(myReshape);

glutDisplayFunc(display);

glEnable(GL_DEPTH_TEST);

glClearColor(1, 1, 1, 0);
glutMainLoop();

return 0;
}

OUTPUT:
Program 4

Develop a OpenGL program to Spin 3D sierpinski gasket using OpenGL


transformation matrices.

#include <GL/glut.h>
#include <math.h>

GLfloat vertices[][3] = {{0.0, 0.0, 1.0}, {0.0, 0.942809, -0.33333}, {-0.816497, -


0.471405, -0.333333}, {0.816497, -0.471405, -0.333333}};
GLfloat colors[][3] = {{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}, {1.0,
1.0, 0.0}};

void triangle(GLfloat *va, GLfloat *vb, GLfloat *vc)


{
glVertex3fv(va);
glVertex3fv(vb);
glVertex3fv(vc);
}

void tetra(GLfloat *a, GLfloat *b, GLfloat *c, GLfloat *d)


{
glColor3fv(colors[0]);
triangle(a, b, c);
glColor3fv(colors[1]);
triangle(a, c, d);
glColor3fv(colors[2]);
triangle(a, d, b);
glColor3fv(colors[3]);
triangle(b, d, c);
}

void divide_tetra(GLfloat *a, GLfloat *b, GLfloat *c, GLfloat *d, int m)
{
GLfloat mid[6][3];
int j;

if (m > 0)
{
for (j = 0; j < 3; j++)
mid[0][j] = (a[j] + b[j]) / 2;
for (j = 0; j < 3; j++)
mid[1][j] = (a[j] + c[j]) / 2;
for (j = 0; j < 3; j++)
mid[2][j] = (a[j] + d[j]) / 2;
for (j = 0; j < 3; j++)
mid[3][j] = (b[j] + c[j]) / 2;
for (j = 0; j < 3; j++)
mid[4][j] = (c[j] + d[j]) / 2;
for (j = 0; j < 3; j++)
mid[5][j] = (b[j] + d[j]) / 2;

divide_tetra(a, mid[0], mid[1], mid[2], m - 1);


divide_tetra(mid[0], b, mid[3], mid[5], m - 1);
divide_tetra(mid[1], mid[3], c, mid[4], m - 1);
divide_tetra(mid[2], mid[4], d, mid[5], m - 1);
}
else
tetra(a, b, c, d);
}

GLfloat theta = 0.0;

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(theta, 0.0, 1.0, 0.0);
divide_tetra(vertices[0], vertices[1], vertices[2], vertices[3], 4);
glFlush();
glutSwapBuffers();
}
void spinDisplay(void)
{
theta += 0.5;
if (theta > 360.0)
theta -= 360.0;
glutPostRedisplay();
}

void reshape(int w, int h)


{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-2.0, 2.0, -2.0 * (GLfloat)h / (GLfloat)w, 2.0 * (GLfloat)h /
(GLfloat)w, -10.0, 10.0);
else
glOrtho(-2.0 * (GLfloat)w / (GLfloat)h, 2.0 * (GLfloat)w / (GLfloat)h, -
2.0, 2.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
}

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


{
if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
glutIdleFunc(spinDisplay);
if (btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
glutIdleFunc(NULL);
}

void myinit()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0, 2.0, -2.0, 2.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char **argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("3D Sierpinski Gasket");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glEnable(GL_DEPTH_TEST);
myinit();
glutMainLoop();
}

OUTPUT:
Program 5
Develop a OpenGL program to Clip 2D lines using Cohen-Sutherland
algorithm.
#include <stdio.h>
#include <GL/glut.h>

double xmin = 50, ymin = 50, xmax = 100, ymax = 100; //window
coordinates
double xvmin = 200, yvmin = 200, xvmax = 300, yvmax = 300; //viewport coordinates
const int LEFT = 1; // code words for LEFT, RIGHT, BOTTOM &TOP.
const int RIGHT = 2;
const int BOTTOM = 4;
const int TOP = 8;

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;
}

void CohenSutherland(double x0, double y0,double x1, double y1)


{
int outcode0, outcode1, outcodeOut;
bool accept = false, done = false;
outcode0 = ComputeOutCode (x0, y0); //calculate the region of 1st point
outcode1 = ComputeOutCode (x1, y1); //calculate the region of 2nd point

do
{
if (!(outcode0 | outcode1))
{
accept = true;
done = true;
}
else if (outcode0 & outcode1)
done = true;
else
{
double x, y;
double m = (y1 - y0)/(x1 - x0);
outcodeOut = outcode0? outcode0: outcode1;

if (outcodeOut & TOP)


{
x = x0 + (1/m) * (ymax - y0);
y = ymax;
}
else if (outcodeOut & BOTTOM)
{
x = x0 + (1/m) * (ymin - y0);
y = ymin;
}
else if (outcodeOut & RIGHT)
{
y = y0 + m * (xmax - x0);
x = xmax;
}
else
{
y = y0 + m * (xmin - x0);
x = xmin;
}
/* Intersection calculations over */

if (outcodeOut == outcode0)
{
x0 = x;
y0 = y;
outcode0 = ComputeOutCode (x0, y0);
}
else
{
x1 = x;
y1 = y;
outcode1 = ComputeOutCode (x1, y1);
}
}
}
while (!done);

if (accept)
{
double sx = (xvmax - xvmin) / (xmax - xmin);
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();

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, 1, 1);//white

glBegin(GL_LINES);
glVertex2d (x0, y0);
glVertex2d (x1, y1);
glEnd();

glBegin(GL_LINE_LOOP);
glVertex2f(xmin, ymin);
glVertex2f(xmax, ymin);
glVertex2f(xmax, ymax);
glVertex2f(xmin, ymax);
glEnd();

CohenSutherland(x0, y0, x1, y1);

glFlush();
}

void myinit()
{
glClearColor(0, 0, 0, 1);//black
gluOrtho2D(0, 500, 0, 500);
}

int main(int argc, char **argv)


{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Cohen Sutherland Line Clipping Algorithm");

myinit();
glutDisplayFunc(display);

glutMainLoop();
}

OUTPUT:

Program 6
Develop a menu driven program to animate the polygon using 3D
geometric transformations.
#include <GL/glut.h>
#include <stdio.h>

GLfloat vertices[][3] = {
{-1.0, -1.0, -1.0},
{1.0, -1.0, -1.0},
{1.0, 1.0, -1.0},
{-1.0, 1.0, -1.0},
{-1.0, -1.0, 1.0},
{1.0, -1.0, 1.0},
{1.0, 1.0, 1.0},
{-1.0, 1.0, 1.0}
};

GLint menu_id;
GLint angle_x = 0, angle_y = 0, angle_z = 0;
GLint translate_x = 0, translate_y = 0, translate_z = 0;
GLfloat scale_x = 1.0, scale_y = 1.0, scale_z = 1.0;

void drawPolygon() {
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex3fv(vertices[0]);
glVertex3fv(vertices[1]);
glVertex3fv(vertices[2]);
glVertex3fv(vertices[3]);
glEnd();

glBegin(GL_POLYGON);
glColor3f(0.0, 1.0, 0.0);
glVertex3fv(vertices[4]);
glVertex3fv(vertices[5]);
glVertex3fv(vertices[6]);
glVertex3fv(vertices[7]);
glEnd();

glBegin(GL_POLYGON);
glColor3f(0.0, 0.0, 1.0);
glVertex3fv(vertices[0]);
glVertex3fv(vertices[1]);
glVertex3fv(vertices[5]);
glVertex3fv(vertices[4]);
glEnd();

glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 0.0);
glVertex3fv(vertices[2]);
glVertex3fv(vertices[3]);
glVertex3fv(vertices[7]);
glVertex3fv(vertices[6]);
glEnd();

glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 1.0);
glVertex3fv(vertices[1]);
glVertex3fv(vertices[5]);
glVertex3fv(vertices[6]);
glVertex3fv(vertices[2]);
glEnd();

glBegin(GL_POLYGON);
glColor3f(0.0, 1.0, 1.0);
glVertex3fv(vertices[0]);
glVertex3fv(vertices[4]);
glVertex3fv(vertices[7]);
glVertex3fv(vertices[3]);
glEnd();
}

void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(translate_x, translate_y, translate_z);
glRotatef(angle_x, 1.0, 0.0, 0.0);
glRotatef(angle_y, 0.0, 1.0, 0.0);
glRotatef(angle_z, 0.0, 0.0, 1.0);
glScalef(scale_x, scale_y, scale_z);

drawPolygon();

glutSwapBuffers();
}

void menu(int value) {


switch(value) {
case 1:
angle_x += 5;
break;
case 2:
angle_y += 5;
break;
case 3:
angle_z += 5;
break;
case 4:
translate_x += 1;
break;
case 5:
translate_y += 1;
break;
case 6:
translate_z += 1;
break;
case 7:
scale_x += 0.1;
scale_y += 0.1;
scale_z += 0.1;
break;
case 8:
scale_x -= 0.1;
scale_y -= 0.1;
scale_z -= 0.1;
break;
case 9:
angle_x = angle_y = angle_z = 0;
translate_x = translate_y = translate_z = 0;
scale_x = scale_y = scale_z = 1.0;
break;
case 10:
exit(0);
}
glutPostRedisplay();
}

int main(int argc, char **argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("3D Transformations");

glutCreateMenu(menu);
glutAddMenuEntry("Rotate X", 1);
glutAddMenuEntry("Rotate Y", 2);
glutAddMenuEntry("Rotate Z", 3);
glutAddMenuEntry("Translate X", 4);
glutAddMenuEntry("Translate Y", 5);
glutAddMenuEntry("Translate Z", 6);
glutAddMenuEntry("Scale Up", 7);
glutAddMenuEntry("Scale Down", 8);
glutAddMenuEntry("Reset", 9);
glutAddMenuEntry("Exit", 10);
glutAttachMenu(GLUT_RIGHT_BUTTON);

glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutMainLoop();

return 0;
}
OUTPUT:
Program 7
Develop a OpenGL program to draw a color cube and allow the user to
move the camera suitably to experiment with perspective viewing.

#include <stdlib.h>
#include <GL/glut.h>

GLfloat vertices[][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}
};

GLfloat colors[][3] = { {1,0,0},


{1,1,0},
{0,1,0},
{0,0,1},
{1,0,1},
{1,1,1},
{0,1,1},
{0.5,0.5,0.5}
};

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


GLint axis = 2;
GLdouble viewer[]= {0.0, 0.0, 5.0}; // initial viewer location //

void polygon(int a, int b, int c, int d)


{
glBegin(GL_POLYGON);
glColor3fv(colors[a]);
glVertex3fv(vertices[a]);
glColor3fv(colors[b]);
glVertex3fv(vertices[b]);
glColor3fv(colors[c]);
glVertex3fv(vertices[c]);
glColor3fv(colors[d]);
glVertex3fv(vertices[d]);
glEnd();
}

void colorcube(void)
{
polygon(0,3,2,1);
polygon(0,4,7,3);
polygon(5,4,0,1);
polygon(2,3,7,6);
polygon(1,2,6,5);
polygon(4,5,6,7);
}
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);
colorcube();
glFlush();
glutSwapBuffers();
}

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;
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();
}

void myReshape(int w, int h)


{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glFrustum(-2.0, 2.0, -2.0 * (GLfloat) h/ (GLfloat) w, 2.0* (GLfloat) h /
(GLfloat) w,2.0, 20.0);
else
glFrustum(-2.0, 2.0, -2.0 * (GLfloat) w/ (GLfloat) h, 2.0* (GLfloat) w /
(GLfloat) h, 2.0, 20.0);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char **argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("Colorcube Viewer");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutKeyboardFunc(keys);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}

OUTPUT:

Program 8
Develop a OpenGL program 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.
#include<GL/glut.h>
void teapot(GLfloat x,GLfloat y,GLfloat z)
{
glPushMatrix();
glTranslatef(x, y, z);
glutSolidTeapot(0.1);
glPopMatrix();
}
void tableTop(GLfloat x, GLfloat y, GLfloat z)
{
glPushMatrix();
glTranslatef(x, y, z);
glScalef(0.6, 0.02, 0.5);
glutSolidCube(1);
glPopMatrix();
}
void tableLeg(GLfloat x, GLfloat y, GLfloat z)
{
glPushMatrix();
glTranslatef(x, y, z);
glScalef(0.02, 0.3, 0.02);
glutSolidCube(1);
glPopMatrix();
}
void wall(GLfloat x, GLfloat y, GLfloat z)
{
glPushMatrix();
glTranslatef(x, y, z);
glScalef(1, 1, 0.02);
glutSolidCube(1);
glPopMatrix();
}
void light()
{
GLfloat mat_ambient[] = {1, 1, 1, 1};
GLfloat mat_diffuse[] = {0.5, 0.5, 0.5, 1};
GLfloat mat_specular[] = {1, 1, 1, 1};
GLfloat mat_shininess[] = {50.0f};

glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);


glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

GLfloat light_position[] = {2, 6, 3, 1};


GLfloat light_intensity[] = {0.7, 0.7, 0.7, 1};
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_intensity);
}
void display()
{
GLfloat teapotP = -0.07, tabletopP = -0.15, tablelegP = 0.2, wallP = 0.5;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

gluLookAt(-2, 2, 5, 0, 0, 0, 0, 1, 0);

light(); //Adding light source to your project


teapot(0, teapotP, 0); //Create teapot

tableTop(0, tabletopP, 0); //Create table’s top

tableLeg(tablelegP, -0.3, tablelegP); //Create 1st leg


tableLeg(-tablelegP, -0.3, tablelegP); //Create 2nd leg
tableLeg(-tablelegP, -0.3, -tablelegP); //Create 3rd leg
tableLeg(tablelegP, -0.3, -tablelegP); //Create 4th leg

wall(0, 0, -wallP); //Create 1st wall


glRotatef(90, 1, 0, 0);

wall(0, 0, wallP); //Create 2nd wall


glRotatef(90, 0, 1, 0);

wall(0, 0, wallP); //Create 3rd wall


glFlush();
}
void myinit()
{
glClearColor(0, 0, 0, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1, 10);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Teapot on a table");

myinit();

glutDisplayFunc(display);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

glShadeModel(GL_SMOOTH);

glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);

glutMainLoop();
}

OUTPUT:
Program 9
Develop a OpenGL program to draw a simple scene containing few 3D
objects and provide day and night effect. Define suitably the position
and properties of the light source used in the scene.

#include <GL/glut.h>
#include <stdio.h>

GLfloat angleCube = 0.0f; // Rotation angle for the cube


GLfloat angleSphere = 0.0f; // Rotation angle for the sphere

GLfloat lightPosition[] = {0.0f, 100.0f, 0.0f, 1.0f}; // Light position


GLfloat lightAmbient[] = {0.0f, 0.0f, 0.0f, 1.0f}; // Ambient light color
GLfloat lightDiffuse[] = {1.0f, 1.0f, 1.0f, 1.0f}; // Diffuse light color
GLfloat lightSpecular[] = {1.0f, 1.0f, 1.0f, 1.0f}; // Specular light color

void initGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black
glEnable(GL_DEPTH_TEST); // Enable depth testing for z-buffer
glEnable(GL_LIGHTING); // Enable lighting
glEnable(GL_LIGHT0); // Enable light source 0
glEnable(GL_COLOR_MATERIAL); // Enable color material
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); // Set light position
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient); // Set ambient light color
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse); // Set diffuse light color
glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular); // Set specular light color
}

void reshape(int width, int height) {


glViewport(0, 0, width, height); // Set viewport to window dimensions
glMatrixMode(GL_PROJECTION); // Switch to projection matrix mode
glLoadIdentity(); // Reset projection matrix
gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f); // Set
perspective projection
glMatrixMode(GL_MODELVIEW); // Switch back to modelview matrix mode
}

void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth
buffers
glLoadIdentity(); // Reset the current matrix

glTranslatef(-1.5f, 0.0f, -6.0f); // Move left and into the screen

glPushMatrix(); // Save the current matrix


glRotatef(angleCube, 1.0f, 1.0f, 1.0f); // Rotate the cube
glColor3f(1.0f, 0.0f, 0.0f); // Set the color to red
glutSolidCube(2.0f); // Draw the cube
glPopMatrix(); // Restore the saved matrix

glTranslatef(3.0f, 0.0f, 0.0f); // Move right

glPushMatrix(); // Save the current matrix


glRotatef(angleSphere, 1.0f, 1.0f, 1.0f); // Rotate the sphere
glColor3f(0.0f, 0.0f, 1.0f); // Set the color to blue
glutSolidSphere(1.0f, 20, 20); // Draw the sphere
glPopMatrix(); // Restore the saved matrix

glutSwapBuffers(); // Swap the front and back buffers to display the rendered
image
}

void update(int value) {


angleCube += 2.0f; // Increment the cube rotation angle
if (angleCube > 360) {
angleCube -= 360;
}

angleSphere += 1.0f; // Increment the sphere rotation angle


if (angleSphere > 360) {
angleSphere -= 360;
}

glutPostRedisplay(); // Mark the current window as needing to be redisplayed


glutTimerFunc(16, update, 0); // Call the update function again after 16
milliseconds
}

int main(int argc, char** argv) {


glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); // Set display mode
glutInitWindowSize(800, 600); // Set window size
glutCreateWindow("OpenGL Day/Night Scene"); // Create window with the given
title
glutDisplayFunc(display); // Register display callback function
glutReshapeFunc(reshape); // Register reshape callback function
glutTimerFunc(16, update, 0); // Call update function after 16 milliseconds
initGL(); // Initialize OpenGL
glutMainLoop(); // Enter the GLUT event processing loop
return 0;
}

OUTPUT:

You might also like