openGL光照随相机旋转

openGL lighting rotates with camera

本文关键字:相机 旋转 openGL      更新时间:2023-10-16

我正试图建立一些简单的大学课程,我刚刚试图点亮它,但灯光似乎与相机旋转,这是完全恼人的…

我已经使用粘贴器将我的代码粘贴到下面(这样它就不会拉伸页面),但这里也有一点解释:

我在Display::Init(使用PoolLight类)中添加了灯光。在Display::Resize中设置摄像头。它被放置在'pool table'的上方(0,0,80),向下看(0,0,-1),X轴向前/向上(1,0,0)。

相机的这个简单的移动和旋转也调整了灯光,但没有调整几何形状(或者它可能调整了几何形状,而不是灯光,我不是很确定)。

我在这里读了一些关于同样问题的其他答案,但我真的不太明白。我希望有人能用更简单的术语给我解释一下,或者指出一些我不小心漏掉的明显的东西。

总之,下面是代码:

主显示类

PoolLight类:

#include "PoolLight.h"
#include "Glut/glut.h"
#include "GL/gl.h"
#include "GL/glu.h"
PoolLight::PoolLight(GLenum lightNumber, GLenum lightType, float red, float green, float blue, bool distant, float posX, float posY, float posZ)
{
    this->lightNumber = lightNumber;
    this->lightType = lightType;
    color[0] = red; color[1] = green; color[2] = blue; color[3] = 1;
    position[0] = posX; position[1] = posY; position[2] = posZ; position[3] = (int) (!distant);
    glLightfv(lightNumber, lightType, color);
    glLightfv(lightNumber, GL_POSITION, position);
    enabled(true);
}

PoolLight::~PoolLight(void)
{
}
void PoolLight::setSpotlight(float angle, float attenuation, float dirX, float dirY, float dirZ) {
    glLightf(lightNumber, GL_SPOT_EXPONENT, angle);
    glLightf(lightNumber, GL_CONSTANT_ATTENUATION, attenuation);
    glLightf(lightNumber, GL_LINEAR_ATTENUATION, 0.0f);
    glLightf(lightNumber, GL_QUADRATIC_ATTENUATION, 0.0f);
    spotDirection[0] = dirX; spotDirection[1] = dirY; spotDirection[2] = dirZ;
    glLightfv(lightNumber, GL_SPOT_DIRECTION, spotDirection);
    glLightf(lightNumber, GL_SPOT_CUTOFF, 60);
}
void PoolLight::enabled(bool enabled) {
    if (enabled) glEnable(lightNumber);
    else glDisable(lightNumber);
}

改变摄像机位置后,用光源的世界坐标再次调用glLightfv(lightNumber, GL_POSITION, position)。然后绘制场景