jueves, 29 de marzo de 2012

Carrito 2D

#include <GL/glut.h>
float posicion;
int grados;
void reshape(int width, int height){
  glViewport(0, 0, width, height);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-5, 5, -5, 5, 0.1f, 20);
  glTranslatef(-3.5f, -2.0f, -15.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.0f, 0.0f);
    glutSolidSphere(0.1f,20,20);
   glPopMatrix();
   glPushMatrix();
    glTranslatef(0.5f,0.0f, 0.0f);
    glutSolidSphere(0.1f,20,20);
   glPopMatrix();
   glPushMatrix();
    glTranslatef(0.0f,0.4f, 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(1.0f,0.0f,0.5f); 
  glTranslatef(1.2f,0.0f, 0.0f);
  glRotatef(grados, 0.0f, 0.0f, 1.0f);
  glutSolidSphere(1.0f,30,30);
  dibujar_tornillos();
  glPopMatrix();
}
void dibujar_rueda_y_tornillos_detras(){
  glPushMatrix();
  glColor3f(1.0f,0.0f,0.5f); 
  glTranslatef(5.8f,0.0f, 3.0f);
  glRotatef(grados, 0.0f, 0.0f, 1.0f);
  glutSolidSphere(1.0f,30,30);
  dibujar_tornillos();
  glPopMatrix();
}

void dibujar_ventanas(){
  glPushMatrix();
  glColor3f(0.0f,0.0f,0.5f); 
  glTranslatef(1.1f,2.1f, 0.01f);
  glBegin(GL_QUADS);
      glVertex3f(0.0f, 0.0f, 0.0f);
      glVertex3f(0.8f, 0.0f, 0.0f);
      glVertex3f(0.8f, 0.8f, 0.0f);
   glVertex3f(0.0f, 0.0f, 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, 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(0.0,1.0,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);
}
int main(int argc, char **argv){
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  glutInitWindowPosition(50, 50);
  glutInitWindowSize(500, 500);
  glutCreateWindow("cARRITO RUN RUN");
  init();
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutMainLoop();
  return 0;
}







No hay comentarios:

Publicar un comentario