2
\$\begingroup\$

I have created a mesh in Blender (just a sphere with a texture over it; it's supposed to look like mars) and I've exported it as OBJ. I've tried it both with exporting with normals in Blender's export settings and also with the aiProcess_GenNormals flag. But for some reason I'm still getting this lighting issue.

There's a lot of C++ code but I'm not sure if any of it is relevant to this issue, so I'll just include the shaders.

The vertex shader:

#version 330 core

layout(location = 0) in vec3 vertexpos_modelspace;
layout(location = 1) in vec2 texcoord_vertex;
layout(location = 2) in vec3 normal_vertex;

uniform mat4 world;
uniform mat4 view;
uniform mat4 projection;

out vec2 texcoord_fragment;
out vec3 normal_fragment;

void main()
{
    texcoord_fragment = texcoord_vertex;

    vec4 transformed_normal;
    transformed_normal = vec4(normal_vertex, 1);
    transformed_normal = world * transformed_normal;
    normal_fragment = transformed_normal.xyz;

    gl_Position.xyzw = projection * view * world * vec4(vertexpos_modelspace, 1);
}

The fragment shader:

#version 330 core

uniform sampler2D texsampler;

uniform vec3 light_direction;

in vec2 texcoord_fragment;
in vec3 normal_fragment;

out vec4 color;

void main()
{
    float lighting = 0;
    lighting = dot(normalize(normal_fragment), normalize(light_direction));
    lighting = clamp(lighting, 0, 1);

    vec4 sample = texture(texsampler, texcoord_fragment).rgba;

    color = sample * lighting;
}

And the result looks like this:

I can't figure out why this is happening. It doesn't appear this way in Blender, just in my application.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Fixed it. Turns out Blender was in flat shading mode instead of smooth shading, so the normals were per-triangle instead of per-vertex when exported (didn't realize that setting effected the exported mesh). And if you want Assimp to generate the normals for you in the same way it requires the aiProcess_GenSmoothNormals flag (instead of the aiProcess_GenNormals flag).

enter image description here

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .