Gamma correct pixels before writing out

This commit is contained in:
2025-09-23 15:36:08 +01:00
parent a2559d373f
commit b15edd1906

View File

@@ -4,6 +4,7 @@
#include "rng.h" #include "rng.h"
#include <float.h> #include <float.h>
#include <math.h>
#include <threads.h> #include <threads.h>
#define MAX_ITER 10 #define MAX_ITER 10
@@ -13,6 +14,8 @@
#define SAMPLE_WEIGHT (1.0 / (double)NSAMPLES) #define SAMPLE_WEIGHT (1.0 / (double)NSAMPLES)
#define SAMPLE_STDDEV 0.333 #define SAMPLE_STDDEV 0.333
#define GAMMA 2.2
#define NTHREADS 40 #define NTHREADS 40
typedef struct { typedef struct {
@@ -52,17 +55,22 @@ trace(ray_t ray, const obj_t *scene, unsigned scene_count, rng_t *rng)
} }
ray = scatter(hit, rng); ray = scatter(hit, rng);
coeff *= 0.5; coeff *= 0.3;
} }
return black; return black;
} }
static double linear_to_gamma(double channel)
{
return pow(fmin(channel, 1.0), 1.0 / GAMMA);
}
static void setpix(vec3_t col, pix_t *out) static void setpix(vec3_t col, pix_t *out)
{ {
out->r = UINT16_MAX * col.x; out->r = UINT16_MAX * linear_to_gamma(col.x);
out->g = UINT16_MAX * col.y; out->g = UINT16_MAX * linear_to_gamma(col.y);
out->b = UINT16_MAX * col.z; out->b = UINT16_MAX * linear_to_gamma(col.z);
out->a = UINT16_MAX; out->a = UINT16_MAX;
} }