1

我想在我的 3d 世界中设置一盏灯,定位在一个角落,当我用鼠标移动时,我希望它保持在那里并保持原样。

当我使用 glut: glutSolidSphere 中的函数时,一切看起来都还不错。但是当我在我的世界中添加一个四边形并用鼠标移动时,四边形上的灯光会发生变化。知道如何解决这个问题吗?

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);
}

所以我在 camera_setup() 之后设置了灯光调用,但现在四边形仍然无法按我的意愿工作。灯光仍然在变化,我很确定我的法线是正确的(检查了两次)。

我在想,这可能是因为我在移动/旋转时没有改变我的 _camPosX、Y、Z 值吗?

4

2 回答 2

3

打电话gluLookAt之前glRotate/glTranslate应该有帮助...

其他想法 :

  • 在 camera::setup 中只调用一次 glMatrixMode 和 glLoadIdentity。
  • 不要在 camera::setup 中调用 glRotate 和 glTranslate。这与相机无关。这是您的对象的转换
  • 将 glRotate 和 glTranslate 包裹在 glPushMatrix/glPopMatrix 中。这样,下次绘制对象时,变换就不会累积。
  • glTranslatef(0.0f, 0.0f, 0.0f) 没用。这意味着:向当前矩阵添加一个零位移。换句话说:什么都不做
  • 因此,将 pushmatrix/popmatrix 包裹起来是无用的
  • 正如 datenwolf 所说,我看不到您实际指示灯光位置的位置
于 2011-05-18T18:49:09.780 回答
2

您必须在移动世界/设置相机后设置灯光的位置,即添加

glLightfv(GL_LIGHT0+n, GL_POSITION, light_position)

之后马上打电话

camera->setup();
于 2011-05-18T15:41:11.080 回答