1.
Using OpenGL functions, Write a program to create 2D sierpinski gasket using recursive
Subdivision method.
Program:
#include<GL/glut.h>
#include<stdio.h>
#include<stdlib.h>
typedef float point[3];
GLfloat v[3][2] = {{0.0,0.0},{500.0,0.0},{250.0,500.0}};
int n;
void triangle(point a, point b, point c)
{
glBegin(GL_POLYGON);
glVertex2fv(a);
glVertex2fv(b);
glVertex2fv(c);
glEnd();
}
void divide_tri(GLfloat *a, GLfloat *b, GLfloat *c, int m)
{
GLfloat v0[2],v1[2],v2[2];
int j;
if (m>0)
{
for(j=0;j<2;j++)
v0[j]=(a[j]+b[j])/2.0;
for(j=0;j<2;j++)
v1[j]=(a[j]+c[j])/2.0;
for(j=0;j<2;j++)
v2[j]=(b[j]+c[j])/2.0;
divide_tri(a,v0,v1,m-1);
divide_tri(c,v1,v2,m-1);
divide_tri(b,v2,v0,m-1);
}
else
triangle(a,b,c);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
divide_tri(v[0],v[1],v[2],n);
glFlush();
}
void myinit()
{
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,500.0,0.0,500.0);
glMatrixMode(GL_MODELVIEW);
}
void main(int argc,char **argv)
{
printf("Number of subdivisions\n");
scanf("%d",&n);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(0.0,0.0);
glutInitWindowSize(500.0,500.0);
glutCreateWindow("2D Gasket");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}
2. Program to recursively subdivide a tetrahedron to form 3D Sierpinski gasket. The
number of recursive steps is to be specified by the user.
Program:
#include <GL/glut.h>
#include <stdlib.h>
#include<stdio.h>
typedef float point[3];
point v[]={ {0.0,0.0,1.0},
{0.0,0.943,-0.33},
{-0.816,-0.471,-0.33},
{0.816,-0.471,0.33}};
int n;
void triangle(point a,point b,point c)
{
glBegin(GL_POLYGON);
glNormal3fv(a);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glEnd();
}
void divide_tri(point a,point b,point c,int m)
{
point v1,v2,v3;
int j;
if (m>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]=(b[j]+c[j])/2;
divide_tri(a,v1,v2,m-1);
divide_tri(c,v2,v3,m-1);
divide_tri(b,v3,v1,m-1);
}
else
triangle(a,b,c);
}
void tetrahedron(int m)
{
glColor3f(1.0,0.0,0.0);
divide_tri(v[0],v[1],v[2],m);
glColor3f(0.0,1.0,0.0);
divide_tri(v[3],v[2],v[1],m);
glColor3f(0.0,0.0,1.0);
divide_tri(v[0],v[3],v[1],m);
glColor3f(0.0,0.0,0.0);
divide_tri(v[0],v[2],v[3],m);
}
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.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);
glutPostRedisplay();
}
int main(int argc,char **argv)
{
printf(“Enter the number of recursive steps you want\n”);
scanf(“%d”, &n);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("3d gasket");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0,1.0,1.0,1.0);
glutMainLoop();
}
3. Write a C program using OpenGL functions to do the following
i. Create a rectangle when mouse right button is clicked
ii. To exit from the program using keyboard
Program:
#include<GL/glut.h>
#include<stdio.h>
void display(void)
{
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
}
void rect(){
glBegin(GL_QUADS);
glColor3f(1.0,1.0,1.0);
glVertex2f(-0.5,0.5);
glVertex2f(0.5,0.5);
glVertex2f(0.5,-0.5);
glVertex2f(-0.5,-0.5);
glEnd();
glFlush();
}
void myMouse(int btn,int state,int x,int y)
{
if(btn==GLUT_RIGHT_BUTTON &&state == GLUT_DOWN)
rect();
}
void myKey(unsigned char key,int x,int y)
{
if(key == 'q'||key =='Q')
exit(0);
}
void main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Simple Program");
glutMouseFunc(myMouse);
glutKeyboardFunc(myKey);
glutDisplayFunc(display);
glutMainLoop();
}
4. Write a C program using OpenGL functions to create the given object using display list
Program:
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#define BOX 1
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glNewList(BOX, GL_COMPILE_AND_EXECUTE);
glBegin(GL_LINE_LOOP);
glVertex2f(50, 50);
glVertex2f(50, 100);
glVertex2f(100, 100);
glVertex2f(100, 50);
glEnd();
glEndList();
glTranslatef(50, 0, 0);
glCallList(BOX);
glTranslatef(50, 0, 0);
glCallList(BOX);
glTranslatef(50, 0, 0);
glCallList(BOX);
glFlush();
}
void myinit()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(1.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 300.0, 0.0, 300.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(300, 300);
glutInitWindowPosition(0, 0);
glutCreateWindow("Display List");
glutDisplayFunc(display);
myinit();
glutMainLoop();
return 0;
}
5. Write a program using OpenGL functions to generate characters using display list
Program:
#include<GL/glut.h>
#include<stdio.h>
void OurFont ( char c)
{
Switch(c)
{
case ‘a’:
……..
Break ( );
case ‘A’:
.....
Break ( );
case ‘b’:
…….
Break ( );
case ‘B’:
……
Break ( );
.
.
.
case ‘o’:
glTranslatef(0.5, 0.5, 0.0) // move to center
glBegin(GL-QUAD_STRIP);
for (i=0; i<=12; i++) //12 vertices
{
Angle=3.14159/6.0 *I ; //30degrees in radians
glVertex2f(0.4*cos(angle)+0.5, 0.4*cos(angle)+0.5);
glVertex2f(0.5*cos(angle)+0.5, 0.5*cos(angle)+0.5);
}
glEnd();
break;
.
}
}
Display()
{
Base = glGenLists(256); //returns index of first of 256 consecutive available ids
For (i=0; i=256; i++)
{
GLNewList(base +I, GL_COMPILE)
OurFont (i);
glEndList();
}
void myinit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity()
gluOrtho2(50.0, 50.0, 50.0, 50.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutCreateWindow("square");
init();
glutDisplayFuncDisplay);
glutMainLoop();
}
6. Write program using OpenGL functions to create outline of human face using display list
#include<GL/glut.h>
#include<stdio.h>
#define EYE 1
# define FACE 2
Display ( )
{
glNewList(EYE)
/* eye code*/
glEndList();
glNewList (FACE)
/* draw the outline*/
glTranslatef(….); /* right eye position */
glCallList (EYE);
glTranslatef(….); /* Left eye position */
glCallList (EYE);
glTranslatef(….); /* Left ear position */
glCallList (EYE);
glTranslatef(….); /* right ear position */
glCallList (EYE);
glTranslatef(….); /* nose position */
glCallList (EYE);
glEndList( );
}
void myinit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity()
gluOrtho2(50.0, 50.0, 50.0, 50.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutCreateWindow("square");
init();
glutDisplayFunc(myDisplay);
glutMainLoop();
}
7. Program to create square using location from mouse (return value from mouse function)
Program:
#include <GL/glut.h>
#include <stdlib.h>
#include<stdio.h>
GLSizei wh=500, ww=500; //initial window width and height
Glfloat size = 3.0;
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
}
void drawsquare(int x, int y)
{
y=wh-y;
glBegin(GL_POLYGON);
glVertex2f(x+size, y+size);
glVertex2f(x-size, y+size);
glVertex2f(x-size, y-size);
glVertex2f(x+size, y-size);
glEnd();
glFlush();
}
Void mymouse(int btb, int state, int x, int y)
{
if(btn==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
drawsquare(x,y);
if(btn==GLUT_RIGHT_BUTTON&&state==GLUT_DOWN)
exit();
void myReshape (GLsizei w, GLsizei h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
glMatrixMode(GL_MODELVIEW);
glViewport(0,0,w,h);
ww=w;
wh=h;
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutCreateWindow("square");
glutReshapeFunc(myReshape);
glutDisplayFunc(myDisplay);
glutMouseFunc(myMouse);
glClearColor(1.0,1.0,1.0,1.0);
glutMainLoop();
}
8. Write a C program with OpenGL API using menus to create the following
Program:
#include<GL/glut.h>
#include<stdio.h>
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
}
void line ()
{
void point ()
{
void main_menu( id)
{
Switch(id);
{
Case 1: quit;
Break( );
Case 2: line();
Break;
Case 3: point();
Break;
}
}
void myinit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity()
gluOrtho2(50.0, 50.0, 50.0, 50.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutCreateWindow("square");
glutDisplayFunc(myDisplay);
myinit();
glutCreateMenu(main_menu);
glutAddMenuEntry(“Quit”,1);
glutAddMenuEntry(“line”,2);
glutAddMenuEntry(“point”,3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
}
9. Write a C program with OpenGL API using menus to create the following
#include<GL/glut.h>
#include<stdio.h>
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
}
void line ()
{
void point ()
{
}
void main_menu( id)
{
Switch(id);
{
Case 1: quit;
Break( );
//Case 2: primitive();
// break
}
}
void sub_menu( id)
{
Switch(id);
{
Case 1: line();
Break( );
Case 2: point();
break
}
}
void myinit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity()
gluOrtho2(50.0, 50.0, 50.0, 50.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutCreateWindow("square");
glutDisplayFunc(myDisplay);
myinit();
sub_menu=glutCreateMenu(submenu);
glutAddMenuEntry(“line”,2);
glutAddMenuEntry(“point”,3);
glutCreateMenu(main_menu);
glutAddMenuEntry(“Quit”,1);
glutAddSubMenu( “primitive”, Sub_menu);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
}
10. Write a C program using OpenGL functions to create a rotating square.
#include<GL/glut.h>
#include<stdio.h>
#include<math.h>
GLfloat theta, thetar;
void display(){
GLfloat thetar=theta/(3.142/180);
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0,0.0,0.0,1.0);
glBegin(GL_POLYGON);
glColor3f(1.0,0.4,0.3);
glVertex2f(-cos(thetar),-sin(thetar));
glVertex2f(sin(thetar),-cos(thetar));
glVertex2f(cos(thetar),sin(thetar));
glVertex2f(-sin(thetar),cos(thetar));
glEnd();
glFlush();
}
void idle(){
theta += 0.0000002;
if((theta)>= 360.0)
theta -=0.000002;
glutPostRedisplay();
}
void myMouse(int button,int state,int x,int y){
if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
glutIdleFunc(idle);
if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
glutIdleFunc(NULL);
}
void mykey(unsigned char key,int x, int y){
if (key=='q'|| key=='Q')
exit(0);
}
void myinit()
{
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,500.0,0.0,500.0);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
}
void main(int argc,char**argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Rotating Square");
glutMouseFunc(myMouse);
glutKeyboardFunc(mykey);
glutDisplayFunc(display);
idle();
myinit();
glutMainLoop();
}