/
Inicio :: Foros

 F.A.Q.F.A.Q.                  Conéctese para revisar sus mensajesConéctese para revisar sus mensajes   

Duda OpenGL

 
      Índice del Foro elrincondelc.com -> Gráficos
Ver tema anterior :: Ver siguiente tema  
AutorMensaje
cazagavilan



Registrado: 14 Abr 2011
Mensajes: 75

MensajePublicado: 30/04/2012 12:52 pm
Título: Duda OpenGL

Hola!

Tengo un codigo en el que he añadido unas luces y tengo que hacer que se desactiven o activen con ciertas teclas, he probado varias cosas pero no me funciona...

A ver si me podeis dar una pista.

Código:
#include <stdlib.h>
#include <gl\glut.h>
#include "windows.h"

static int spin = 0; // Angulo de giro

// Inicializamos propiedades de material, fuente de luz, modelo de iluminacion y bufer de profundidad
void init(void)
{
   glClearColor(0.0,0.0,0.0,0.0); // Color de fondo
   glShadeModel(GL_SMOOTH); // Como se van a colocar los colores
   // glShadeMode(GL_FLAT)
   glEnable(GL_LIGHTING);// Activando luces
   glEnable(GL_LIGHT0);
   glEnable(GL_LIGHT1);
   glEnable(GL_LIGHT2);
   glEnable(GL_DEPTH_TEST); // Activando buffer de profundidad
}

