Well, I am not very sure what the problem is, but when I rotate the objects some parts of the Front of the object is not rendered an you can se the inside of the object, other thing that I've noticed is that it looks like the light is moving with the object, if I´m not wrong the modelview matrix is transforming the position of the light, what I want is to leave the light in a static position, so that the modelview matrix doesn't transform the position of the light... :huh:
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
typedef enum {FIG1,FIG2,FIG3,FIG4,FIG5,FIG6,FIG7,FIG8,FIG9,FIG10,FIG11,FIG12, C1, C2, C3, C4, F1, F2, F3, F4} opcionesMenu;
int FIG=FIG1;
int F;
int FS=FIG1;
float alpha, beta;
int xs, ys;
GLfloat Tx=0;
GLfloat Ty=0;
GLfloat Sx=1;
GLfloat Sy=1;
// valores de las luces y coordenadas
GLfloat LuzAmbiente[] = { 0.5f, 0.0f, 0.0f, 1.0f};
GLfloat LuzDifusa[] = { 1.0f, 0.5f, 0.5f, 1.0f };
GLfloat LuzEspecular[] = { 1.0f, 1.0f, 1.0f, 1.0f};
GLfloat LuzPosicion[] = { 0.0f, 0.0f, 0.0f, 1.0f };
GLfloat gold_Ka[4] = { 0.24725f, 0.1995f, 0.0745f, 1.0f };
GLfloat gold_Kd[4] = { 0.75164f, 0.60648f, 0.22648f, 1.0f };
GLfloat gold_Ks[4] = { 0.628281f, 0.555802f, 0.366065f, 1.0f };
GLfloat gold_Ke = 41.2f;
GLfloat silver_Ka[4] = { 0.05f, 0.05f, 0.05f, 1.0f };
GLfloat silver_Kd[4] = { 0.4f, 0.4f, 0.4f, 1.0f };
GLfloat silver_Ks[4] = { 0.7f, 0.7f, 0.7f, 1.0f };
GLfloat silver_Ke = 12.0f;
void onMenu(int opcion)
{
switch(opcion)
{
case FIG1:
FIG=FIG1;
break;
case FIG2:
FIG=FIG2;
break;
case FIG3:
FIG=FIG3;
break;
case FIG4:
FIG=FIG4;
break;
case FIG5:
FIG=FIG5;
break;
case FIG6:
FIG=FIG6;
break;
case FIG7:
FIG=FIG7;
break;
case FIG8:
FIG=FIG8;
break;
case FIG9:
FIG=FIG9;
break;
case FIG10:
FIG=FIG10;
break;
case FIG11:
FIG=FIG11;
break;
case FIG12:
FIG=FIG12;
break;
}
glutPostRedisplay();
}
void onColor(int opcion)
{
switch(opcion)
{
case C1:
glColor3f(1,0,0);
break;
case C2:
glColor3f(0,1,0);
break;
case C3:
glColor3f(0,0,1);
break;
case C4:
glColor3f(1,1,1);
break;
}
glutPostRedisplay();
}
void onFondo(int opcion)
{
switch(opcion)
{
case F1:
glClearColor(0.3,0,0,0);
break;
case F2:
glClearColor(0,0.3,0,0);
break;
case F3:
glClearColor(0,0,0.3,0);
break;
case F4:
glClearColor(0,0,0,0);
break;
}
glutPostRedisplay();
}
void Keys(int key, int x, int y) {
switch(key) {
case GLUT_KEY_UP :
Ty+=0.05;
break;
case GLUT_KEY_DOWN :
Ty-=0.05;
break;
case GLUT_KEY_RIGHT :
Tx+=0.05;
break;
case GLUT_KEY_LEFT :
Tx-=0.05;
break;
}
}
void SKeys(unsigned char key, int x, int y) {
switch((int)key) {
case 45 :
Sy-=0.05;
Sx-=0.05;
break;
case 43 :
Sy+=0.05;
Sx+=0.05;
break;
}
}
void onMouse(int button, int state, int x, int y)
{
if ( (button == GLUT_LEFT_BUTTON) & (state == GLUT_DOWN) ) {
xs = x; ys = y;
}
}
void onMotion(int x, int y)
{
alpha = (alpha + (y - ys));
beta = (beta + (x - xs));
xs = x; ys = y;
glutPostRedisplay();
}
void DisplayX(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor( 0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0,1.0,0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_LIGHTING);
}
void Menu(void)
{
int menuFIG, menuPrincipal, menuC, menuF;
menuFIG = glutCreateMenu(onMenu);
glutAddMenuEntry("Torus", FIG1);
glutAddMenuEntry("Esfera", FIG2);
glutAddMenuEntry("Cono", FIG3);
glutAddMenuEntry("Tetrahedro", FIG4);
glutAddMenuEntry("Octahedro", FIG5);
glutAddMenuEntry("Dodcahedro", FIG6);
glutAddMenuEntry("Icosahedro", FIG7);
glutAddMenuEntry("Tetera", FIG8);
glutAddMenuEntry("Cilindro", FIG9);
glutAddMenuEntry("Disco", FIG10);
glutAddMenuEntry("Disco parcial", FIG11);
glutAddMenuEntry("Esfera", FIG12);
menuC = glutCreateMenu(onColor);
glutAddMenuEntry("Rojo", C1);
glutAddMenuEntry("Verde", C2);
glutAddMenuEntry("Azul", C3);
glutAddMenuEntry("Blanco", C4);
menuF = glutCreateMenu(onFondo);
glutAddMenuEntry("Rojo", F1);
glutAddMenuEntry("Verde", F2);
glutAddMenuEntry("Azul", F3);
glutAddMenuEntry("Negro", F4);
menuPrincipal = glutCreateMenu(onMenu);
glutAddSubMenu("Color Figura", menuC);
glutAddSubMenu("Color Fondo", menuF);
glutAddSubMenu("Figura", menuFIG);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void idle(void)
{
GLUquadricObj *MyQuadric;
glLightfv(GL_LIGHT0, GL_AMBIENT, LuzAmbiente);
glLightfv(GL_LIGHT0, GL_DIFFUSE, LuzDifusa);
glLightfv(GL_LIGHT0, GL_SPECULAR, LuzEspecular);
glLightfv(GL_LIGHT0, GL_POSITION, LuzPosicion);
glEnable(GL_LIGHT0);
glMaterialfv(GL_FRONT, GL_AMBIENT, gold_Ka);
glMaterialfv(GL_FRONT, GL_DIFFUSE, gold_Kd);
glMaterialfv(GL_FRONT, GL_SPECULAR, gold_Ks);
glMaterialf(GL_FRONT, GL_SHININESS, gold_Ke);
glMaterialfv(GL_BACK, GL_AMBIENT, silver_Ka);
glMaterialfv(GL_BACK, GL_DIFFUSE, silver_Kd);
glMaterialfv(GL_BACK, GL_SPECULAR, silver_Ks);
glMaterialf(GL_BACK, GL_SHININESS, silver_Ke);
if(FS!=FIG)
{
FS=FIG;
Tx=0;
Ty=0;
Sx=1;
Sy=1;
ys=xs=0;
alpha=beta=0;
}
glTranslatef(Tx,Ty,0);
glScalef(Sx,Sy,0);
glRotatef(alpha, 1.0f, 0.0f, 0.0f);
glRotatef(beta, 0.0f, 1.0f, 0.0f);
MyQuadric=gluNewQuadric();
gluQuadricDrawStyle(MyQuadric, GLU_LINE);
switch(FIG)
{
case FIG1: glutSolidTorus ( 0.3 , 0.5 , 25 , 50);
break;
case FIG2: glutSolidSphere ( 0.7 , 30 , 30 );
break;
case FIG3: glutSolidCone ( 0.7, 1 , 25, 30 ); ;
break;
case FIG4: glutWireTetrahedron ();
break;
case FIG5: glutWireOctahedron ();
break;
case FIG6: glutWireDodecahedron ();
break;
case FIG7: glutWireIcosahedron (); ;
break;
case FIG8: glutSolidTeapot(0.5);
break;
case FIG9: gluCylinder(MyQuadric, 0.3, 0.3, 0.9, 20, 10 );
break;
case FIG10: gluDisk(MyQuadric, 0.3, 0.5, 20, 5);
break;
case FIG11: gluPartialDisk(MyQuadric, 0.3, 0.5, 20, 5, 0, 90);
break;
case FIG12: gluSphere(MyQuadric, 0.7 , 30 , 30);
break;
}
glFlush();
glutSwapBuffers();
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(400,400);
glutInitWindowPosition(300,200);
glutCreateWindow("Transformaciones 2D");
glClearColor (0.0, 0.0, 0.0, 0.0);
glutDisplayFunc(DisplayX);
glutIdleFunc(idle);
glutMouseFunc(onMouse);
glutMotionFunc(onMotion);
glutSpecialFunc(Keys);
glutKeyboardFunc(SKeys);
Menu();
glutMainLoop();
}












