commit 32bddcd0f6386444d79c04c31d70507a2ddf2667
parent c96bb2e0a359abac82a72f2921622d2871fff43f
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Wed, 7 Sep 2016 11:30:35 +0200
Implement the ssf_bxdf API
Diffstat:
4 files changed, 154 insertions(+), 5 deletions(-)
diff --git a/README.md b/README.md
@@ -2,8 +2,8 @@
This library defines a set of Bidirectional Reflectance|Transmittance
Distribution Functions (BRDF & BTDF). It also provides a Bidirectional
-Scattering Distribution Function (BSDF) data structure as a collection BRDFs
-and BDTFs, that describes the way the light is scattered at a surface.
+Scattering Distribution Function (BSDF) data structure that is a collection of
+BRDFs and BDTFs that describes the way the light is scattered at a surface.
## How to build
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -43,11 +43,13 @@ set(VERSION_MINOR 0)
set(VERSION_PATCH 0)
set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})
-set(SSF_FILES_SRC )
-set(SSF_FILES_INC ssf.h)
+set(SSF_FILES_SRC ssf_bxdf.c)
+set(SSF_FILES_INC_API ssf.h)
+set(SSF_FILES_INC ssf_bxdf_c.h)
set(SSF_FILES_DOC COPYING README.md)
rcmake_prepend_path(SSF_FILES_SRC ${SSF_SOURCE_DIR})
rcmake_prepend_path(SSF_FILES_INC ${SSF_SOURCE_DIR})
+rcmake_prepend_path(SSF_FILES_INC_API ${SSF_SOURCE_DIR})
rcmake_prepend_path(SSF_FILES_DOC ${PROJECT_SOURCE_DIR}/../)
if(BUILD_STATIC)
@@ -89,6 +91,6 @@ install(TARGETS ssf
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin)
-install(FILES ${SSF_FILES_INC} DESTINATION include/star/)
+install(FILES ${SSF_FILES_INC_API} DESTINATION include/star/)
install(FILES ${SSF_FILES_DOC} DESTINATION share/doc/star-sf/)
diff --git a/src/ssf_bxdf.c b/src/ssf_bxdf.c
@@ -0,0 +1,97 @@
+/* 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_bxdf_c.h"
+
+#include <rsys/mem_allocator.h>
+#include <rsys/ref_count.h>
+
+#include <string.h>
+
+/*******************************************************************************
+ * Helper functions
+ ******************************************************************************/
+static void
+bxdf_release(ref_T* ref)
+{
+ struct ssf_bxdf* bxdf = CONTAINER_OF(ref, struct ssf_bxdf, ref);
+ ASSERT(ref);
+ if(bxdf->data) MEM_RM(bxdf->allocator, bxdf->data);
+ MEM_RM(bxdf->allocator, bxdf);
+}
+
+/*******************************************************************************
+ * Exported functions
+ ******************************************************************************/
+res_T
+ssf_bxdf_ref_get(struct ssf_bxdf* bxdf)
+{
+ if(!bxdf) return RES_BAD_ARG;
+ ref_get(&bxdf->ref);
+ return RES_OK;
+}
+
+res_T
+ssf_bxdf_ref_put(struct ssf_bxdf* bxdf)
+{
+ if(!bxdf) return RES_BAD_ARG;
+ ref_put(&bxdf->ref, bxdf_release);
+ return RES_OK;
+}
+
+/*******************************************************************************
+ * Local functions
+ ******************************************************************************/
+res_T
+bxdf_create
+ (struct mem_allocator* allocator,
+ const size_t sizeof_data,
+ const size_t alignof_data,
+ struct ssf_bxdf** out_bxdf)
+{
+ struct mem_allocator* mem_allocator = NULL;
+ struct ssf_bxdf* bxdf = NULL;
+ res_T res = RES_OK;
+ ASSERT(out_bxdf);
+
+ mem_allocator = allocator ? allocator : &mem_default_allocator;
+ bxdf = MEM_CALLOC(mem_allocator, 1, sizeof(struct ssf_bxdf));
+ if(!bxdf) {
+ res = RES_MEM_ERR;
+ goto error;
+ }
+ ref_init(&bxdf->ref);
+ bxdf->allocator = mem_allocator;
+ if(!sizeof_data) goto exit;
+
+ bxdf->data = MEM_ALLOC_ALIGNED(bxdf->allocator, sizeof_data, alignof_data);
+ if(!bxdf->data) {
+ res = RES_MEM_ERR;
+ goto error;
+ }
+ memset(bxdf->data, 0, sizeof_data);
+
+exit:
+ *out_bxdf = bxdf;
+ return res;
+error:
+ if(bxdf) {
+ SSF(bxdf_ref_put(bxdf));
+ bxdf = NULL;
+ }
+ goto exit;
+}
+
diff --git a/src/ssf_bxdf_c.h b/src/ssf_bxdf_c.h
@@ -0,0 +1,50 @@
+/* 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/>. */
+
+#ifndef SSF_BXDF_C_H
+#define SSF_BXDF_C_H
+
+#include <rsys/ref_count.h>
+
+struct mem_allocator;
+
+typedef double /* Sampled radiance */
+(*bxdf_sample_func_T)
+ (void* data, /* BxDF internal data */
+ const double u, /* Canonical random number */
+ const double v, /* Canonical random number */
+ const double w[3], /* Incoming direction. Point inward the surface */
+ const double N[3], /* Normalized normal */
+ double dir[4]); /* Sampled direction. The PDF is stored in dir[3] */
+
+/* Generic Bidirectional <Reflec|Transmit>tance Distirbution Function */
+struct ssf_bxdf {
+ bxdf_sample_func_T sample;
+ void* data; /* Specific internal data of the BxDF */
+
+ /* Private data */
+ ref_T ref;
+ struct mem_allocator* allocator;
+};
+
+extern LOCAL_SYM res_T
+bxdf_create
+ (struct mem_allocator* allocator, /* May be NULL <=> Use default allocator */
+ const size_t sizeof_data,
+ const size_t alignof_data,
+ struct ssf_bxdf** bxdf);
+
+#endif /* SSF_BXDF_C_H */
+