commit f38664cfbe50a90f2f022bc8b091b207bdc30104
parent 2f5d33d507293fade2e3fc4a3c35e635f6b3433d
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Thu, 18 Jan 2018 18:58:32 +0100
Test scene composed of meshes and spherical shapes
Diffstat:
3 files changed, 154 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_box)
new_test(test_s3d_sphere_instance)
new_test(test_s3d_primitive)
new_test(test_s3d_trace_ray_instance)
diff --git a/src/test_s3d_sphere.c b/src/test_s3d_sphere.c
@@ -54,6 +54,8 @@ main(int argc, char** argv)
float dir[3];
float range[2];
float N[3], P[3], tmp[3];
+ float lower[3];
+ float upper[3];
float area;
float volume;
size_t nprims;
@@ -147,6 +149,10 @@ main(int argc, char** argv)
CHK(s3d_scene_view_primitives_count(view, &nprims) == RES_OK);
CHK(nprims == 2);
+ CHK(s3d_scene_view_get_aabb(view, lower, upper) == RES_OK);
+ CHK(f3_eq_eps(lower, f3_splat(tmp, -2), 1.e-6f));
+ CHK(f3_eq_eps(upper, f3(tmp, 6, 2, 2), 1.e-6f));
+
CHK(s3d_scene_view_get_primitive(view, 0, &prim0) == RES_OK);
CHK(s3d_scene_view_get_primitive(view, 1, &prim1) == RES_OK);
CHK(prim0.prim_id == 0);
diff --git a/src/test_s3d_sphere_box.c b/src/test_s3d_sphere_box.c
@@ -0,0 +1,147 @@
+/* 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_cbox.h"
+#include "test_s3d_utils.h"
+
+#include <rsys/image.h>
+#include <rsys/float3.h>
+
+int
+main(int argc, char** argv)
+{
+ struct mem_allocator allocator;
+ struct s3d_device* dev;
+ struct s3d_shape* box;
+ struct s3d_shape* sphere;
+ struct s3d_scene* scn;
+ struct s3d_scene_view* view;
+ struct s3d_vertex_data vdata;
+ struct cbox_desc desc;
+ 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 tmp[3];
+ float lower[3];
+ float upper[3];
+ float proj_ratio;
+ size_t x, y;
+ 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, &sphere) == RES_OK);
+ CHK(s3d_sphere_setup(sphere, f3(center, 150, 200, 90), 90) == RES_OK);
+ CHK(s3d_scene_attach_shape(scn, sphere) == RES_OK);
+ CHK(s3d_shape_ref_put(sphere) == RES_OK);
+
+ CHK(s3d_shape_create_sphere(dev, &sphere) == RES_OK);
+ CHK(s3d_sphere_setup(sphere, f3(center, 400, 200, 90), 90) == RES_OK);
+ CHK(s3d_scene_attach_shape(scn, sphere) == RES_OK);
+ CHK(s3d_shape_ref_put(sphere) == RES_OK);
+
+ desc.vertices = cbox_walls;
+ desc.indices = cbox_walls_ids;
+ vdata.usage = S3D_POSITION;
+ vdata.type = S3D_FLOAT3;
+ vdata.get = cbox_get_position;
+ CHK(s3d_shape_create_mesh(dev, &box) == RES_OK);
+ CHK(s3d_mesh_setup_indexed_vertices(box, cbox_walls_ntris, cbox_get_ids,
+ cbox_walls_nverts, &vdata, 1, &desc) == RES_OK);
+ CHK(s3d_scene_attach_shape(scn, box) == RES_OK);
+ CHK(s3d_shape_ref_put(box) == RES_OK);
+
+ CHK(s3d_scene_view_create(scn, S3D_TRACE, &view) == RES_OK);
+ CHK(s3d_scene_view_get_aabb(view, lower, upper) == RES_OK);
+ CHK(f3_eq(lower, f3(tmp, 0, 0, 0)));
+ CHK(f3_eq(upper, f3(tmp, 552, 559, 548)));
+
+ 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, 278.f, -1000.f, 273.f);
+ f3(tgt, 278.f, 0.f, 273.f);
+ f3(up, 0, 0, 1);
+ 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);
+
+ CHK(s3d_device_ref_put(dev) == RES_OK);
+ CHK(s3d_scene_ref_put(scn) == 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;
+}