Facebook
From Fabio, 10 Months ago, written in C++.
Embed
Download Paste or View Raw
Hits: 423
  1. #include <GL/glut.h>
  2.  
  3. float x_position = 0;
  4. float speed = 0.1f;
  5.  
  6. void display()
  7. {
  8.  glClear(GL_COLOR_BUFFER_BIT);
  9.  
  10.  glTranslatef(x_position, 0, 0);
  11.  
  12.  glColor3f(1, 0, 0);
  13.  glBegin(GL_QUADS);
  14.   glVertex2f(-2, 2);
  15.   glVertex2f(2, 2);
  16.   glVertex2f(2, -2);
  17.   glVertex2f(-2, -2);
  18.  glEnd();
  19.  glutSwapBuffers();
  20. }
  21.  
  22. void teclado(unsigned char key, int x, int y) {
  23.  glutPostRedisplay();
  24.  
  25.  if (key == 27) {
  26.   exit(0);
  27.  }
  28.  if (key == 97) {
  29.   x_position = -speed;
  30.  }
  31.  if (key == 100) {
  32.   x_position = speed;
  33.  }
  34. }
  35.  
  36. int main(int argc, char** argv)
  37. {
  38.  glutInit(&argc;, argv);
  39.  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
  40.  glutInitWindowSize(640, 640);
  41.  glutCreateWindow("OpenGL");
  42.  glutDisplayFunc(display);
  43.  glutKeyboardFunc(teclado);
  44.  gluOrtho2D(-10, 10, -10, 10);
  45.  glClearColor(0.5, 0.7, 0.5, 0);
  46.  glutMainLoop();
  47.  return 0;
  48. }
  49.