You are on page 1of 2

#include <gl/glut.

h>
float alpha, beta;
int x0, y0;
float sx, sy, sz;
float tx = 0;
float ty = 0;
GLfloat luz_difusa[] = {(float)232/256, (float)243/256, (float)12/256};
GLfloat luz_ambiente[] = {(float)200/256, (float)25/256, (float)12/256};
GLfloat luz_especular[] = {(float)232/256, (float)243/256, (float)12/256};
GLfloat posicion_luz[] = {2.0, 0.0, 10, 0.0};
void specialKeyDown(int Key, int x, int y)
{
switch (Key){
case GLUT_KEY_RIGHT:
tx += 0.2;
break;
case GLUT_KEY_LEFT:
tx -= 0.2;
break;
case GLUT_KEY_UP:
ty += 0.2;
break;
case GLUT_KEY_DOWN:
ty -= 0.2;
break;
case GLUT_KEY_PAGE_UP:
sx = sy = sz += 0.02;
break;
case GLUT_KEY_PAGE_DOWN:
sx = sy = sz -= 0.02;
break;
}
glutPostRedisplay();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(20.0f, 1.0f, 1.0f, 10.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(-0.0f, 0.0f, -5.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
glTranslatef(tx, ty, 0.0);
glRotatef(alpha, 1.0f, 0.0f, 0.0f);
glRotatef(beta, 0.0f, 1.0f, 0.0f);
glScalef(sx, sy, sz);
glutSolidCube(0.5);
glFlush();
glutSwapBuffers();
}
void init(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLightfv(GL_LIGHT0, GL_DIFFUSE, luz_difusa);
glLightfv(GL_LIGHT0, GL_POSITION, posicion_luz);
glLightfv(GL_LIGHT0, GL_AMBIENT, luz_ambiente);
glLightfv(GL_LIGHT0, GL_SPECULAR, luz_especular);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
}
void onMouse(int button, int state, int x, int y)
{
if ( (button == GLUT_LEFT_BUTTON) & (state == GLUT_DOWN) ) {
x0 = x; y0 = y;
}
}
void onMotion(int x, int y)
{
alpha = (alpha + (y - y0));
beta = (beta + (x - x0));
x0 = x; y0 = y;
glutPostRedisplay();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow("Uso del mouse");
glutDisplayFunc(display);
glutMouseFunc(onMouse);
glutMotionFunc(onMotion);
glutSpecialFunc(specialKeyDown);
init();
glutMainLoop();
}

You might also like