Social Icons

Friday, 2 May 2014

Bouncing Ball In Opengl

#include <GL/glut.h>
#define true 1
#define false 0
static spinning = false;
static const int FPS = 60;
float flag=1,u=0.0,d=0.0;
static GLfloat currentAngleOfRotation = 45.0;

void reshape(GLint w, GLint h) {

  GLfloat aspect = (GLfloat)w / (GLfloat)h;
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  if (w <= h) {
    glOrtho(-50.0, 50.0, -50.0/aspect, 50.0/aspect, -1.0, 1.0);
//     f1=1;
  } else {
    glOrtho(-50.0*aspect, 50.0*aspect, -50.0, 50.0, -1.0, 1.0);
 //    f2=1;
  }
}

void display() {
   float angle,x,y;
   int i;
  glClear(GL_COLOR_BUFFER_BIT);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glRotatef(currentAngleOfRotation, 0.0, 0.0, 1.0);
  glScalef(10.0,10.0,10.0);
//  printf("f1=%d f2=%d",f1,f2);
 
  glBegin(GL_POINTS);
   for(i=0;i<360;i++)
   {
    angle=2*3.14*i/360;
    x=cos(angle);
    y=sin(angle);
    glVertex2f(x+u,y+u);
   }

  glEnd();
  glFlush();
  glutSwapBuffers();
}

void timer(int v)
{

 if(d==0)
  {
  u=u+0.03;
  if(u>2.9)
  d=1;
  }
   if(d==1)
  {
  u=u-0.03;
  if(u<-2.9)
  d=0;
  }
 

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

}



int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  glutInitWindowPosition(80, 80);
  glutInitWindowSize(800, 500);
  glutCreateWindow("Spinning Square");
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
  glutTimerFunc(100, timer, 0);
 
  glutMainLoop();
}

No comments:

Post a Comment

 

Sample Text

Sample text

 
Just Programming Cse DriveReputation Management