开发者

Opengl fixed light is still moving

I want to set a light in my 3d world, positioned in a corner and when I move with my mouse I want it to stay there and just be.

When I use the function from glut: glutSolidSphere, everything looks okay. But when I add a quad in my world and I move with my mouse, the lighting on the quad changes. Any idea how to solve this?

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_ambi开发者_如何学编程ent[] = {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);
}

So I setted the light call after the camera_setup() But now the quad is still not working as I want it to. The light still changes and I am pretty sure that my normals are correct (checked them twice).

I was thinking, is this possible because I don't change my _camPosX, Y, Z values when I move/rotate?


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


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