OpenGL定向光不照亮对象的正面和背面

问题描述 投票:0回答:1

我正在尝试进行realistic昼夜循环,但是方向灯似乎没有照亮我对象的正面和背面。

[当另一侧照亮时,看起来真的很不自然。

Day night cycle gif

顶点着色器

#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)
opengl glsl lighting pyopengl
1个回答
0
投票

陈述

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);

}
© www.soinside.com 2019 - 2024. All rights reserved.