domingo, 15 de abril de 2012

Carro en 2D


#include <GL/glut.h>

float pos;
int gr;

void reshape(int width, int height){
  glViewport(0, 0, width, height);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
 
    glOrtho(-10, 10, -10, 10, 0.1f, 20);
  glTranslatef(-3.0f, 0.0f, -15.0f);
 
  glMatrixMode(GL_MODELVIEW);
}

void rueda_d(){
  glPushMatrix();
  glColor3f(0,0,0);
  glTranslatef(1.5f,0.0f, 0.0f);
  glRotatef(gr, 0.0f, 0.0f, 1.0f);
  glutSolidSphere(1.0f,20,20);
  glPopMatrix();
}

void rueda_t(){
  glPushMatrix();
  glColor3f(0,0,0);
  glTranslatef(5.5f,0.0f, 3.0f);
  glRotatef(gr, 0.0f, 0.0f, 1.0f);
  glutSolidSphere(1.0f,20,20);
  glPopMatrix();
}


void ventanas(){
  glPushMatrix();
  glColor3f(0,0,0);
  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(1.0f, -1.0f, 0.0f);
      glVertex3f(-1.0f, -1.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();
}

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

  glTranslatef(pos,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();
  coche();

  techo();
  ventanas();


  rueda_d();

  rueda_t();

  glutSwapBuffers();
}

void init(){
  glClearColor(1,1,1,0);
  pos=0;
  gr=0;
  glEnable(GL_DEPTH_TEST);
}


void keyboard(unsigned char key, int x, int y){
  switch(key)
    {
    case 'm':
    case 'M':
      pos+=0.1f;
      gr-=180/3.14;
      display();
      break;
 
    case 'n':
    case 'N':
      pos-=0.1f;
      gr+=180/3.14;
      display();
      break;
    }
}

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

Corrida




No hay comentarios:

Publicar un comentario