jueves, 19 de abril de 2012

CARRO 2D EN MOVIEMIENTO

//#include "stdafx.h"

// moverpelota.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <GL/glut.h>
GLfloat anguloCuboX = 0.0f;
GLfloat anguloCuboY = 0.0f;
GLfloat anguloEsfera = 0.0f;

GLint ancho=900;
GLint alto=600;
    int ang =0 ;
    int movx = 0;
int hazPerspectiva = 0;
void reshape(int width, int height)
{
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if(hazPerspectiva)
        gluPerspective(60.0f,(GLfloat)width/(GLfloat)height, 1.0f, 20.0f);
            
     else
       glOrtho(-4,4, -4, 4, 1, 10);
       glMatrixMode(GL_MODELVIEW);
    ancho = width;
    alto = height;
}
void Piso(void)
{
    glColor3f(0.5f, 0.5f, 0.6f);
 glScalef(1.5f,0.0f,1.5f);
    glBegin(GL_QUADS);       //cara abajo
    glVertex3f( 10.0f,-10.0f, -10.0f);
    glVertex3f( 10.0f,-10.0f,  10.0f);
    glVertex3f(-10.0f,-10.0f,  10.0f);
    glVertex3f(-10.0f,-10.0f, -10.0f);
    glEnd();
}
void carro(void)
{
   glColor3f(0.5f, 0.5f, 1.0f);

    glBegin(GL_POLYGON);       //carro
    glVertex3f(-1.5f,0.5f, -2.0f);
    glVertex3f( 3.5f,0.5f, -2.0f);
    glVertex3f(3.5f,2.0f,  -2.0f);
    glVertex3f(2.0f,2.0f, -2.0f);
    glVertex3f(1.f,3.0f, -2.0f);
    glVertex3f(-0.5f,3.0f, -2.0f);
    glVertex3f(-1.5f,2.0f, -2.0f);
    glEnd();
}
void display()
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glTranslatef(0.0f, 0.0f, -5.0f);
    glRotatef(15, 1.0f, 0.0f, 0.0f);
    glRotatef(15, 0.0f, 1.0f, 0.0f);
     Piso();
  // dibuja rueda
 
 //dibuja 2da rueda
 glLoadIdentity();

  glColor3f(-1.0f, 0.0f, 1.0f);
  glTranslatef(movx,0.0,0.0);
  carro();
  glLoadIdentity();
    
       glTranslatef(-.5f,0.5f,-1.0f);
      glColor3f(0.0f, 0.0f, 0.0f);
       glRotatef(ang,1.0,0.0,0.0);
       glTranslatef(movx,0.0,0.0);
       glutSolidSphere(0.5f, 16, 16);
       glLoadIdentity();

       glTranslatef(3.0f,0.5f,-1.0f);
       glColor3f(0.0f, 0.0f, 0.0f);
       glRotatef(ang,1.0,0.0,0.0);
       glTranslatef(movx,0.0,0.0);
    
      glutSolidSphere(0.5f, 16, 16);
       glLoadIdentity();

    glFlush();
    glutSwapBuffers();

}
void init()
{
    glClearColor(0,0,0,0);
    glEnable(GL_DEPTH_TEST);
    ancho = 600;
    alto = 900;
}
void idle()
{
    display();
}


void specialkeyevent( int key, int Xx, int Yy )
{

 
    switch ( key ) {

  case GLUT_KEY_LEFT:
      movx-=1;
      ang-=1;
      display();
      break;
  case GLUT_KEY_RIGHT:
      movx+=1;
      ang-=1;
      display();
      break;

   
 }
 glutPostRedisplay();
}


int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(ancho, alto);
    glutCreateWindow("Cubo 1");
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutIdleFunc(idle);
    glutSpecialFunc(specialkeyevent );
    glutMainLoop();
    return 0;
}