Lab Manual
Lab Manual
#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 display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
void init()
{
glClearColor(0,0,0,1); //setting to black
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-800, 800, -800, 800);
glMatrixMode(GL_MODELVIEW);
}
if(option==2)
where_to_rotate=2; // rotate around customer's coordinates
if(option==3)
where_to_rotate=3; // stop rotation
}
OUTPUT:
Program 3
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
#include <GL/glut.h>
#include <math.h>
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;
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 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);
}
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;
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 == 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();
glFlush();
}
void myinit()
{
glClearColor(0, 0, 0, 1);//black
gluOrtho2D(0, 500, 0, 500);
}
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();
}
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>
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();
}
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};
gluLookAt(-2, 2, 5, 0, 0, 0, 0, 1, 0);
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>
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 display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth
buffers
glLoadIdentity(); // Reset the current matrix
glutSwapBuffers(); // Swap the front and back buffers to display the rendered
image
}
OUTPUT: