GLSL片段着色器输出黑屏

时间:2017-11-05 15:51:58

标签: opengl graphics glsl shadow-mapping

所以,这是我的片段着色器,带有闪电般的闪电和阴影:

#version 330 core

in vec3 FragmentPosition;
in vec3 Normal;
in vec2 TextureCoord;

out vec4 frag_color;


uniform bool use_texture;

uniform int texture_id;
uniform sampler2D checkers_texture;
uniform sampler2D red_checkers_texture;

uniform vec3 object_color;

uniform vec3 light_position;
uniform vec3 light_color;

uniform vec3 view_position;

uniform float far_plane;
uniform samplerCube depth_map;


struct LightningData {
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};


const int GridSamples = 20;
vec3 GridSamplingDisk[GridSamples] = vec3[]
(
   vec3(1, 1,  1), vec3( 1, -1,  1), vec3(-1, -1,  1), vec3(-1, 1,  1),
   vec3(1, 1, -1), vec3( 1, -1, -1), vec3(-1, -1, -1), vec3(-1, 1, -1),
   vec3(1, 1,  0), vec3( 1, -1,  0), vec3(-1, -1,  0), vec3(-1, 1,  0),
   vec3(1, 0,  1), vec3(-1,  0,  1), vec3( 1,  0, -1), vec3(-1, 0, -1),
   vec3(0, 1,  1), vec3( 0, -1,  1), vec3( 0, -1, -1), vec3( 0, 1, -1)
);


LightningData blinn_phong(vec3 color) {
    LightningData result;

    result.ambient = 0.05 * color;

    vec3 light_direction = normalize(light_position - FragmentPosition);
    vec3 normal = normalize(Normal);
    float diff = max(dot(light_direction, normal), 0.0);
    result.diffuse = diff * color;

    vec3 view_direction = normalize(view_position - FragmentPosition);
    vec3 reflect_direction = reflect(-light_direction, normal);

    vec3 halfway_direction = normalize(light_direction + view_direction);

    float BlinnPhongComponent = 32.0;
    float spec = pow(max(dot(normal, halfway_direction), 0.0), BlinnPhongComponent);
    result.specular = light_color * spec;

    return result;
}


float calculate_shadow() {
    vec3 fragment_to_light = FragmentPosition - light_position;

    float closest_depth = texture(depth_map, fragment_to_light).r * far_plane;
    float current_depth = length(fragment_to_light);

    float Fault = 0.05;
    return ((current_depth - Fault) > closest_depth) ? 1.0 : 0.0;
}


void shadow_debug() {
    vec3 fragment_to_light = FragmentPosition - light_position;

    float closest_depth = texture(depth_map, fragment_to_light).r * far_plane;
    float current_depth = length(fragment_to_light);

    frag_color = vec4(vec3(closest_depth / far_plane), 1.0);
}


vec4 calculate_result_color(LightningData lightning_data, float shadow) {
    return vec4(lightning_data.ambient + (1.0 - shadow) * (lightning_data.diffuse + lightning_data.specular), 1.0);
}


vec4 calculate_texture() {
    vec4 DefaultColor = vec4(0.5, 0., 0.5, 0.);

    switch (texture_id) {
    case 0:
        return texture(checkers_texture, TextureCoord);
    case 1:
        return texture(red_checkers_texture, TextureCoord);
    default:
        return DefaultColor;
    }
}

vec4 calculate_color() {
    return vec4(object_color, 1.0);
}

void main() {
    vec3 color = (use_texture ? calculate_texture() : calculate_color()).rgb;

    LightningData lightning_data = blinn_phong(color);
    float shadow = calculate_shadow();

    frag_color = vec4(lightning_data.ambient + (1.0 - shadow) * (lightning_data.diffuse + lightning_data.specular), 1.0);
}

此代码输出黑屏。 经过一些调试,我得出结论,邪恶的根源是阴影和颜色倍增,因为这段代码呈现昏暗但仍然可见的图片:

frag_color = vec4(lightning_data.ambient + (1.0 - 1.0) * (lightning_data.diffuse + lightning_data.specular), 1.0);

这段代码给出了明亮的图片:

frag_color = vec4(lightning_data.ambient + (1.0 - 0.0) * (lightning_data.diffuse + lightning_data.specular), 1.0);

因此,shadow变量中的值必须既不是1.0,也不是0.0。 然而,改为这样可以看到可见的图像,阴影部分看不见,没有阴影的部分明亮(按预期完成):

frag_color = vec4(vec3(1.0 - shadow), 1.0);

因此shadow必须包含1.00.0 - 这是一个矛盾。

可能是着色器处理和floatvec3乘法有问题?我正在使用Radeon HD 8610G的笔记本电脑。

1 个答案:

答案 0 :(得分:0)

@derhass给出了正确的解释。我错误地使用了纹理单元。