我正在尝试进行realistic昼夜循环,但是方向灯似乎没有照亮我对象的正面和背面。
[当另一侧照亮时,看起来真的很不自然。
#version 330
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
in vec3 position;
in vec2 textureCoords;
in vec3 vertNormal;
out vec2 newTexture;
out vec3 fragNormal;
void main()
{
fragNormal = (model*vec4(vertNormal, 0.0f)).xyz;
gl_Position = projection * view * model * vec4(position, 1.0f);
newTexture = textureCoords;
}
#version 330
in vec2 newTexture;
in vec3 fragNormal;
out vec4 outColor;
uniform sampler2D samplerTexture;
uniform float sunx;
uniform float suny;
void main()
{
vec3 ambientLightIntensity = vec3(0.1f, 0.1f, 0.1f);
vec3 sunLightIntensity = vec3(0.9f, 0.9f, 0.9f);
vec3 sunLightDirection = normalize(vec3(sunx, suny, 0f)-fragNormal);
vec4 texel = texture(samplerTexture, newTexture);
vec3 lightIntensity = ambientLightIntensity + sunLightIntensity * max(dot(fragNormal,
sunLightDirection), 0.0f);
outColor = vec4(texel.rgb * lightIntensity, texel.a);
}
if sunx == 255:
reversepath = -1
if sunx == -255:
reversepath = 1
suny = (reversepath*-1)*math.sqrt((255**2)-(sunx**2))
sunx = sunx + (reversepath*8.5)
陈述
vec3(sunx, suny, 0f) - fragNormal
没有意义,因为vec3(sunx, suny, 0f)
是位置,fragNormal
是向量。
要从片段到光源获取矢量,必须从光源的位置减去片段的位置:
vec3(sunx, suny, 0f) - fragPosition
顶点着色器:
#version 330
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
in vec3 position;
in vec2 textureCoords;
in vec3 vertNormal;
out vec2 newTexture;
out vec3 fragNormal;
out vec3 fragPosition;
void main()
{
fragNormal = mat3(model) * vertNormal;
fragPosition = (model * vec4(position, 0.0f)).xyz;
gl_Position = projection * view * model * vec4(position, 1.0f);
newTexture = textureCoords;
}
片段着色器
#version 330
in vec2 newTexture;
in vec3 fragNormal;
in vec3 fragPosition;
out vec4 outColor;
uniform sampler2D samplerTexture;
uniform float sunx;
uniform float suny;
void main()
{
vec3 ambientLightIntensity = vec3(0.1f, 0.1f, 0.1f);
vec3 sunLightIntensity = vec3(0.9f, 0.9f, 0.9f);
vec3 sunLightDirection = normalize(vec3(sunx, suny, 0f) - fragPosition);
vec4 texel = texture(samplerTexture, newTexture);
vec3 lightIntensity = ambientLightIntensity + sunLightIntensity *
max(dot(fragNormal, sunLightDirection), 0.0f);
outColor = vec4(texel.rgb * lightIntensity, texel.a);
}