// ProyectoFinal.cpp: define el punto de entrada de la aplicación de consola. // #include "stdafx.h" #include #include "glut.h" #include #define GL_PI 3.1415f double rotllantas=0; double rotllantastraceras=0; double aumento=2; double rotPiramide= 0; double posLuz=0; double rotCubo=0; double rotCuboAumento=0; bool banderaCubo = false; GLfloat verticesPiramide [][3] = { //base {-4,0,-4},{-4,0,4},{4,0,4},{4,0,-4}, //cara 1 {0,4,0},{4,0,-4},{4,0,4}, //cara2 {0,4,0},{4,0,4},{-4,0,4}, //cara3 {0,4,0},{-4,0,4},{-4,0,-4}, //cara4 {0,4,0},{-4,0,-4},{4,0,-4}}; GLfloat xRot = 0.45f; GLfloat yRot = 0.35f, Rot_Torus_vect[] = {0.0f, 1.0f, 0.0f}, Translate_Sph_only[] = {1.0f, 0.5f, 0.0f}, Translate_Tor_only[] = {0.0f, 0.5f, 0.0f}, trans[] = {0.0f, 0.0f, -1.0f}, ktrans = 0.0f, deltatrans = 0.1f, jetrot = 0.0f, deljetrot = 0.1f; GLfloat datos_luz[4] = { 0., 70., -40., 1. }; GLboolean bCull = glIsEnabled(GL_CULL_FACE); GLboolean bDepth =! glIsEnabled(GL_DEPTH_TEST); //variables para configuracion luz Phong GLboolean bOutline = (GLboolean)true; 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 }; GLfloat local_view[] = { 0.0 }; 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 }; GLfloat posicion1=30, posicion2=-30, pos=0; 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); } GLenum shademode = GL_FLAT; GLfloat ex = 0.0f; GLfloat ey = 50.0f; GLfloat ez = -120.0f; GLfloat cx=0.0f; GLfloat cy=0.0f; GLfloat cz=0.0f; GLfloat delta = 0.01f; GLfloat deltaR = 0.01f; 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 } }; ///////// datos del plano de piso y matriz de sombra ////////// GLfloat datos_piso[4] = { 0., 1., 0., 150. }; GLfloat Mat_Sombra[16]; GLfloat ALFA = 0.75f; /////////////////////////para texturas://////////////////////////////// #define checkImageWidth 256 #define checkImageHeight 256 #define subImageWidth 64 #define subImageHeight 64 static GLubyte checkImage[checkImageHeight][checkImageWidth][4]; static GLubyte otherImage[checkImageHeight][checkImageWidth][4]; static GLubyte subImage[subImageHeight][subImageWidth][4]; static GLuint texName[2]; GLfloat ctrlpoints[4][4][3] = { { {-1.5, -1.5, 4.0}, {-0.5, -1.5, 2.0}, {0.5, -1.5, -2.0}, {1.5, -1.5, 4.0}}, { {-1.5, -0.5, 1.0}, {-0.5, -2.5, 0.0}, {2.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, 0.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, 2.0}} }; GLfloat texpts[2][2][2] = { {{0.0, 0.0}, {0.0, 2.0}}, {{3.0, 0.0}, {3.0, 2.0}} }; //////////// para seleccionar una cara destinguida del cubo //////// #define BUFSIZE 512 GLuint selectBuf[BUFSIZE]; #define CARA_DESTINGUIDA 111 GLboolean CARA_DESTINGUIDA_TOCADA; //revise uso de esta variable a continuacion! GLfloat STARTcolor_de_cara[] = { 0.0f, 1.0f, 1.0f }, color_de_cara_DESTINGUIDA[3]; void initColors() { rotCubo=0; rotCuboAumento=0; for (int i = 0; i<3; i++) { color_de_cara_DESTINGUIDA[i] = STARTcolor_de_cara[i]; } } ///////// fin "para seleccionar" /////////////////// 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 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 SetupRCCurvaText(){ // 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); 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, 11, 170, 170, subImageWidth, subImageHeight, GL_RGBA, GL_UNSIGNED_BYTE, subImage); //Segunda textura. glBindTexture(GL_TEXTURE_2D, texName[1]); 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); //setup evaluator 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); } void SetupRC() { // Black background glClearColor(0.7f, 0.5f, 1.0f, 0.8f); //para seleccion initColors(); // 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); 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); glBindTexture(GL_TEXTURE_2D, texName[1]); 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); //setup evaluator 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); //Congiguración luz 0 //glEnable(GL_LIGHTING); glEnable(GL_CULL_FACE); //glEnable(GL_DEPTH_TEST); glPolygonMode(GL_BACK, GL_FILL); // 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. }; //////////////////////////////// DESDE AQUI EMPIEZA CODIGO LUZ DE PHONG //////////////////////////////////////////// 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, datos_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); } 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 } }; void cuadrado() { glBegin(GL_POLYGON); glNormal3f(0., 0., -1. ); glVertex2f(-1., -1.); glVertex2f(-1., 1.); glVertex2f(1., 1.); glVertex2f(1., -1.); glEnd(); } void front(){ glColor3fv(colors[5]); glPushMatrix(); //glTranslatef(45,-20,0); glTranslatef(pos,-70,0); glScalef(0.01,0.5,1); glutSolidCube(60); glPopMatrix(); glTranslatef(0,-70,0); glColor3f(0,0,0); glPushMatrix(); glTranslatef(pos+32,8,0); glRotatef(45,0,0,1); glScalef(0.01,0.6,1); glutWireCube(60); glPopMatrix(); } void back(){ glColor3fv(colors[5]); glPushMatrix(); glTranslatef(pos-45,-20,0); glScalef(0.01,0.5,1); glutSolidCube(60); glPopMatrix(); glColor3f(0,0,0); glPushMatrix(); glTranslatef(pos-40,8,0); glRotatef(-21,0,0,1); glScalef(0.01,0.45,1); glutWireCube(60); glPopMatrix(); } void top(){ glColor3fv(colors[5]); glPushMatrix(); glTranslatef (pos-8,20,0); glScalef(.9,0.01,1); glutSolidCube(60); glPopMatrix(); } void bottom(){ glColor3fv(colors[5]); glPushMatrix(); glTranslatef (pos,-35,0); glScalef(1.5,0.01,1); glutSolidCube(60); glPopMatrix(); } void left(){ glColor3fv(colors[5]); glPushMatrix(); glTranslatef(pos,-20, 30); glScalef(1.5,0.5,0.01); glutSolidCube(60); glPopMatrix(); } void rigth(){ glColor3fv(colors[5]); glPushMatrix(); glTranslatef(pos,-20,-30); glScalef(1.5,0.5,0.01); glutSolidCube(60); glPopMatrix(); } void wheel(int x, int y, int z){ glColor3f(0,0,0); glPushMatrix(); glRotatef(0., 90., 1., 0.); glTranslatef(x, -30, z); glScaled(10,10,10); glRotated(0+rotllantas,0,0,1); cuadrado(); glPopMatrix(); } void border(){ glColor3f(0,0,0); glPushMatrix(); glTranslatef(pos-7,-8.5,-29); glScalef(0.01,0.9,0.01); glutWireCube(60); glPopMatrix(); //otro lateral glColor3f(0,0,0); glPushMatrix(); glTranslatef(pos-7,-8.5,29); glScalef(0.01,0.9,0.01); glutWireCube(60); glPopMatrix(); } void automovil(GLenum mode){ glShadeModel(GL_FLAT); glPushMatrix(); front(); back(); top(); bottom(); border(); wheel(posicion2,-30,31); glPushMatrix(); glRotated(rotllantastraceras,0,0,1); wheel(posicion2,-30,-31); glPopMatrix(); glPushMatrix(); glRotated(rotllantastraceras,0,0,1); wheel(posicion1,30,31); glPopMatrix(); wheel(posicion1,30,-31); left(); rigth(); glPopMatrix(); } void automovil_sin_coloor(){ glPushMatrix(); glTranslatef(45,-20,0); glTranslatef(pos,-70,0); glScalef(0.01,0.5,1); glutSolidCube(60); glPopMatrix(); glTranslatef(0,-70,0); glPushMatrix(); glTranslatef(pos+32,8,0); glRotatef(45,0,0,1); glScalef(0.01,0.6,1); glutWireCube(60); glPopMatrix(); /////// glPushMatrix(); glTranslatef(pos-45,-20,0); glScalef(0.01,0.5,1); glutSolidCube(60); glPopMatrix(); glPushMatrix(); glTranslatef(pos-40,8,0); glRotatef(-21,0,0,1); glScalef(0.01,0.45,1); glutWireCube(60); glPopMatrix(); //////////// glPushMatrix(); glTranslatef (pos-8,20,0); glScalef(.9,0.01,1); glutSolidCube(60); glPopMatrix(); ////////// glPushMatrix(); glTranslatef (pos,-35,0); glScalef(1.5,0.01,1); glutSolidCube(60); glPopMatrix(); /////// glPushMatrix(); glTranslatef(pos,-20, 30); glScalef(1.5,0.5,0.01); glutSolidCube(60); glPopMatrix(); /////////////// glPushMatrix(); glTranslatef(pos,-20,-30); glScalef(1.5,0.5,0.01); glutSolidCube(60); glPopMatrix(); ///////////// glPushMatrix(); glTranslatef(pos-7,-8.5,-29); glScalef(0.01,0.9,0.01); glutWireCube(60); glPopMatrix(); ////////////////// glPushMatrix(); glTranslatef(pos-7,-8.5,29); glScalef(0.01,0.9,0.01); glutWireCube(60); glPopMatrix(); } //Partes sin color para automovil void front_sin_color(){ glPushMatrix(); glTranslatef(45,-20,0); glTranslatef(pos,-70,0); glScalef(0.01,0.5,1); glutSolidCube(60); glPopMatrix(); //vidrio glTranslatef(0,-70,0); glPushMatrix(); glTranslatef(pos+32,8,0); glRotatef(45,0,0,1); glScalef(0.01,0.6,1); glutWireCube(60); glPopMatrix(); } void back_sin_color(){ glPushMatrix(); glTranslatef(pos-45,-20,0); glScalef(0.01,0.5,1); glutSolidCube(60); glPopMatrix(); //vidrio glPushMatrix(); glTranslatef(pos-40,8,0); glRotatef(-21,0,0,1); glScalef(0.01,0.45,1); glutWireCube(60); glPopMatrix(); } void top_sin_color(){ glPushMatrix(); glTranslatef (pos-8,20,0); glScalef(.9,0.01,1); glutSolidCube(60); glPopMatrix(); } void bottom_sin_color(){ glPushMatrix(); glTranslatef (pos,-35,0); glScalef(1.5,0.01,1); glutSolidCube(60); glPopMatrix(); } void left_sin_color(){ glPushMatrix(); glTranslatef(pos,-20, 30); glScalef(1.5,0.5,0.01); glutSolidCube(60); glPopMatrix(); } void rigth_sin_color(){ glPushMatrix(); glTranslatef(pos,-20,-30); glScalef(1.5,0.5,0.01); glutSolidCube(60); glPopMatrix(); } void border_sin_color(){ glPushMatrix(); glTranslatef(pos-7,-8.5,-29); glScalef(0.01,0.9,0.01); glutWireCube(60); glPopMatrix(); //otro lateral glPushMatrix(); glTranslatef(pos-7,-8.5,29); glScalef(0.01,0.9,0.01); glutWireCube(60); glPopMatrix(); ////borde ventana } void automovil_sin_color(){ glShadeModel(GL_FLAT); glPushMatrix(); glScalef(30., 30., 30.); //top_sin_color(); front_sin_color(); //back_sin_color(); bottom_sin_color(); left_sin_color(); rigth_sin_color(); glPopMatrix(); } void rayas_en_cuadrado() { glColor4f(0., 0., 0.0, ALFA); glBegin(GL_LINES); int i, n = 15; for (i = 0; i <= n; i++) { glVertex2f(-1. + i*2. / n, -1.); glVertex2f(-1. + i*2. / n, 1.); glVertex2f(-1., -1. + i*2. / n); glVertex2f(1., -1. + i*2. / n); } glEnd(); } void cube2_sin_color(void) { glShadeModel(GL_FLAT); glPushMatrix(); glScalef(30., 30., 30.); glPushMatrix(); glTranslatef(0., 0., 1.); cuadrado(); glPopMatrix(); glPushMatrix(); glRotatef(90., 0., 1., 0.); glTranslatef(0., 0., 1.); cuadrado(); glPopMatrix(); glPushMatrix(); glRotatef(180., 0., 1., 0.); glTranslatef(0., 0., 1.); cuadrado(); glPopMatrix(); glPushMatrix(); glRotatef(270., 0., 1., 0.); glTranslatef(0., 0., 1.); cuadrado(); glPopMatrix(); glPushMatrix(); glRotatef(90., 1., 0., 0.); glTranslatef(0., 0., 1.); cuadrado(); glPopMatrix(); glPushMatrix(); glRotatef(-90., 1., 0., 0.); glTranslatef(0., 0., 1.); cuadrado(); glPopMatrix(); glPopMatrix(); } void SombradeMiMundo(){ glPushMatrix(); gltMakeShadowMatrix(datos_piso, datos_luz, Mat_Sombra); glMultMatrixf(Mat_Sombra); glColor4f(0.4, 0.4, 0.4, ALFA); automovil_sin_coloor(); glPopMatrix(); } void colorcube2(GLenum mode) { glShadeModel(GL_FLAT); glPushMatrix(); glScalef(30., 30., 30.); glPushMatrix(); glTranslatef(0., 0., 1.); /*glColor3fv(colors[2]); cuadrado();*/ /*inicio cara con textura*/ glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); glBindTexture(GL_TEXTURE_2D, texName[0]); glBegin(GL_QUADS); glTexCoord2f(0.0, 0.0); glVertex2f(-1., -1.); glTexCoord2f(0.0, 1.0); glVertex2f(-1., 1.); glTexCoord2f(1.0, 1.0); glVertex2f(1., 1.); glTexCoord2f(1.0, 0.0); glVertex2f(1., -1.); glEnd(); glDisable(GL_TEXTURE_2D); /*fin cara con textura*/ glPopMatrix(); // INICIO de construccion de CARA_DESTINGUIDA para seleccion glPushMatrix(); glRotated(rotCubo+rotCuboAumento,1,0,0); if (mode == GL_SELECT) glLoadName(CARA_DESTINGUIDA);//= glPopNAme(); + glPushName(CARA_DESTINGUIDA); glColor3fv(/*color_de_cara_DESTINGUIDA*/colors[2]); glRotatef(90., 0., 1., 0.); glTranslatef(0., 0., 1.); cuadrado(); if (mode == GL_SELECT) glLoadName(0); //= glPopNAme(); + glPushName(0); glPopMatrix(); // FIN de construccion de CARA_DESTINGUIDA glPushMatrix(); glColor3fv(colors[2]); glRotatef(180., 0., 1., 0.); glTranslatef(0., 0., 1.); cuadrado(); glPopMatrix(); glPushMatrix(); glColor3fv(colors[2]); glRotatef(270., 0., 1., 0.); glTranslatef(0., 0., 1.); cuadrado(); glPopMatrix(); glPushMatrix(); glColor3fv(colors[2]); glRotatef(90., 1., 0., 0.); glTranslatef(0., 0., 1.); cuadrado(); glPopMatrix(); glPushMatrix(); glColor3fv(colors[2]); glRotatef(-90., 1., 0., 0.); glTranslatef(0., 0., 1.); cuadrado(); glPopMatrix(); glPopMatrix(); } void CurvaText() { glEnable(GL_AUTO_NORMAL); glPushMatrix(); glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); glBindTexture(GL_TEXTURE_2D, texName[1]); glTranslatef(1.0, 0.0, 0.0); glScalef(30.0, -30.0, 30.0); glRotatef(85.0, 1.0, 1.0, 1.0); glEvalMesh2(GL_FILL, 0, 20, 0, 20); glDisable(GL_TEXTURE_2D); glPopMatrix(); glDisable(GL_AUTO_NORMAL); } void CurvaText_sinColor() { glPushMatrix(); glTranslatef(350.0, 0, 0.0); glScalef(30.0, -30.0, 30.0); glRotatef(85.0, 1.0, 1.0, 1.0); glEvalMesh2(GL_FILL, 0, 20, 0, 20); glPopMatrix(); } void bandera(){ glPushMatrix(); glTranslated(-70,240,180); glScalef(.5,.5,.5); glRotated(45,0,1,0); glRotated(-40,0,0,1); glTranslated(500,0,0); CurvaText(); glPopMatrix(); glColor3f(0,0.4,0); glPushMatrix(); glTranslatef(+100,0,0); glScalef(.2,3,.2); glutSolidCube(60); glPopMatrix(); } void semaforo(){ glPushMatrix(); glTranslated(100,100,-60); glScalef(6., 6., 6.); glColor3f(0,1,0); glutSolidSphere(1., 20., 20.); glPopMatrix(); glPushMatrix(); glTranslated(100,80,-60); glScalef(6., 6., 6.); glColor3f(1,1,0); glutSolidSphere(1., 20., 20.); glPopMatrix(); glPushMatrix(); glTranslated(100,60,-60); glScalef(6., 6., 6.); glColor3f(1,0,0); glutSolidSphere(1., 20., 20.); glPopMatrix(); } void cono(){ glNormal3f(0., 0., -1. ); glPushMatrix(); glTranslatef(2, -1, 0); glRotatef(-90,1,0,0); glRotatef(1,0,0,1); glColor3f(128, 0.5, 0); glutSolidCone(1.0,2.5,22.0,5.0); glPopMatrix(); } void conoSombra(){ glPushMatrix(); glTranslatef(0, -1, 0); glRotatef(-90,1,0,0); glRotatef(1,0,0,1); glutSolidCone(1.0,2.5,22.0,5.0); glPopMatrix(); } void piramide(){ glBegin(GL_QUADS); //vértices en 3d glColor3f(1,0,0);//rojo glVertex3fv(verticesPiramide[1]);//4 glColor3f(1,1,0);//amarillo glVertex3fv(verticesPiramide[2]);//3 glColor3f(0,0,1);//azul glVertex3fv(verticesPiramide[3]);//2 glColor3f(0,1,0);//verde glVertex3fv(verticesPiramide[4]);//1 glEnd(); //------------------caras de la pirámide //para las caras triangulares //se usará GL_TRIANGLES //--------Cara 4 amarillo con blanco glBegin(GL_TRIANGLES); //vértices en 3d glColor3f(1,1,1);//blanco glVertex3fv(verticesPiramide[5]); glColor3f(1,1,0); glVertex3fv(verticesPiramide[6]);//1 glVertex3fv(verticesPiramide[7]);//4 glEnd(); //--------Cara 1 rojo con blanco glBegin(GL_TRIANGLES); //vértices en 3d glColor3f(1,1,1);//blanco glVertex3fv(verticesPiramide[8]); glColor3f(1,0,0);//rojo glVertex3fv(verticesPiramide[9]); glVertex3fv(verticesPiramide[10]); glEnd(); //--------Cara 3 verde con blanco glBegin(GL_TRIANGLES); //vértices en 3d glColor3f(1,1,1);//blanco glVertex3fv(verticesPiramide[11]); glColor3f(0,1,0);//verde glVertex3fv(verticesPiramide[12]); glVertex3fv(verticesPiramide[13]); glEnd(); //-------Cara 2 azul con blanco glBegin(GL_TRIANGLES); //vértices en 3d glColor3f(1,1,1);//blanco glVertex3fv(verticesPiramide[14]); glColor3f(0,0,1);//azul glVertex3fv(verticesPiramide[15]); glVertex3fv(verticesPiramide[16]); glEnd(); } void piso() { glPushMatrix(); glTranslatef(0, -datos_piso[3], 0); glRotatef(270.f, 1.f, 0.f, 0.f); // voltea el piso al la cara invisible glScalef(350.f, 350.f, 350.f); //piso 3 veces mas ancho que cubo glColor4f(50.7f, .7f, 30.7f, ALFA); cuadrado(); //cuadro_sin_color(); glDisable(GL_DEPTH_TEST); rayas_en_cuadrado(); if (bDepth)glEnable(GL_DEPTH_TEST); glPopMatrix(); } void StencilConfig() {// step 1 // 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 piso();//DrawGround(); // 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); // end step 1 } void luz() { glColor4f(1.f, 1.f, 0.f, 1.f - ALFA); glPushMatrix(); glTranslatef(datos_luz[0], datos_luz[1], datos_luz[2]); glScalef(10., 10., 10.); glutSolidSphere(1., 20., 20.); glPopMatrix(); } void objetos_Mundo(GLenum mode){ //auto glPushMatrix(); glTranslated(-250,-30,30); automovil(mode); glPopMatrix(); //semaforo glPushMatrix(); glTranslated(190,-100,30); semaforo(); glPopMatrix(); //cono glPushMatrix(); glTranslated(-200,-120,80); glPushMatrix(); glScaled(10,20,10); cono(); glPopMatrix(); glPopMatrix(); glPushMatrix(); glTranslated(0,-110,110); glRotated(-90,0,1,0); colorcube2(mode); glPopMatrix(); } void objetos_Mundo2(GLenum mode){ //auto glPushMatrix(); glTranslated(-250,-30,30); automovil(mode); glPopMatrix(); //semaforo glPushMatrix(); glTranslated(190,-100,30); semaforo(); glPopMatrix(); //cono glPushMatrix(); glTranslated(-200,-120,80); glPushMatrix(); glScaled(10,20,10); cono(); glPopMatrix(); glPopMatrix(); } void RenderScene(GLenum mode) { glMatrixMode(GL_PROJECTION); if (mode == GL_RENDER) glLoadIdentity(); gluPerspective(60.0, 1.0, 1.5, 1500.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(ex, ey, ez, cx, cy, cz, 0.0, 1.0, 0.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // Turn culling on if flag is set if (bCull /*alterar con tecla F1*/) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE); // Enable depth testing if flag is set if (bDepth /*alterar con tecla F2*/) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST); // Draw the back side as a wireframe only, if flag is set if (bOutline /*alterar con tecla F3*/) glPolygonMode(GL_BACK, GL_LINE); else glPolygonMode(GL_BACK, GL_FILL); // Save matrix state and do the rotation glPushMatrix(); glRotatef(xRot, 1.0f, 0.0f, 0.0f); glRotatef(yRot, 0.0f, 1.0f, 0.0f); //para reflejo StencilConfig(); glFrontFace(GL_CCW); glPushMatrix(); glTranslatef(0, /*o '+'?*/-datos_piso[3], 0); glScalef(1, -1, 1); // geometry is mirrored by ground glTranslatef(0, /*o '-'*/datos_piso[3], 0); glEnable(GL_LIGHTING); //colorcube2(mode); //colorcube2(); //Draw_Objects(); objetos_Mundo2(mode); glPushMatrix(); glTranslated(200,-60,-30); bandera(); glPopMatrix(); glTranslated(-230,-140,-80); glScaled(10,30,10); glRotated(rotPiramide,0,1,0); piramide(); glPopMatrix(); glDisable(GL_LIGHTING); luz();//Draw_Light(); glPopMatrix(); glFrontFace(GL_CW); glDisable(GL_STENCIL_TEST); //fin "reflejo" luz(); //mezclar mundo reflejado con piso glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glDisable(GL_DEPTH_TEST); // para luchar con errores de redonde piso(); StencilConfig(); glDisable(GL_DEPTH_TEST); glPushMatrix(); //glTranslated(0,0,60); glTranslated(-250,-30,30); SombradeMiMundo(); glPopMatrix(); glDisable(GL_STENCIL_TEST); glDisable(GL_BLEND); //fin 'mezclar' nuevo con viejo if (bDepth) glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); //colorcube2(mode); //<-para seleccion; antes fue colorcube2(); //glTranslated(0,-30,0); objetos_Mundo(mode); glDisable(GL_LIGHTING); glPushMatrix(); glTranslated(200,-60,-30); bandera(); glPopMatrix(); glTranslated(-230,-140,-80); glScaled(10,30,10); glRotated(rotPiramide,0,1,0); piramide(); glPopMatrix(); glPopMatrix(); // Flush drawing commands glFlush();//glutSwapBuffers(); } //para la accion relacionada a seleccion: void anallizename(int name) { int i; if (name == CARA_DESTINGUIDA) { CARA_DESTINGUIDA_TOCADA = true; for (i = 0; i<3; i++) color_de_cara_DESTINGUIDA[i] += 0.25f*(1.0f - color_de_cara_DESTINGUIDA[i]); } glutPostRedisplay(); } /* procesar toques (seleccion)*/ void processHits(GLint hits, GLuint buffer[]) { if (hits == 0) {/*sin toques -> restore all colors*/ initColors(); return; } CARA_DESTINGUIDA_TOCADA = false; 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++); } if (!CARA_DESTINGUIDA_TOCADA) /*restore all colors*/ initColors(); } /*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); rotCuboAumento=50; 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(); } 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); /*para tomar bajo control la alteraciones de glMatrixMode, los llamados a continuacion se trasladaron a RenderScene */ //glMatrixMode(GL_PROJECTION); //glLoadIdentity(); //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); } if(key == GLUT_KEY_F5) //Rotate camera around origin in Oyx plane {dx=ex; dz=-ey; GLfloat s=sqrtf(ez*ez+ey*ey+ex*ex); ey-=delta*dx; ex-=delta*dx; GLfloat s1=sqrtf(ez*ez+ey*ey+ex*ex)/s; ey/=s1; ez/=s1; ex/=s1;} if(key == GLUT_KEY_F6) //Rotate camera around origin in Oyx plane {dx=-ex; dz= ey; GLfloat s=sqrtf(ey*ey+ez*ez+ex*ex); ey-=delta*dx; ez-=delta*dz; GLfloat s1=sqrtf(ey*ey+ez*ez+ex*ex)/s; ey/=s1; ez/=s1; ex/=s1;} if (key == GLUT_KEY_F7) {//increase height of light: LUZ[1] datos_luz[1] *= (1.0f + deltaR); } if (key == GLUT_KEY_F8) {//reduce height of light: LUZ[1] datos_luz[1] /= (1.0f + deltaR); } if (key == GLUT_KEY_F9) {//increase X of light: LUZ[0] datos_luz[0] += 100.*deltaR; } if (key == GLUT_KEY_F10) {//reduce X of light: LUZ[0] datos_luz[0] -= 100.*deltaR; } // Refresh the Window glutPostRedisplay(); } void ControlTeclado(unsigned char teclado,int x,int y ){ /* Según la tecla pulsada incremento una u otra variable de movimiento */ switch(teclado){ case 'w': rotllantas=rotllantas-10; glutPostRedisplay(); break; case 'd': posicion1=posicion1+aumento; posicion2=posicion2+aumento; pos=pos+aumento; rotllantas=rotllantas-5+aumento; break; case 'a': posicion1=posicion1-aumento; posicion2=posicion2-aumento; pos=pos-aumento; rotllantas=rotllantas+5-aumento; break; case 'o': datos_luz[1] *= (1.0f + deltaR); break; case 'u': datos_luz[1] /= (1.0f + deltaR); break; case 'l': datos_luz[0] += 100.*deltaR; break; case 'j': datos_luz[0] -= 100.*deltaR; break; case 'i': datos_luz[2] += 100.*deltaR; break; case 'k': datos_luz[2] -= 100.*deltaR; break; //Rotar cara case 'r': rotCubo=rotCubo+1+rotCuboAumento; break; } /* Le digo a OpenGL que dibuje de nuevo cuando pueda */ glutPostRedisplay( ); } void reshape (int w, int h) { glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluPerspective(65.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef (0.0, 0.0, -5.0); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE/*GLUT_DOUBLE*/ | GLUT_RGB | GLUT_STENCIL); glutInitWindowSize(600, 400); glutInitWindowPosition(50, 50); glutCreateWindow(argv[0]); glutDisplayFunc(display); glutReshapeFunc(reshape); glutReshapeFunc(ChangeSize); glutKeyboardFunc(ControlTeclado); glutSpecialFunc(SpecialKeys); glutMouseFunc(pickCara_del_Cubo); SetupRC(); glutMainLoop(); return 0; }