martes, 27 de marzo de 2012

CARRO 2D

#include <GL/glut.h>

float posicion;
int grados;

void reshape(int width, int height){
  glViewport(0, 0, width, height);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  //gluPerspective(60.0f,1.0f, 0.01f, 100.0f);
    glOrtho(-10, 10, -10, 10, 0.1f, 20);
  glTranslatef(-3.0f, 0.0f, -15.0f);
  //glRotatef(40, 1.0f, 1.0f, 0.0f);
  glMatrixMode(GL_MODELVIEW);
}


void dibujar_tornillos(){
  glPushMatrix();
   glColor3f(0.0f,0.0f,0.0f); 
   glTranslatef(0.0f,0.0f, 1.0f);
   glPushMatrix();
    glTranslatef(-0.5f,-0.1f, 0.0f);
    glutSolidSphere(0.1f,20,20);
   glPopMatrix();
   glPushMatrix();
    glTranslatef(0.5f,-0.1f, 0.0f);
    glutSolidSphere(0.1f,20,20);
   glPopMatrix();
   glPushMatrix();
    glTranslatef(0.0f,0.5f, 0.0f);
    glutSolidSphere(0.1f,20,20);
   glPopMatrix();
   glPushMatrix();
    glTranslatef(-0.2f,-0.5f, 0.0f);
    glutSolidSphere(0.1f,20,20);
   glPopMatrix();
   glPushMatrix();
    glTranslatef(0.2f,-0.5f, 0.0f);
    glutSolidSphere(0.1f,20,20);
   glPopMatrix();
  glPopMatrix();
}

void dibujar_rueda_y_tornillos_delante(){
  glPushMatrix();
  glColor3f(0.5f,0.5f,0.5f); 
  glTranslatef(1.5f,0.0f, 0.0f);
  glRotatef(grados, 0.0f, 0.0f, 1.0f);
  glutSolidSphere(1.0f,20,20);
  dibujar_tornillos();
  glPopMatrix();
}

void dibujar_rueda_y_tornillos_detras(){
  glPushMatrix();
  glColor3f(0.5f,0.5f,0.5f); 
  glTranslatef(5.5f,0.0f, 3.0f);
  glRotatef(grados, 0.0f, 0.0f, 1.0f);
  glutSolidSphere(1.0f,20,20);
  dibujar_tornillos();
  glPopMatrix();
}


void dibujar_ventanas(){
  glPushMatrix();
  glColor3f(0.0f,0.0f,1.0f); 
  glTranslatef(1.2f,2.0f, 0.01f);

  glBegin(GL_TRIANGLES);
      glVertex3f(0.0f, 0.0f, 0.0f);
      glVertex3f(0.8f, 0.0f, 0.0f);
      glVertex3f(0.8f, 0.8f, 0.0f);
  glEnd();

  glBegin(GL_QUADS);
      glVertex3f(0.8f, 0.0f, 0.0f);
      glVertex3f(1.7f, 0.0f, 0.0f);
      glVertex3f(1.7f, 0.8f, 0.0f);
      glVertex3f(0.8f, 0.8f, 0.0f);
  glEnd();

  glTranslatef(1.9f,0.0f, 0.0f);

  glBegin(GL_QUADS);
      glVertex3f(0.0f, 0.0f, 0.0f);
      glVertex3f(0.9f, 0.0f, 0.0f);
      glVertex3f(0.9f, 0.8f, 0.0f);
      glVertex3f(0.0f, 0.8f, 0.0f);
  glEnd();

  glBegin(GL_TRIANGLES);
      glVertex3f(0.9f, 0.0f, 0.0f);
      glVertex3f(1.7f, 0.0f, 0.0f);
      glVertex3f(0.9f, 0.8f, 0.0f);
  glEnd();

  glPopMatrix();
}

void techo(){

  glPushMatrix();

  glTranslatef(1.0f,2.0f, 0.0f);

  glBegin(GL_TRIANGLES);
      glVertex3f(0.0f, 0.0f, 0.0f);
      glVertex3f(1.0f, 0.0f, 0.0f);
      glVertex3f(1.0f, 1.0f, 0.0f);
  glEnd();

  glBegin(GL_QUADS);
      glVertex3f(1.0f, 0.0f, 0.0f);
      glVertex3f(3.0f, 0.0f, 0.0f);
      glVertex3f(3.0f, 1.0f, 0.0f);
      glVertex3f(1.0f, 1.0f, 0.0f);
  glEnd();

  glBegin(GL_TRIANGLES);
      glVertex3f(3.0f, 0.0f, 0.0f);
      glVertex3f(4.0f, 0.0f, 0.0f);
      glVertex3f(3.0f, 1.0f, 0.0f);
  glEnd();
  glPopMatrix();
}

dibujar_cuerpo_coche(){
  glColor3f(1,0,0); 

  glTranslatef(posicion,0.0f, 0.0f);

  glBegin(GL_QUADS);
      glVertex3f(0.0f, 0.0f, 0.0f);
      glVertex3f(7.0f, 0.0f, 0.0f);
      glVertex3f(7.0f, 2.0f, 0.0f);
      glVertex3f    (0.0f, 2.0f, 0.0f);
  glEnd();   
}

void display(){
  GLfloat angulo;
  int i;

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glLoadIdentity();
  dibujar_cuerpo_coche();
 
  techo();
  dibujar_ventanas();

  dibujar_rueda_y_tornillos_delante();
  dibujar_rueda_y_tornillos_detras();


  glutSwapBuffers();
}

void init(){
  glClearColor(0,0,0,0);
  posicion=0;
  grados=0;
  glEnable(GL_DEPTH_TEST);
}


void keyboard(unsigned char key, int x, int y){
  switch(key)
    {
    case 'p':
    case 'P':
      posicion+=0.1f;
      grados-=180/3.14;
      display();
      break;
  
    case 'o':
    case 'O':
      posicion-=0.1f;
      grados+=180/3.14;
      display();
      break;

    case 27:   // escape
//      exit(0);
      break;
    }
}

int main(int argc, char **argv){
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  glutInitWindowPosition(50, 50);
  glutInitWindowSize(500, 500);
  glutCreateWindow("Practica Final");
  init();
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutKeyboardFunc(keyboard);
  glutMainLoop();
  return 0;
}

PROGRAMA CORRIENDO: