GLSL点光源着色器随相机移动 [英] GLSL point light shader moving with camera

查看:127
本文介绍了GLSL点光源着色器随相机移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试为LWJGL游戏使用着色器制作基本的静态点光源,但是随着相机位置的平移和旋转,该光源看起来好像在移动.这些着色器是从OpenGL 4.3指南中稍作修改的,因此我不确定为什么它们不能按预期工作.谁能解释为什么这些着色器无法正常工作,以及我如何才能使它们正常工作?

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 rotated. These shaders are slightly modified from the OpenGL 4.3 guide, so I'm not sure why they aren't working as intended. Can anyone explain why these shaders aren't working as intended and what I can do to get them to work?

顶点着色器:

varying vec3 color, normal;
varying vec4 vertexPos;

void main() {
    color = vec3(0.4);
    normal = normalize(gl_NormalMatrix * gl_Normal);
    vertexPos = gl_ModelViewMatrix * gl_Vertex;
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

片段着色器:

varying vec3 color, normal;
varying vec4 vertexPos;

void main() {
    vec3 lightPos = vec3(4.0);
    vec3 lightColor = vec3(0.75);
    vec3 lightDir = lightPos - vertexPos.xyz;
    float lightDist = length(lightDir);
    float attenuation = 1.0 / (3.0 + 0.007 * lightDist +  0.000008 * lightDist * lightDist);
    float diffuse = max(0.0, dot(normal, lightDir));
    vec3 ambient = vec3(0.4, 0.4, 0.4);
    vec3 finalColor = color * (ambient + lightColor * diffuse * attenuation);
    gl_FragColor = vec4(finalColor, 1.0);
}

推荐答案

如果有人感兴趣,我最终找到了解决方案.删除对gl_NormalMatrix和gl_ModelViewMatrix的调用即可解决此问题.

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

这篇关于GLSL点光源着色器随相机移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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