opengl固定灯还在动

发布于 2024-11-08 16:47:10 字数 3819 浏览 0 评论 0原文

我想在 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 值?

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把回忆走一遍 2024-11-15 16:47:10

glRotate / glTranslate 之前调用 gluLookAt 应该会有所帮助...

其他想法:

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

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
雨落星ぅ辰 2024-11-15 16:47:10

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

glLightfv(GL_LIGHT0+n, GL_POSITION, light_position)

在之后立即添加调用

camera->setup();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文