Codigo
#include <GL/glut.h>
GLfloat TrasX=0.0f;
GLfloat TrasY=0.0f;
GLfloat TrasZ=0.0f;
GLint ancho=400;
GLint alto=400;
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.25f, 0.25f, 0.25f);
glScalef(5.0f,0.0f,5.0f);
glBegin(GL_QUADS);
glVertex3f( 2.5f,-1.0f, -1.0f);
glVertex3f( 2.5f,-1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f, -1.0f);
glEnd();
}
void carro(void)
{
glColor3f(1.0f, 0.0f, 0.0f);
glTranslatef(0.0f, -2.0f, 0.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, -2.0f, -5.0f);
glRotatef(10, 1.0f, 0.0f, 0.0f);
glRotatef(10, 0.0f, 1.0f, 0.0f);
// dibuja piso
Piso();
// dibuja rueda
glLoadIdentity();
glTranslatef(3.0f,-1.5f,-1.0f);
glTranslatef(TrasX,TrasY,TrasZ);
glColor3f(0.0f, 0.0f, 0.0f);
glutSolidSphere(0.5f, 16, 16);
//dibuja 2da rueda
glLoadIdentity();
glColor3f(0.0f, 1.0f, 1.0f);
glLoadIdentity();
glTranslatef(TrasX,TrasY,TrasZ);
carro();
glLoadIdentity();
glTranslatef(-.5f,-1.5f,-1.0f);
glTranslatef(TrasX,TrasY,TrasZ);
glColor3f(0.0f, 0.0f, 0.0f);
glutSolidSphere(0.5f, 16, 16);
glFlush();
glutSwapBuffers();
}
void init()
{
glClearColor(1,1,1,1);
glEnable(GL_DEPTH_TEST);
ancho = 400;
alto = 400;
}
void idle()
{
display();
}
void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 'p':
case 'P':
hazPerspectiva=1;
reshape(ancho,alto);
break;
case 'o':
case 'O':
hazPerspectiva=0;
reshape(ancho,alto);
break;
case 'a':
case 'A':
TrasX-=.10;
break;
case 's':
case 'S':
TrasX+=.10;
break;
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(ancho, alto);
glutCreateWindow("Carro");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
CORRIDA
No hay comentarios:
Publicar un comentario