commit 2f5d33d507293fade2e3fc4a3c35e635f6b3433d
parent 4d8742a6c34b6d02f870f52ca59c73ab73dafa5d
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Thu, 18 Jan 2018 18:28:16 +0100
Test the instancing of spherical shapes
Diffstat:
2 files changed, 198 insertions(+), 0 deletions(-)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -149,6 +149,7 @@ if(NOT NO_TEST)
new_test(test_s3d_seams)
new_test(test_s3d_shape)
new_test(test_s3d_sphere)
+ new_test(test_s3d_sphere_instance)
new_test(test_s3d_primitive)
new_test(test_s3d_trace_ray_instance)
new_test(test_s3d_trace_ray_sphere)
diff --git a/src/test_s3d_sphere_instance.c b/src/test_s3d_sphere_instance.c
@@ -0,0 +1,197 @@
+/* Copyright (C) |Meso|Star> 2015-2018 (contact@meso-star.com)
+ *
+ * This software is a computer program whose purpose is to describe a
+ * virtual 3D environment that can be ray-traced and sampled both robustly
+ * and efficiently.
+ *
+ * This software is governed by the CeCILL license under French law and
+ * abiding by the rules of distribution of free software. You can use,
+ * modify and/or redistribute the software under the terms of the CeCILL
+ * license as circulated by CEA, CNRS and INRIA at the following URL
+ * "http://www.cecill.info".
+ *
+ * As a counterpart to the access to the source code and rights to copy,
+ * modify and redistribute granted by the license, users are provided only
+ * with a limited warranty and the software's author, the holder of the
+ * economic rights, and the successive licensors have only limited
+ * liability.
+ *
+ * In this respect, the user's attention is drawn to the risks associated
+ * with loading, using, modifying and/or developing or reproducing the
+ * software by the user in light of its specific status of free software,
+ * that may mean that it is complicated to manipulate, and that also
+ * therefore means that it is reserved for developers and experienced
+ * professionals having in-depth computer knowledge. Users are therefore
+ * encouraged to load and test the software's suitability as regards their
+ * requirements in conditions enabling the security of their systems and/or
+ * data to be ensured and, more generally, to use and operate it in the
+ * same conditions as regards security.
+ *
+ * The fact that you are presently reading this means that you have had
+ * knowledge of the CeCILL license and that you accept its terms. */
+
+#include "s3d.h"
+#include "test_s3d_camera.h"
+#include "test_s3d_utils.h"
+
+#include <rsys/image.h>
+#include <rsys/float2.h>
+#include <rsys/float3.h>
+
+static void
+test_sampling
+ (struct s3d_scene_view* view,
+ const unsigned geom_id,
+ const unsigned inst0_id)
+{
+ struct s3d_attrib attr0;
+ struct s3d_attrib attr1;
+ struct s3d_primitive prim;
+ int N = 10000;
+ int i;
+ float center[3];
+ float sum;
+ float st[2];
+ float E, V, SE;
+
+ /* Check that 50 percents of samples lie onto the 1st instance */
+ sum = 0;
+ FOR_EACH(i, 0, N) {
+ const float u = rand_canonic();
+ const float v = rand_canonic();
+ const float w = rand_canonic();
+
+ CHK(s3d_scene_view_sample(view, u, v, w, &prim, st) == RES_OK);
+ CHK(prim.geom_id == geom_id);
+ if(prim.inst_id == inst0_id) {
+ sum += 1;
+ }
+
+ CHK(s3d_primitive_get_attrib(&prim, S3D_POSITION, st, &attr0) == RES_OK);
+ CHK(s3d_primitive_get_attrib
+ (&prim, S3D_GEOMETRY_NORMAL, st, &attr1) == RES_OK);
+
+ if(prim.inst_id == inst0_id) {
+ f3_sub(attr0.value, attr0.value, f3(center,-1.5, 0, 0));
+ } else {
+ f3_sub(attr0.value, attr0.value, f3(center, 1.5, 0, 0));
+ }
+ f3_mulf(attr1.value, attr1.value, 2.f);
+ CHK(f3_eq_eps(attr0.value, attr1.value, 1.e-3f));
+ }
+ E = sum / (float)N;
+ V = sum / (float)N - E*E;
+ SE = (float)sqrt(V/(float)N);
+ CHK(eq_epsf(E, 0.5, SE));
+}
+
+static void
+test_ray_tracing(struct s3d_scene_view* view)
+{
+ struct image img;
+ struct camera cam;
+ const size_t img_sz[2] = {640, 480};
+ float pos[3] = {0, 0, 0};
+ float tgt[3] = {0, 0, 0};
+ float up[3] = {0, 1, 0};
+ float proj_ratio;
+ size_t x, y;
+
+ image_init(NULL, &img);
+ CHK(image_setup
+ (&img, img_sz[0], img_sz[1], img_sz[0]*3, IMAGE_RGB8, NULL) == RES_OK);
+
+ f3(pos, 0, 0, -10);
+ f3(tgt, 0, 0, 0);
+ f3(up, 0, 1, 0);
+ proj_ratio = (float)img_sz[0] / (float)img_sz[1];
+ camera_init(&cam, pos, tgt, up, (float)PI*0.25f, proj_ratio);
+
+ FOR_EACH(y, 0, img_sz[1]) {
+ float pixel[2];
+ pixel[1] = (float)y / (float)img_sz[1];
+ FOR_EACH(x, 0, img_sz[0]) {
+ const size_t ipix = (y*img_sz[0] + x)*3/*RGB*/;
+ struct s3d_hit hit;
+ const float range[2] = {0, FLT_MAX};
+ float org[3];
+ float dir[3];
+
+ pixel[0] = (float)x/(float)img_sz[0];
+ camera_ray(&cam, pixel, org, dir);
+ CHK(s3d_scene_view_trace_ray(view, org, dir, range, NULL, &hit) == RES_OK);
+ if(S3D_HIT_NONE(&hit)) {
+ ((uint8_t*)img.pixels)[ipix+0] = 0;
+ ((uint8_t*)img.pixels)[ipix+1] = 0;
+ ((uint8_t*)img.pixels)[ipix+2] = 0;
+ } else {
+ float normal[3] = {0.f, 0.f, 0.f};
+ float dot;
+ f3_normalize(normal, hit.normal);
+ dot = absf(f3_dot(normal, dir));
+ ((uint8_t*)img.pixels)[ipix+0] = (uint8_t)(dot*255.f);
+ ((uint8_t*)img.pixels)[ipix+1] = (uint8_t)(dot*255.f);
+ ((uint8_t*)img.pixels)[ipix+2] = (uint8_t)(dot*255.f);
+ }
+ }
+ }
+
+ /* Write image */
+ CHK(image_write_ppm_stream(&img, 0, stdout) == RES_OK);
+ image_release(&img);
+}
+
+int
+main(int argc, char** argv)
+{
+ struct mem_allocator allocator;
+ struct s3d_device* dev;
+ struct s3d_shape* sphere0;
+ struct s3d_shape* sphere1;
+ struct s3d_scene* scn;
+ struct s3d_scene_view* view;
+ unsigned geom_id;
+ unsigned inst0_id;
+ unsigned inst1_id;
+ float center[3];
+ (void)argc, (void)argv;
+
+ CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK);
+ CHK(s3d_device_create(NULL, &allocator, 0, &dev) == RES_OK);
+ CHK(s3d_scene_create(dev, &scn) == RES_OK);
+
+ CHK(s3d_shape_create_sphere(dev, &sphere0) == RES_OK);
+ CHK(s3d_sphere_setup(sphere0, f3_splat(center, 0), 2) == RES_OK);
+ CHK(s3d_shape_get_id(sphere0, &geom_id) == RES_OK);
+ CHK(s3d_scene_attach_shape(scn, sphere0) == RES_OK);
+ CHK(s3d_shape_ref_put(sphere0) == RES_OK);
+
+ CHK(s3d_scene_instantiate(scn, &sphere0) == RES_OK);
+ CHK(s3d_scene_instantiate(scn, &sphere1) == RES_OK);
+ CHK(s3d_shape_get_id(sphere0, &inst0_id) == RES_OK);
+ CHK(s3d_shape_get_id(sphere0, &inst1_id) == RES_OK);
+ CHK(s3d_scene_ref_put(scn) == RES_OK);
+
+ CHK(s3d_scene_create(dev, &scn) == RES_OK);
+ CHK(s3d_scene_attach_shape(scn, sphere0) == RES_OK);
+ CHK(s3d_scene_attach_shape(scn, sphere1) == RES_OK);
+ CHK(s3d_instance_set_position(sphere0, f3(center,-1.5, 0, 0)) == RES_OK);
+ CHK(s3d_instance_set_position(sphere1, f3(center, 1.5, 0, 0)) == RES_OK);
+
+ CHK(s3d_scene_view_create
+ (scn, S3D_TRACE|S3D_GET_PRIMITIVE|S3D_SAMPLE, &view) == RES_OK);
+
+ test_sampling(view, geom_id, inst0_id);
+ test_ray_tracing(view);
+
+ CHK(s3d_device_ref_put(dev) == RES_OK);
+ CHK(s3d_scene_ref_put(scn) == RES_OK);
+ CHK(s3d_shape_ref_put(sphere0) == RES_OK);
+ CHK(s3d_shape_ref_put(sphere1) == RES_OK);
+ CHK(s3d_scene_view_ref_put(view) == RES_OK);
+
+ check_memory_allocator(&allocator);
+ mem_shutdown_proxy_allocator(&allocator);
+ CHK(mem_allocated_size() == 0);
+ return 0;
+}