From 8acb27a788f24f85f38cf4ca45f2c3124128fa26 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Tue, 8 Aug 2017 10:40:14 -0700 Subject: ray: add rudimentary gamma correction color banding has been quite visible, and somewhat expected with a direct conversion from the linear float color space to the 8-bit integral rgb color components. A simple lookup table is used here to non-linearly map the values, table generation is taken from Greg Ward's REAL PIXELS gem in Graphics Gems II. --- src/libs/ray/Makefile.am | 2 +- src/libs/ray/ray_gamma.c | 15 +++++++++++++++ src/libs/ray/ray_gamma.h | 37 +++++++++++++++++++++++++++++++++++++ src/libs/ray/ray_render.c | 5 ++++- src/libs/ray/ray_scene.h | 1 + src/modules/ray/ray.c | 3 ++- 6 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 src/libs/ray/ray_gamma.c create mode 100644 src/libs/ray/ray_gamma.h diff --git a/src/libs/ray/Makefile.am b/src/libs/ray/Makefile.am index dc13a0d..f055d94 100644 --- a/src/libs/ray/Makefile.am +++ b/src/libs/ray/Makefile.am @@ -1,4 +1,4 @@ noinst_LIBRARIES = libray.a -libray_a_SOURCES = ray_3f.h ray_camera.c ray_camera.h ray_color.h ray_euler.c ray_euler.h ray_light_emitter.h ray_object.h ray_object_light.h ray_object_plane.h ray_object_point.h ray_object_sphere.h ray_object_type.h ray_ray.h ray_render.c ray_render.h ray_render_object.h ray_render_object_plane.h ray_render_object_point.h ray_render_object_sphere.h ray_scene.h ray_surface.h +libray_a_SOURCES = ray_3f.h ray_camera.c ray_camera.h ray_color.h ray_euler.c ray_euler.h ray_gamma.c ray_gamma.h ray_light_emitter.h ray_object.h ray_object_light.h ray_object_plane.h ray_object_point.h ray_object_sphere.h ray_object_type.h ray_ray.h ray_render.c ray_render.h ray_render_object.h ray_render_object_plane.h ray_render_object_point.h ray_render_object_sphere.h ray_scene.h ray_surface.h libray_a_CFLAGS = -ffast-math libray_a_CPPFLAGS = -I@top_srcdir@/src diff --git a/src/libs/ray/ray_gamma.c b/src/libs/ray/ray_gamma.c new file mode 100644 index 0000000..21a9afc --- /dev/null +++ b/src/libs/ray/ray_gamma.c @@ -0,0 +1,15 @@ +#include + +#include "ray_gamma.h" + +void ray_gamma_prepare(float gamma, ray_gamma_t *res_gamma) +{ + if (res_gamma->gamma == gamma) + return; + + /* This is from graphics gems 2 "REAL PIXELS" */ + for (unsigned i = 0; i < 1024; i++) + res_gamma->table[i] = 256.0f * powf((((float)i + .5f) / 1024.0f), 1.0f/gamma); + + res_gamma->gamma = gamma; +} diff --git a/src/libs/ray/ray_gamma.h b/src/libs/ray/ray_gamma.h new file mode 100644 index 0000000..c46d631 --- /dev/null +++ b/src/libs/ray/ray_gamma.h @@ -0,0 +1,37 @@ +#ifndef _RAY_GAMMA_H +#define _RAY_GAMMA_H + +#include + +#include "ray_color.h" + +typedef struct ray_gamma_t { + float gamma; + uint8_t table[1024]; +} ray_gamma_t; + +void ray_gamma_prepare(float gamma, ray_gamma_t *res_gamma); + +/* convert a color into a gamma-corrected, packed, 32-bit rgb pixel value */ +static inline uint32_t ray_gamma_color_to_uint32_rgb(ray_gamma_t *gamma, ray_color_t color) { + uint32_t pixel; + + if (color.x > 1.0f) + color.x = 1.0f; + + if (color.y > 1.0f) + color.y = 1.0f; + + if (color.z > 1.0f) + color.z = 1.0f; + + pixel = (uint32_t)gamma->table[(unsigned)floorf(1023.0f * color.x)]; + pixel <<= 8; + pixel |= (uint32_t)gamma->table[(unsigned)floorf(1023.0f * color.y)]; + pixel <<= 8; + pixel |= (uint32_t)gamma->table[(unsigned)floorf(1023.0f * color.z)]; + + return pixel; +} + +#endif diff --git a/src/libs/ray/ray_render.c b/src/libs/ray/ray_render.c index e551d8c..1c7c79c 100644 --- a/src/libs/ray/ray_render.c +++ b/src/libs/ray/ray_render.c @@ -5,6 +5,7 @@ #include "ray_camera.h" #include "ray_color.h" +#include "ray_gamma.h" #include "ray_render_object.h" #include "ray_ray.h" #include "ray_scene.h" @@ -18,6 +19,7 @@ typedef struct ray_render_t { ray_color_t ambient_light; ray_camera_frame_t frame; + ray_gamma_t gamma; ray_render_object_t objects[]; } ray_render_t; @@ -207,7 +209,7 @@ void ray_render_trace_fragment(ray_render_t *render, fb_fragment_t *fb_fragment) ray_camera_fragment_begin(&render->frame, fb_fragment, &ray, &fragment); do { do { - *buf = ray_color_to_uint32_rgb(trace_ray(render, &ray)); + *buf = ray_gamma_color_to_uint32_rgb(&render->gamma, trace_ray(render, &ray)); buf++; } while (ray_camera_fragment_x_step(&fragment)); @@ -236,6 +238,7 @@ ray_render_t * ray_render_new(const ray_scene_t *scene, const ray_camera_t *came render->camera = camera; render->ambient_light = ray_3f_mult_scalar(&scene->ambient_color, scene->ambient_brightness); + ray_gamma_prepare(scene->gamma, &render->gamma); ray_camera_frame_prepare(camera, &render->frame); for (i = 0, object = scene->objects; object->type; object++) diff --git a/src/libs/ray/ray_scene.h b/src/libs/ray/ray_scene.h index ff9c440..2c7308f 100644 --- a/src/libs/ray/ray_scene.h +++ b/src/libs/ray/ray_scene.h @@ -11,6 +11,7 @@ typedef struct ray_scene_t { ray_color_t ambient_color; float ambient_brightness; + float gamma; } ray_scene_t; #endif diff --git a/src/modules/ray/ray.c b/src/modules/ray/ray.c index 5a2dd40..c0c067e 100644 --- a/src/modules/ray/ray.c +++ b/src/modules/ray/ray.c @@ -112,7 +112,8 @@ static ray_scene_t scene = { .objects = objects, .lights = lights, .ambient_color = { .x = 1.0f, .y = 1.0f, .z = 1.0f }, - .ambient_brightness = .04f, + .ambient_brightness = .1f, + .gamma = .55f, }; static float r; -- cgit v1.2.3