All CG Programs
All CG Programs
#include<GL/glut.h>
#include<stdio.h>
glColor3f(1.0,0.0,0.0);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
float dx = x2 - x1;
float dy = y2 - y1;
float m = dy/dx;
if(m < 1)
x = x2;
y = y2;
x2 = x1;
if(decision_parameter >= 0)
x = x+1;
y = y+1;
else
x = x+1;
y = y;
draw_pixel(x, y);
}
else if(m > 1)
if(dy < 0)
x = x2;
y = y2;
y2 = y1;
draw_pixel(x, y);
if(decision_parameter >= 0)
x = x+1;
y = y+1;
else
y = y+1;
x = x;
draw_pixel(x, y);
else if (m == 1)
int x = x1;
int y = y1;
draw_pixel(x, y);
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);
glFlush();
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
PROGRAM 2:-
#include<GL/glut.h>
#include<stdio.h>
int x,y;
glPointSize(5);
glBegin(GL_POINTS);
glEnd();
glColor3f(1,0,0);
glVertex2f(x,y);
glVertex2f(x+400,y+300);
glVertex2f(x+300,y+0);
glEnd();
void display()
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
translate_y = 0;
translate_y = y;
void init()
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
{
if(option==1)
if(option==2)
if(option==3)
glutAddMenuEntry("Stop Rotation",3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
PROGRAM 3:-
#include<stdlib.h>
#include<GL/glut.h>
1, -1, -1,
1, 1, -1,
-1, 1, -1,
-1, -1, 1,
1, -1, 1,
1, 1, 1,
-1, 1, 1
};
GLfloat colors[]= {0, 0, 0,
1, 0, 0,
1, 1, 0,
0, 1, 0,
0, 0, 1,
1, 0, 1,
1, 1, 1,
0, 1, 1
};
2, 3, 7, 6,
0, 4, 7, 3,
1, 2, 6, 5,
4, 5, 6, 7,
0, 1, 5, 4
};
void display(void)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(theta[0], 1, 0, 0);
glRotatef(theta[1], 0, 1, 0);
glRotatef(theta[2], 0, 0, 1);
glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,cubeIndices);
glutSwapBuffers();
void spinCube()
theta[axis] += 2;
theta[axis] -= 360;
glutPostRedisplay();
axis=0;
if(btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN)
axis=1;
axis=2;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
else
glMatrixMode(GL_MODELVIEW);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutDisplayFunc(display);
glutIdleFunc(spinCube);
glutMouseFunc(mouse);
glEnable(GL_DEPTH_TEST);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glColor3f(1, 1, 1);
glutMainLoop();
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::PROGRAM 4:-
#include <stdlib.h>
#include <GL/glut.h>
{1,-1,-1},
{1,1,-1},
{-1,1,-1},
{-1,-1,1},
{1,-1,1},
{1,1,1},
{-1,1,1}
};
{1,1,0},
{0,1,0},
{0,0,1},
{1,0,1},
{1,1,1},
{0,1,1},
{0.5,0.5,0.5}
};
GLint axis = 2;
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);
colorcube();
glFlush();
glutSwapBuffers();
theta[axis] += 2.0;
display();
display();
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);
glutInit(&argc, argv);
glutInitWindowSize(500, 500);
glutCreateWindow("Colorcube Viewer");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutKeyboardFunc(keys);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
PROGRAM 5:-
#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.
int code = 0;
code |= BOTTOM;
code |= RIGHT;
code |= LEFT;
return code;
do
if (!(outcode0 | outcode1))
accept = true;
done = true;
else
double x, y;
y = ymax;
y = ymin;
y = y0 + m * (xmax - x0);
x = xmax;
else
y = y0 + m * (xmin - x0);
x = xmin;
if (outcodeOut == outcode0)
x0 = x;
y0 = y;
else
x1 = x;
y1 = y;
while (!done);
if (accept)
glBegin(GL_LINE_LOOP);
glVertex2f(xvmin, yvmin);
glVertex2f(xvmax, yvmin);
glVertex2f(xvmax, yvmax);
glVertex2f(xvmin, yvmax);
glEnd();
glBegin(GL_LINES);
glEnd();
void display()
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 1, 1);//white
glBegin(GL_LINES);
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
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
myinit();
glutDisplayFunc(display);
glutMainLoop();
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
PROGRAM 6:-
#include<GL/glut.h>
glPushMatrix();
glTranslatef(x, y, z);
glutSolidTeapot(0.1);
glPopMatrix();
glPushMatrix();
glTranslatef(x, y, z);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(x, y, z);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(x, y, z);
glScalef(1, 1, 0.02);
glutSolidCube(1);
glPopMatrix();
void light()
{
GLfloat mat_ambient[] = {1, 1, 1, 1};
void display()
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(-2, 2, 5, 0, 0, 0, 0, 1, 0);
glRotatef(90, 1, 0, 0);
glRotatef(90, 0, 1, 0);
glFlush();
void myinit()
glClearColor(0, 0, 0, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
}
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();
}
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
PROGRAM 7:-
#include<stdlib.h>
#include<stdio.h>
#include<GL/glut.h>
point v[]= {{0, 0, 1}, {0, 1, 0}, {-1, -0.5, 0}, {1, -0.5, 0}};
int n;
glBegin(GL_POLYGON);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glEnd();
point v1,v2,v3;
int j;
if(n>0)
{
v1[j] = (a[j]+b[j])/2;
v2[j] = (a[j]+c[j])/2;
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);
glColor3f(0, 0, 1);
glColor3f(0, 0, 0);
void display(void)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
tetrahedron(n);
glFlush();
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
else
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
scanf("%d",&n);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glClearColor(1, 1, 1, 0);
glutMainLoop();
return 0;
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
PROGRAM 8:-
//8. Develop a menu driven program to animate a flag using Bezier curve algorithm.
#include<GL/glut.h>
#include<stdio.h>
#include<math.h>
#define PI 3.1416
float theta = 0;
struct point
GLfloat x, y, z;
};
int factorial(int n)
if (n<=1)
return(1);
else
n=n*factorial(n-1);
return n;
int r;
for(r=0; r<=n; r++) //start from nC0, then nC1, nC2, nC3 till nCn
int i, n = number_of_control_points-1;
float bernstein_polynomial;
actual_bezier_point -> x = 0;
actual_bezier_point -> y = 0;
actual_bezier_point -> z = 0;
point actual_bezier_point;
float t;
int *hold_ncr_values, i;
glBegin(GL_LINE_STRIP);
t=float(i)/float(number_of_bezier_points);
glVertex2f(actual_bezier_point.x, actual_bezier_point.y);
}
glEnd();
delete[] hold_ncr_values;
void display()
glClear(GL_COLOR_BUFFER_BIT);
point control_points_array[4] = {{100, 400, 0}, {150, 450, 0}, {250, 350, 0},{300, 400, 0}};
theta += 2;
glPushMatrix();
glPointSize(5);
glTranslatef(0, -0.8, 0 );
glPopMatrix();
glLineWidth(5);
glBegin(GL_LINES);
glVertex2f(100,400);
glVertex2f(100,40);
glEnd();
glutPostRedisplay();
glutSwapBuffers();
void init()
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,500,0,500);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(0, 0);
glutInitWindowSize(500,500);
init();
glutDisplayFunc(display);
glutMainLoop();
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
PROGRAM 9:-
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
float x1, x2, x3, x4, y1, y2, y3, y4; // our polygon has 4 lines - so 8 coordinates
void edgedetect(float x1, float y1, float x2, float y2, int *left_edge, int *right_edge)
int i;
if ((y2-y1)<0) // decide where to start
temp = y1;
y1 = y2;
y2 = temp;
temp = x1;
x1 = x2;
x2 = temp;
if ((y2-y1)!=0)
else
x_slope = x2 - x1;
x = x1;
if (x < left_edge[i])
left_edge[i] = x;
if (x > right_edge[i])
right_edge[i] = x;
x = x + x_slope;
glBegin (GL_POINTS);
glEnd ();
void scanfill (float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4)
int i, y;
right_edge [i] = 0;
}
edgedetect (x1, y1, x2, y2, left_edge, right_edge);
glFlush ();
void display()
x1 = 200, y1 = 200;
x2 = 100, y2 = 300;
x3 = 200, y3 = 400;
x4 = 300, y4 = 300;
glClear (GL_COLOR_BUFFER_BIT);
glBegin (GL_LINE_LOOP);
glEnd ();
void init()
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE|GLUT_RGB);
init ();
glutDisplayFunc (display);
glutMainLoop ();