Social Icons

Friday 2 May 2014

Bouncing Sphere in Opengl

#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
float x=2,y=0,d=0;
void display()
{      
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glMatrixMode(GL_MODELVIEW);
   
   
     
      glPushMatrix();
      glTranslatef(0.0,-2.0+y,0.0);
      glRotatef(x,1.0,0.0,0.0);
      glutSolidSphere(0.5,30,40);
     
      glPopMatrix();
      glFlush();

 glutPostRedisplay();
 }
void reshape(GLint w, GLint h) {

  glMatrixMode(GL_PROJECTION);
  GLfloat aspect = (GLfloat)(w) / (GLfloat)(h);
  glLoadIdentity();
  if (w <= h) {
 
    glOrtho(-2.5, 2.5, -2.5/aspect, 2.5/aspect, -10.0, 10.0);
  } else {
 
    glOrtho(-2.5*aspect, 2.5*aspect, -2.5, 2.5, -10.0, 10.0);
  }

}



void timer(int v)
{
x=x+0.03;
     

 if(d==0)
  {glColor3f(1.0,0.0,0.0);
   y=y+0.03;
   if(y>4)
    d=1;
  }
   if(d==1)
  {
  glColor3f(1.0,1.0,0.0);
   y=y-0.03;
   if(y<-0.8)
    d=0;
  }


 glutPostRedisplay();
 glutTimerFunc(1000/60,timer,v);

}

int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  glutInitWindowPosition(80, 80);
  glutInitWindowSize(800, 600);
  glutCreateWindow("Cyan Shapes in Yellow Light");
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
  glutTimerFunc(100, timer, 0);

  glutMainLoop();
}

No comments:

Post a Comment

 

Sample Text

Sample text

 
Just Programming Cse DriveReputation Management