You are on page 1of 10

// OPEN_GL_test.cpp : Defines the entry point for the console application.

//
// OPEN_GL_test.cpp : Defines the entry point for the console application.
//
/*
Descripcin del funcionamiento de los controles:
KEY_LEFT ->Rota alrededor izquierdo del objeto
KEY_RIGHT ->Rota alrededor derecho del objeto
KEY_UP ->Rota hacia arriba del objeto pero conforme van cambiando el valor de la
s variables, rota hacia arriba y abajo
y a la izquierda, formando un angulo distinto a 90 y 0 grados
KEY_DOWN ->Rota hacia abajo del objeto pero conforme van cambiando el valor de l
as variables, rota hacia arriba y abajo
y a la derecha, formando un angulo distinto a 90 y 0 grados
KEY_F1 ->desaparece el color de las caras traseras
KEY_F2 ->resalta el color de las caras traseras
KEY_F3 ->Aparece el color de las caras delanteras
KEY_F4 ->Regresa a la posicin inicial del objeto
KEY_F5 ->Cambia el color del fondo
KEY_F6 ->Regresa al color del fondo original
KEY_F7 ->Aleja al objeto
KEY_F8 ->Acercamos al objeto
KEY_F9 ->Rota al objeto sobre el eje z de manera positiva o negativa (depende de
l cuadrado)
KEY_F9 ->Rota al objeto sobre el eje z de manera negativa o negativa (depende de
l cuadrado)
*/
#include "stdafx.h"
/////////////////////////////////////////////////
#define GL_PI 3.1415f
#define YGROUND -40.f
typedef float vector3D[4];
GLfloat xRot = 0.45f;
GLfloat yRot = 0.35f;
GLboolean bCull = glIsEnabled(GL_CULL_FACE);
GLboolean bDepth = glIsEnabled(GL_DEPTH_TEST);
GLboolean bOutline = (GLboolean)true;
GLboolean rotacion = (GLboolean)false;
/////////////////////////////////////////////////
GLenum shademode = GL_FLAT;
/////////////////////////////////////////////////
GLfloat ex = 0.0f, x, y, z, g = 0, c = 0;
GLfloat ey = 85.0f;
GLfloat ez = -350.0f;
GLfloat vPosLight[3] = { -60.f, 100.f, -70.f }, yground = YGROUND, lightPos[4] =
{ -60.f, 100.f, -70.f };
GLfloat vPlanoPiso[4] = { 0.f, 1.f, 0.f, YGROUND }, vGroundPlane[4] = { 0.0f, 1.
0f, 0.0f, -yground },
vPosLuz[3] = { 2.f, 5.f, -3.f }, MatSombra[16], shadowMat[16], colv[4];
/////////////////////////////////////////////////
GLfloat delta = 0.07f;
GLfloat deltaR = 0.07f;
///////////////////////////////////////////////////////////
void MatrizParaSombra(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()
{
colv[3] = 0.25;
// Black background
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// Set drawing color to green
//glColor3f(0.0f, 1.0f, 0.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);
MatrizParaSombra(vGroundPlane, lightPos, shadowMat);
}
//FUNCION QUE CONSTRUYE LA BASE O CARA DEL CUADRADO DE ABAJO
void Cara_sin_color() {
glBegin(GL_QUADS);//Crea la cara base
glVertex3f(25.0f, -25.0f, 25.0f);
glVertex3f(25.0f, -25.0f, -25.0f);
glVertex3f(-25.0f, -25.0f, -25.0f);
glVertex3f(-25.0f, -25.0f, 25.0f);
glEnd();

void Cubo(GLfloat g, GLfloat c) {


//glColor3f(1.0f, 1.0f, 0.4f);
colv[0] = 1.f, colv[1] = 1.f, colv[2] = 1.f;
glColor4fv(colv);
glPushMatrix();
glRotatef(0.0f-g , 0.f, 0.f, 1.f);//cara base
glTranslatef(0.0f, 0.0, 0.0f);
Cara_sin_color();
glPopMatrix();
//glColor3f(1.0f, 1.0f, 0.0f);
colv[0] = 1.f, colv[1] = 1.f, colv[2] = 0.f;
glColor4fv(colv);
glPushMatrix();
glRotatef(90.f-g, 0.f, 0.f, 1.f);//cara izquierda
/*glTranslatef(0.0f, 0.0f, c);*/
Cara_sin_color();
glPopMatrix();
//glColor3f(0.98f, 0.04f, 0.7f);
colv[0] = 0.0f, colv[1] = 1.0f, colv[2] = 0.0f;
glColor4fv(colv);
glPushMatrix();
glRotatef(-90.f-g, 0.f, 0.f, 1.f);//cara derecha
/*glTranslatef(0.0f, 0.0f, c);*/
Cara_sin_color();
glPopMatrix();
//glColor3f(0.0f, 0.0f, 1.0f);
colv[0] = 0.f, colv[1] = 0.f, colv[2] = 0.f;
glColor4fv(colv);
glPushMatrix();
glRotatef(180.f-g, 0.f, 0.f, 1.f);//cara arriba
/*glTranslatef(0.0f, 0.0f, c);*/
Cara_sin_color();
glPopMatrix();
//glColor3f(1.0f, 0.0f, 0.0f);
colv[0] = 1.f, colv[1] = 0.f, colv[2] = 0.f;
glColor4fv(colv);
glPushMatrix();
glRotatef(90.f, 0.f, 1.f, 0.f);//cara enfrente
glRotatef(90.f, 0.f, 0.f, 1.f);
glRotatef(0.0f-g, 0.f, 1.f, 0.f);
/*glTranslatef(0.0f, 0.0f, c);*/
Cara_sin_color();
glPopMatrix();

//glColor3f(0.f, 0.1f, 0.f);


colv[0] = 0.f, colv[1] = 0.1f, colv[2] = 1.f;
glColor4fv(colv);
glPushMatrix();
glRotatef(-90.f, 0.f, 1.f, 0.f);//cara atrs
glRotatef(90.f, 0.f, 0.f, 1.f);
glRotatef(0.0f+g, 0.f, 1.f, 0.f);
/*glTranslatef(0.0f, 0.0f, c);*/
Cara_sin_color();
glPopMatrix();
}
void cuboSimp(GLfloat g, GLfloat c)
{
glPushMatrix();
glRotatef(0.0f - g, 0.f, 0.f, 1.f);//cara base
Cara_sin_color();
glPopMatrix();
glPushMatrix();
glRotatef(90.f - g, 0.f, 0.f, 1.f);//cara izquierda
Cara_sin_color();
glPopMatrix();
glPushMatrix();
glRotatef(-90.f - g, 0.f, 0.f, 1.f);//cara derecha
Cara_sin_color();
glPopMatrix();
glPushMatrix();
glRotatef(180.f - g, 1.f, 0.f, 0.f);//cara arriba
Cara_sin_color();
glPopMatrix();
glPushMatrix();
glRotatef(90.f - g, 1.f, 0.f, 0.f);//cara enfrente
Cara_sin_color();
glPopMatrix();
glPushMatrix();
glRotatef(-90.f + g, 1.f, 0.f, 0.f);//cara atrs
Cara_sin_color();
glPopMatrix();
}

////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////
////FUNCION QUE CONSTRUYE LA BASE O CARA DEL CUADRADO DE ARRIBA
void Cara_sin_color1() {

glBegin(GL_QUADS);//Crea la cara base


glVertex3f(25.0f, -25.0f, 25.0f);
glVertex3f(25.0f, -25.0f, -25.0f);
glVertex3f(-25.0f, -25.0f, -25.0f);
glVertex3f(-25.0f, -25.0f, 25.0f);
glEnd();
}
void Cubo1(GLfloat g, GLfloat c) {
//glColor3f(1.0f, 1.0f, 0.0f);
colv[0] = 1.f, colv[1] = 1.f, colv[2] = 1.f;
glColor4fv(colv);
glPushMatrix();
glTranslatef(0.0f, 70.70f, 0.0f);
glRotatef(0.0f + g, 0.f, 0.f, 1.f);//cara base
Cara_sin_color1();
glPopMatrix();
//glColor3f(1.0f, 1.0f, 0.0f);
colv[0] = 0.f, colv[1] = 1.f, colv[2] = 0.f;
glColor4fv(colv);
glPushMatrix();
glTranslatef(0.0f, 70.70f, 0.0f);
glRotatef(90.f + g, 0.f, 0.f, 1.f);//cara izquierda
Cara_sin_color1();
glPopMatrix();
//glColor3f(0.98f, 0.04f, 0.7f);
colv[0] = 1.0f, colv[1] = 1.0f, colv[2] = 0.0f;
glColor4fv(colv);
glPushMatrix();
glTranslatef(0.0f, 70.70f, 0.0f);//cara derecha
glRotatef(-90.f + g, 0.f, 0.f, 1.f);
Cara_sin_color1();
glPopMatrix();
//glColor3f(0.0f, 0.0f, 1.0f);
colv[0] = 0.f, colv[1] = 0.f, colv[2] = 0.f;
glColor4fv(colv);
glPushMatrix();
glTranslatef(0.0f, 70.70f, 0.0f);//cara arriba
glRotatef(180.f + g, 0.f, 0.f, 1.f);
Cara_sin_color1();
glPopMatrix();
//glColor3f(1.0f, 0.0f, 0.0f);
colv[0] = 0.f, colv[1] = 0.f, colv[2] = 1.0f;
glColor4fv(colv);
glPushMatrix();
glTranslatef(0.0f, 70.70f, 0.0f);
glRotatef(90.f, 0.f, 1.f, 0.f);//cara enfrente
glRotatef(90.f, 0.f, 0.f, 1.f);
glRotatef(0.0f + g, 0.f, 1.f, 0.f);
Cara_sin_color1();
glPopMatrix();

//glColor3f(0.f, 0.1f, 0.f);


colv[0] = 1.f, colv[1] = 0.0f, colv[2] = 0.f;
glColor4fv(colv);
glPushMatrix();
glTranslatef(0.0f, 70.70f, 0.0f);
glRotatef(-90.f, 0.f, 1.f, 0.f);//cara atrs
glRotatef(90.f, 0.f, 0.f, 1.f);
glRotatef(0.0f - g, 0.f, 1.f, 0.f);
Cara_sin_color1();
glPopMatrix();
}

void cuboSimp1(GLfloat g, GLfloat c)


{
glPushMatrix();
glTranslatef(0.0f, 70.70f, 0.0f);
glRotatef(0.0f + g, 0.f, 0.f, 1.f);//cara base
Cara_sin_color1();
glPopMatrix();
glPushMatrix();
glTranslatef(0.0f, 70.70f, 0.0f);
glRotatef(90.f+g, 0.f, 0.f, 1.f);//cara izquierda
Cara_sin_color1();
glPopMatrix();
glPushMatrix();
glTranslatef(0.0f, 70.70f, 0.0f);
glRotatef(-90.f+g, 0.f, 0.f, 1.f);//cara derecha
Cara_sin_color1();
glPopMatrix();
glPushMatrix();
glTranslatef(0.0f, 70.70f, 0.0f);
glRotatef(180.f+g, 1.f, 0.f, 0.f);//cara arriba
Cara_sin_color1();
glPopMatrix();
glPushMatrix();
glTranslatef(0.0f, 70.70f, 0.0f);
glRotatef(90.f+g, 1.f, 0.f, 0.f);//cara enfrente
Cara_sin_color1();
glPopMatrix();
glPushMatrix();
glTranslatef(0.0f, 70.70f, 0.0f);
glRotatef(-90.f-g, 1.f, 0.f, 0.f);//cara atrs
Cara_sin_color1();
glPopMatrix();
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////
//FUNCION QUE CNSTRUYE AL PISO
void Piso() {

glPushMatrix();
glBegin(GL_QUADS);//Creamos el piso
glColor3f(0.0f, 0.7f, 0.75f);
glVertex3f(1000.0f, -35.0f, 1000.0f);
glVertex3f(1000.0f, -35.0f, -1000.0f);
glVertex3f(-1000.0f, -35.0f, -1000.0f);
glVertex3f(-1000.0f, -35.0f, 1000.0f);
glEnd();
//glDisable(GL_DEPTH_TEST);
glBegin(GL_LINES);
int i = 0;
glColor3f(0.9f, 0.9, 0.9f);
for (i = -1000; i < 1000; i++) {
glVertex3f(i, -35.f, -1000.f);
glVertex3f(i, -35.f, 1000.f);
i += 20;
}
glEnd();
glBegin(GL_LINES);
int j = 0;
glColor3f(0.9f, 0.9f, 0.9f);
for (j = -1000; j < 1000; j++) {
glVertex3f(-1000.0f, -35.0f, j);
glVertex3f(1000.0f, -35.0f, j);
j += 20;
}
glEnd();

if (bDepth) glEnable(GL_DEPTH_TEST);
else
glDisable(GL_DEPTH_TEST);
glPopMatrix();
}
void Sombra()
{
glPushMatrix();
glColor3f(0.2f, 0.2f, 0.2f);
glMultMatrixf(shadowMat);
cuboSimp(g, c);
cuboSimp1(g, c);
glPopMatrix();
}
void Esfera()
{
//glColor3f(1.f, 1.f, 0.f);
colv[0] = 1.f, colv[1] = 1.f, colv[2] = 0.f;
glColor4fv(colv);
glPushMatrix();

glTranslatef(vPosLight[0], vPosLight[1], vPosLight[2]);


glutSolidSphere(7.5f, 20, 20);
glPopMatrix();
}
void Reflejo() {
glFrontFace(GL_CCW);
glPushMatrix();
glScalef(1.f, -1.f, 1.f);
glTranslatef(0.f, 70.f, 0.f);
Cubo(g, c);
Esfera();
Cubo1(g, c);
glPopMatrix();
glFrontFace(GL_CW);
}
//funcion RenderScene
void RenderScene(void) {
// Reset coordinate system
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
// Clear the window and the depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_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);
glFrontFace(GL_CCW);
Piso();
glFrontFace(GL_CW);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Sombra ();
Reflejo ();
glDisable (GL_BLEND);
Cubo (g, c);
Cubo1(g, c);
Esfera();
// Restore transformations
glPopMatrix();
// Flush drawing commands

glFlush();
//glutSwapBuffers();
}
// 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();
gluPerspective(60.0, 1.0, 10.5, 5000.0);
glMatrixMode(GL_MODELVIEW);
}
void SpecialKeys(int key, int x, int y)
{
GLfloat dx, dz;
if (key == GLUT_KEY_F9) {
rotacion = true;
c -= 2.35f;
g -= 5.0f;
}
if (key == GLUT_KEY_F10) {
//GLfloat c = 0.f;
//glPushMatrix();
rotacion = true;
c += 2.35f;
g += 5.0f;
//glTranslatef(0.f, 0.f, c);
//glPopMatrix();
}
if (key == GLUT_KEY_UP) {
dx = -ez;
dz = ey;
GLfloat s = sqrtf(ex*ex + ey*ey + ez*ez);
ey += delta*dx;
ez += delta*dz;
GLfloat s1 = sqrtf(ex*ex + ey*ey + ez*ez) / s;
ey /= s1;
}
if (key == GLUT_KEY_DOWN) {
dx = -ez;
dz = ey;
GLfloat s = sqrtf(ex*ex + ey*ey + ez*ez);
ey -= delta*dx;
ez -= delta*dz;
GLfloat s1 = sqrtf(ex*ex + ey*ey + ez*ez) / s;
ey /= s1;
}
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
if
if
if

(key
(key
(key
(key

==
==
==
==
ex
ey
ez

GLUT_KEY_F1) bCull = !bCull;


GLUT_KEY_F2)bDepth = !bDepth;
GLUT_KEY_F3)bOutline = !bOutline;
GLUT_KEY_F4) {
= 0.0f;
= 85.0f;
= -350.0f;

}
if (key == GLUT_KEY_F5) {
glClearColor(0.f, 1.f, 1.f, 0.f);
}
if (key == GLUT_KEY_F6) {
glClearColor(0.0f, 0.75f, 01.0f, 0.0f);
}
if (key == GLUT_KEY_F7) {

//increase distance from camera to origi

n
ex *= (1.0f + deltaR);
ey *= (1.0f + deltaR);
ez *= (1.0f + deltaR);
}
if (key == GLUT_KEY_F8) {
//reduce distance from camera to origin
(close up)
ex *= (1.0f - deltaR);
ey *= (1.0f - deltaR);
ez *= (1.0f - deltaR);
}
// Refresh the Window
glutPostRedisplay();
// Refresh the Window
glutPostRedisplay();
}
///////////////////////////////////////////////////////////
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 800);
glutInitWindowPosition(450, 40);
glutCreateWindow(argv[0]);
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
SetupRC();
glutMainLoop();
return 0;
}

You might also like