0% found this document useful (0 votes)
54 views19 pages

Ball Bouncing

The document describes an OpenGL program that simulates filling a bucket with water. It defines functions to draw the bucket, tap, water drops, and filled water level. As the program runs over time, the water level height variable increases, simulating the bucket filling up until it reaches the top.

Uploaded by

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

Ball Bouncing

The document describes an OpenGL program that simulates filling a bucket with water. It defines functions to draw the bucket, tap, water drops, and filled water level. As the program runs over time, the water level height variable increases, simulating the bucket filling up until it reaches the top.

Uploaded by

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

BALL BOUNCING

#include<GL/glut.h>
#include "glsupport.h"
int transy=0,f1=0,ht=500,transx=0;
void obj2()
{
setcolor(BROWN);
line(0,20,1000,20);
}
void obj1()
{
setcolor(RED);
fillellipse(50,45,30,40);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
obj2();
glPushMatrix();
glTranslatef(transx,transy,0);
obj1();
glPopMatrix();
glEnd();
glFlush();
}
void time_update()
{
if(f1==0)
{
transy=transy+10;
transx=transx+1;
if(transy>ht)
f1=1;
if(transx>=400)
{
transx=400;
transy=20;
}
}
if(f1==1)
{
transy=transy-10;
transx=transx+1;
if(transy==20){
f1=0;
ht=ht-100;
}
if(transx>=400)
{
transx=400;
transy=20;
}
}
glutPostRedisplay();
glutTimerFunc(15,time_update,0);
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Bouncing Ball");
glClearColor(0.0,0.0,0.0,0.0);
glViewport(0,100,0,100);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,600,0,600);
glutDisplayFunc(display);
glutTimerFunc(25,time_update,0);
glutMainLoop();
return 0;
}

OUTPUT
CAR MOVING OVER A HUMP

#include<GL/glut.h>
#include "glsupport.h"
int
transyc=0,f1=0,ht=500,transxc=20,ang=0,transyw1=0,transyw2=0,transxw1=
0,
transxw2=0,rot_angle_car=0;
void road()
{
setcolor(15);
line(0,10,1000,10);

}
void car()
{
setcolor(BROWN);
glRecti(0,30,200,60);
setcolor(BROWN);
glRecti(50,60,120,90);
setcolor(DARKGRAY);
int front[6]={0,60,50,90,50,60};
triangle(front);
setcolor(DARKGRAY);
int back[6]={120,60,120,90,170,60};
triangle(back);
}
void hump()
{
setcolor(DARKGRAY);
pieslice(450,10,0,180,20);
}

void wheel()
{ setcolor(15);
pieslice(0,0,0,80,20);
//setcolor(LIGHTBLUE);
pieslice(0,0,90,80,20);
//setcolor(LIGHTCYAN);
pieslice(0,0,180,80,20);
//setcolor(LIGHTMAGENTA);
pieslice(0,0,270,80,20);
}
void time_update()
{

if(transxw1>=500)
{
transxw1=40;
}
else
{
transxw1=transxw1+2;
transxw2=transxw1+70;
transxc=transxw1-30;
transyw1=40;
transyw2=40;
transyc=0;
}
if(transxw1>=420 && transxw1<=490)
{
transyw1=50;
transyc=10;
}
if(transxw2>=390 && transxw2<=440)
{
transyw2=50;
transyc=10;
}
if(rot_angle_car>0)
rot_angle_car=rot_angle_car-4;
else
rot_angle_car=360;

glutPostRedisplay();
glutTimerFunc(15,time_update,0);

}
void display(void)
{

glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(transxc,transyc+10,0);
car();
glPopMatrix();
glPushMatrix();
glTranslatef(transxw1,transyw1,0);
glRotatef(rot_angle_car,0.f,0.f,1.f);
wheel();
//glTranslatef(-40,-20,0);

glPopMatrix();
glPushMatrix();
glTranslatef(transxw1+120,transyw2,0);
glRotatef(rot_angle_car,0.f,0.f,1.f);
wheel();
//glTranslatef(-40,-20,0);

glPopMatrix();
hump();
road();
glEnd();
glFlush();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("CAR");
glClearColor(0.0,0.0,0.0,0.0);
glViewport(0,0,800,800);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100,800,-100,800);
glutDisplayFunc(display);
glutTimerFunc(15,time_update,0);
glutMainLoop();
return 0;
}

