Open GL 光照问题
我已经在游戏引擎上工作了一个月了,我已经完成了基本的 OpenGL 内容。然而,我无法像我期望的那样开始工作的一件事是照明。 (注:这是我第一次认真使用 OpenGL)
我想要的是接近真实的光照模拟 - 面向光的表面比远处的表面被照亮更多,等等。基本光应该有一个位置和一种颜色。这就是我认为它可以实现的方式:
float lightv[4]; // populated with (0.6, 0.6, 0.6, 1)
float positionv[4]; // populated with (0, 10, 0, 1)
int lightID = GL_LIGHT0;
int attenuationType = GL_LINEAR_ATTENUATION;
float attenuationValue = 1;
glLightf(lightID, attenuationType, attenuationValue);
glLightfv(lightID, GL_DIFFUSE, lightv);
glLightfv(lightID, GL_POSITION, positionv);
glEnable(lightID);
它没有做我期望它做的事情,而是给我照明,就好像相机所在的地方有一盏灯一样!每个表面都有相同的照明!
我做错了什么?
谢谢你,我很感激。
I've been working on a game engine for a month now, and I've finished the basic OpenGL stuff. However, the one thing that I can't get to work like I expect it to is the lighting.
(Note: This is the first time I seriously worked with OpenGL)
What I want is a close to realistic lighting simulation - where the surfaces facing the light are lit up more than those farther, etc.. The basic light should have a position and a color. This is how I thought it could be implemented:
float lightv[4]; // populated with (0.6, 0.6, 0.6, 1)
float positionv[4]; // populated with (0, 10, 0, 1)
int lightID = GL_LIGHT0;
int attenuationType = GL_LINEAR_ATTENUATION;
float attenuationValue = 1;
glLightf(lightID, attenuationType, attenuationValue);
glLightfv(lightID, GL_DIFFUSE, lightv);
glLightfv(lightID, GL_POSITION, positionv);
glEnable(lightID);
Instead of doing what I expect it to do, it gives me lighting as if there was a light where the camera is! Every surface has the same lighting!
What am I doing wrong?
Thank you, I appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先要做的是确保在某个时刻调用了
glEnable(GL_LIGHTING);
。除此之外,首先要检查的是您的法线是否正确。为了使光照正常工作,您需要为绘制的每个顶点设置一个法线集。如果您已经设置了法线,则应确保它们都是单位长度。如果它们的长度不是一,光照就会表现得很奇怪。如果这就是应有的样子,那么您应该记住,当您设置灯光的位置时,它会被当前的模型视图矩阵修改,就像它是一个顶点一样。如果这些事情都不相关,我会看看是否可以进一步想出一些东西。The first thing to do is make sure you have
glEnable(GL_LIGHTING);
called at some point. Past that, the first thing to check is that your normals are correct. For lighting to work properly you will need to have a normal set for every vertex you draw. If you have already set your normals, you should make sure that they are all of unit length. If they do not have a length of one, lighting can act oddly. If that is all as it should be, you should keep in mind that when you set the position of a light, it is modified by the current Modelview matrix as if it were a vertex. If none of those things are relevant, I'll see if I can think of something further.设置
GL_MODELVIEW
变换后设置灯光位置,因为它受当前变换矩阵的影响。否则你就会得到你发现的“头灯”效果。Set your light position after you set up your
GL_MODELVIEW
transform, since it's affected by the current transform matrix. Otherwise you get the "headlamp" effect you have discovered.