Programa Iluminacion
Codigo
#include <GL/glut.h>
float escala=0.2;
float angX=0.0f,angY=0.0f;
int ResX=800,ResY=600;
enum{EJE=2};
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;
//Definición del modelo de una luz
GLfloat light_Ambient [4] = { 0.2, 0.2, 0.2, 1.0};
GLfloat light_Diffuse [4] = { 1.0, 1.0, 1.0, 1.0};
GLfloat light_Position [4] = {20.0, 15.0, 10.0, 1.0};
//Definición de las caracteristicas opticas del material: coeficientes de reflexión
GLfloat material [4] = {1.0f, 0.2f, 0.2f, 1.0f };
GLfloat RedMaterial [4] = {1.0, 0.0, 0.0, 1.0 };
GLfloat GreenMaterial [4] = {0.0, 1.0, 0.0, 1.0 };
GLfloat BlueMaterial [4] = {0.0, 0.0, 1.0, 1.0 };
GLfloat WhiteMaterial [4] = {1.0, 1.0, 1.0, 1.0 };
GLfloat BlackMaterial [4] = {0.0, 0.0, 0.0, 1.0 };
GLfloat GrayMaterial [4] = {0.6, 0.6, 0.6, 1.0 };
void luces(void){
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_Ambient );
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_Diffuse );
glLightfv(GL_LIGHT0, GL_POSITION, light_Position );
}
void EjesReferencia()
{
glNewList(1,GL_COMPILE);
glBegin (GL_LINES);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, RedMaterial );
glVertex3f ( 0.0, 0.0, 0.0);
glVertex3f (20.0, 0.0, 0.0);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, GreenMaterial );
glVertex3f ( 0.0, 0.0, 0.0);
glVertex3f ( 0.0,20.0, 0.0);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, WhiteMaterial );
glVertex3f ( 0.0, 0.0, 0.0);
glVertex3f ( 0.0, 0.0,20.0);
glEnd();
glEndList();
}
void pEJE(){
glNewList(EJE,GL_COMPILE);
glPushMatrix();
glutSolidSphere(1.0f,50,50);
glPopMatrix();
glEndList();
}
void RenderScene(void){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glClearColor( 0.2f, 0.6f, 0.2f, 1.0f );
//AJUSTANDO LA CÁMARA
glLoadIdentity( );
glTranslatef(0.0f,0.0f,-5.0f);
glRotatef(angY,1.0f,0.0f,0.0f);
glRotatef(angX,0.0f,1.0f,0.0f);
//EJES DE REFERENCIA
glLineWidth(5);
glCallList(1);
glPushMatrix();
glScalef(escala,escala,escala);
//glTranslatef(1.0f,1.0f,1.0f);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, RedMaterial );
glPushMatrix();
glTranslatef(posX,posY,0.0f);
glCallList(EJE);
glPopMatrix();
//glFlush();
glutSwapBuffers();
}
void raton(int x, int y){
angX=(float)x;
angY=(float)y;
}
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 QUE REBOTA");
glutDisplayFunc(RenderScene);
glutKeyboardFunc(teclado);
glutPassiveMotionFunc(raton);
glutIdleFunc(idle);
//OPENGL
glViewport( 0, 0, ResX, ResY );
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
glFrustum(-(float)ResX/ResY, (float)ResX/ResY, -1.0, 1.0, 2, 10000.0);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
luces();
EjesReferencia();
pEJE();
glutMainLoop();
return 0;
}
No hay comentarios:
Publicar un comentario