17. Graphics Program to simulate Keyboard Control.


#include <GL/gl.h>
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
void initGL()
{
glClearColor(0.0f,0.0f,0.0f,1.0f);
}
float xr=0, yr=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0,0,1);
glBegin(GL_TRIANGLES);
glVertex2i(200+xr, 100+yr);
glVertex2i(300+xr, 100+yr);
glVertex2i(300+xr, 200+yr);
glVertex2i(200+xr, 200+yr);
glEnd();
glutSwapBuffers();
}
void specialKey(int key, int x, int y)
{
switch(key)
{
case GLUT_KEY_UP:yr++;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN:yr--;
   glutPostRedisplay();
   break;
case GLUT_KEY_LEFT:xr--;
   glutPostRedisplay();
   break;
case GLUT_KEY_RIGHT:xr++;
   glutPostRedisplay();
   break;
}
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(400,400);
glutInitWindowPosition(600,50);
glutCreateWindow("Key Board Control");
glutDisplayFunc(display);
initGL();
gluOrtho2D(0.0,400,0.0,400);
glutSpecialFunc(specialKey);
glutMainLoop();
return 0;
}

Comments