jueves, 10 de mayo de 2012

Práctica de Interacción.


//Secccion de librerias
#include "stdafx.h"
#include <GL/glut.h>
#include <math.h>


//seccion de prototipos
void Inicializa();
void Dibuja();
void Teclado(unsigned char, int, int);
void Especial(int, int, int);
void Anima();
void Raton(int, int, int, int);
void Pasivo(int, int);
void Mueve(int, int);
void Tiempo(int);


//variables globales
float ambiental[4]={.6,.5,.3,0};
float difusa[4]={.3,.4,.7,0};
float especular[4]={1,1,1,0};
float posicion[4]={0,7,2,1};
float rx,ry,rz,sx=1,sy=1,sz=1,tx,ty,tz;


void main()
{
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(600, 600); 
glutInitWindowPosition(100,50); 
glutCreateWindow("Ejemplo Interactividad");
glutDisplayFunc(Dibuja);
Inicializa();
glutKeyboardFunc(Teclado);
glutSpecialFunc(Especial);
glutIdleFunc(Anima);
glutMouseFunc(Raton);
glutMotionFunc(Mueve);
glutPassiveMotionFunc(Pasivo);
glutTimerFunc(10,Tiempo,10);
glutMainLoop();
}


void Inicializa()
{
glClearColor(0.235235,0.235325,0.134,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90,1,0.1,50);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
glLightfv(GL_LIGHT0,GL_AMBIENT,ambiental);
glLightfv(GL_LIGHT0,GL_DIFFUSE,difusa);
glLightfv(GL_LIGHT0,GL_SPECULAR,especular);
glLightfv(GL_LIGHT0,GL_POSITION,posicion);
glLightf(GL_LIGHT0,GL_CONSTANT_ATTENUATION,1);
glLightf(GL_LIGHT0,GL_QUADRATIC_ATTENUATION,0.0001);
glLightf(GL_LIGHT0,GL_LINEAR_ATTENUATION,0.0001);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}


void Dibuja()
{
GLUquadricObj *p;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
p=gluNewQuadric();


gluQuadricDrawStyle(p,GLU_FILL);
gluQuadricNormals(p,GLU_SMOOTH);
gluQuadricOrientation(p,GLU_OUTSIDE);
glMaterialfv(GL_FRONT,GL_SPECULAR,especular);
ambiental[0]=0;ambiental[1]=1;ambiental[2]=0;
glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE, ambiental);
glMaterialf(GL_FRONT,GL_SHININESS,20);
glShadeModel(GL_SMOOTH);
glTranslated(-4,5,-15);
glRotated(rx,1,0,0);
glScaled(sx,sx,sx);
gluCylinder(p,1,1,4,20,20);
gluDeleteQuadric(p);


glLoadIdentity();
glMaterialfv(GL_FRONT,GL_SPECULAR,especular);
ambiental[0]=0;ambiental[1]=0;ambiental[2]=1;
glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE, ambiental);
glMaterialf(GL_FRONT,GL_SHININESS,20);
glShadeModel(GL_SMOOTH);
glTranslated(0,-5,-15);
glTranslated(tx,ty,tz);
glRotated(ry,0,1,0);
glScaled(sy,sy,sy);
glutSolidTeapot(2);


glLoadIdentity();
glMaterialfv(GL_FRONT,GL_SPECULAR,especular);
ambiental[0]=1;ambiental[1]=0;ambiental[2]=0;
glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE, ambiental);
glMaterialf(GL_FRONT,GL_SHININESS,20);
glShadeModel(GL_FLAT);
glTranslated(4,5,-15);
glRotated(rz,0,0,1);
glScaled(sz,sz,sz);
glutSolidTorus(1,2,10,10);


glutSwapBuffers();
}
void Anima()
{
rx+=.5;
glutPostRedisplay();
}


void Teclado(unsigned char t, int x, int y)
{
if(t=='c' || t=='C')
{
sx-=.1;
glutPostRedisplay();
}
if(t=='t' || t=='T')
{
sy-=.1;
glutPostRedisplay();
}
if(t=='o' || t=='O')
{
sz-=.1;
glutPostRedisplay();
}
}


void Especial(int t, int x, int y)
{
if(t==GLUT_KEY_RIGHT)
{
tx+=.2;
glutPostRedisplay();
}
if(t==GLUT_KEY_LEFT)
{
tx-=.2;
glutPostRedisplay();
}
if(t==GLUT_KEY_UP)
{
ty+=.2;
glutPostRedisplay();
}
if(t==GLUT_KEY_DOWN)
{
ty-=.2;
glutPostRedisplay();
}
if(t==GLUT_KEY_PAGE_DOWN)
{
tz-=.2;
glutPostRedisplay();
}
if(t==GLUT_KEY_PAGE_UP)
{
tz+=.2;
glutPostRedisplay();
}
}


void Raton(int t, int e, int x, int y)
{
if(t==GLUT_LEFT_BUTTON && e==GLUT_UP)
{
sx+=.1;
glutPostRedisplay();
}
if(t==GLUT_RIGHT_BUTTON && e==GLUT_UP)
{
sy+=.1;
glutPostRedisplay();
}
if(t==GLUT_MIDDLE_BUTTON && e==GLUT_UP)
{
sz+=.1;
glutPostRedisplay();
}
}


void Mueve(int x, int y)
{
rz-=1;
glutPostRedisplay();
}


void Pasivo(int x, int y)
{
rz+=1;
glutPostRedisplay();
}


void Tiempo(int p)
{
ry+=1;
glutPostRedisplay();
glutTimerFunc(p+1,Tiempo,p);
}

No hay comentarios:

Publicar un comentario