// Recuperación 22P.cpp : Este archivo contiene la función "main". La ejecución del programa comienza y termina ahí. // /* #include "glut.h" #include #include #include */ #include "stdafx.h" #include #include #define ALPHA 0.5 #define NumLineas 30 #define BUFSIZE 512 //Implementacion textura #define checkImageWidth 256 #define checkImageHeight 256 #define subImageWidth 64 #define subImageHeight 64 #define Cabeza 1 #define OrejaIzq 2 GLuint selectBuf[BUFSIZE]; GLboolean bCull = glIsEnabled(GL_CULL_FACE); GLboolean bDepth = glIsEnabled(GL_DEPTH_TEST); GLboolean bOutline = (GLboolean)false; GLenum shademode = GL_FLAT; static int giroA = 0, giroB = 180, giroC = 180; GLfloat velocidadA; GLfloat velocidadB; GLfloat ex = 2.0f; GLfloat ey = 2.0f; GLfloat ez = 13.0f; GLfloat delta = 0.01f; GLfloat deltaR = 0.01f; GLfloat xRot = 0.45f; GLfloat yRot = 0.35f; GLfloat matrizSombra[16]; GLfloat v[8][3]; GLfloat piso[] = { 0, 1,0, 5 }; GLfloat poz_luz[] = { 0., 8., 0. , 1.0 }; GLfloat ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f }; GLfloat diffuseLight[] = /*{ 0.7f, 0.7f, 0.7f, 1.0f };*/{ 0.3f, 0.3f, 0.3f, 1.0f }; GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f }; GLfloat STARTbackgroundcolor[] = { 0.0f, 0.0f, 1.0f, 1.0f }; GLfloat STARTplanebodycolor[] = { 0.0f, 1.0f, 0.0f }; GLfloat colors[][3] = { { .5,0.0,0.5 },{ 1.0,0.0,0.0 }, { 1.0,1.0,0.0 },{ 0.0,1.0,0.0 }, { 0.0,0.0,0.0 },{ 1.0,0.0,1.0 }, { 1.0,1.0,1.0 },{ 0.0,1.0,1.0 } }; GLfloat texpts[2][2][2] = { {{0.1, 0.0}, {0.1, 0.0}},{{3.0, 1.0}, {1.0, 3.0}} }; GLfloat ctrlpoints[4][4][3] = { { {-1.5, -1.5, 0.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, 4, 4.0}, {-0.5, 0.5, 0.0}, {0.5, 1.5, 3.0}, {1.5, 0.5, 4.0}}, { {-1.5, 1.5, -2.0}, {-0.5, 3, -2.0}, {0.5, 1.5, 0.0}, {1.5, 1.5, -1.0}} }; static GLint faces[6][4] = { {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4}, {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} }; static GLubyte checkImage[checkImageHeight][checkImageWidth][5]; static GLubyte otherImage[checkImageHeight][checkImageWidth][5]; static GLubyte subImage[subImageHeight][subImageWidth][8]; static GLuint texName[3]; void makeCheckImages(void) {//Textura en objeto curvo 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; c = ((((i & 0x10) == 0) ^ ((j & 0x10)) == 0)) * 255; otherImage[i][j][0] = (GLubyte)0; otherImage[i][j][1] = (GLubyte)0; otherImage[i][j][2] = (GLubyte)c; otherImage[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 + c - 122; subImage[i][j][3] = (GLubyte)255; } } } void SetupRC(void) { velocidadA = 1; velocidadB = 1; glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glShadeModel(GL_SMOOTH); 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); glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0); glFrontFace(GL_CW); glEnable(GL_LIGHTING); glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); glPolygonMode(GL_BACK, GL_FILL); // the light source GL_LIGHT0 is enabled: // Set up and enable light 0 glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight); glLightfv(GL_LIGHT0, GL_SPECULAR, specular); glEnable(GL_LIGHT0); // 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_SPECULAR, specref); glMateriali(GL_FRONT, GL_SHININESS, 128); makeCheckImages(); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glGenTextures(1, 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_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage); glTexSubImage2D(GL_TEXTURE_2D, 0, 12, 170, subImageWidth, subImageHeight, GL_RGBA, GL_UNSIGNED_BYTE, subImage); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 100, subImageWidth, subImageHeight, GL_RGBA, GL_UNSIGNED_BYTE, subImage); glTexSubImage2D(GL_TEXTURE_2D, 0, 170, 12, subImageWidth, subImageHeight, GL_RGBA, GL_UNSIGNED_BYTE, subImage); glTexSubImage2D(GL_TEXTURE_2D, 0, 170, 0, subImageWidth, subImageHeight, GL_RGBA, GL_UNSIGNED_BYTE, subImage); glEnable(GL_TEXTURE_2D); glFrontFace(GL_CW); } void normalize(GLfloat v[3]) {//Guarda el vector normal 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]) {//Mapeo del vetor normal 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' Calculo del vector normal!!!! 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 drawBox(GLfloat size, GLenum type) { v[0][0] = v[1][0] = v[2][0] = v[3][0] = -size / 2; v[4][0] = v[5][0] = v[6][0] = v[7][0] = size / 2; v[0][1] = v[1][1] = v[4][1] = v[5][1] = -size / 2; v[2][1] = v[3][1] = v[6][1] = v[7][1] = size / 2; v[0][2] = v[3][2] = v[4][2] = v[7][2] = -size / 2; v[1][2] = v[2][2] = v[5][2] = v[6][2] = size / 2; GLfloat norm[3]; for (int i = 5; i >= 0; i--) { glBegin(type); gltGetNormalVector(&v[faces[i][1]][0], &v[faces[i][2]][0], &v[faces[i][0]][0], norm); glVertex3fv(&v[faces[i][0]][0]); glVertex3fv(&v[faces[i][1]][0]); glVertex3fv(&v[faces[i][2]][0]); glVertex3fv(&v[faces[i][3]][0]); glNormal3fv(norm); glEnd(); } } void glutSolidCubeM(GLdouble size) { drawBox(size, GL_QUADS); } void cuadrado() { glBegin(GL_QUADS); glVertex2f(1., 1.); glVertex2f(-1., 1.); glVertex2f(-1., -1.); glVertex2f(1., -1.); glEnd(); } void cabeza(bool color) { glPushMatrix(); glPushName(Cabeza); glRotatef(giroA, 0, 1, 0); /* cara 1 */ if (color) glColor3fv(colors[6]); glPushMatrix(); glTranslatef(0., 0., 1.); cuadrado(); glPopMatrix(); /* cara 2 */ if (color) glColor3fv(colors[6]); glPushMatrix(); glRotatef(90., 0., 1., 0.); glTranslatef(0., 0., 1.); cuadrado(); glPopMatrix(); /* cara 3 */ if (color) glColor3fv(colors[6]); glPushMatrix(); glRotatef(180., 0., 1., 0.); glTranslatef(0., 0., 1.); cuadrado(); glPopMatrix(); /* cara 4 */ if (color) glColor3fv(colors[6]); glPushMatrix(); glRotatef(-90., 0., 1., 0.); glTranslatef(0., 0., 1.); cuadrado(); glPopMatrix(); /* cara 5 */ if (color) glColor3fv(colors[6]); glPushMatrix(); glRotatef(90., 0., 0., 1.); glRotatef(-90., 0., 1., 0.); glTranslatef(0., 0., 1.); cuadrado(); glPopMatrix(); /* cara 6 */ if (color) glColor3fv(colors[6]); glPushMatrix(); glRotatef(-90., 0., 0., 1.); glRotatef(-90., 0., 1., 0.); glTranslatef(0., 0., 1.); cuadrado(); glPopMatrix(); if (color) glColor3f(1.0f, 0.0f, 0.0f); glPushMatrix(); //glRotatef(90., 0., 0., 1.); //glRotatef(-90., 0., 1., 0.); //glTranslatef(0., 0., 1.); glScalef(0.2, 0.2, 0.2); glTranslatef(-1.5, 1., 5.1); cuadrado(); glPopMatrix(); if (color) glColor3f(1.0f, 0.0f, 0.0f); glPushMatrix(); glScalef(0.2, 0.2, 0.2); glTranslatef(1.5, 1., 5.1); cuadrado(); glPopMatrix(); glPopName(); glPopMatrix(); } void orejaIzq(bool color) { glPushMatrix(); glRotatef(giroA, 0, 1, 0); glPushName(OrejaIzq); //glTranslatef(-.8, 1.0, 0.0); glTranslatef(-.5, 1.0, 0.0); glRotatef((GLfloat)giroB, 1.0, 0.0, 0.0); glTranslatef(0.0, -0.7, 0.0); if (color) glColor3f(1.0f, 0.75f, 0.8f); glScalef(0.7, 1.5, 0.2); glutSolidCubeM(1.0); glPopMatrix(); glPopName(); } void orejaDer(bool color) { glPushMatrix(); glRotatef(giroA, 0, 1, 0); glTranslatef(0.5, 1.0, 0.0); glRotatef((GLfloat)giroB, 1.0, 0.0, 0.0); glTranslatef(0.0, -0.7, 0.0); if (color) glColor3f(1.0f, 0.75f, 0.8f); glScalef(0.7, 1.5, 0.2); glutSolidCubeM(1.0); glPopMatrix(); } void orejaDer2(bool color) { glPushMatrix(); //GIRO A glRotatef(giroA, 0, 1, 0); //GIRO B glTranslatef(0.5, 1.0, 0.0); glRotatef((GLfloat)giroB, 1.0, 0.0, 0.0); glTranslatef(0.0, -0.7, 0.0); glTranslatef(0, -1.5, 0); //GIRO B glTranslatef(0, .7, 0); glRotatef((GLfloat)giroC, 1.0, 0.0, 0.0); glTranslatef(0, .7, 0); if (color) glColor3f(1.0f, 0.75f, 0.8f); glScalef(0.7, 1.5, 0.2); glutSolidCubeM(1.0); glPopMatrix(); } void orejaIzq2(bool color) { glPushMatrix(); //GIRO A glRotatef(giroA, 0, 1, 0); //GIRO B glTranslatef(-0.5, 1.0, 0.0); glRotatef((GLfloat)giroB, 1.0, 0.0, 0.0); glTranslatef(0.0, -0.7, 0.0); glTranslatef(0, -1.5, 0); //GIRO B glTranslatef(0, .7, 0); glRotatef((GLfloat)giroC, 1.0, 0.0, 0.0); glTranslatef(0, .7, 0); if (color) glColor3f(1.0f, 0.75f, 0.8f); glScalef(0.7, 1.5, 0.2); glutSolidCubeM(1.0); glPopMatrix(); } void crearConejo(int mode) { glPushMatrix(); cabeza(true); orejaIzq(true); orejaIzq2(true); orejaDer(true); orejaDer2(true); glPopMatrix(); } void ObjetoCurvo(void) {//Objeto curvo glEnable(GL_TEXTURE_2D); glEnable(GL_AUTO_NORMAL); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); glBindTexture(GL_TEXTURE_2D, texName[0]); glPushMatrix(); glColor3f(0, 0.0, 0); glRotatef(90, 0, 0, 1); glTranslatef(3.0, 6, 0); glScalef(1, 1, 1); glRotatef(240, 0, 1, 0); glRotatef(85.0, 1.0, 1.0, 1.0); glEvalMesh2(GL_FILL, 0, 20, 0, 20); glPopMatrix(); glDisable(GL_TEXTURE_2D); glDisable(GL_AUTO_NORMAL); } void ObjetoCurvo_nocolor(void) {//Sombra Objeto curvo glPushMatrix(); glTranslatef(3.0, 0, 0); glScalef(1, 1, 1); glRotatef(240, 0, 1, 0); glRotatef(85.0, 1.0, 1.0, 1.0); glEvalMesh2(GL_FILL, 0, 20, 0, 20); 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 Objetos_sin_color() { glPushMatrix(); //glScalef(10., 10., 10.); cabeza(false); orejaIzq(false); orejaIzq2(false); orejaDer(false); orejaDer2(false); glPopMatrix(); } void crearSombras() {//llama a las funciones de las sombras de los objetos para ser visualizados por igual glPushMatrix(); Objetos_sin_color(); ObjetoCurvo_nocolor(); glPopMatrix(); } void Objetos(int mode) {//llama las funciones de crear objetos glPushMatrix(); crearConejo(mode); ObjetoCurvo(); glPopMatrix(); } void setSpeed() {//reincia la velocidad del giro velocidadA = 1; velocidadB = 1; } void anallizename(int name) {//Permite cambiar la velocidad de giro si uno de estos objetos es accionado int i; if (name == Cabeza) { setSpeed(); return; } else if (name == OrejaIzq) { for (i = 0; i < 3; i++) { velocidadB += 1; } } glutPostRedisplay(); } void processHits(GLint hits, GLuint buffer[])//velocidad dependiendo de analizame { if (hits == 0) { setSpeed(); return; } unsigned int i, j; GLuint names; GLuint* ptr; ptr = (GLuint*)buffer; for (i = 0; i < hits; i++) { names = *ptr; ptr += 3; for (j = 0; j < names; j++) anallizename(*ptr++); } } #define NUMLIN 20 void lineas() { glColor3f(1., 1., 1.); glBegin(GL_LINES); for (int i = 0; i < NUMLIN; i++) { glVertex3f(-1. + i * 2. / (NUMLIN - 1.), -1., 0); glVertex3f(-1. + i * 2. / (NUMLIN - 1.), 1., 0); glVertex3f(-1., 1. - i * 2. / (NUMLIN - 1.), 0); glVertex3f(1., 1. - i * 2. / (NUMLIN - 1.), 0); } glEnd(); } void Piso() { glPushMatrix(); glTranslatef(0., -piso[3], 0.); glRotatef(90., 1., 0., 0.); glScalef(10., 10., 10.); glColor4f(0.8f, 0.8f, 0.8f, 0.8); glScalef(4., 4., 4.); cuadrado(); glDisable(GL_DEPTH_TEST); lineas(); if (bDepth) glEnable(GL_DEPTH_TEST); glPopMatrix(); } void sombradeMiMundo(GLfloat datosdeLuz[], GLfloat datosdePlano[]) {//proporciona la sombra del objeto dependiendo del emisor de luz glColor4f(0.3, 0.3, 0.3, ALPHA); glPushMatrix(); gltMakeShadowMatrix(datosdePlano, datosdeLuz, matrizSombra); glMultMatrixf(matrizSombra); crearSombras(); glPopMatrix(); } void foco() {//funcion donde se crea la fuente de luz y puede ser posicionada diferente glPushMatrix(); glColor3fv(colors[2]); glLightfv(GL_LIGHT0, GL_POSITION, poz_luz); glPushMatrix(); glTranslatef(poz_luz[0], poz_luz[1], poz_luz[2]); glutSolidSphere(0.5, 15, 15); glPopMatrix(); 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 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 renderScene(int mode) { // Reset coordinate system glMatrixMode(GL_PROJECTION); if (mode == GL_RENDER) glLoadIdentity(); gluPerspective(60.0, 1.0, 1.5, 500.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(ex, ey, ez, 0.0, 0.0, 0.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 | 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(); glRotatef(xRot, 1.0f, 0.0f, 0.0f); glRotatef(yRot, 0.0f, 1.0f, 0.0f); /*Reflejo del mundo*/ Stencil_Config(); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_LIGHTING); glFrontFace(GL_CCW); glPushMatrix(); glTranslatef(0., -piso[3], 0.); glScalef(1., -1., 1.); glTranslatef(0., piso[3], 0.); foco(); glEnable(GL_LIGHTING); Objetos(mode); glDisable(GL_LIGHTING); glFrontFace(GL_CW); glPopMatrix(); /*Piso*/ Piso(); /*Sombre objetos*/ glDisable(GL_DEPTH_TEST); sombradeMiMundo(poz_luz, piso); if (bDepth) glEnable(GL_DEPTH_TEST); glDisable(GL_STENCIL_TEST); /*Foco*/ foco(); glEnable(GL_LIGHTING); /*objetos del mundo*/ Objetos(mode); glDisable(GL_LIGHTING); glPopMatrix(); glutSwapBuffers(); } void pickAccion(int button, int state, int x, int y)//revisa si el objeto a sido seleccionado { GLuint selectBuf[BUFSIZE]; 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); renderScene(GL_SELECT); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); hits = glRenderMode(GL_RENDER); processHits(hits, selectBuf); glutPostRedisplay(); } void reshape(int w, int h) { glViewport(0, 0, (GLsizei)w, (GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, 1.0, 1.5, 500.0); glMatrixMode(GL_MODELVIEW); } void SpecialKeys(int key, int x, int y) { GLfloat dx, dy, dz; if (key == GLUT_KEY_UP) {//subir camara dy = -ez; dz = ey; GLfloat s = sqrtf(ex * ex + ey * ey + ez * ez); ey += delta * dy; ez += delta * dz; GLfloat s1 = sqrtf(ex * ex + ey * ey + ez * ez) / s; ex /= s1; ey /= s1; ey /= s1; } if (key == GLUT_KEY_DOWN) {//bajar camara dy = -ez; dz = ey; GLfloat s = sqrtf(ex * ex + ey * ey + ez * ez); ey -= delta * dy; ez -= delta * dz; GLfloat s1 = sqrtf(ex * ex + ey * ey + ez * ez) / s; ex /= s1; ex /= s1; ey /= s1; } if (key == GLUT_KEY_LEFT) {//izquierda 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) {//derecha 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) { giroA = (giroA - (int)velocidadA) % 360; } if (key == GLUT_KEY_F6) { giroA = (giroA + (int)velocidadA) % 360; } if (key == GLUT_KEY_F7) { giroB = (giroB + (int)velocidadB) % 360; } if (key == GLUT_KEY_F8) { giroB = (giroB- (int)velocidadB) % 360; } if (key == GLUT_KEY_F9) { giroC = (giroC + 5) % 360; } if (key == GLUT_KEY_F10) { giroC = (giroC - 5) % 360; } if (key == GLUT_KEY_END) { ex *= (1.0f + deltaR); ey *= (1.0f + deltaR); ez *= (1.0f + deltaR); } if (key == GLUT_KEY_HOME) { ex *= (1.0f - deltaR); ey *= (1.0f - deltaR); ez *= (1.0f - deltaR); } glutPostRedisplay(); } void keyboard(unsigned char key, int x, int y) { GLfloat dx, dz; GLfloat s, s1; switch (key) { case '1': poz_luz[1] += 0.5; break; case '2': poz_luz[1] -= 0.5; break; case '3': poz_luz[0] += 0.5; break; case '4': poz_luz[0] -= 0.5; break; case '5': poz_luz[2] += 0.5; break; case '6': poz_luz[2] -= 0.5; break; case 27: exit(0); break; default: break; } glutPostRedisplay(); } void display() { renderScene(GL_RENDER); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_STENCIL | GLUT_DEPTH); glutInitWindowSize(700, 700); glutInitWindowPosition(150, 150); glutCreateWindow("Proyecto"); SetupRC(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutSpecialFunc(SpecialKeys); glutMouseFunc(pickAccion); glutMainLoop(); }