commit 44a91f2abfd7be202bd1254298c2c5474029a270
parent 4fae190e507ceea73f6b0ce7b413dd0846f8d1ee
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Tue, 17 Mar 2015 12:57:11 +0100
Implement and test the s3d_scene_trace_ray function
Diffstat:
8 files changed, 382 insertions(+), 33 deletions(-)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -68,7 +68,7 @@ rcmake_prepend_path(S3D_FILES_INC ${S3D_SOURCE_DIR})
set_source_files_properties(${S3D_FILES_SRC} PROPERTIES LANGUAGE CXX)
add_library(s3d SHARED ${S3D_FILES_SRC} ${S3D_FILES_INC})
-target_link_libraries(s3d Embree)
+target_link_libraries(s3d Embree m)
set_target_properties(s3d PROPERTIES
DEFINE_SYMBOL S3D_SHARED_BUILD
LINKER_LANGUAGE CXX
@@ -95,6 +95,7 @@ if(NOT NO_TEST)
new_test(test_s3d_device)
new_test(test_s3d_scene)
new_test(test_s3d_shape)
+ new_test(test_s3d_trace_ray)
endif(NOT NO_TEST)
################################################################################
diff --git a/src/s3d.h b/src/s3d.h
@@ -96,9 +96,9 @@ static const struct s3d_vertex_data S3D_VERTEX_DATA_NULL =
struct s3d_hit {
struct s3d_shape* shape; /* Hit shape */
unsigned iprim; /* Index of the intersected primitive */
- const float normal[3]; /* Unormalized geometry normal */
- const float uv[2]; /* Barycentric coordinates of the hit onto `iprim' */
- const float distance; /* Hit distance from the ray origin */
+ float normal[3]; /* Unormalized geometry normal */
+ float uv[2]; /* Barycentric coordinates of the hit onto `iprim' */
+ float distance; /* Hit distance from the ray origin */
};
/* Constant defining a NULL intersection. Should be used to initialize a hit */
diff --git a/src/s3d_scene.c b/src/s3d_scene.c
@@ -32,25 +32,32 @@
#include "s3d.h"
#include "s3d_device_c.h"
+#include "s3d_scene_c.h"
#include "s3d_shape_c.h"
-#include <embree2/rtcore.h>
+#include <rsys/float3.h>
#include <rsys/mem_allocator.h>
-struct s3d_scene {
- struct list_node shapes; /* List of attached shapes */
- struct s3d_device* dev;
- RTCScene rtc_scn;
- ref_T ref;
-};
+#include <embree2/rtcore_ray.h>
/*******************************************************************************
* Helper functions
******************************************************************************/
static void
+delete_rtc_geometry(struct s3d_scene* scn, struct s3d_shape* shape)
+{
+ ASSERT(scn && shape && shape->rtc_geom != INVALID_RTC_GEOMETRY);
+ rtcDeleteGeometry(scn->rtc_scn, shape->rtc_geom);
+ darray_geom2shape_data_get(&scn->geom2shape)[shape->rtc_geom] = NULL;
+ shape->rtc_geom = INVALID_RTC_GEOMETRY;
+}
+
+static res_T
scene_setup(struct s3d_scene* scn)
{
struct list_node* node;
+ res_T res;
+ ASSERT(scn);
LIST_FOR_EACH(node, &scn->shapes) {
struct s3d_shape* shape = CONTAINER_OF
@@ -60,7 +67,7 @@ scene_setup(struct s3d_scene* scn)
const size_t ntris = darray_u32_size_get(&shape->data.mesh.indices)/3;
const size_t nverts = darray_float_size_get
(&shape->data.mesh.attribs[S3D_POSITION])/3;
- ASSERT(IS_ALIGNED(ids, 16));
+ /*ASSERT(IS_ALIGNED(ids, 16));*/
/* The Embree geometry is no more valid */
if(shape->data.mesh.resize_mask && shape->rtc_geom!=INVALID_RTC_GEOMETRY) {
@@ -84,10 +91,27 @@ scene_setup(struct s3d_scene* scn)
sizeof(uint32_t[3]));
rtcSetBuffer(scn->rtc_scn, shape->rtc_geom, RTC_VERTEX_BUFFER, pos, 0,
sizeof(float[3]));
+
+ if(shape->rtc_geom >= darray_geom2shape_size_get(&scn->geom2shape)) {
+ res = darray_geom2shape_resize(&scn->geom2shape, shape->rtc_geom+1);
+ if(res != RES_OK) goto error;
+ }
+ darray_geom2shape_data_get(&scn->geom2shape)[shape->rtc_geom] = shape;
}
}
/* Commit the scene updates */
rtcCommit(scn->rtc_scn);
+exit:
+ return res;
+error:
+ LIST_FOR_EACH(node, &scn->shapes) {
+ struct s3d_shape* shape = CONTAINER_OF
+ (node, struct s3d_shape, scene_attachment);
+ if(shape->rtc_geom != INVALID_RTC_GEOMETRY)
+ delete_rtc_geometry(scn, shape);
+ }
+ rtcCommit(scn->rtc_scn);
+ goto exit;
}
static void
@@ -101,6 +125,7 @@ scene_release(ref_T* ref)
dev = scn->dev;
if(scn->rtc_scn)
rtcDeleteScene(scn->rtc_scn);
+ darray_geom2shape_release(&scn->geom2shape);
MEM_FREE(dev->allocator, scn);
S3D(device_ref_put(dev));
}
@@ -124,6 +149,7 @@ s3d_scene_create(struct s3d_device* dev, struct s3d_scene** out_scn)
res = RES_MEM_ERR;
goto error;
}
+ darray_geom2shape_init(dev->allocator, &scn->geom2shape);
list_init(&scn->shapes);
ref_init(&scn->ref);
S3D(device_ref_get(dev));
@@ -168,11 +194,12 @@ s3d_scene_attach_shape(struct s3d_scene* scn, struct s3d_shape* shape)
{
if(!scn || !shape || !is_list_empty(&shape->scene_attachment))
return RES_BAD_ARG;
- ASSERT(shape->rtc_scn == NULL);
+ ASSERT(shape->scn == NULL);
ASSERT(shape->rtc_geom == INVALID_RTC_GEOMETRY);
S3D(shape_ref_get(shape));
list_add_tail(&scn->shapes, &shape->scene_attachment);
- shape->rtc_scn = scn->rtc_scn;
+ shape->scn = scn;
+ scn->is_updated = 1;
return RES_OK;
}
@@ -191,3 +218,70 @@ s3d_scene_clear(struct s3d_scene* scn)
return RES_OK;
}
+res_T
+s3d_scene_trace_ray
+ (struct s3d_scene* scn,
+ const float org[3],
+ const float dir[3],
+ const float range[2],
+ struct s3d_hit* hit)
+{
+ struct RTCRay ray;
+ res_T res = RES_OK;
+ if(!scn || !org || !dir || !range || !hit) {
+ res = RES_BAD_ARG;
+ goto error;
+ }
+ if(!f3_is_normalized(dir) || range[0] < 0.f || range[0] > range[1]) {
+ res = RES_BAD_ARG;
+ goto error;
+ }
+ if(scn->is_updated) {
+ scene_setup(scn);
+ scn->is_updated = 0;
+ }
+
+ f3_set(ray.org, org);
+ f3_set(ray.dir, dir);
+ ray.tnear = range[0];
+ ray.tfar = range[1];
+ ray.geomID = RTC_INVALID_GEOMETRY_ID;
+ ray.primID = RTC_INVALID_GEOMETRY_ID;
+ ray.instID = RTC_INVALID_GEOMETRY_ID;
+ ray.mask = 0xFFFFFFFF;
+ ray.time = 0.f;
+ rtcIntersect(scn->rtc_scn, ray);
+
+ if(ray.geomID == RTC_INVALID_GEOMETRY_ID) {
+ hit->shape = NULL;
+ } else {
+ f3_set(hit->normal, ray.Ng);
+ hit->uv[0] = ray.u;
+ hit->uv[1] = ray.v;
+ hit->distance = ray.tfar;
+ hit->iprim = ray.primID;
+ ASSERT(ray.geomID < darray_geom2shape_size_get(&scn->geom2shape));
+ hit->shape = darray_geom2shape_data_get(&scn->geom2shape)[ray.geomID];
+ ASSERT(hit->shape != NULL && ray.geomID == hit->shape->rtc_geom);
+ }
+
+exit:
+ return res;
+error:
+ goto exit;
+}
+
+/*******************************************************************************
+ * Local function
+ ******************************************************************************/
+void
+scene_remove_shape(struct s3d_scene* scn, struct s3d_shape* shape)
+{
+ ASSERT(shape->scn == scn);
+ if(shape->rtc_geom != INVALID_RTC_GEOMETRY)
+ delete_rtc_geometry(scn, shape);
+ list_del(&shape->scene_attachment);
+ shape->scn = NULL;
+ S3D(shape_ref_put(shape));
+}
+
diff --git a/src/s3d_scene_c.h b/src/s3d_scene_c.h
@@ -0,0 +1,61 @@
+/* Copyright (C) |Meso|Star> 2015 (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. */
+
+#ifndef S3D_SCENE_C_H
+#define S3D_SCENE_C_H
+
+#include <rsys/dynamic_array.h>
+#include <rsys/list.h>
+#include <rsys/ref_count.h>
+
+#include <embree2/rtcore.h>
+
+#define DARRAY_NAME geom2shape
+#define DARRAY_DATA struct s3d_shape*
+#include <rsys/dynamic_array.h>
+
+struct s3d_scene {
+ struct list_node shapes; /* List of attached shapes */
+ struct darray_geom2shape geom2shape;
+ struct s3d_device* dev;
+ RTCScene rtc_scn;
+ char is_updated; /* Flag defining if the scene description was updated */
+ ref_T ref;
+};
+
+extern LOCAL_SYM void
+scene_remove_shape
+ (struct s3d_scene* scn,
+ struct s3d_shape* shape);
+
+#endif /* S3D_SCENE_C_H */
+
diff --git a/src/s3d_shape.c b/src/s3d_shape.c
@@ -32,6 +32,7 @@
#include "s3d.h"
#include "s3d_device_c.h"
+#include "s3d_scene_c.h"
#include "s3d_shape_c.h"
#include <rsys/mem_allocator.h>
@@ -55,8 +56,8 @@ get_s3d_type_dimension(const enum s3d_type type)
static FINLINE void
shape_delete_rtc_geometry(struct s3d_shape* shape)
{
- ASSERT(shape && shape->rtc_geom != INVALID_RTC_GEOMETRY && shape->rtc_scn);
- rtcDeleteGeometry(shape->rtc_scn, shape->rtc_geom);
+ ASSERT(shape && shape->rtc_geom != INVALID_RTC_GEOMETRY && shape->scn);
+ rtcDeleteGeometry(shape->scn->rtc_scn, shape->rtc_geom);
shape->rtc_geom = INVALID_RTC_GEOMETRY;
}
@@ -259,7 +260,7 @@ shape_release(ref_T* ref)
dev = shape->dev;
/* The shape should not be attached */
ASSERT(is_list_empty(&shape->scene_attachment));
- ASSERT(shape->rtc_scn == NULL);
+ ASSERT(shape->scn == NULL);
/* The rtc_geom should be invalid */
ASSERT(shape->rtc_geom == INVALID_RTC_GEOMETRY);
shape_release_data(shape);
@@ -291,6 +292,7 @@ s3d_shape_create_mesh
mesh_init(dev->allocator, &shape->data.mesh);
shape->type = SHAPE_MESH;
shape->rtc_geom = INVALID_RTC_GEOMETRY;
+ shape->scn = NULL;
S3D(device_ref_get(dev));
shape->dev = dev;
ref_init(&shape->ref);
@@ -328,12 +330,8 @@ s3d_shape_detach(struct s3d_shape* shape)
if(!shape) return RES_BAD_ARG;
if(is_list_empty(&shape->scene_attachment)) /* The shape is not attached */
return RES_OK;
- ASSERT(shape->rtc_scn);
- if(shape->rtc_geom != INVALID_RTC_GEOMETRY)
- shape_delete_rtc_geometry(shape);
- list_del(&shape->scene_attachment);
- shape->rtc_scn = NULL;
- S3D(shape_ref_put(shape));
+ ASSERT(shape->scn);
+ scene_remove_shape(shape->scn, shape);
return RES_OK;
}
@@ -406,6 +404,8 @@ s3d_shape_mesh_setup_indexed_vertices
mesh_setup_attribs(&shape->data.mesh, nverts, attribs + iattr, data);
}
}
+ if(shape->scn) /* Notify the shape update */
+ shape->scn->is_updated = 1;
exit:
return res;
diff --git a/src/s3d_shape_c.h b/src/s3d_shape_c.h
@@ -70,7 +70,7 @@ struct s3d_shape {
struct list_node scene_attachment;
enum shape_type type;
unsigned rtc_geom; /* Embree geometry id */
- RTCScene rtc_scn; /* The RTC scene from which the rtc_geom was created */
+ struct s3d_scene* scn; /* The scene on which the shape is attached */
union {
struct s3d_scene* scene;
diff --git a/src/test_s3d_cbox.h b/src/test_s3d_cbox.h
@@ -90,20 +90,12 @@ const uint32_t cbox_ids[] = {
const unsigned cbox_nids = (unsigned)(sizeof(cbox_ids)/sizeof(uint32_t));
const unsigned cbox_ntris = (unsigned)(sizeof(cbox_ids)/sizeof(uint32_t)/3);
-static INLINE unsigned
-cbox_get_ntris(void* data)
-{
- (void)data;
- CHECK(cbox_nids % 3, 0);
- return (unsigned)(cbox_nids / 3);
-}
-
static INLINE void
cbox_get_ids(const unsigned itri, unsigned ids[3], void* data)
{
const unsigned id = itri * 3;
(void)data;
- CHECK(itri < cbox_get_ntris(data), 1);
+ CHECK(itri < cbox_ntris, 1);
CHECK(id + 2 < cbox_nids, 1);
ids[0] = cbox_ids[id + 0];
ids[1] = cbox_ids[id + 1];
diff --git a/src/test_s3d_trace_ray.c b/src/test_s3d_trace_ray.c
@@ -0,0 +1,201 @@
+/* Copyright (C) |Meso|Star> 2015 (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_cbox.h"
+#include "test_s3d_utils.h"
+
+#include <rsys/image.h>
+#include <rsys/float3.h>
+
+#define IMG_WIDTH 640
+#define IMG_HEIGHT 480
+
+struct camera {
+ float pos[3];
+ float x[3], y[3], z[3]; /* Basis */
+};
+
+static void
+camera_init(struct camera* cam)
+{
+ const float pos[3] = { 178.f, -1000.f, 273.f };
+ const float tgt[3] = { 178.f, 0.f, 273.f };
+ const float up[3] = { 0.f, 0.f, 1.f };
+ const float proj_ratio = (float)IMG_WIDTH/(float)IMG_HEIGHT;
+ const float fov_x = (float)PI * 0.25f;
+ float f = 0.f;
+ ASSERT(cam);
+
+ f3_set(cam->pos, pos);
+ f = f3_normalize(cam->z, f3_sub(cam->z, tgt, pos)); NCHECK(f, 0);
+ f = f3_normalize(cam->x, f3_cross(cam->x, cam->z, up)); NCHECK(f, 0);
+ f = f3_normalize(cam->y, f3_cross(cam->y, cam->z, cam->x)); NCHECK(f, 0);
+ f3_divf(cam->z, cam->z, (float)tan(fov_x*0.5f));
+ f3_divf(cam->y, cam->y, proj_ratio);
+}
+
+static void
+camera_ray
+ (const struct camera* cam,
+ const float pixel[2],
+ float org[3],
+ float dir[3])
+{
+ float x[3], y[3], f;
+ ASSERT(cam && pixel && org && dir);
+
+ f3_mulf(x, cam->x, pixel[0]*2.f - 1.f);
+ f3_mulf(y, cam->y, pixel[1]*2.f - 1.f);
+ f3_add(dir, f3_add(dir, x, y), cam->z);
+ f = f3_normalize(dir, dir); NCHECK(f, 0);
+ f3_set(org, cam->pos);
+}
+
+int
+main(int argc, char** argv)
+{
+ struct mem_allocator allocator;
+ struct s3d_device* dev;
+ struct s3d_hit hit;
+ struct s3d_scene* scn;
+ struct s3d_shape* shape;
+ struct s3d_vertex_data attribs[4];
+ struct camera cam;
+ unsigned char* img = NULL;
+ size_t ix, iy;
+ float org[3] = { 0.f, 0.f, 0.f };
+ float dir[3] = { 0.f, 1.f, 0.f };
+ float range[2] = { 0.f, FLT_MAX };
+ (void)argc, (void)argv;
+
+ mem_init_proxy_allocator(&allocator, &mem_default_allocator);
+
+ if(argc > 1) {
+ img = MEM_ALLOC(&allocator, 3 * IMG_WIDTH * IMG_HEIGHT);
+ NCHECK(img, NULL);
+ }
+
+ CHECK(s3d_device_create(NULL, &allocator, &dev), RES_OK);
+ CHECK(s3d_scene_create(dev, &scn), RES_OK);
+ CHECK(s3d_shape_create_mesh(dev, &shape), RES_OK);
+
+ attribs[0].usage = S3D_POSITION;
+ attribs[0].type = S3D_FLOAT3;
+ attribs[0].get = cbox_get_position;
+ attribs[1] = S3D_VERTEX_DATA_NULL;
+
+ CHECK(s3d_shape_mesh_setup_indexed_vertices
+ (shape, cbox_ntris, cbox_get_ids, cbox_nverts, attribs, NULL), RES_OK);
+ CHECK(s3d_scene_attach_shape(scn, shape), RES_OK);
+
+ CHECK(s3d_scene_trace_ray(NULL, NULL, NULL, NULL, NULL), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(scn, NULL, NULL, NULL, NULL), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(NULL, org, NULL, NULL, NULL), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(scn, org, NULL, NULL, NULL), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(NULL, NULL, dir, NULL, NULL), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(scn, NULL, dir, NULL, NULL), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(NULL, org, dir, NULL, NULL), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(scn, org, dir, NULL, NULL), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(NULL, NULL, NULL, range, NULL), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(scn, NULL, NULL, range, NULL), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(NULL, org, NULL, range, NULL), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(scn, org, NULL, range, NULL), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(NULL, NULL, dir, range, NULL), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(scn, NULL, dir, range, NULL), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(NULL, org, dir, range, NULL), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(scn, org, dir, range, NULL), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(NULL, NULL, NULL, NULL, &hit), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(scn, NULL, NULL, NULL, &hit), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(NULL, org, NULL, NULL, &hit), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(scn, org, NULL, NULL, &hit), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(NULL, NULL, dir, NULL, &hit), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(scn, NULL, dir, NULL, &hit), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(NULL, org, dir, NULL, &hit), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(scn, org, dir, NULL, &hit), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(NULL, NULL, NULL, range, &hit), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(scn, NULL, NULL, range, &hit), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(NULL, org, NULL, range, &hit), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(scn, org, NULL, range, &hit), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(NULL, NULL, dir, range, &hit), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(scn, NULL, dir, range, &hit), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(NULL, org, dir, range, &hit), RES_BAD_ARG);
+ CHECK(s3d_scene_trace_ray(scn, org, dir, range, &hit), RES_OK);
+ f3(dir, 1.f, 1.f, 1.f);
+ CHECK(s3d_scene_trace_ray(scn, org, dir, range, &hit), RES_BAD_ARG);
+
+ camera_init(&cam);
+ FOR_EACH(iy, 0, IMG_HEIGHT) {
+ float pixel[2];
+
+ pixel[1] = (float)iy/(float)IMG_HEIGHT;
+ FOR_EACH(ix, 0, IMG_WIDTH) {
+ const size_t ipix = (iy*IMG_WIDTH + ix) * 3/*RGB*/;
+
+ pixel[0] = (float)ix/(float)IMG_WIDTH;
+ camera_ray(&cam, pixel, org, dir);
+ CHECK(s3d_scene_trace_ray(scn, org, dir, range, &hit), RES_OK);
+ if(!img)
+ continue;
+
+ if(S3D_HIT_NONE(&hit)) {
+ img[ipix+0] = img[ipix+1] = img[ipix+2] = 0;
+ } else {
+ float wi[3], N[3], cos_theta, len;
+ unsigned char color;
+ len = f3_normalize(N, hit.normal);
+ NCHECK(len, 0);
+ cos_theta = f3_dot(f3_minus(wi, dir), N);
+ color = (unsigned char)(cos_theta * 255.f);
+ img[ipix+0] = color;
+ img[ipix+1] = color;
+ img[ipix+2] = color;
+ }
+ }
+ }
+ if(argc > 1) {
+ CHECK(image_ppm_write(argv[1], IMG_WIDTH, IMG_HEIGHT, 3, img), RES_OK);
+ }
+
+ if(img)
+ MEM_FREE(&allocator, img);
+
+ CHECK(s3d_device_ref_put(dev), RES_OK);
+ CHECK(s3d_shape_ref_put(shape), RES_OK);
+ CHECK(s3d_scene_ref_put(scn), RES_OK);
+
+ check_memory_allocator(&allocator);
+ mem_shutdown_proxy_allocator(&allocator);
+ CHECK(mem_allocated_size(), 0);
+
+ return 0;
+}