#why does my light went through objects?

10 messages · Page 1 of 1 (latest)

somber star
#

#version 460
#extension GL_KHR_vulkan_glsl : enable

layout(location = 0) in vec3 NORMAL;
layout(location = 1) in vec2 TEXCOORD;
layout(location = 2) in vec3 COLOR;
layout(location = 3) in vec3 FRAGMENT_POSITION;
layout(location = 4) in vec3 CAMERA_POSITION;

layout(location = 0) out vec4 RESULT;

layout(set = 1, binding = 0) uniform DirectionalUBO
{
    vec4 direction;
    vec4 ambient;
    vec4 diffuse;
    vec4 specular;
} directionalUBO;

layout(set = 2, binding = 0) uniform sampler2D albedo;
layout(set = 2, binding = 1) uniform sampler2D roughness;

vec3 calculateDirectionalLight(vec3 normal, vec3 viewDirection);

void main()
{
    vec3 normal        = normalize(NORMAL);
    vec3 viewDirection = normalize(CAMERA_POSITION - FRAGMENT_POSITION);

    vec3 result = vec3(0.0f);
    result += calculateDirectionalLight(normal, viewDirection);

    RESULT = vec4(result, 1.0f);
}

vec3 calculateDirectionalLight(vec3 normal, vec3 viewDirection)
{
    vec3 lightDirection      = normalize(-directionalUBO.direction.rgb);
    vec3 reflectionDirection = reflect(-lightDirection, normal);
    vec3 halfwayDirection    = normalize(lightDirection + viewDirection);

    vec3 ambient  = directionalUBO.ambient.rgb * texture(albedo, TEXCOORD).rgb;
    vec3 diffuse  = directionalUBO.diffuse.rgb * max(dot(normal, lightDirection), 0.0) * texture(albedo, TEXCOORD).rgb;
    
    //Phong
    //vec3 specular = globalUBO.specular.rgb * pow(max(dot(viewDirection, reflectionDirection), 0.0), 32.0f) * texture(roughness, TEXCOORD).g;
    
    //Blinn-Phong
    vec3 specular = directionalUBO.specular.rgb * pow(max(dot(normal, halfwayDirection), 0.0), 32.0f) * texture(roughness, TEXCOORD).g;

    return (ambient + diffuse + specular);
}
sturdy anchor
#

Because nothing in your code is attempting to occluding or shadow the light. In order to do that you would need to look into techniques such as shadowmapping.

hazy dune
#

Why are you passing fragment position and camera position through a vertex binding? Shouldn't you calculate fragment position in the vertex shader itself and pass the camera position in a UBO or something?

somber star
#
#version 460

layout(location = 0) in vec3 aPosition;
layout(location = 1) in vec3 aNormal;
layout(location = 2) in vec2 aTexCoord;
layout(location = 3) in vec3 aColor;

layout(location = 0) out vec3 NORMAL;
layout(location = 1) out vec2 TEXCOORD;
layout(location = 2) out vec3 COLOR;
layout(location = 3) out vec3 FRAGMENT_POSITION;
layout(location = 4) out vec3 CAMERA_POSITION;

layout(push_constant) uniform Constant
{
    mat4 model;
} constant;

layout(set = 0, binding = 0) uniform CameraUBO
{
    mat4 view;
    mat4 projection;
    vec3 position;
} cameraUBO;

void main()
{
    NORMAL            = aNormal;
    TEXCOORD          = aTexCoord;
    COLOR             = aColor;
    FRAGMENT_POSITION = vec3(constant.model * vec4(aPosition, 1.0));
    CAMERA_POSITION   = cameraUBO.position; 

    gl_Position = cameraUBO.projection * cameraUBO.view * vec4(FRAGMENT_POSITION, 1.0);
}
hazy dune
#

Everything looks good except one little problem with you CameraUBO. Do you know the STD140/430 layout rules? See the uniform block layout section under uniform buffer objects https://learnopengl.com/Advanced-OpenGL/Advanced-GLSL for details, but the TLDR is that a vec3 is 12 bytes, and GLSL requires UBO's to be aligned to 16 bytes by default. So you should either manually align your struct that you upload (alignas(16) after struct keyword in C++), or better yet prefer using vec4s (and you can use the extra component to store something else). WARNING: I don't know if vulkan has some other way of doing this.

somber star
#

@hazy dune like this?

#

so, i can leave last one as 0.0?

#

because positiion only vec3

hazy dune
#

I prefer to align the entire struct: (VULKAN_GLSL_DATA just expands to alignas(16)). And yeah you can leave it to be anything. Just make sure to only use the xyz component from the vector. So CAMERA_POSITION = cameraUBO.position.xyz;