Skip to content

Commit

Permalink
shader: adding gamma correction (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
marsp0 authored Jan 2, 2025
1 parent 670b76a commit 97b6129
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 28 deletions.
54 changes: 34 additions & 20 deletions src/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ vec4_t vec4_scale_with_w(vec4_t v, float scale)
return res;
}

vec4_t vec4_pow(vec4_t v, float pow)
{
v.x = f_pow(v.x, pow);
v.y = f_pow(v.y, pow);
v.z = f_pow(v.z, pow);

return v;
}

vec4_t vec4_negate(vec4_t v)
{
return vec4_new(-v.x, -v.y, -v.z);
Expand Down Expand Up @@ -585,26 +594,6 @@ float f_max(float a, float b)
return a > b ? a : b;
}

int32_t i_max(int32_t a, int32_t b)
{
return a > b ? a : b;
}

int32_t i_min(int32_t a, int32_t b)
{
return a < b ? a : b;
}

uint32_t u_max(uint32_t a, uint32_t b)
{
return a > b ? a : b;
}

uint32_t u_min(uint32_t a, uint32_t b)
{
return a < b ? a : b;
}

float f_clamp(float v, float min, float max)
{
return f_min(max, f_max(min, v));
Expand Down Expand Up @@ -651,3 +640,28 @@ float f_round(float v)
{
return roundf(v);
}

float f_pow(float v, float pow)
{
return powf(v, pow);
}

int32_t i_max(int32_t a, int32_t b)
{
return a > b ? a : b;
}

int32_t i_min(int32_t a, int32_t b)
{
return a < b ? a : b;
}

uint32_t u_max(uint32_t a, uint32_t b)
{
return a > b ? a : b;
}

uint32_t u_min(uint32_t a, uint32_t b)
{
return a < b ? a : b;
}
10 changes: 6 additions & 4 deletions src/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ vec4_t vec4_sub(vec4_t v1, vec4_t v2);
vec4_t vec4_cross(vec4_t v1, vec4_t v2);
vec4_t vec4_scale(vec4_t v, float scale);
vec4_t vec4_scale_with_w(vec4_t v, float scale);
vec4_t vec4_pow(vec4_t v, float pow);
vec4_t vec4_negate(vec4_t v);
vec4_t vec4_normalize(vec4_t v);
vec4_t vec4_hadamard(vec4_t v1, vec4_t v2);
Expand Down Expand Up @@ -170,11 +171,12 @@ float f_max(float a, float b);
float f_ceil(float v);
float f_floor(float v);
float f_round(float v);
int32_t i_max(int32_t a, int32_t b);
int32_t i_min(int32_t a, int32_t b);
uint32_t u_max(uint32_t a, uint32_t b);
uint32_t u_min(uint32_t a, uint32_t b);
float f_clamp(float v, float min, float max);
float f_wrap(float v, float min, float max);
float f_sin(float v);
float f_cos(float v);
float f_pow(float v, float pow);
int32_t i_max(int32_t a, int32_t b);
int32_t i_min(int32_t a, int32_t b);
uint32_t u_max(uint32_t a, uint32_t b);
uint32_t u_min(uint32_t a, uint32_t b);
12 changes: 8 additions & 4 deletions src/shader.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
/* static variables */
/********************/

static const float gamma_val = 2.2f;
static const float one_over_gamma = 1.f / gamma_val;
static camera_t* camera = NULL;
static texture_t* albedo_texture = NULL;
static texture_t* metallic_texture = NULL;
static texture_t* normal_texture = NULL;
// static mat_t tbn_t_w;
static vec4_t v0_w;
static vec4_t v1_w;
static vec4_t v2_w;
Expand Down Expand Up @@ -179,7 +180,7 @@ uint32_t shader_fragment(float w0, float w1, float w2)
float s = f_min(t0.x * w0 + t1.x * w1 + t2.x * w2, 1.f);
float t = f_min(t0.y * w0 + t1.y * w1 + t2.y * w2, 1.f);

vec4_t albedo = vec4_scale(texture_sample(albedo_texture, s, t), 2.2f);
vec4_t albedo = vec4_pow(texture_sample(albedo_texture, s, t), gamma_val);
vec4_t metallic = texture_sample(metallic_texture, s, t);
float rough = metallic.y; // green channel
float metal = metallic.x; // blue channel
Expand Down Expand Up @@ -247,7 +248,10 @@ uint32_t shader_fragment(float w0, float w1, float w2)
col = vec4_scale(col, 1.f);
col = vec4_scale(col, n_dot_l);

vec4_t ambient = vec4_scale(albedo, 0.1f);
// ambient + gamma correction

return vec4_to_bgra(vec4_add(col, ambient));
vec4_t ambient = vec4_scale(albedo, 0.1f);
vec4_t final = vec4_pow(vec4_add(col, ambient), one_over_gamma);

return vec4_to_bgra(final);
}

0 comments on commit 97b6129

Please sign in to comment.