#include "stdafx.h" #include "glut.h" #include //////////////////////////////////////////////////////////////////////////////////////////////// GLfloat ladoC = 40; GLfloat deltaMov = 0.5f; GLfloat v0[3] = {0, 0, 0}; GLfloat v1[3] = {0, 0, ladoC}; GLfloat v2[3] = {ladoC, 0, ladoC}; GLfloat v3[3] = {ladoC, 0, 0}; GLfloat norm[3]; enum caras{ CARA_BASE, CARA_1, CARA_2, CARA_3, CARA_4, CARA_5, VACIO }; bool mov_cara[5] = {false,false,false,false,false}; GLfloat caraAngulo[5] = {90,90,90,90,30};//{90,90,90,90,90}; #define BUFSIZE 512 GLuint selectBuf[BUFSIZE]; GLfloat color_reflejo[4] = {0.8f, 0.8f, 0.8f, 0.65}; GLfloat vectorNorm[5][3] = { {0,1,0}, {0,1,0}, {0,1,0}, {0,1,0}, {0,1,0} }; ////////////////////////////////////////////////////////////////////////////////////////////////// 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} }; #define GL_PI 3.1415f //GLfloat xRot = 0.45f; //GLfloat yRot = 0.35f; GLboolean bCull = glIsEnabled(GL_CULL_FACE); GLboolean bDepth = true; //glIsEnabled(GL_DEPTH_TEST); GLboolean bOutline = (GLboolean)false; GLenum shademode = GL_FLAT; //GL_FLAT; GL_SMOOTH GLfloat ex = 0.0f; GLfloat ey = 0.0f; GLfloat ez = 120.0f; GLfloat delta = 0.05f; GLfloat deltaR = 0.02f; GLfloat pos_luz[4] = { 0., 25., 0, 1. }; GLfloat piso[4] = { 0. , 1., 0., ladoC/2 }; GLfloat MatrizSombra[16]; #define checkImageWidth 256 #define checkImageHeight 256 #define subImageWidth 64 #define subImageHeight 64 static GLuint texName[2]; static GLubyte checkImage[checkImageHeight][checkImageWidth][4]; static GLubyte subImage[subImageHeight][subImageWidth][4]; #define ALFA 0.5 #define ALFA1 0.75 ///////////////////////////////////////////////////////////////////// GLfloat texpts[2][2][2] = {{{0.0, 0.0}, {0.0, 2.0}}, {{3.0, 0.0}, {3.0, 2.0}}}; GLfloat ctrlpoints[4][4][3] = { { {-1.5, -1.5, 4.0}, {-0.5, -1.5, 2.0}, {0.5, -1.5, -1.0}, {1.5, -1.5, 2.0}}, { {-1.5, -0.5, 1.0}, {-0.5, -0.5, 3.0}, {0.5, -0.5, 0.0}, {1.5, -0.5, -1.0}}, { {-1.5, 0.5, 4.0}, {-0.5, 0.5, 0.0}, {0.5, 0.5, 3.0}, {1.5, 0.5, 4.0}}, { {-1.5, 1.5, -2.0}, {-0.5, 1.5, -2.0}, {0.5, 1.5, 0.0}, {1.5, 1.5, -1.0}} }; #define escala_curva 4.0f //////////////////////////////////////////////////////////////////// GLfloat mat_diffuse[] = { 0.8, 0.5, 0.1, 1.0 }; GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat high_shininess[] = { /*100.0*/ /*128*/ 70}; GLfloat ambientLight[] = { 0.1f, 0.1f, 0.1f, 1.0f }, diffuseLight[] = { 0.9f, 0.9f, 0.9f, 1.0f }, lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 }, specular[] = { 1.0f, 1.0f, 1.0f, 1.0f }, specref[] = { 1.0f, 1.0f, 1.0f, 1.0f }; ///////////////////////////////////////////////////////////////////// void normalize(GLfloat v[3]) { GLfloat d = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); if (d == 0.0) { exit(100); return; } // wrong input exit v[0] /= d; v[1] /= d; v[2] /= d; } void normcrossprod(GLfloat v1[3], GLfloat v2[3], GLfloat out[3]) { out[0] = v1[1] * v2[2] - v1[2] * v2[1]; out[1] = v1[2] * v2[0] - v1[0] * v2[2]; out[2] = v1[0] * v2[1] - v1[1] * v2[0]; normalize(out); } void gltGetNormalVector(GLfloat v0[3], GLfloat //build normal 'norm' v1[3], GLfloat v2[3], GLfloat norm[3]) // by the triangle vrtices { GLfloat d1[3], d2[3]; for (int j = 0; j < 3; j++) { d1[j] = v0[j] - v1[j]; d2[j] = v1[j] - v2[j]; } normcrossprod(d1, d2, norm); } //////////////////////////////////////////////////////////////////// void makeCheckImages(void) { int i, j, c; for (i = 0; i < checkImageHeight; i++) { for (j = 0; j < checkImageWidth; j++) { c = ((((i & 0x8) == 0) ^ ((j & 0x8)) == 0)) * 255; checkImage[i][j][0] = (GLubyte)c; checkImage[i][j][1] = (GLubyte)c; checkImage[i][j][2] = (GLubyte)c; checkImage[i][j][3] = (GLubyte)255; } } for (i = 0; i < subImageHeight; i++) { for (j = 0; j < subImageWidth; j++) { c = ((((i & 0x4) == 0) ^ ((j & 0x4)) == 0)) * 255; subImage[i][j][0] = (GLubyte)c; subImage[i][j][1] = (GLubyte)0; subImage[i][j][2] = (GLubyte)0; subImage[i][j][3] = (GLubyte)255; } } } void cuadrado() { gltGetNormalVector(v0,v1,v2,norm); glBegin(GL_QUADS); glNormal3fv(norm); glVertex3fv(v0); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); glEnd(); } void cara1(){ /*cara 1*/ glPushMatrix(); glPushName(CARA_1); glColor3fv(colors[1]); glTranslatef(ladoC, 0, 0); glRotatef(caraAngulo[0], 0, 0, 1); cuadrado(); glLoadName(VACIO); glPopMatrix(); } void cara2(){ /*cara 2*/ glPushMatrix(); glPushName(CARA_2); glColor3fv(colors[1]); glTranslatef(0, 0, ladoC); glRotatef(-caraAngulo[1], 1, 0, 0); cuadrado(); glLoadName(VACIO); glPopMatrix(); } void cubo(){ //glRotatef(180, 0, 0, 1); glPushMatrix(); glTranslatef(-ladoC/2, -ladoC/2, -ladoC/2); /*base*/ /*glPushName(CARA_BASE); glLoadName(VACIO);*/ //glColor3fv(colors[1]); //cuadrado(); /*cara 1*/ cara1(); /*glPushMatrix(); glPushName(CARA_1); glColor3fv(colors[1]); glTranslatef(ladoC, 0, 0); glRotatef(caraAngulo[0], 0, 0, 1); cuadrado(); glLoadName(VACIO); glPopMatrix();*/ /*cara 2*/ cara2(); /*glPushMatrix(); glPushName(CARA_2); glColor3fv(colors[1]); glTranslatef(0, 0, ladoC); glRotatef(-caraAngulo[1], 1, 0, 0); cuadrado(); glLoadName(VACIO); glPopMatrix();*/ /*cara 3*/ glPushMatrix(); glPushName(CARA_3); glColor3fv(colors[1]); glRotatef(-90, 0, 1, 0); glRotatef(-caraAngulo[2], 1, 0, 0); cuadrado(); glLoadName(VACIO); glPopMatrix(); /*cara 4*/ glPushMatrix(); glPushName(CARA_4); glColor3fv(colors[1]); glRotatef(90, 0, 1, 0); glRotatef(caraAngulo[3], 0, 0, 1); cuadrado(); glLoadName(VACIO); /*cara 5*/ glPushMatrix(); glPushName(CARA_5); glColor3fv(colors[1]); glTranslatef(ladoC, 0, 0); glRotatef(caraAngulo[4], 0, 0, 1); cuadrado(); glLoadName(VACIO); glPopMatrix(); glPopMatrix(); glPopMatrix(); } void curva(){ glPushMatrix(); glScalef(escala_curva, escala_curva, escala_curva); glEnable(GL_TEXTURE_2D); glColor3f(1.0, 1.0, 1.0); glEvalMesh2(GL_FILL, 0, 20, 0, 20); glDisable(GL_TEXTURE_2D); glPopMatrix(); } void curva_sin_color(){ glPushMatrix(); glScalef(escala_curva, escala_curva, escala_curva); glEnable(GL_TEXTURE_2D); glColor4f(0.0, 0.0, 0.0, 0.5); glEvalMesh2(GL_FILL, 0, 20, 0, 20); glDisable(GL_TEXTURE_2D); glPopMatrix(); } ////////////////////////////////////////////////////////////////////////////////////////////////////// void gltMakeShadowMatrix(GLfloat vPlaneEquation[], GLfloat vLightPos[], GLfloat destMat[]) { GLfloat dot; // Dot product of plane and light position dot = vPlaneEquation[0] * vLightPos[0] + vPlaneEquation[1] * vLightPos[1] + vPlaneEquation[2] * vLightPos[2] + vPlaneEquation[3] * vLightPos[3]; // Now do the projection // First column destMat[0] = dot - vLightPos[0] * vPlaneEquation[0]; destMat[4] = 0.0f - vLightPos[0] * vPlaneEquation[1]; destMat[8] = 0.0f - vLightPos[0] * vPlaneEquation[2]; destMat[12] = 0.0f - vLightPos[0] * vPlaneEquation[3]; // Second column destMat[1] = 0.0f - vLightPos[1] * vPlaneEquation[0]; destMat[5] = dot - vLightPos[1] * vPlaneEquation[1]; destMat[9] = 0.0f - vLightPos[1] * vPlaneEquation[2]; destMat[13] = 0.0f - vLightPos[1] * vPlaneEquation[3]; // Third Column destMat[2] = 0.0f - vLightPos[2] * vPlaneEquation[0]; destMat[6] = 0.0f - vLightPos[2] * vPlaneEquation[1]; destMat[10] = dot - vLightPos[2] * vPlaneEquation[2]; destMat[14] = 0.0f - vLightPos[2] * vPlaneEquation[3]; // Fourth Column destMat[3] = 0.0f - vLightPos[3] * vPlaneEquation[0]; destMat[7] = 0.0f - vLightPos[3] * vPlaneEquation[1]; destMat[11] = 0.0f - vLightPos[3] * vPlaneEquation[2]; destMat[15] = dot - vLightPos[3] * vPlaneEquation[3]; } void SetupRC() { // Black background glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // glShadeModel(GL_FLAT); glEnable(GL_DEPTH_TEST); glMap2f(GL_MAP2_TEXTURE_COORD_2, 0, 1, 2, 2, 0, 1, 4, 2, &texpts[0][0][0]); glEnable(GL_MAP2_TEXTURE_COORD_2); glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &ctrlpoints[0][0][0]); glEnable(GL_MAP2_VERTEX_3); glEnable(GL_AUTO_NORMAL); glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0); // makeCheckImages(); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glGenTextures(2, texName); glBindTexture(GL_TEXTURE_2D, texName[0]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage); glTexSubImage2D(GL_TEXTURE_2D, 0, 48, 170, subImageWidth, subImageHeight, GL_RGBA, GL_UNSIGNED_BYTE, subImage); glTexSubImage2D(GL_TEXTURE_2D, 0, 48, 48, subImageWidth, subImageHeight, GL_RGBA, GL_UNSIGNED_BYTE, subImage); glTexSubImage2D(GL_TEXTURE_2D, 0, 170, 48, subImageWidth, subImageHeight, GL_RGBA, GL_UNSIGNED_BYTE, subImage); glTexSubImage2D(GL_TEXTURE_2D, 0, 170, 170, subImageWidth, subImageHeight, GL_RGBA, GL_UNSIGNED_BYTE, subImage); // Set color shading model to flat glShadeModel(shademode); glDisable(GL_DEPTH_TEST); // // Set up and enable light 0 glFrontFace(GL_CCW); glEnable(GL_LIGHTING); glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); GLfloat att[] = { 1. }; glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight); glLightfv(GL_LIGHT0, GL_SPECULAR, specular); //glLightfv(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, att); //comentado por prof //glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); //glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view); glEnable(GL_LIGHT0); //glLightfv(GL_LIGHT0, GL_POSITION, LUZ); //<- se llama en luz() // Enable color tracking glEnable(GL_COLOR_MATERIAL); // Front material ambient and diffuse colors track glColor glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); // All materials hereafter have full specular reflectivity // with a high shine glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess); // Clockwise-wound polygons are front facing; this is reversed // because we are using triangle fans glFrontFace(GL_CW); } ////////////////////////////////////////////////////////////////////////////////////////////////// void foco() { glColor4f(1.0f, 1.0f, 0.f, 1.0f - ALFA); glLightfv(GL_LIGHT0, GL_POSITION, pos_luz); glPushMatrix(); glTranslatef(pos_luz[0], pos_luz[1], pos_luz[2] ); glutSolidSphere(1, 20, 20); glPopMatrix(); } #define NUMLIN 10 void lineas_piso() { glColor3f(1., 1., 1.); glBegin(GL_LINES); GLfloat n = ladoC/NUMLIN; for (int i = 0; i < NUMLIN; i++) { glVertex3f(ladoC - i * n, 0, 0); glVertex3f(ladoC - i * n, 0, ladoC); glVertex3f(0, 0, ladoC - i * n); glVertex3f(ladoC, 0, ladoC - i * n); } glEnd(); } void Piso() { glPushMatrix(); glPushName(CARA_BASE); glTranslatef(-ladoC/2, -ladoC/2, -ladoC/2); glColor4fv(color_reflejo); cuadrado(); glLoadName(VACIO); glDisable(GL_DEPTH_TEST); lineas_piso(); if (bDepth) glEnable(GL_DEPTH_TEST); glPopMatrix(); } void Objetos() { glPushMatrix(); //cubo(); curva(); glPopMatrix(); } void Objetos_sin_color() { glPushMatrix(); curva_sin_color(); glPopMatrix(); } //////////////////////////////////////////////////////////////////////////// void SombradeMiMundo(GLfloat datosdeLuz[], GLfloat datosdePlano[]) { glColor4f(0.4, 0.4, 0.4, ALFA); glPushMatrix(); gltMakeShadowMatrix(datosdePlano, datosdeLuz, MatrizSombra); glMultMatrixf(MatrizSombra); Objetos_sin_color(); glPopMatrix(); } ///////////////////////////////////////////////////////////////////////////////// void Stencil_config() { // turning off writing to the color buffer and depth buffer so we only // write to stencil buffer glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); glDepthMask(GL_FALSE); //// enable stencil buffer //glEnable(GL_STENCIL_TEST); // write a one to the stencil buffer everywhere we are about to draw glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF); // this is to always pass a one to the stencil buffer where we draw glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); // render the plane which the shadow will be on // color and depth buffer are disabled, only the stencil buffer // will be modified //DrawFloor(0,0,0); Piso(); // turn the color and depth buffers back on glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); glDepthMask(GL_TRUE); // until stencil test is diabled, only write to areas where the // stencil buffer has a one. This is to draw the shadow only on // the floor. glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF); // don't modify the contents of the stencil buffer glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); } void Stencil_config_cara1() { // turning off writing to the color buffer and depth buffer so we only // write to stencil buffer glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); glDepthMask(GL_FALSE); //// enable stencil buffer //glEnable(GL_STENCIL_TEST); // write a one to the stencil buffer everywhere we are about to draw glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF); // this is to always pass a one to the stencil buffer where we draw glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); // render the plane which the shadow will be on // color and depth buffer are disabled, only the stencil buffer // will be modified //DrawFloor(0,0,0); glPushMatrix(); glTranslatef(-ladoC/2, -ladoC/2, -ladoC/2); cara1(); glPopMatrix(); // turn the color and depth buffers back on glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); glDepthMask(GL_TRUE); // until stencil test is diabled, only write to areas where the // stencil buffer has a one. This is to draw the shadow only on // the floor. glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF); // don't modify the contents of the stencil buffer glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); } ///////////////////////////////////////////////////////////////////////////// // Called to draw scene void RenderScene(GLint mode) { glMatrixMode(GL_PROJECTION); if (mode == GL_RENDER) glLoadIdentity(); glInitNames(); //inicializar pila de nombres para seleccion //glOrtho (-100.0, 100.0, -100, 100, -270.0, 270.0); //glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0); gluPerspective(60.0, 1.0, 1.5, 500.0); glMatrixMode(GL_MODELVIEW); // Reset coordinate system glLoadIdentity(); gluLookAt(ex, ey, ez, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); // Clear the window and the depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // 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_BACK, GL_LINE); else glPolygonMode(GL_BACK, GL_FILL); // Save matrix state and do the rotation glPushMatrix(); /*mundo virtual REFLEJADO*/ // enable stencil buffer glEnable(GL_STENCIL_TEST); Stencil_config(); glFrontFace(GL_CCW); glPushMatrix(); glTranslatef(0.0f, -piso[3]/*ALTURA_PISO*/, 0.0f); glScalef(1.0f, -1.0f, 1.0f); glTranslatef(0.0f, piso[3]/*- ALTURA_PISO*/, 0.0f); glEnable(GL_LIGHTING); Objetos(); cubo(); glDisable(GL_LIGHTING); foco(); glPopMatrix(); glFrontFace(GL_CW); // enable stencil buffer glDisable(GL_STENCIL_TEST); /*FIN: mundo REFLEJADO*/ /*mundo virtual*/ foco(); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); Piso(); //cubo(); glDisable(GL_BLEND); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // enable stencil buffer glEnable(GL_STENCIL_TEST); Stencil_config(); glDisable(GL_DEPTH_TEST); SombradeMiMundo(pos_luz, piso); if (bDepth) glEnable(GL_DEPTH_TEST); glDisable(GL_BLEND); glDisable(GL_STENCIL_TEST); /*glClear(GL_STENCIL_BUFFER_BIT); glPushMatrix(); glEnable(GL_STENCIL_TEST); Stencil_config_cara1(); GLfloat piso2[4] = { 0, 1, 0, ladoC/2 }; SombradeMiMundo(pos_luz, piso2); if (bDepth) glEnable(GL_DEPTH_TEST); glDisable(GL_BLEND); glDisable(GL_STENCIL_TEST);*/ glDisable(GL_STENCIL_TEST); glPopMatrix(); glEnable(GL_LIGHTING); cubo(); Objetos(); glDisable(GL_LIGHTING); glPopMatrix(); // Flush drawing commands //glFlush(); glutSwapBuffers(); } void display(void) //<- envoltura a RenderScene() para seleccion { glClear(GL_COLOR_BUFFER_BIT); RenderScene(GL_RENDER); } // Called by GLUT library when the window has changed size void ChangeSize(GLsizei w, GLsizei h) { // Set Viewport to window dimensions glViewport(0, 0, (GLsizei)w, (GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); //glOrtho (-100.0, 100.0, -100, 100, -270.0, 270.0); //glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0); gluPerspective(60.0, 1.0, 1.5, 500.0); glMatrixMode (GL_MODELVIEW); } //////////////////////////////////////////////////////////////////////////////////////////////// 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_PAGE_UP){ pos_luz[1] += delta*10; } if(key == GLUT_KEY_PAGE_DOWN){ pos_luz[1] -= delta*10; } //Move camera in a straight vertical line on y axis if(key == GLUT_KEY_HOME) { ey += delta*20; } //Move camera in a straight vertical line on y axis if(key == GLUT_KEY_END) { ey -= delta*20; } 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); } if(key == GLUT_KEY_F5){ pos_luz[2] += (delta*30); } if(key == GLUT_KEY_F6){ pos_luz[2] -= (delta*30); } if(key == GLUT_KEY_F7){ pos_luz[0] += (delta*30); } if(key == GLUT_KEY_F8){ pos_luz[0] -= (delta*30); } if(key == GLUT_KEY_F9){ for (int i=0; i<5; i++){ caraAngulo[i] = mov_cara[i]? (caraAngulo[i] + deltaMov) : caraAngulo[i]; } } if(key == GLUT_KEY_F10){ for (int i=0; i<5; i++){ caraAngulo[i] = mov_cara[i]? (caraAngulo[i] - deltaMov) : caraAngulo[i]; } } // Refresh the Window glutPostRedisplay(); // Refresh the Window glutPostRedisplay(); } ////////////////////////////////////////////////////////////////////////////// void anallizename(int name) { for (int i=0; i<5; i++){ mov_cara[i] = false; } switch(name){ case CARA_1: mov_cara[0] = true; break; case CARA_2: mov_cara[1] = true; break; case CARA_3: mov_cara[2] = true; break; case CARA_4: mov_cara[3] = true; break; case CARA_5: mov_cara[4] = true; break; } glutPostRedisplay(); } void processHits(GLint hits, GLuint buffer[]) { if (hits == 0) {/*sin toques*/ for (int i=0; i<5; i++){ mov_cara[i] = false; } return; } unsigned int i, j; GLuint names, *ptr; ptr = (GLuint *)buffer; for (i = 0; i < hits; i++) { /* for each hit */ names = *ptr; ptr += 3; for (j = 0; j < names; j++) /* for each name */ anallizename(*ptr++); } } /*seleccionar una cara del cubo*/ void pickCara_del_Cubo(int button, int state, int x, int y) { GLint hits; GLint viewport[4]; if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN) return; glGetIntegerv(GL_VIEWPORT, viewport); glSelectBuffer(BUFSIZE, selectBuf); (void)glRenderMode(GL_SELECT); glInitNames(); glPushName(0); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); /* create 5x5 pixel picking region near cursor location */ gluPickMatrix((GLdouble)x, (GLdouble)(viewport[3] - y), 5.0, 5.0, viewport); /*inicio: restaurar la matrix de proyeccion*/ RenderScene(GL_SELECT); glMatrixMode(GL_PROJECTION); glPopMatrix(); /*FIN restaurar la matrix de proyeccion*/ glMatrixMode(GL_MODELVIEW); hits = glRenderMode(GL_RENDER); processHits(hits, selectBuf); glutPostRedisplay(); } ////////////////////////////////////////////////////////////////////////////// int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_STENCIL); glutInitWindowSize(200, 200); glutInitWindowPosition(100, 100); glutCreateWindow(argv[0]); glutDisplayFunc(display); glutReshapeFunc(ChangeSize); glutSpecialFunc(SpecialKeys); glutMouseFunc(pickCara_del_Cubo); SetupRC(); glutMainLoop(); return 0; }