Opengl固定灯仍然在移动

时间:2011-05-18 14:02:34

标签: opengl light material

我想在我的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()之后设置了light调用 但是现在四核仍然无法正常工作。灯仍在变化,我很确定我的法线是正确的(检查了两次)。

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

2 个答案:

答案 0 :(得分:3)

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

其他想法:

  • 在camera :: setup中只调用glMatrixMode和glLoadIdentity一次。
  • 不要在camera :: setup中调用glRotate和glTranslate。这与相机无关。这是你的对象的转换
  • 在glPushMatrix / glPopMatrix中包装glRotate和glTranslate。这样,下次绘制对象时,转换将不会被累积。
  • glTranslatef(0.0f,0.0f,0.0f)没用。这意味着:向当前矩阵添加空位移。换句话说:不要做任何事情
  • 将pushmatrix / popmatrix包裹在此周围是没用的
  • 正如datenwolf所说,我看不到你实际指明灯光位置的位置

答案 1 :(得分:2)

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

glLightfv(GL_LIGHT0+n, GL_POSITION, light_position)

后立即拨打电话

camera->setup();