Domanda

Voglio impostare una luce nel mio mondo 3D, posizionato in un angolo e quando mi muovo con il mio mouse lo voglio che rimanga lì e solo essere.

Quando uso la funzione da Glut: GlutolidSphere, tutto sembra ok. Ma quando aggiungo un quad nel mio mondo e mi muovo con il mio mouse, l'illuminazione sul quad cambia.Qualche idea come risolvere questo?

void World::paint(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

     float no_mat[] = {0.0f, 0.0f, 0.0f, 1.0f};
     float mat_ambient[] = {0.7f, 0.7f, 0.7f, 1.0f};
     float mat_ambient_color[] = {0.8f, 0.8f, 0.2f, 1.0f};
     float mat_diffuse[] = {0.1f, 0.5f, 0.8f, 1.0f};
     float mat_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
     float no_shininess = 0.0f;
     float low_shininess = 5.0f;
     float high_shininess = 100.0f;
     float mat_emission[] = {0.3f, 0.2f, 0.2f, 0.0f};



     camera->setup();
     light->assignComponentsToGLLightX();
     glEnable(GL_LIGHTING);

    glMatrixMode( GL_MODELVIEW );




     // WORKS
     float temp[] = {0.7f, 0.0f, 0.0f, 1.0f};
     glPushMatrix();

     glTranslatef(3.75f, 3.0f, 0.0f);
     glMaterialfv(GL_FRONT, GL_AMBIENT, temp);
     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
     glMaterialf(GL_FRONT, GL_SHININESS, low_shininess);
     glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
     glutSolidSphere( 3.0, 25, 25 );
     glPopMatrix();


     // Doesn't work
       glPushMatrix();

       glTranslatef(0.0f, 0.0f, 0.0f);
       glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
       glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
       glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
       glMaterialf(GL_FRONT, GL_SHININESS, low_shininess);
       glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);

       glBegin(GL_QUADS);
       //Front
       //glNormal3f(0.0f, 0.0f, 1.0f);
       glNormal3f(-1.0f, 0.0f, 1.0f);

       glVertex3f(-1.5f, -1.0f, 1.5f);
       glNormal3f(1.0f, 0.0f, 1.0f);
       glVertex3f(1.5f, -1.0f, 1.5f);
       glNormal3f(1.0f, 0.0f, 1.0f);
       glVertex3f(1.5f, 1.0f, 1.5f);
       glNormal3f(-1.0f, 0.0f, 1.0f);
       glVertex3f(-1.5f, 1.0f, 1.5f);

       //Right
       //glNormal3f(1.0f, 0.0f, 0.0f);
       glNormal3f(1.0f, 0.0f, -1.0f);
       glVertex3f(1.5f, -1.0f, -1.5f);
       glNormal3f(1.0f, 0.0f, -1.0f);
       glVertex3f(1.5f, 1.0f, -1.5f);
       glNormal3f(1.0f, 0.0f, 1.0f);
       glVertex3f(1.5f, 1.0f, 1.5f);
       glNormal3f(1.0f, 0.0f, 1.0f);
       glVertex3f(1.5f, -1.0f, 1.5f);

       //Back
       //glNormal3f(0.0f, 0.0f, -1.0f);
       glNormal3f(-1.0f, 0.0f, -1.0f);
       glVertex3f(-1.5f, -1.0f, -1.5f);
       glNormal3f(-1.0f, 0.0f, -1.0f);
       glVertex3f(-1.5f, 1.0f, -1.5f);
       glNormal3f(1.0f, 0.0f, -1.0f);
       glVertex3f(1.5f, 1.0f, -1.5f);
       glNormal3f(1.0f, 0.0f, -1.0f);
       glVertex3f(1.5f, -1.0f, -1.5f);

       //Left
       //glNormal3f(-1.0f, 0.0f, 0.0f);
       glNormal3f(-1.0f, 0.0f, -1.0f);
       glVertex3f(-1.5f, -1.0f, -1.5f);
       glNormal3f(-1.0f, 0.0f, 1.0f);
       glVertex3f(-1.5f, -1.0f, 1.5f);
       glNormal3f(-1.0f, 0.0f, 1.0f);
       glVertex3f(-1.5f, 1.0f, 1.5f);
       glNormal3f(-1.0f, 0.0f, -1.0f);
       glVertex3f(-1.5f, 1.0f, -1.5f);
       glEnd();
   glPopMatrix();

void Camera::setup() const
{
    glRotatef(_rotX, 1.0,0.0,0.0);
    glRotatef(_rotY, 0.0,1.0,0.0);
    glTranslated(-_moveX,-_moveY,-_moveZ);

    gluLookAt(3.0 , 5.0 , 25.0,
              0.0, 0.0, 0.0,
              0.0,   1.0,   0.0 );

}

void Light::assignComponentsToGLLightX() const
{
    glLightfv(_light, GL_AMBIENT, _ambientLight);
    glLightfv(_light, GL_DIFFUSE, _diffuseLight);
    glLightfv(_light, GL_SPECULAR, _specularLight);
    glLightfv(_light, GL_POSITION, _position);
}
.

Quindi ho impostato la chiara della luce dopo la fotocamera_setup () Ma ora il quad non funziona ancora come lo voglio.La luce cambia ancora e sono abbastanza sicuro che le mie normali siano corrette (li ha controllati due volte).

Stavo pensando, è possibile perché non modifico i miei valori _campoSx, y, z quando mi sposta / ruotando?

È stato utile?

Soluzione

Calling gluLookAt before glRotate / glTranslate should help...

Other thoughts :

  • call glMatrixMode and glLoadIdentity only once, in camera::setup.
  • don't call glRotate and glTranslate in camera::setup. This has nothing to do with the camera. This is your object's tranformation
  • Wrap glRotate and glTranslate inside glPushMatrix/glPopMatrix. This way, the next time you draw an object, transformations won't be cummulated.
  • glTranslatef(0.0f, 0.0f, 0.0f) is useless. It means : add a null displacement to the current matrix. In other words: don't do anything
  • wrapping pushmatrix/popmatrix around this is thus useless
  • As datenwolf said, I don't see where you actually indicate the light's position

Altri suggerimenti

You must set the lights' positions after moving the world / setting the camera i.e. add

glLightfv(GL_LIGHT0+n, GL_POSITION, light_position)

calls right after

camera->setup();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top