#include "stdafx.h" #define GL_PI 3.1415f GLfloat xRot = 0.45f; GLfloat yRot = 0.35f; GLboolean bCull = glIsEnabled(GL_CULL_FACE); GLboolean bDepth = glIsEnabled(GL_DEPTH_TEST); GLboolean bOutline = (GLboolean)true; GLenum shademode = GL_FLAT; GLfloat ex = 0.0f; GLfloat ey = 100.0f; GLfloat ez = -600.0f; GLfloat delta = 0.01f; GLfloat deltaR = 0.01f; GLfloat luz[4] = { 0., 250., 100., 1. }; GLfloat PISO[4] = { 0., 1., 0., 150. }; /////////////////////////////////////////////////////////// GLfloat Mat_Sombra[16]; 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); // Set color shading model to flat glShadeModel(shademode); // // Clockwise-wound polygons are front facing; this is reversed // because we are using triangle fans glFrontFace(GL_CW); } //Comienza codigo del cubo/////////////////////////////////////////////////////////////////////////777 GLfloat vertices[][3] = { { -1.0,-1.0,-1.0 },{ 1.0,-1.0,-1.0 }, { 1.0,1.0,-1.0 },{ -1.0,1.0,-1.0 },{ -1.0,-1.0,1.0 }, { 1.0,-1.0,1.0 },{ 1.0,1.0,1.0 },{ -1.0,1.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 } }; GLfloat ALFA = 0.75; void cuadrado_unitario() { glBegin(GL_POLYGON); glVertex2f(-1., -1.); glVertex2f(-1., 1.); glVertex2f(1., 1.); glVertex2f(1., -1.); glEnd(); } void cubo_sincolor(void) { glPushMatrix(); glTranslatef(0, 0, 1); cuadrado_unitario(); glPopMatrix(); //segunda cara del cubo glPushMatrix(); glRotatef(90, 1., 0., 0.); glTranslatef(0, 0, 1); cuadrado_unitario(); glPopMatrix(); //Tercera cara de cubo glPushMatrix(); glRotatef(180, 1., 0., 0.); glTranslatef(0, 0, 1); cuadrado_unitario(); glPopMatrix(); //Cuarta cara de cubo glPushMatrix(); glRotatef(-90, 1., 0., 0.); glTranslatef(0, 0, 1); cuadrado_unitario(); glPopMatrix(); //Quinta cara de cubo glPushMatrix(); glRotatef(90, 0., 1., 0.); glTranslatef(0, 0, 1); cuadrado_unitario(); glPopMatrix(); //Sexta cara de cubo glPushMatrix(); glRotatef(-90, 0., 1., 0.); glTranslatef(0, 0, 1); cuadrado_unitario(); glPopMatrix(); } void colorcube1(void) { glPushMatrix(); glTranslatef(0, 0, 1); glColor4f(1.0, 0.0, 0.0, ALFA /*colors[1]*/);//Rojo cuadrado_unitario(); glPopMatrix(); //segunda cara del cubo glPushMatrix(); glRotatef(90, 1., 0., 0.); glTranslatef(0, 0, 1); glColor4f(0.0, 1.0, 1.0 , ALFA/*colors[7]*/);//turquesa cuadrado_unitario(); glPopMatrix(); //Tercera cara de cubo glPushMatrix(); glRotatef(180, 1., 0., 0.); glTranslatef(0, 0, 1); glColor4f(1.0, 0.0, 1.0, ALFA /*colors[5]*/);//magenta cuadrado_unitario(); glPopMatrix(); //Cuarta cara de cubo glPushMatrix(); glRotatef(-90, 1., 0., 0.); glTranslatef(0, 0, 1); glColor4f(0.0, 0.0, 1.0, ALFA /*colors[4]*/);//azul cuadrado_unitario(); glPopMatrix(); //Quinta cara de cubo glPushMatrix(); glRotatef(90, 0., 1., 0.); glTranslatef(0, 0, 1); glColor4f(1.0, 1.0, 0.0, ALFA/* colors[2]*/);//azul cuadrado_unitario(); glPopMatrix(); //Sexta cara de cubo glPushMatrix(); glRotatef(-90, 0., 1., 0.); glTranslatef(0, 0, 1); glColor4f(0.0, 1.0, 0.0, ALFA /*colors[3]*/);//verde cuadrado_unitario(); glPopMatrix(); } static GLfloat theta[] = { 0.0,0.0,0.0 }; static GLint axis = 2; //Termina codigo de cubo////////////////////////////////////////////////////////////////////////////////////////////// // 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, 1500.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_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() { glColor4f(0.7, 0.7, 0.7, ALFA); glPushMatrix(); glTranslatef(0., -PISO[3], -100.); glScalef(250., 250., 250.); glRotatef(270., 1, 0, 0); cuadrado_unitario(); glPopMatrix(); } void foco() { glColor4f(1, 1, 0., ALFA); glPushMatrix(); glTranslatef(luz[0], luz[1], luz[2]); glScalef(10., 10., 10.); glutSolidSphere(1, 20, 20); glPopMatrix(); } void Objetos_de_mundo() { glPushMatrix(); glScalef(50.f, 50.f, 50.f); colorcube1(); glPopMatrix(); } void Objetos_de_mundo_sincolor() { glPushMatrix(); glScalef(50.f, 50.f, 50.f); cubo_sincolor(); glPopMatrix(); } void SombradeMiMundo() { glColor3f(0.4, 0.4, 0.4); glPushMatrix(); gltMakeShadowMatrix(PISO, luz, Mat_Sombra); glMultMatrixf(Mat_Sombra); //El fragmento glPush ... glPop representa "Objetos_de_MiMundo() sin color" Objetos_de_mundo_sincolor(); glPopMatrix(); } void RenderScene(void) { // Reset coordinate system glLoadIdentity(); gluLookAt(ex, ey, ez, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);//giramos camara alrededor del objeto //gluLookAt (0.0, 0.0, -100.0, 0.0, 0.0, -100.0, 0.0, 1.0, 0.0); GLfloat x, y, angle; // Storage for coordinates and angles int iPivot = 1; // Used to flag alternating colors // Clear the window and the depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//DEpth_Buffer_Bit no funiciona si en main no esta glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB| GLUT_DEPTH); Depth definida en main // 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); //inicio reflejo glFrontFace(GL_CCW); glPushMatrix(); glTranslatef(0, -PISO[3], 0); glScalef(1, -1, 1); glTranslatef(0, PISO[3], 0); Objetos_de_mundo(); foco(); glPopMatrix(); glFrontFace(GL_CW); //fin reflejo glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); piso(); foco(); glDisable(GL_BLEND); glDisable(GL_DEPTH_TEST); SombradeMiMundo(); if (bDepth) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST); Objetos_de_mundo(); // Flush drawing commands glFlush(); // glutSwapBuffers(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(/*GLUT_DOUBLE*/GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(200, 200); glutInitWindowPosition(100, 100); glutCreateWindow(argv[0]); glutDisplayFunc(RenderScene); //glutDisplayFunc(display); glutReshapeFunc(ChangeSize); glutSpecialFunc(SpecialKeys);//responsable de cualquier tecla funcional SetupRC(); glutMainLoop(); return 0; }