旋转相机时的OpenGL光照问题

发布于 2024-08-14 13:28:13 字数 830 浏览 6 评论 0原文

我在游戏世界中绘制建筑物,并使用以下代码对它们进行着色:

GLfloat light_ambient[] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat light_position[] = {135.66f, 129.83f, 4.7f, 1.0f};

glShadeModel(GL_SMOOTH);

glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);

glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glColorMaterial(GL_FRONT, GL_AMBIENT);

效果很好。

但是当我开始在世界中飞行时,灯光会对此做出反应,就好像世界是一个正在旋转的物体一样。所以当我的相机角度改变时,灯光也会改变。

我如何恢复该旋转?所以照明会认为我实际上并没有旋转世界,然后我可以让我的建筑物具有静态阴影,该阴影会根据太阳在天空中的位置而变化。

编辑:这是渲染代码:

int DrawGLScene()
{

    // stuff

    glLoadIdentity();

    glRotatef(XROT, 1.0f, 0.0f, 0.0f);
    glRotatef(YROT, 0.0f, 1.0f, 0.0f);
    glRotatef(ZROT, 0.0f, 0.0f, 1.0f);

    glTranslatef(-XPOS, -YPOS, -ZPOS);

    // draw world
}

I draw buildings in my game world, i shade them with the following code:

GLfloat light_ambient[] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat light_position[] = {135.66f, 129.83f, 4.7f, 1.0f};

glShadeModel(GL_SMOOTH);

glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);

glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glColorMaterial(GL_FRONT, GL_AMBIENT);

It works nicely.

But when i start flying in the world, the lighting reacts to that as if the world was an object that is being rotated. So the lights changes when my camera angle changes.

How do i revert that rotation? so the lighting would think that i am not actually rotating the world, and then i could make my buildings have static shading which would change depending on where the sun is on the sky.

Edit: here is the rendering code:

int DrawGLScene()
{

    // stuff

    glLoadIdentity();

    glRotatef(XROT, 1.0f, 0.0f, 0.0f);
    glRotatef(YROT, 0.0f, 1.0f, 0.0f);
    glRotatef(ZROT, 0.0f, 0.0f, 1.0f);

    glTranslatef(-XPOS, -YPOS, -ZPOS);

    // draw world
}

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

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

发布评论

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

评论(2

纵情客 2024-08-21 13:28:13

http://www.opengl.org/resources/faq/technical/lights。 htm

请参阅#18.050

简而言之,您需要确保在正确的参考帧中定义灯光位置,并在每个帧中应用适当的变换以将其保持在您想要的位置。

编辑:

随着 OpenGL 3.1 中固定功能管道的删除,此处的代码和答案已被弃用。现在正确的答案是将您的灯光位置传递到顶点/片段着色器中,并使用世界空间中的该位置执行着色。此计算根据您正在进行的光照类型(PBR、phong、延迟等)而有所不同。

http://www.opengl.org/resources/faq/technical/lights.htm

See #18.050

In short, you need to make sure you're defining your light position in the right reference frame and applying the appropriate transforms each frame to keep it where you want it.

Edit:

With the removal of the fixed function pipeline in OpenGL 3.1 the code and answer here are deprecated. The correct answer now is to pass your light position(s) into your vertex/fragment shaders and perform your shading using that position in world space. This calculation varies based on the type of lighting you're doing (PBR, phong, deferred etc).

很快妥协 2024-08-21 13:28:13

我相信你的问题是由于 OpenGL Lights 在世界坐标中指定,但内部存储在相机坐标中这一事实引起的。这意味着一旦放置了灯光,每当您移动相机时,灯光都会随之移动。

要解决此问题,只需在移动相机时重新定位灯光即可。

I believe your problem arises from the fact that OpenGL Lights are specified in World Coordinates, but stored internally in Camera Coordinates. This means that once you place a light, whenever you move your camera, your light will move with it.

To fix this, simply reposition your lights whenever you move your camera.

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