mrumtl

Describe materials that vary spectrally
git clone git://git.meso-star.fr/mrumtl.git
Log | Files | Refs | README | LICENSE

commit bbd2ece72073444bb6c2378f22848acb13c0af88
parent 647e3d3a30af2ca05d10138838aeaa9f6b26cc1e
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Mon,  2 Mar 2020 16:08:35 +0100

Add Star-MTL binding

Diffstat:
MREADME.md | 2+-
Mcmake/CMakeLists.txt | 15+++++++++++----
Msrc/mrumtl.h | 2+-
Asrc/mrumtl_smtl.c | 195+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/mrumtl_smtl.h | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 290 insertions(+), 6 deletions(-)

diff --git a/README.md b/README.md @@ -21,6 +21,6 @@ for further informations on CMake. ## Licenses Copyright (C) 2020 [|Meso|Star>](http://www.meso-star.com) -<contact@meso-star.com>. MRUMTL is free software released under the GPL v3+ +<contact@meso-star.com>. MruMtl is free software released under the GPL v3+ license: GNU GPL version 3 or later. You are welcome to redistribute them under certain conditions; refer to the COPYING file for details. diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -25,6 +25,7 @@ option(NO_TEST "Do not build tests" OFF) ################################################################################ find_package(RCMake 0.4 REQUIRED) find_package(RSys 0.9 REQUIRED) +find_package(StarMTL) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${RCMAKE_SOURCE_DIR}) include(rcmake) @@ -45,6 +46,15 @@ set(MRUMTL_FILES_INC ) set(MRUMTL_FILES_INC_API mrumtl.h) set(MRUMTL_FILES_DOC COPYING README.md) +if(NOT StarMTL_FOUND) + message(WARNING "Cannot find StarMTL. " + "Build the MruMtl library without the StarMTL binding.") +else() + set(MRUMTL_FILES_SRC ${MRUMTL_FILES_SRC} mrumtl_smtl.c) + set(MRUMTL_FILES_INC_API ${MRUMTL_FILES_INC_API} mrumtl_smtl.h) +endif() + + # Prepend each file in the `MRUMTL_FILES_<SRC|INC>' list by `MRUMTL_SOURCE_DIR' rcmake_prepend_path(MRUMTL_FILES_SRC ${MRUMTL_SOURCE_DIR}) rcmake_prepend_path(MRUMTL_FILES_INC ${MRUMTL_SOURCE_DIR}) @@ -54,16 +64,13 @@ rcmake_prepend_path(MRUMTL_FILES_DOC ${PROJECT_SOURCE_DIR}/../) add_library(mrumtl SHARED ${MRUMTL_FILES_SRC} ${MRUMTL_FILES_INC} ${MRUMTL_FILES_INC_API}) target_link_libraries(mrumtl RSys) -#if(CMAKE_COMPILER_IS_GNUCC) -# target_link_libraries(htgop m) -#endif() set_target_properties(mrumtl PROPERTIES DEFINE_SYMBOL MRUMTL_SHARED_BUILD VERSION ${VERSION} SOVERSION ${VERSION_MAJOR}) -rcmake_setup_devel(mrumtl MRUMTL ${VERSION} modradurb/mrumtl_version.h) +rcmake_setup_devel(mrumtl MruMtl ${VERSION} modradurb/mrumtl_version.h) ################################################################################ # Add tests diff --git a/src/mrumtl.h b/src/mrumtl.h @@ -98,7 +98,7 @@ mrumtl_brdf_get_type MRUMTL_API double mrumtl_brdf_lambertian_get_reflectivity (const struct mrumtl_brdf* brdf); - + MRUMTL_API double mrumtl_brdf_specular_get_reflectivity (const struct mrumtl_brdf* brdf); diff --git a/src/mrumtl_smtl.c b/src/mrumtl_smtl.c @@ -0,0 +1,195 @@ +/* Copyright (C) 2020 |Meso|Star> (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 /* getopt support */ + +#include "mrumtl.h" +#include "mrumtl_smtl.h" + +#include <getopt.h> + +struct args { + const char* brdf_filename; + int verbose; +}; +static const struct args ARGS_NULL; + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static void +args_release(struct args* args) +{ + ASSERT(args); + *args = ARGS_NULL; +} + +static res_T +args_init(struct args* args, int argc, char** argv) +{ + int opt; + res_T res = RES_OK; + ASSERT(args); + + *args = ARGS_NULL; + + /* Reset the getopt global variables */ + optind = 0; + opterr = 0; + + while((opt = getopt(argc, argv, "b:v")) != -1) { + switch(opt) { + case 'b': args->brdf_filename = optarg; break; + case 'v': args->verbose = 1; break; + default: res = RES_BAD_ARG; break; + } + if(res != RES_OK) { + if(optarg) { + fprintf(stderr, "%s: invalid option argument '%s' -- '%c'\n", + argv[0], optarg, opt); + } + goto error; + } + } + + if(!args->brdf_filename) { + fprintf(stderr, + "%s: missing the no BRDF filename -- option '-b'\n", + argv[0]); + res = RES_BAD_ARG; + goto error; + } + +exit: + return res; +error: + args_release(args); + goto exit; +} + +/******************************************************************************* + * Exported functions + ******************************************************************************/ +res_T +smtl_program_init + (struct logger* logger, + struct mem_allocator* allocator, + int argc, + char** argv, + void** out_prog) +{ + struct args args = ARGS_NULL; + struct mrumtl* mrumtl = NULL; + res_T res = RES_OK; + + if(!argc || !argv || !out_prog) { + res = RES_BAD_ARG; + goto error; + } + + res = args_init(&args, argc, argv); + if(res != RES_OK) goto error; + + res = mrumtl_create(logger, allocator, args.verbose, &mrumtl); + if(res != RES_OK) goto error; + + res = mrumtl_load(mrumtl, args.brdf_filename); + if(res != RES_OK) goto error; + +exit: + args_release(&args); + if(out_prog) *out_prog = mrumtl; + return res; +error: + if(mrumtl) { + MRUMTL(ref_put(mrumtl)); + mrumtl = NULL; + } + goto exit; +} + +void +smtl_program_release(void* program) +{ + MRUMTL(ref_put(program)); +} + +const char* +smtl_program_get_mtl_name(void* program) +{ + return mrumtl_get_name(program); +} + +enum smtl_mtl_type +smtl_program_get_mtl_type(void* program) +{ + (void)program; + return SMTL_MTL_OPAQUE; +} + +enum smtl_brdf_type +smtl_program_get_brdf_type + (void* program, + const double wavelength, + const struct smtl_fragment* frag) +{ + const struct mrumtl* mrumtl = program; + const struct mrumtl_brdf* brdf = NULL; + enum smtl_brdf_type type = SMTL_BRDF_NONE__; + (void)frag; + ASSERT(program && frag); + + MRUMTL(fetch_brdf(mrumtl, wavelength, &brdf)); + + switch(mrumtl_brdf_get_type(brdf)) { + case MRUMTL_BRDF_LAMBERTIAN: type = SMTL_BRDF_LAMBERTIAN; break; + case MRUMTL_BRDF_SPECULAR: type = SMTL_BRDF_SPECULAR; break; + default: FATAL("Unreachable code.\n"); break; + } + return type; +} + +double +smtl_program_brdf_lambertian_get_reflectivity + (void* program, + const double wavelength, + const struct smtl_fragment* frag) +{ + const struct mrumtl* mrumtl = program; + const struct mrumtl_brdf* brdf = NULL; + (void)frag; + ASSERT(program && frag); + + MRUMTL(fetch_brdf(mrumtl, wavelength, &brdf)); + ASSERT(mrumtl_brdf_get_type(brdf) == MRUMTL_BRDF_LAMBERTIAN); + return mrumtl_brdf_lambertian_get_reflectivity(brdf); +} + +double +smtl_program_brdf_specular_get_reflectivity + (void* program, + const double wavelength, + const struct smtl_fragment* frag) +{ + const struct mrumtl* mrumtl = program; + const struct mrumtl_brdf* brdf = NULL; + (void)frag; + ASSERT(program && frag); + + MRUMTL(fetch_brdf(mrumtl, wavelength, &brdf)); + ASSERT(mrumtl_brdf_get_type(brdf) == MRUMTL_BRDF_LAMBERTIAN); + return mrumtl_brdf_specular_get_reflectivity(brdf); +} + diff --git a/src/mrumtl_smtl.h b/src/mrumtl_smtl.h @@ -0,0 +1,82 @@ +/* Copyright (C) 2020 |Meso|Star> (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/>. */ + +#ifndef MRUMTL_SMTL_H +#define MRUMTL_SMTL_H + +#include <star/smtl.h> +#include <rsys/rsys.h> + +/* + * Usage: mrumtl [OPTION]... -b BRDF + * Manage the BRDF of a surface + * + * -b BRDF MruMtl file storing BRDF data + * -v make mrumtl verbose + */ + +BEGIN_DECLS + +/******************************************************************************* + * Common functions + ******************************************************************************/ +MRUMTL_API res_T +smtl_program_init + (struct logger* logger, /* NULL <=> use default logger */ + struct mem_allocator* allocator, /* NULL <=> use default allocator */ + int argc, + char* argv[], + void** out_prog); + +MRUMTL_API void +smtl_program_release + (void* program); + +/******************************************************************************* + * General material attribs + ******************************************************************************/ +MRUMTL_API const char* +smtl_program_get_mtl_name + (void* program); + +MRUMTL_API enum smtl_mtl_type +smtl_program_get_mtl_type + (void* program); + +/******************************************************************************* + * BRDF attribs + ******************************************************************************/ +MRUMTL_API enum smtl_brdf_type +smtl_program_get_brdf_type + (void* program, + const double wavelength, + const struct smtl_fragment* frag); + +MRUMTL_API double +smtl_program_brdf_lambertian_get_reflectivity + (void* program, + const double wavelength, + const struct smtl_fragment* frag); + +MRUMTL_API double +smtl_program_brdf_specular_get_reflectivity + (void* program, + const double wavelength, + const struct smtl_fragment* frag); + +END_DECLS + +#endif /* MRUMTL_SMTL_H */ +