// ---------------- FUNCION DE DIBUJADO -------------------
void dibujado(void)
{
   GLfloat position0[] = {0.0, 0.0, 1.5, 1.0};
   GLfloat position1[] = {0.0, 1.5, 0.0, 1.0};
   GLfloat position2[] = {1.5, 0.0, 0.0, 1.0};
   GLfloat difusse0[] = {1.0, 0.0, 0.0, 1.0};
   GLfloat difusse1[] = {0.0, 1.0, 0.0, 1.0};
   GLfloat difusse2[] = {0.0, 0.0, 1.0, 1.0};
   GLfloat spec0[] = {1.0, 1.0, 1.0, 1.0};
   GLfloat spec1[] = {1.0, 1.0, 1.0, 1.0};
   GLfloat spec2[] = {1.0, 1.0, 1.0, 1.0};
//---------------- MATERIAL ORO --------------------
   GLfloat gold_amb[] = {0.24725, 0.1995, 0.0745, 1.0};
   GLfloat gold_dif[] = {0.75164, 0.60648, 0.22648, 1.0};
   GLfloat gold_spec[] = {0.628281, 0.555802, 0.366065, 1.0};
   GLfloat gold_shin = 51.2;

   glLightfv(GL_LIGHT0, GL_DIFFUSE, difusse0);
   glLightfv(GL_LIGHT1, GL_DIFFUSE, difusse1);
   glLightfv(GL_LIGHT2, GL_DIFFUSE, difusse2);

   glLightfv(GL_LIGHT0, GL_SPECULAR, spec0);
   glLightfv(GL_LIGHT1, GL_SPECULAR, spec1);
   glLightfv(GL_LIGHT2, GL_SPECULAR, spec2);

   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glPushMatrix();
   gluLookAt(5, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
   glPushMatrix();
   glRotated((GLdouble) spin, 0.0, 1.0, 0.0);
   glLightfv(GL_LIGHT0,GL_POSITION,position0);

   glTranslated(0.0, 0.0, 1.5);
   glDisable (GL_LIGHTING);

   glColor3f(1.0,0.0,0.0);
   glutWireSphere(0.1,10,10);
   glEnable(GL_LIGHTING);
   glPopMatrix();
   glPushMatrix();
   glRotated((GLdouble) spin*2, 1.0,0.0,0.0);
   glLightfv(GL_LIGHT1, GL_POSITION, position1);

   glTranslated(0.0,1.5,0.0);
   glDisable(GL_LIGHTING);
   glColor3f(0.0,1.0,0.0);
   glutWireSphere(0.1,10,10);
   glEnable(GL_LIGHTING);
   glPopMatrix();
   glPushMatrix();
   glRotated((GLdouble) spin*4, 0.0,0.0,1.0);
   glLightfv(GL_LIGHT2, GL_POSITION, position2);
   glTranslated(1.5,0.0,0.0);
   glDisable(GL_LIGHTING);
   glColor3f(0.0,0.0,1.0);
   glutWireSphere(0.1,10,10);
   glEnable(GL_LIGHTING);
   glPopMatrix();
   glMaterialfv(GL_FRONT, GL_AMBIENT, gold_amb);
   glMaterialfv(GL_FRONT, GL_DIFFUSE, gold_dif);
   glMaterialfv(GL_FRONT, GL_SPECULAR, gold_spec);
   glMaterialf(GL_FRONT, GL_SHININESS, gold_shin);
   glutSolidTeapot(1);
   glPopMatrix();
   glFlush();
}

//-------------------- FUNCION REDIMENSION ------------------
void redimensionar( int ancho, int alto)
{
   glViewport(0,0, (GLsizei) ancho, (GLsizei) alto);
   glMatrixMode(GL_PROJECTION); // Activamos la pila de modelado
   glLoadIdentity();
   gluPerspective(40.0, (GLfloat) ancho/ (GLfloat) alto, 1.0,20.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}

//-------------------- FUNCION TECLADO ----------------
void teclado(unsigned char key, int x, int y)
{
   switch(key)
   {
   case 114:
      

   case 27:
      exit(0);
      break;
   }
}

//-----------------------FUNCION ANIMACION -----------------
void animacion()
{
   spin = (spin+1) % 360;
   Sleep(50);
   glutPostRedisplay();
}

//--------------- PRINCIPAL ------------------
int main( int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
   glutInitWindowSize(800, 600);
   glutInitWindowPosition(100,100);
   glutCreateWindow(argv[0]);
   init();
   glutDisplayFunc(dibujado);
   glutReshapeFunc(redimensionar);
   glutIdleFunc(animacion);
   glutKeyboardFunc(teclado);
   glutMainLoop();
   return 0;
}


Muchas gracias!
Volver arriba
cazagavilan



Registrado: 14 Abr 2011
Mensajes: 75

MensajePublicado: 30/04/2012 1:01 pm
Título:

Yo imagino que deberia de ser: if (GL_LIGHT0 == "activado")
se desactiva y else glDisable(GL_LIGHT0), pero al no poder añadirle ningun valor... y tampoco se si OpenGL tiene algo para comprobar si esta la luz activa..
Volver arriba
daltomi



Registrado: 28 Abr 2007
Mensajes: 335
Ubicación: Argentina

MensajePublicado: 30/04/2012 6:23 pm
Título:

La cuestión es no tanto como desactivar las 3 luces (yo las llamo focales) sino el resultado final que quieras.
Si desactivas las luces focales el objeto "tetera" se dibujará como un contorno, una especie de mancha. Si quieres evitar ésto hay que modificar un poco tú código para que exista una luz que funcione como "luz ambiental", así la "tetera" se vera completa sin importar si las luces focales existen o no.
Te dejo un link de una captura que realice para mostrarte la cuestión:

Para la luz ambiental utiliza el indice GL_LIGHT0, ya que OpenGL tiene al menos 1 luz por defecto y es ésta.
Entonces las 3 luces focales son GL_LIGHT1, 2, 3. Para desactivarlas es como tú dices, usa glDisable y glEnable caso contrario.

Algo de código, al menos lo más importante para que puedas guiarte:
Código:

void crear_luces()
{
   const GLfloat difusse0[] = {1.0, 0.0, 0.0, 1.0};
   const GLfloat difusse1[] = {0.0, 1.0, 0.0, 1.0};
   const GLfloat difusse2[] = {0.0, 0.0, 1.0, 1.0};
   const GLfloat spec0[] = {1.0, 1.0, 1.0, 1.0};
   const GLfloat spec1[] = {1.0, 1.0, 1.0, 1.0};
   const GLfloat spec2[] = {1.0, 1.0, 1.0, 1.0};
//---------------- MATERIAL ORO --------------------
   const GLfloat gold_amb[] = {0.24725, 0.1995, 0.0745, 1.0};
   const GLfloat gold_dif[] = {0.75164, 0.60648, 0.22648, 1.0};
   const GLfloat gold_spec[] = {0.628281, 0.555802, 0.366065, 1.0};
   const GLfloat gold_shin = 51.2;

   glLightfv(GL_LIGHT1, GL_DIFFUSE, difusse0);
   glLightfv(GL_LIGHT2, GL_DIFFUSE, difusse1);
   glLightfv(GL_LIGHT3, GL_DIFFUSE, difusse2);

   glLightfv(GL_LIGHT1, GL_SPECULAR, spec0);
   glLightfv(GL_LIGHT2, GL_SPECULAR, spec1);
   glLightfv(GL_LIGHT3, GL_SPECULAR, spec2);
   
   glMaterialfv(GL_FRONT, GL_AMBIENT, gold_amb);
   glMaterialfv(GL_FRONT, GL_DIFFUSE, gold_dif);
   glMaterialfv(GL_FRONT, GL_SPECULAR, gold_spec);
   glMaterialf(GL_FRONT, GL_SHININESS, gold_shin);
}

void init(void)
{
   ....etc........

   crear_luces();
}


void luz_focal(bool b)
{
    if(b)
    {
        glEnable(GL_LIGHT1);
        glEnable(GL_LIGHT2);
        glEnable(GL_LIGHT3);
    }
    else
    {
        glDisable(GL_LIGHT1);
        glDisable(GL_LIGHT2);
        glDisable(GL_LIGHT3);
    }
}

void luz_ambiente(bool b)
{
    (b) ? glEnable(GL_LIGHT0) : glDisable(GL_LIGHT0);
}

void dibujado(void)
{
   const GLfloat position0[] = {0.0, 0.0, 1.5, 1.0};
   const GLfloat position1[] = {0.0, 1.5, 0.0, 1.0};
   const GLfloat position2[] = {1.5, 0.0, 0.0, 1.0};

   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   
   glPushMatrix();
   
   gluLookAt(5, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

   // desactivamos para todas las esferas.
   glDisable (GL_LIGHTING);

   glPushMatrix();
       glRotated((GLdouble) spin, 0.0, 1.0, 0.0);
       glLightfv(GL_LIGHT1,GL_POSITION,position0);
       glTranslated(0.0, 0.0, 1.5);
       glColor3f(1.0,0.0,0.0);
       glutSolidSphere(0.1,10,10);
   glPopMatrix();
       
   glPushMatrix();
        glRotated((GLdouble) spin*2, 1.0,0.0,0.0);
        glLightfv(GL_LIGHT2,GL_POSITION,position1);
        glTranslated(0.0,1.5,0.0);
        glColor3f(0.0,1.0,0.0);
        glutSolidSphere(0.1,10,10);
   glPopMatrix();

   glPushMatrix();
       glRotated((GLdouble) spin*4, 0.0,0.0,1.0);
       glLightfv(GL_LIGHT3,GL_POSITION,position2);
       glTranslated(1.5,0.0,0.0);
       glColor3f(0.0,0.0,1.0);
       glutSolidSphere(0.1,10,10);
   glPopMatrix();
   
   glEnable(GL_LIGHTING);

   glutSolidTeapot(1);
   
   glPopMatrix();
   
   glFlush();
}

void teclado(unsigned char key, int x, int y)
{
   static bool bLuzFocal = true;
   static bool bLuzAmbiente = true;
   static const unsigned char KEY_SCAPE = 27;

   switch(key)
   {
      case KEY_SCAPE:
         exit(0);
         break;
      case 'l':
         luz_focal(bLuzFocal);
         bLuzFocal = !bLuzFocal;
         break;
      case 'a':
         luz_ambiente(bLuzAmbiente);
         bLuzAmbiente = !bLuzAmbiente;
         break;
   }
}


Saludos
Volver arriba
Dirección AIM
cazagavilan



Registrado: 14 Abr 2011
Mensajes: 75

MensajePublicado: 03/05/2012 12:02 am
Título:

Muchas gracias
Volver arriba
      Índice del Foro elrincondelc.com -> Gráficos
Página 1 de 1Todas las horas están en GMT - 8 Horas

 
No puede crear mensajes
No puede responder temas
No puede editar sus mensajes
No puede borrar sus mensajes
No puede votar en encuestas

(c) ElRincondelC.com

Un proyecto de UrlanHeat.com