normal map

This commit is contained in:
Михаил Капелько committed 2026-10-19 16:07:01 +03:00
1 parent 2ef1589d55
commit df4ac26658
3 files changed
+67 -3

No files matched your search

+23
View File
@@ -7,6 +7,8 @@ in vec2 vTexCoord;
in vec4 vLightClip;
uniform sampler2D texture0;
uniform sampler2D u_normalMap;
uniform float u_normalStrength;
uniform sampler2D u_shadowMap;
uniform vec3 u_lightPos;
uniform vec3 u_lightColor;
@@ -55,12 +57,33 @@ float shadowFactor()
return sum / count;
}
mat3 cotangentFrame(vec3 N, vec3 p, vec2 uv)
{
vec3 dp1 = dFdx(p);
vec3 dp2 = dFdy(p);
vec2 duv1 = dFdx(uv);
vec2 duv2 = dFdy(uv);
vec3 dp2perp = cross(dp2, N);
vec3 dp1perp = cross(N, dp1);
vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;
vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;
float invmax = inversesqrt(max(dot(T, T), dot(B, B)));
return mat3(T * invmax, B * invmax, N);
}
void main()
{
vec4 texel = texture(texture0, vTexCoord);
vec3 albedo = texel.rgb * u_albedoColor;
vec3 N = normalize(vNormal);
if (u_normalStrength > 0.0)
{
vec3 nm = texture(u_normalMap, vTexCoord).rgb * 2.0 - 1.0;
nm.xy *= u_normalStrength;
N = normalize(cotangentFrame(N, vWorldPos, vTexCoord) * nm);
}
if (u_isGround > 0.5)
{
vec2 uv = vWorldPos.xz;
Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

+44 -3
View File
@@ -8,6 +8,7 @@
#include <math.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
static int sr_width = 1;
static int sr_height = 1;
@@ -27,6 +28,7 @@ static int sr_has_model = 0;
static Material sr_cube_material = { 0 };
static Material sr_cube_shadow_material = { 0 };
static Texture2D sr_cube_normal = { 0 };
static Model sr_ground_model = { 0 };
static Material sr_ground_material = { 0 };
@@ -53,6 +55,8 @@ static int loc_albedo_color = -1;
static int loc_shadow_map = -1;
static int loc_shadow_bias = -1;
static int loc_shadow_softness = -1;
static int loc_normal_map = -1;
static int loc_normal_strength = -1;
static int loc_light_mvp = -1;
static int loc_is_ground = -1;
static int loc_grid_color = -1;
@@ -100,6 +104,8 @@ static void sr_load_shaders(const char *main_vs, const char *main_fs,
loc_shadow_map = GetShaderLocation(sr_main_shader, "u_shadowMap");
loc_shadow_bias = GetShaderLocation(sr_main_shader, "u_shadowBias");
loc_shadow_softness = GetShaderLocation(sr_main_shader, "u_shadowSoftness");
loc_normal_map = GetShaderLocation(sr_main_shader, "u_normalMap");
loc_normal_strength = GetShaderLocation(sr_main_shader, "u_normalStrength");
loc_light_mvp = GetShaderLocation(sr_main_shader, "u_lightMVP");
loc_is_ground = GetShaderLocation(sr_main_shader, "u_isGround");
loc_grid_color = GetShaderLocation(sr_main_shader, "u_gridColor");
@@ -156,6 +162,12 @@ void sr_load_model(const char *gltf_path)
sr_model = LoadModel(gltf_path);
sr_has_model = (sr_model.meshCount > 0);
// The matching normal map sits next to the glTF; load it separately.
char asset_dir[1024];
const char *slash = strrchr(gltf_path, '/');
size_t dir_len = (slash != NULL) ? (size_t)(slash - gltf_path + 1) : 0;
snprintf(asset_dir, sizeof(asset_dir), "%.*s", (int)dir_len, gltf_path);
if (sr_has_model)
{
int cube_mat_index = (sr_model.materialCount > 0) ? sr_model.meshMaterial[0] : 0;
@@ -166,6 +178,15 @@ void sr_load_model(const char *gltf_path)
SetTextureFilter(base_tex, TEXTURE_FILTER_BILINEAR);
SetTextureWrap(base_tex, TEXTURE_WRAP_CLAMP);
char normal_path[1024];
snprintf(normal_path, sizeof(normal_path), "%scube_normal.png", asset_dir);
if (FileExists(normal_path))
{
sr_cube_normal = LoadTexture(normal_path);
SetTextureFilter(sr_cube_normal, TEXTURE_FILTER_BILINEAR);
SetTextureWrap(sr_cube_normal, TEXTURE_WRAP_CLAMP);
}
sr_cube_shadow_material = sr_cube_material;
sr_cube_shadow_material.shader = sr_shadow_shader;
}
@@ -242,14 +263,28 @@ static void sr_set_light_mvp(Matrix model)
SetShaderValueMatrix(sr_main_shader, loc_light_mvp, light_mvp);
}
static void sr_draw_object(Model model, Material material, Matrix model_mat, float is_ground, float emissive)
static void sr_bind_normal(Texture2D tex)
{
int unit = 2;
SetShaderValue(sr_main_shader, loc_normal_map, &unit, SHADER_UNIFORM_INT);
rlActiveTextureSlot(2);
if (tex.id > 0) rlEnableTexture(tex.id);
rlActiveTextureSlot(0);
}
static void sr_draw_object(Model model, Material material, Matrix model_mat,
Texture2D normal, float is_ground, float emissive)
{
sr_set_light_mvp(model_mat);
float albedo[3] = { 1.0f, 1.0f, 1.0f };
float normal_strength = (normal.id > 0) ? 1.0f : 0.0f;
SetShaderValue(sr_main_shader, loc_albedo_color, albedo, SHADER_UNIFORM_VEC3);
SetShaderValue(sr_main_shader, loc_emissive, &emissive, SHADER_UNIFORM_FLOAT);
SetShaderValue(sr_main_shader, loc_is_ground, &is_ground, SHADER_UNIFORM_FLOAT);
SetShaderValue(sr_main_shader, loc_normal_strength, &normal_strength, SHADER_UNIFORM_FLOAT);
sr_bind_normal(normal);
DrawMesh(model.meshes[0], material, model_mat);
}
@@ -331,12 +366,12 @@ void sr_render(void)
rlActiveTextureSlot(0);
// --- Ground (procedural grid floor) ---
sr_draw_object(sr_ground_model, sr_ground_material, MatrixTranslate(0.0f, 0.0f, 0.0f), 1.0f, 0.0f);
sr_draw_object(sr_ground_model, sr_ground_material, MatrixTranslate(0.0f, 0.0f, 0.0f), (Texture2D){ 0 }, 1.0f, 0.0f);
// --- Cubes ---
if (sr_has_model)
{
sr_draw_object(sr_model, sr_cube_material, sr_cube_transform(), 0.0f, 0.0f);
sr_draw_object(sr_model, sr_cube_material, sr_cube_transform(), sr_cube_normal, 0.0f, 0.0f);
}
// Flush while the projection/modelview matrices are still active, otherwise
@@ -364,6 +399,12 @@ void sr_shutdown(void)
UnloadModel(sr_ground_model);
if (sr_cube_normal.id > 0)
{
UnloadTexture(sr_cube_normal);
sr_cube_normal.id = 0;
}
if (sr_shadow_color > 0) rlUnloadTexture(sr_shadow_color);
if (sr_shadow_depth > 0) rlUnloadTexture(sr_shadow_depth);
if (sr_shadow_fbo > 0) rlUnloadFramebuffer(sr_shadow_fbo);