c++ - openGL光照随相机旋转

标签 c++ opengl lighting

我正在尝试为 uni 类(class)构建一些简单的东西,我只是试图点亮它,但灯似乎随着相机旋转,这非常烦人......

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

我在 Display::Init 中添加了灯光(使用 PoolLight 类)。 相机在 Display::Resize 中设置。它位于 (0, 0, 80) 的“台球 table ”上方,向下看 (0, 0, -1),X 轴向前/向上 (1, 0, 0)。

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

我已经阅读了这里关于同一问题的其他一些答案,但我不太了解。我希望有人能用更简单的术语向我解释,或者可能发现一些我不小心排除的明显的东西。

无论如何,这是代码:

Main Display Class

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)。然后绘制场景。

关于c++ - openGL光照随相机旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5633253/

相关文章:

cocoa - 当我使用 OpenGL Core Profile 3.2 时,我的三角形不渲染

c++ - OpenGL : How to use glMapBuffer with Qt?

c++ - 打开 GL 照明问题

c - OpenGL 中奇怪的方形照明伪影

c++ - 推导以下代码的执行时间增长率(BigO)

c++ - SDL中如何实现转义序列

c++ - CUDA 并行化嵌套 for 循环

c++ - glm 在简单翻译中返回 nan

c++ - OpenGL 漫反射照明计算错误

c++ - 在C++中推导继承方法的父类