WAP to use KeyBoard Key

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

float left=0.0;
float right=0.0;
float up=0.0;
float down=0.0;
int x1=-1;
int x2=1;
float x=0;
void display()
{
     glClear(GL_COLOR_BUFFER_BIT);
     glClearColor(0.0,1.0,0.0,0);
     glColor3f(1.0,0.0,0.0);
     glPointSize(5);
     glRectf(-0.1,-0.1,0.1,0.1);
     glLineWidth(5);
    if(x<10)
    {
     glBegin(GL_POINTS);
     glTranslatef(left+right,0,0);
         for(float i=-100;i<=100;i+=0.1)
         {
                   glVertex2f(i+left+right,0.3);
              glVertex2f(i+left+right,-0.3);
              }
         }
             
              else if(x<20)
              {
                   glBegin(GL_POINTS);
                   glTranslatef(left+right,0,0);
                 
                   for(float i=-100;i<=100;i+=0.1)
                 {  glVertex2f(i+left+right,0.3);
                   glVertex2f(i+left+right+0.05,0.3);
                   glVertex2f(i+left+right,-0.3);
                   glVertex2f(i+left+right+0.05,-0.3);
                   }
              }

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

}

void spinleft()
{
     x+=0.01;
     left-=0.0001;
     while(left>=0.9)
     left=-0.9;
     if(x>=20)
     x=0;
     glutPostRedisplay();
}
/*void spinup()
{
     up+=0.0001;
     while(left>0.9)
     up=-0.9;
     glutPostRedisplay();
}*/
void spinright()
{
x+=0.01;
     right+=0.0001;
     while(right>0.9)
     right=-0.9;
     if(x>=20)
     x=0;
     glutPostRedisplay();
     }
/*void spindown()
{
     down-=0.0001;
     while(right>0.9)
     down=-0.9;
     glutPostRedisplay();
     }*/

void keyboard(int key,int x,int y)
{
 if(key==GLUT_KEY_LEFT)
  glutIdleFunc(spinleft);
if(key==GLUT_KEY_RIGHT)
  glutIdleFunc(spinright);
 }

 void keyboard1(unsigned char key,int x,int y)
{

  if(key==' ')
  glutIdleFunc(NULL);

 }

int main(int argc,char **argv)
{
     glutInit(&argc,argv);
     glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
     glutInitWindowSize(500,500);

     glutCreateWindow("display");
     glutDisplayFunc(display);
     glutSpecialFunc(keyboard);
     glutKeyboardFunc(keyboard1);
   
     glutMainLoop();
}


Popular posts from this blog

Shutdown Pc

Ellipse using OpenGl

String Comparisons