WAP for Bouncing Balls in OpenGl

#include<GL/glu.h>
#include<GL/glut.h>
#include<GL/gl.h>

int flag=1;
float tx=0;

void display()
{
  glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
  glClearColor(1, 0, 0, 1.0);
  glColor3f(1.0, 0.0, 0.0);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(80.0, 4.0/3.0, 1, 10);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluLookAt(4, 4, 4, 0, 0, 0, 0, 1, 0);

  glColor3f(1.0, 1.0, 1.0);
  glBegin(GL_LINES);
  for (float i =-9; i <= 9; i += 0.5)
   {
    glVertex3f(i, 0,-9); glVertex3f(i, 0, 9);

    glVertex3f(-9, 0, i); glVertex3f(9, 0, i);
  }
  glEnd();

  glPushMatrix();
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(80.0, 1.0/1.0, 1, 60);
  gluLookAt(0, 0, 10, 0,0,0, 1, 1, 0);
  glColor3f(1.0,1.0,0);
  glTranslatef(tx+2.5,tx+2.5,0);
  glutSolidSphere(1,50,50);
  glPopMatrix();
 
  glPushMatrix();
  glLoadIdentity();
  gluPerspective(80.0, 4.0/3.0, 1, 80);
  gluLookAt(0, 0, 10,4,-4,0, 1, 1 ,0);
  glColor3f(1.0,0.0,0);
  glTranslatef(tx+2.5,tx+2.5,0);
  glutSolidSphere(1,50,50);
  glPopMatrix();
 
  glPushMatrix();
  glLoadIdentity();
  gluPerspective(80.0, 4.0/3.0, 1, 60);
  gluLookAt(0, 0, 10,0,7,0, 1, 0 ,0);
  glColor3f(0.0,0.0,1.0);
  glTranslatef((tx*2)+3,0,0);
  glutSolidSphere(1,50,50);
  glPopMatrix();
glFlush();
glutSwapBuffers();
glutPostRedisplay();
}

void init() {
  GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
  GLfloat yellow[] = { 1.0, 1.0, 0.0, 1.0 };
  GLfloat cyan[] = { 0.0, 1.0, 1.0, 1.0 };
  GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
  GLfloat red[] = { 1.0, 0, 0, 1.0 };
  GLfloat direction[] = { 0, 1, 1, 0.0 };

  glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, yellow);
  glMaterialfv(GL_FRONT, GL_SPECULAR, cyan);
  glMaterialf(GL_FRONT, GL_SHININESS,128);

  glLightfv(GL_LIGHT0, GL_AMBIENT, black);
  glLightfv(GL_LIGHT0, GL_DIFFUSE, yellow);
  glLightfv(GL_LIGHT0, GL_SPECULAR, white);
  glLightfv(GL_LIGHT0, GL_POSITION, direction);

  glEnable(GL_LIGHTING);                // so the renderer considers light
  glEnable(GL_LIGHT0);                  // turn LIGHT0 on
  glEnable(GL_DEPTH_TEST);              // so the renderer considers depth
}


void timer(int v)
{
     if(flag == 1)
     {
             tx = tx-0.15;

             if(tx <=-1.2)
             {
              flag=0;
              }
}
 else if(flag == 0)
 {
      tx=tx+0.10;
      if(tx>=1.2)
      {
       flag = 1;
       }
}
     glutPostRedisplay();
     glutTimerFunc(100,timer,1);
}
void keyboard(unsigned char k, int x, int y)
{
    switch (k)
    {
    case ' ':
    {
     glutTimerFunc(100,timer,1);
    }
    break;
    case 's':
    exit(0);
    }
    glutPostRedisplay();
}

int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  glutInitWindowPosition(10, 10);
  glutInitWindowSize(700,700);
  glutCreateWindow("Square");
  glutDisplayFunc(display);
  init();
 glutKeyboardFunc(keyboard);
  glutMainLoop();
}




Popular posts from this blog

Shutdown Pc

Ellipse using OpenGl

String Comparisons