jueves, 4 de octubre de 2012

Pelota Que rebota



#include <GL/glut.h>

float angX=0.0f,angY=0.0f;
int ResX=600,ResY=600;

float angB=0.0f,angC=0.0f,angDE=0.0f;
float aceX=0.0f,aceY=-0.0001f;
float velX=0.001f,velY=0.0f;
float posX=0.0f,posY=10.0f;


GLfloat material [4] = {-1.0f, -1.0f,  1.f };
GLfloat Pblanca [4] = {1.0f,  1.0f,  1.0f};
GLfloat Tverde [4] = {-1.0, 1.0, 1.0, 1.f };

void Ejes()
{
glNewList(1,GL_COMPILE);
glBegin (GL_LINES);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, Tverde );
glVertex3f ( 0.0, .0,-20.0);
glVertex3f ( 0.0, 0.0,20.0);
glEnd();
glEndList();
}

void pEJE(){
glNewList(2,GL_COMPILE);
glPushMatrix();
glutSolidSphere(0.9f,100,100);
glPopMatrix();
glEndList();
}



void RenderScene(void){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);


glLoadIdentity( );
glTranslatef(1.0f,0.0f,-10.0f);
glRotatef(angY,1.0f,0.0f,0.0f);
glRotatef(angX,0.0f,1.0f,0.0f);

glLineWidth(20);
glCallList(1);
glPushMatrix();
glScalef(1,1,1);
glTranslatef(1.0f,1.0f,1.0f);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, Pblanca );
glPushMatrix();
glTranslatef(posX,posY,0.0f);
glCallList(2);
glPopMatrix();
glutSwapBuffers();
}


void idle(void){
velY+=aceY; posY+=velY;
velX+=aceX; posX+=velX;
if(posY<0.0f){posY=0.0f; velY*=-0.8f;
if(velY<0.01f){velX=0.0f; velY=0.0f;}
}
glutPostRedisplay();
}


int main(int a, char *b[]){
glutInit(&a,b);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(ResX,ResY);
glutInitWindowPosition(100,100);

glutCreateWindow("Pelota Rebote");
glutDisplayFunc(RenderScene);

glutIdleFunc(idle);
glViewport( 0, 0, ResX, ResY );
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );

glFrustum(-(float)ResX/ResY, (float)ResX/ResY, -1.0, 2.0, 1, 1000000.0);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);

Ejes();
pEJE();
glutMainLoop();
return 0;
}

Corrida del programa pelota que rebota









No hay comentarios:

Publicar un comentario