旋转相机时的OpenGL照明问题 [英] OpenGL lighting problem when rotating the camera

查看:64
本文介绍了旋转相机时的OpenGL照明问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

效果很好.

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

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.

这里是渲染代码:

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
}

推荐答案

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

见#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.

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

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).

这篇关于旋转相机时的OpenGL照明问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