GLSL point light shader moving with camera

前端 未结 2 611
鱼传尺愫
鱼传尺愫 2021-01-24 08:00

I\'ve been trying to make a basic static point light using shaders for an LWJGL game, but it appears as if the light is moving as the camera\'s position is being translated and

相关标签:
2条回答
  • 2021-01-24 08:50

    The critical value here, lightPos, was being set as a function of vertexPos, which you have expressed in screen space (this happened because its original world space form was multiplied by modelView). Screen space stays with the camera, not anything in the 3D world. So to have a non-moving light source with respect to some absolute point in world space (like [4.0, 4.0, 4.0]), you could just leave your object's points in that space as you found out.

    But getting rid of modelview is not a good idea, since the whole point of the model matrix is to place your objects where they belong (so you can re-use your vertex arrays with changes only to the model matrix, instead of burdening them with specifying every single shape's vertex positions from scratch).

    A better way is to perform the modelView multiplication on both vertexPos AND lightPos. This way you're treating lightPos as originally a quantity in world space, but then doing the comparison in screen space. The math to get light intensities from normals will work out to the same in either space and you'll get a correct looking light source.

    0 讨论(0)
  • 2021-01-24 09:06

    If anyone's interested, I ended up finding the solution. Removing the calls to gl_NormalMatrix and gl_ModelViewMatrix solved the problem.

    0 讨论(0)
提交回复
热议问题