#include "stdafx.h" /*ejercicio: cubo, piso, foco + controles mediante teclas*/ GLfloat colors[][3] = { { 0.0,0.0,0.0 },{ 1.0,0.0,0.0 }, { 1.0,1.0,0.0 },{ 0.0,1.0,0.0 },{ 0.0,0.0,1.0 }, { 1.0,0.0,1.0 },{ 1.0,1.0,1.0 },{ 0.0,1.0,1.0 } }; GLfloat ex = 15.0f; GLfloat ey = 30.0f; GLfloat ez = -10.0f; GLfloat delta = 0.01f; GLfloat deltaR = 0.01f; GLenum shademode = GL_FLAT; GLboolean bCull = glIsEnabled(GL_CULL_FACE); GLboolean bDepth = glIsEnabled(GL_DEPTH_TEST); GLboolean bOutline = (GLboolean)true; void cuadrado() { glBegin(GL_POLYGON); glVertex2f(1., 1.); glVertex2f(1., -1.); glVertex2f(-1., -1.); glVertex2f(-1., 1.); glEnd(); } void colorcube1(void) { glPushMatrix(); glScalef(0.3, 0.3, 0.3); /* cara1 */ glPushMatrix(); glColor3fv(colors[1]); glTranslatef(0., 0., 1.); cuadrado(); glPopMatrix(); /* cara2 */ glPushMatrix(); glColor3fv(colors[2]); glRotatef(90., 1., 0., 0.); glTranslatef(0., 0., 1.); cuadrado(); glPopMatrix(); /* cara3 */ glPushMatrix(); glColor3fv(colors[3]); glRotatef(180., 1., 0., 0.); glTranslatef(0., 0., 1.); cuadrado(); glPopMatrix(); /* cara4 */ glPushMatrix(); glColor3fv(colors[4]); glRotatef(270., 1., 0., 0.); glTranslatef(0., 0., 1.); cuadrado(); glPopMatrix(); /* cara5 */ glPushMatrix(); glColor3fv(colors[5]); glRotatef(90., 0., 0., 1.); glRotatef(270., 1., 0., 0.); glTranslatef(0., 0., 1.); cuadrado(); glPopMatrix(); /* cara6 */ glPushMatrix(); glColor3fv(colors[7]); glRotatef(-90., 0., 0., 1.); glRotatef(270., 1., 0., 0.); glTranslatef(0., 0., 1.); cuadrado(); glPopMatrix(); glPopMatrix(); } void SpecialKeys(int key, int x, int y) { GLfloat dx, dz; if (key == GLUT_KEY_UP) {//increase distance from camera to origin ex *= (1.0f + deltaR); ey *= (1.0f + deltaR); ez *= (1.0f + deltaR); } if (key == GLUT_KEY_DOWN) {//reduce distance from camera to origin (close up) ex *= (1.0f - deltaR); ey *= (1.0f - deltaR); ez *= (1.0f - deltaR); } if (key == GLUT_KEY_LEFT) //Rotate camera around origin in Oxz plane { dx = -ez; dz = ex; GLfloat s = sqrtf(ex*ex + ey*ey + ez*ez); ex += delta*dx; ez += delta*dz; GLfloat s1 = sqrtf(ex*ex + ey*ey + ez*ez) / s; ex /= s1; ey /= s1; ey /= s1; } if (key == GLUT_KEY_RIGHT) //Rotate camera around origin in Oxz plane { dx = -ez; dz = ex; GLfloat s = sqrtf(ex*ex + ey*ey + ez*ez); ex -= delta*dx; ez -= delta*dz; GLfloat s1 = sqrtf(ex*ex + ey*ey + ez*ez) / s; ex /= s1; ey /= s1; ey /= s1; } if (key == GLUT_KEY_F1) bCull = !bCull; if (key == GLUT_KEY_F2)bDepth = !bDepth; if (key == GLUT_KEY_F3)bOutline = !bOutline; if (key == GLUT_KEY_F4) { if (shademode == GL_FLAT) { shademode = GL_SMOOTH; } else { if (shademode == GL_SMOOTH) { shademode = GL_FLAT; } }; glShadeModel(shademode); } // Refresh the Window glutPostRedisplay(); } void piso() { glColor3f(0.5, 0.5, 0.5); glPushMatrix(); glScalef(1.5, 1.5, 1.5); glRotatef(90., 1, 0, 0); glTranslatef(0., 0., 1.); cuadrado(); glPopMatrix(); } void foco() { glColor3fv(colors[2]); glPushMatrix(); glTranslatef(0.3, 2., -0.2); glScalef(.05, .05, .05); glutSolidSphere(1., 20, 20); glPopMatrix(); } void display(void) { /* display callback, clear frame buffer and z buffer, rotate cube and draw, swap buffers */ // Turn culling on if flag is set if (bCull) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE); // Enable depth testing if flag is set if (bDepth) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST); // Draw the back side as a wireframe only, if flag is set if (bOutline)glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); gluLookAt(ex, ey, ez, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); piso(); colorcube1(); foco(); glFlush(); glutSwapBuffers(); } void myReshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); /*if (w <= h) glOrtho(-2.0, 2.0, -2.0 * (GLfloat)h / (GLfloat)w, 2.0 * (GLfloat)h / (GLfloat)w, -100.0, 100.0); else glOrtho(-2.0 * (GLfloat)w / (GLfloat)h, 2.0 * (GLfloat)w / (GLfloat)h, -2.0, 2.0, -100.0, 100.0);*/ gluPerspective(60.0, 1.0, 1.5, 500.0); glMatrixMode(GL_MODELVIEW); } void main(int argc, char **argv) { glutInit(&argc, argv); /* need both double buffering and z buffer */ glutInitDisplayMode(/*GLUT_DOUBLE*/GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow("colorcube"); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutSpecialFunc(SpecialKeys); //glutIdleFunc(spinCube); //glutMouseFunc(mouse); glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */ glShadeModel(GL_FLAT); glutMainLoop(); }