当我使用点光源着色器渲染我的3D场景时,我遇到了一些问题。灯光似乎与相机一起旋转,灯光也像定向光而不是点光源。我正在关注glsl点光源的本教程。 GLSL Core Tutorial – Point Lights
我的顶点着色器:
#version 150 core
in vec4 in_Position;
in vec4 in_Color;
in vec2 in_TextureCoord;
in vec3 in_Normal;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
uniform mat3 normal;
out Data {
vec3 normal;
vec3 eye;
vec3 lightDir;
vec2 st;
} Out;
void main(void) {
vec4 pos = view * model * in_Position;
vec3 norm = normal * in_Normal;
//Light Position
vec4 l_pos = view * model * vec4(0,1,0,1);
Out.normal = norm;
Out.lightDir = vec3(l_pos - pos);
Out.st = in_TextureCoord;
Out.eye = vec3(-pos);
gl_Position = projection * view * model * in_Position;
}
My Fragment Shader:
#version 150 core
uniform sampler2D texture_diffuse;
in Data {
vec3 normal;
vec3 eye;
vec3 lightDir;
vec2 st;
} DataIn;
out vec4 out_Color;
void main(void) {
vec4 diffuse = texture(texture_diffuse, DataIn.st).rgba;
vec4 spec = vec4(0.0);
vec4 specular = vec4(0.2,0.2,0.2,1);
vec4 ambient = vec4(0.2,0.2,0.2,1);
float shininess = 100;
vec3 n = normalize(DataIn.normal);
vec3 l = normalize(DataIn.lightDir);
vec3 e = normalize(DataIn.eye);
float intensity = max(dot(n,l), 0.0);
if (intensity > 0.0) {
vec3 h = normalize(l + e);
float intSpec = max(dot(h,n), 0.0);
spec = specular * pow(intSpec, shininess);
}
out_Color = max(intensity * diffuse + spec, ambient);
}
问题图片: