star-sf

Set of surface and volume scattering functions
git clone git://git.meso-star.fr/star-sf.git
Log | Files | Refs | README | LICENSE

commit 60930be92d761f6742ca1858c531fd626c6a6ed8
parent 1f02ac9a68ef25e76833bb15253da15d98b45b06
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed,  5 Oct 2016 11:35:28 +0200

Add and test the constant Fresnel term

Diffstat:
Mcmake/CMakeLists.txt | 2++
Msrc/ssf.h | 8++++++++
Asrc/ssf_fresnel_constant.c | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/test_ssf_fresnel_constant.c | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 141 insertions(+), 0 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -57,6 +57,7 @@ set(SSF_FILES_SRC ssf_bsdf.c ssf_bxdf.c ssf_fresnel.c + ssf_fresnel_constant.c ssf_fresnel_dielectric_conductor.c ssf_fresnel_dielectric_dielectric.c ssf_fresnel_no_op.c @@ -103,6 +104,7 @@ if(NOT NO_TEST) new_test(test_ssf_bsdf) new_test(test_ssf_bxdf) new_test(test_ssf_fresnel) + new_test(test_ssf_fresnel_constant) new_test(test_ssf_fresnel_dielectric_conductor) new_test(test_ssf_fresnel_dielectric_dielectric) new_test(test_ssf_fresnel_no_op) diff --git a/src/ssf.h b/src/ssf.h @@ -184,6 +184,9 @@ SSF_API const struct ssf_bxdf_type ssf_microfacet2_reflection; /******************************************************************************* * Built-in Fresnel terms ******************************************************************************/ +/* Fresnel term is a constant; Fr = k with k in [0, 1]. */ +SSF_API const struct ssf_fresnel_type ssf_fresnel_constant; + /* Fresnel term for perfect reflection; Fr = 1 */ SSF_API const struct ssf_fresnel_type ssf_fresnel_no_op; @@ -390,6 +393,11 @@ ssf_fresnel_dielectric_conductor_setup const double eta_t, /* Real part of the refraction id of the conductor */ const double k_t); /* Imaginary part of the refraction id of the conductor */ +SSF_API res_T +ssf_fresnel_constant_setup + (struct ssf_fresnel* fresnel, + const double constant); /* in [0, 1] */ + /******************************************************************************* * Microfacet Distribution API - Note that by convention the outgoing direction * `wo' and the half vector `wh' point outward the surface. diff --git a/src/ssf_fresnel_constant.c b/src/ssf_fresnel_constant.c @@ -0,0 +1,73 @@ +/* Copyright (C) |Meso|Star> 2016 (contact@meso-star.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include "ssf.h" +#include "ssf_fresnel_c.h" + +struct fresnel_constant { + double k; /* in [0, 1] */ +}; + +/******************************************************************************* + * Private functions + ******************************************************************************/ +static res_T +fresnel_constant_init(struct mem_allocator* allocator, void* fresnel) +{ + struct fresnel_constant* f = fresnel; + (void)allocator; + ASSERT(fresnel); + f->k = 1; /* <=> No op */ + return RES_OK; +} + +static void +fresnel_constant_release(void* fresnel) +{ + (void)fresnel; +} + +static double +fresnel_constant_eval(void* fresnel, const double cos_theta_i) +{ + struct fresnel_constant* f = fresnel; + (void)cos_theta_i; + return f->k; +} + +/******************************************************************************* + * Exported symbols + ******************************************************************************/ +const struct ssf_fresnel_type ssf_fresnel_constant = { + fresnel_constant_init, + fresnel_constant_release, + fresnel_constant_eval, + sizeof(struct fresnel_constant), + ALIGNOF(struct fresnel_constant) +}; + +res_T +ssf_fresnel_constant_setup(struct ssf_fresnel* fresnel, const double value) +{ + struct fresnel_constant* f; + if(!fresnel || !FRESNEL_TYPE_EQ(&fresnel->type, &ssf_fresnel_constant)) + return RES_BAD_ARG; + if(value < 0 || value > 1) + return RES_BAD_ARG; + f = fresnel->data; + f->k = value; + return RES_OK; +} + diff --git a/src/test_ssf_fresnel_constant.c b/src/test_ssf_fresnel_constant.c @@ -0,0 +1,58 @@ +/* Copyright (C) |Meso|Star> 2016 (contact@meso-star.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#define _POSIX_C_SOURCE 200112L /* nextafter support */ + +#include "ssf.h" +#include "test_ssf_utils.h" + +int +main(int argc, char** argv) +{ + struct mem_allocator allocator; + struct ssf_fresnel* fresnel; + struct ssf_fresnel* dummy; + (void)argc, (void)argv; + + mem_init_proxy_allocator(&allocator, &mem_default_allocator); + CHECK(ssf_fresnel_create(&allocator, &ssf_fresnel_constant, &fresnel), RES_OK); + CHECK(ssf_fresnel_create(&allocator, &fresnel_dummy, &dummy), RES_OK); + + CHECK(ssf_fresnel_constant_setup(NULL, -1), RES_BAD_ARG); + CHECK(ssf_fresnel_constant_setup(fresnel, -1), RES_BAD_ARG); + CHECK(ssf_fresnel_constant_setup(NULL, 0.5), RES_BAD_ARG); + CHECK(ssf_fresnel_constant_setup(fresnel, 0), RES_OK); + CHECK(ssf_fresnel_constant_setup(fresnel, 1), RES_OK); + CHECK(ssf_fresnel_constant_setup(fresnel, 0.5), RES_OK); + CHECK(ssf_fresnel_constant_setup(fresnel, nextafter(0, -1)), RES_BAD_ARG); + CHECK(ssf_fresnel_constant_setup(fresnel, nextafter(1, 2)), RES_BAD_ARG); + CHECK(ssf_fresnel_constant_setup(dummy, 0.5), RES_BAD_ARG); + + CHECK(ssf_fresnel_eval(fresnel, 0), 0.5); + CHECK(ssf_fresnel_eval(fresnel, 1), 0.5); + CHECK(ssf_fresnel_eval(fresnel, 0.1234), 0.5); + CHECK(ssf_fresnel_constant_setup(fresnel, 0.123), RES_OK); + CHECK(ssf_fresnel_eval(fresnel, 0), 0.123); + CHECK(ssf_fresnel_eval(fresnel, 1), 0.123); + CHECK(ssf_fresnel_eval(fresnel, 0.25), 0.123); + + CHECK(ssf_fresnel_ref_put(fresnel), RES_OK); + CHECK(ssf_fresnel_ref_put(dummy), RES_OK); + + check_memory_allocator(&allocator); + mem_shutdown_proxy_allocator(&allocator); + CHECK(mem_allocated_size(), 0); + return 0; +}