0

Fragment shader:

#version 460 core
layout ( location = 0 ) out vec4 FragColor;


const float inv_atan_u_ = 0.1591;
const float inv_atan_v_ = 0.3183;
const float gamma = 2.4;
const float one_gamma = 1.0/gamma;

struct Material
{
    vec3 albedo;
    float metallic;
    float roughness;
};


in vec3 normal;
in vec3 eye;
in vec3 frag_pos;
in int mat_idx;


uniform sampler2D tex_irad;
uniform sampler2D tex_integr;
uniform sampler2D tex_pref;
//uniform sampler2D tex_alb;
uniform Material materials[7];

vec2 uv_from_vec3(vec3 t_direction)
{
    float u = atan(t_direction.y, t_direction.x);
    float v = asin(t_direction.z);
    u *= inv_atan_u_;
    v *= inv_atan_v_;
    u += 0.5f;
    v += 0.5f;
    return vec2(1.0 - u, 1.0 - v);
}

vec3 gamma_expand(vec3 t_color)
{
    return pow(t_color, vec3(gamma));
}

vec3 gamma_compress(vec3 t_color)
{
    return pow(t_color, vec3(one_gamma));
}


vec3 fresnel(float cos_theta_o, vec3 F_0)
{
    vec3 F = F_0 + (1 - F_0) * pow(clamp(1.0 - cos_theta_o, 0.0, 1.0), 5.0);
    return F;
}

vec3 diffuse_irad()
{
    vec2 irad_tex_coords = uv_from_vec3(normal);
    vec4 irad_color = texture(tex_irad, irad_tex_coords);
    return irad_color.rgb;
}

vec3 pref_env_map(vec3 w_i, float t_roughness)
{
    vec2 pref_tex_coords = uv_from_vec3(w_i);
    vec3 result = textureLod(tex_pref, pref_tex_coords, t_roughness * 6).rgb;
    return result;
}


void main( void )
{   
    vec3 albedo = materials[mat_idx].albedo;
    float roughness = materials[mat_idx].roughness;
    float metallic = materials[mat_idx].metallic;

    vec3 w_o = normalize(eye - frag_pos);
    vec3 n = normal;
    vec3 w_i = normalize(reflect(-w_o, normal));
    vec3 F_0 = mix(vec3(0.04), albedo, metallic);
    float cos_theta_o = max(dot(w_o, normal), 0.0);

    //--------------------------------------------------------------------//
    // Diffuse part
    vec3 F = fresnel(cos_theta_o, F_0);
    vec3 F_d = (vec3(1.0) - F) * (1.0 - metallic);

    vec3 L_diffuse = F_d * albedo * diffuse_irad();

    //--------------------------------------------------------------------//
    // Specular part
    vec2 s_b = texture(tex_integr, vec2(cos_theta_o, roughness)).rg;
    vec3 L_specular = pref_env_map(w_i, roughness);

    //--------------------------------------------------------------------//
    // All together
    vec3 L_out = L_diffuse + (F_0 * s_b.r + s_b.g) * L_specular;
    FragColor = vec4(L_out, 1.0);
}

Vertex shader:

#version 460 core

// vertex attributes
layout ( location = 0 ) in vec4 in_position_ms;
layout ( location = 1 ) in vec3 in_normal_ms;
layout ( location = 2 ) in int in_mat_idx;

//out vec4 outcol;

// uniform variables
uniform mat4 P; // Model View Projection
uniform vec3 uEye;

out int mat_idx;
out vec3 normal;
out vec3 eye;
out vec3 frag_pos;



void main( void )
{
    vec4 position = P * (in_position_ms);
    gl_Position = position;
    normal = in_normal_ms;
    mat_idx = in_mat_idx;
    eye = uEye;
    frag_pos = position.xyz;
}

Mind only first 3 rows in main and then those uniforms associated with it in fragment shader.

I've tried looking at the host code, looked at how it generates the names to set the uniform and it looks good. I've tried using strings looking like: "materials[0].albedo" and it worked on host. I tried also constant indexing on device code like: materials[0].albedo. It seems the problem is dynamic indexing but I don't know any way around it. I tried using for loop but to no avail. Also the way my "filling" method in C++ works is that it might not send data in order, meaning it wouldn't firstly fill materials[0], then materials[1].

  • Are you sure that the shader links successfully? Int varyings have to be marked with the flat keyword. Otherwise the linker should state something like "error C5215: Integer varying XXX must be flat". – BDL Apr 03 '23 at 22:09
  • Related: https://stackoverflow.com/questions/27581271/flat-qualifier-in-glsl – BDL Apr 03 '23 at 22:11
  • I've tried, but there is now somehow only mat_idx of value 0 and 6, nothing in between. – Eskimo Joe Apr 03 '23 at 22:30
  • Edit: its only 0 – Eskimo Joe Apr 03 '23 at 23:14

0 Answers0