0
\$\begingroup\$

cylinderI'm trying to add model loading to my opengl/sdl2 renderer and I'm successfully loading meshes using assimp after following the tutorial at learn opengl, however when I make my own simple models and import them, the shaders seem to not work correctly and I get strange lighting artifacts in the form of lots of shadows and black areas.

I've tried to follow the advice of this other question to eliminate the problem, but it doesn't seem to work. I attached a picture of what a simple cylinder looks like when being rendered. Note that for the cube I calculated all of the normals and stuff myself and the lighting shader works just fine for it. it is only on blender meshes that the lighting is off. I attached the shader code as well

Edit: I went and tested out a cube model made in blender and am having the same result where it seems to be calculating the normals incorrectly when exporting. I set the ambient light to black and put the light shining down on the corner of the cubes. My cube has light on both faces that it should , but blender's cube does not. Cubes

Also notably, the light changes depending on where I position my camera as well (only on the blender cube), even though it shouldn't matter where the camera is because the light source doesn't move.

Edit 2: I went and retested the backpack model from the learn opengl site and I'm still getting strange shadows but to a much lesser extent, I add a pic and for context, the shadows look ok, but as you can see are much darker than they reasonable should be. I think one of my calculations is happening in the wrong space but I can't figure out which calculation it is. Backpack

Edit 3: I've been playing around and have found that the issue lies in the specular part of my shader and I believe it is happening that way due to the specular map (or maybe lack of one) I'm not sure. I'm gonna play around with blender and see if there is a way to fix the export specular texture.

Vertex Shader:

#version 460 core
layout (location = 0) in vec3 pos;
layout (location = 1) in vec3 color;
layout (location = 2) in vec3 normal;
layout (location = 3) in vec3 tangent;
layout (location = 4) in vec3 bitangent;
layout (location = 5) in vec2 uvs;
layout (location = 6) in float opacity;

out vec2 tex_coords;
out vec4 out_color;
out vec3 frag_pos;
out vec3 out_normal;
out vec3 out_tangent;
out vec3 out_bitangent;

uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;

void main() {
    frag_pos = vec3(model * vec4(pos, 1.0));
    out_normal = mat3(transpose(inverse(model))) * normal;

    out_color = vec4(color, opacity);
    out_tangent = tangent;
    out_bitangent = bitangent;
    tex_coords = uvs;
    
    gl_Position = proj * view * vec4(frag_pos , 1.0);
}

Fragment Shader:

#version 460 core
in vec4 out_color;
in vec3 frag_pos;
in vec3 out_normal;
in vec3 in_tangent;
in vec3 in_bitangent;
in vec2 tex_coords;

out vec4 frag_color;

uniform vec3 view_pos;

struct Material {
    sampler2D ambient_tex;
    sampler2D height_tex;
    sampler2D diffuse_tex;
    sampler2D specular_tex;
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
    float shininess;
};

struct Light {
    vec3 position;
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};

uniform Material material;
uniform Light light;

void main() {
    // ambient
    vec3 ambient = (light.ambient * material.ambient) * 
                   vec3(texture(material.ambient_tex , tex_coords)); // * vec3(out_color);

    // diffuse
    vec3 norm = normalize(out_normal);
    vec3 light_dir = normalize(light.position - frag_pos);
    float diff = max(dot(norm , light_dir) , 0.0);
    vec3 diffuse = (light.diffuse * material.diffuse) * (diff * vec3(texture(material.diffuse_tex , tex_coords)));

    // specular
    vec3 view_dir = normalize(view_pos - frag_pos);
    vec3 reflect_dir = reflect(-light_dir , norm);
    float spec = pow(max(dot(view_dir , reflect_dir) , 0.0) , material.shininess);
    vec3 specular = (light.specular * material.specular) * (spec * vec3(texture(material.specular_tex , tex_coords)));

    vec3 result = ambient + diffuse + specular;
    frag_color = vec4(result , 1.0) * texture(material.diffuse_tex , tex_coords);
}
\$\endgroup\$

0

You must log in to answer this question.

Browse other questions tagged .