[ad_1]
I have been making an attempt to implement regular mapping into my customized OpenGL shader. The end result I am getting is virtually there, however it seems to be just like the Level Gentle
just isn’t illuminating the mesh evenly.
For context, I lately added lighting, added a Level Gentle
and a rectangle with a texture to do common lighting and bought this end result:
It is a desired end result, the Level Gentle
(it is the small yellow dice above the rectangle) illuminates the rectangle with the brick texture evenly.
I then wished so as to add a standard map, so as an alternative of utilizing the vertex normals, I might use normals from one other texture. The end result I am getting is that this:
The result’s virtually there, the traditional map is being utilized, and the bricks virtually look 3D. The one downside is that one nook of the mesh stays darkish, despite the fact that the sunshine is strictly in the midst of the rectangle, so I might count on the illumination to be even.
In case it helps, this is a gif that reveals the Level Gentle shifting in an 8
sample above the rectangle:
My present try is to get the normals from the traditional map and convert them from Tangent House to View House (the place the lighting takes impact already).
Right here is the Vertex Shader
I am utilizing:
// vertex attributes
in vec3 color;
in vec3 regular;
in vec2 texCoord;
in vec3 tangent;
in vec3 bitangent;
// output to fragment shader
out vec4 outColour;
out vec2 outTexCoord;
out vec3 outNormal_view;
out vec3 outLightPos_view;
out vec3 outPos_view;
out mat3 outTBN;
// mild place in international area
uniform vec3 lightPos;
void important() {
// vertex positions
vec4 pos = matrix_projection *matrix_view *matrix_model *place;
gl_Position = pos;
// convert mild and vertex positions to view area and move them to fragment shader
vec3 lightPos_view = vec3((matrix_view *vec4(lightPos, 1.0)).rgb);
vec3 pos_view = vec3(matrix_view *matrix_model *place);
outLightPos_view = lightPos_view;
outPos_view = pos_view;
// convert vertex normals to view spacce and move them to fragment shader
vec3 normal_world = mat3(transpose(inverse(matrix_view *matrix_model))) *regular.rgb;
outNormal_view = normal_world;
// TBN matrix to transform normals from tangent area to view area
vec3 normal_view = normalize( vec3(matrix_view *matrix_model * vec4(regular , 0.0)) );
vec3 tangent_view = normalize( vec3(matrix_view *matrix_model * vec4(tangent , 0.0)) );
vec3 bitangent_view = normalize( vec3(matrix_view *matrix_model * vec4(bitangent, 0.0)) );
mat3 TBN = mat3( tangent_view, bitangent_view, normal_view );
outTBN = TBN;
outColour = vec4(color.rgb, 1.0);
outTexCoord = texCoord;
}
And right here is the Fragment Shader
:
// enter from vertex shader
in vec4 outColour;
in vec2 outTexCoord;
in vec3 outNormal_view;
in vec3 outLightPos_view;
in vec3 outPos_view;
in mat3 outTBN;
// texture and it is regular map
uniform sampler2D picture;
uniform sampler2D image_normal;
void important() {
// normals from texture to view area
vec3 norm = avdl_texture(image_normal, outTexCoord).rgb;
norm = normalize(norm * 2.0 - 1.0);
norm = normalize(outTBN *norm);
// mild values in view area
vec3 lightColour = vec3(1.0, 1.0, 1.0);
vec3 lightDir = normalize(outLightPos_view -outPos_view.rgb);
// `outNnormal_view` is utilizing vertex normals and it really works
// `norm` is utilizing regular map normals, and it reveals the darkish nook
vec3 normal_view = outNormal_view;
//vec3 normal_view = norm;
float diff = max(dot(normal_view.rgb, lightDir), 0.0);
vec3 diffuse = diff * lightColour;
gl_FragColor = vec4(outColour.rgb *diffuse, 1.0) *texture2D(picture, outTexCoord);
}
I am nonetheless studying, however I can verify on this particular situation, the rectangle vertices have the Regular (0, 1, 0)
, Tangent (1, 0, 0)
and Bitangent (0, 0, 1)
. From what I perceive that is how they need to seem like on a flat floor that factors up, however I am unsure if I am messing one thing up within the TBN
calculations.
For reference, I have been utilizing the tutorial from right here: https://learnopengl.com/Superior-Lighting/Regular-Mapping – the feel and regular map had been taken instantly from there, for studying functions.
[ad_2]