OUTPUT
MAN LANDING ON A PARACHUTE

#include<GL/glut.h>
#include "glsupport.h"
int
planex=500,planey=0,flag=0,mx=0,my=0,start=0,rot_leg=0,rot_ang=0,headfla
g=0;
int fade=0;

void plane()
{

setcolor(BLUE);
glRecti(300,400,450,350);
pieslice(300,375,90,180,25);
arc(350,400,90,35,35);

setcolor(CYAN);
int top[6]= {350,400,350,435,450,400};
triangle(top);
int tale1[6]={450,400,480,435,450,380};
triangle(tale1);
int tale2[6]={450,400,480,380,450,350};
triangle(tale2);
}
void head()
{

pieslice(380,400,0,-360,10);
}
void head1()
{
glPushMatrix();
setcolor(BLACK);
pieslice(380,400,0,-360,10);
glPopMatrix();
}
void man()
{
setcolor(RED);
pieslice(250,320,0,-360,10);
line(250,320,250,255);
line(230,340,250,290);
line(270,340,250,290);

}
void leg()
{
line(247,255,220,225);
line(247,255,265,225);
}
void parachute()
{
pieslice(250,340,0,180,30);
line(230,340,250,290);
line(270,340,250,290);
}
void man1()
{
setcolor(RED);
pieslice(250,320,0,-360,10);
line(250,320,250,255);
line(250,290,220,260);

}
void fadep()
{
pieslice(200,250,290,180,30);
line(200,260,220,260);
line(200,240,220,260);
}
void display(void)
{

glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(planex,planey,0);
plane();
setcolor(RED);
head();
glPopMatrix();

if(flag==1)
{
glPushMatrix();
glTranslatef(mx,my,0);
man();

glPopMatrix();

}
if(flag==1)
{
glPushMatrix();
glTranslatef(mx+6,my,0);
leg();
glPopMatrix();
}

if(start==1)
{
glPushMatrix();
glTranslatef(mx,my,0);
setcolor(RED);
parachute();
setcolor(BLACK);
head();
setcolor(RED);
glPopMatrix();
}
if(fade==1)
{
glPushMatrix();
glTranslatef(mx,my,0);
fadep();
glPopMatrix();
glPushMatrix();
glTranslatef(mx+6,my,0);
glRotatef(rot_leg,0,0,1);
leg();
glPopMatrix();
glPushMatrix();
glTranslatef(mx,my,0);
man1();
glPopMatrix();
}

glEnd();
glFlush();
}

void time_update()
{
planex--;
if(planex<=-130 || planey>=300)
{
start=1;
flag=1;

//headflag=1;
//setcolor(BLACK);
//head();
my--;
}

if(my<=-300)
{
//headflag=1;
flag=0;
fade=1;
start=0;
my++;
mx++;
}
if(rot_ang>0)
rot_ang--;
else
rot_ang=360;
if(rot_leg>0)
rot_leg=rot_leg-0.5;
else
{
rot_leg=1;
}

glutPostRedisplay();
glutTimerFunc(10,time_update,0);
}

int main(int argc,char **argv)


{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Man Parachute");
glClearColor(0.0,0.0,0.0,0.0);
//glViewport(0,0,800,800);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100,800,-100,800);
glutDisplayFunc(display);
glutTimerFunc(10,time_update,0);
glutMainLoop();
return 0;
}
OUTPUT
BUCKET FILLING WITH WATER

#include<GL/glut.h>
#include "glsupport.h"
int i=168,flag=0;
float ht=-30;
void bucket()
{
setcolor(YELLOW);
//line(100,10,150,10);
//line(90,100,160,100);
//line(100,10,90,100);
//line(150,10,160,100);
glRecti(60,-30,160,80);

}
void tap()
{
setcolor(GREEN);
line(-100,190,50,190);
line(-100,220,50,220);
line(50,190,50,220);
line(50,198,95,198);
line(50,212,95,212);
line(95,188,95,222);
line(95,188,125,188);
line(95,222,125,222);
line(125,188,125,222);
line(105,188,105,168);
line(115,188,115,168);
line(105,168,115,168);

}
void drop()
{
setcolor(CYAN);
for(float i=108;i<114;i=i+0.3)
{
long x=glrandom(147L);
line(i,x+20,i,x+25);
}
}
void fill()
{
setcolor(CYAN);
glRecti(60,-30,160,ht);

}
void time_update()
{
if(ht>=-30 &&ht<80)
{
ht=ht+0.5;
}
if(ht==79)
{
flag=1;
}
glutPostRedisplay();
glutTimerFunc(10,time_update,0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
bucket();
glPopMatrix();
glPushMatrix();
tap();
glPopMatrix();
if(flag==0)
{
glPushMatrix();
drop();
glPopMatrix();
}

glPushMatrix();
fill();
glPopMatrix();
glEnd();
glFlush();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Bucket Filling");
glClearColor(0,0,0,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100,800,-100,800);
glutDisplayFunc(display);
glutTimerFunc(10,time_update,0);
glutMainLoop();
return 0;
}

OUTPUT
MAN HOLDING UMBRELLA ON RAINING

#include<GL/glut.h>
#include "glsupport.h"
int planex=-100,planey=0,flag=0,mx=-100,my=-
100,start=0,rot_leg=0,rot_ang=0,headflag=0,fade=0,flag1=0,flag2=0;
void rain()
{
setcolor(CYAN);
for(float i=-100;i<1500;i=i+0.3)
{
long x=glrandom(800L);
line(i,x+20,i,x+25);
}
}
void man()
{
setcolor(RED);
pieslice(250,320,0,-360,10);
line(250,320,250,255);
line(250,290,220,260);
line(300,310,250,290);

}
void man1()
{
setcolor(RED);
pieslice(250,320,0,-360,10);
line(250,320,250,255);
line(250,290,220,260);
setcolor(BROWN);
pieslice(223,270,210,50,60);

}
void umbrella()
{
setcolor(BROWN);
line(300,300,300,350);
pieslice(300,350,360,180,70);

}
void leg()
{
setcolor(RED);
line(247,255,220,225);
line(247,255,265,225);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
if(flag1==1)
{
glPushMatrix();
rain();
glPopMatrix();
}
/*glPushMatrix();
glTranslatef(mx+6,my,0);
leg();
glPopMatrix(); */
glPushMatrix();
glTranslatef(mx+6,my,0);
glRotatef(rot_leg,0,0,1);
leg();
glPopMatrix();
glPushMatrix();
if(flag1==0)
{
glTranslatef(mx,my,0);
man1();
glPopMatrix();
}
else
{
glPushMatrix();
glTranslatef(mx,my,0);
man();
glPopMatrix();
}
if(flag1==1)
{
glPushMatrix();
glTranslatef(mx,my,0);
umbrella();
glPopMatrix();
}
glEnd();
glFlush();
}
void time_update()
{

mx++;

if(mx>=20)
flag1=1;
if(mx>=400)
flag1=0;
if(rot_ang>0)
rot_ang--;
else
rot_ang=360;
if(rot_leg>0)
rot_leg=rot_leg-0.5;
else
{
rot_leg=1;
}
glutPostRedisplay();
glutTimerFunc(10,time_update,0);
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Rain");
glClearColor(0.0,0.0,0.0,0.0);
//glViewport(0,0,800,800);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100,800,-100,800);
glutDisplayFunc(display);
glutTimerFunc(10,time_update,0);
glutMainLoop();
return 0;
}
OUTPUT

You might also like