star-3d

Surface structuring for efficient 3D geometric queries
git clone git://git.meso-star.fr/star-3d.git
Log | Files | Refs | README | LICENSE

commit 55e38691d3b6983359e110c15d3f7cdaa257d7b8
parent 8b847db0c8cac495dff66ea814346210c6e1ff23
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Thu,  6 Apr 2017 12:45:12 +0200

Use the new RSys image API

Diffstat:
Msrc/test_s3d_trace_ray.c | 49+++++++++++++++++--------------------------------
Msrc/test_s3d_trace_ray_instance.c | 43+++++++++++++++++++------------------------
2 files changed, 36 insertions(+), 56 deletions(-)

diff --git a/src/test_s3d_trace_ray.c b/src/test_s3d_trace_ray.c @@ -64,6 +64,7 @@ int main(int argc, char** argv) { struct mem_allocator allocator; + struct image img; struct s3d_device* dev; struct s3d_hit hit; struct s3d_scene* scn; @@ -78,7 +79,6 @@ main(int argc, char** argv) struct s3d_primitive prims[30]; struct camera cam; struct cbox_desc desc; - unsigned char* img = NULL; unsigned ntris, nverts; size_t nprims; size_t ix, iy; @@ -95,26 +95,18 @@ main(int argc, char** argv) unsigned short_block_id; size_t i; char filter = 0; - char* img_name = NULL; mem_init_proxy_allocator(&allocator, &mem_default_allocator); - if(argc > 1) { - if(!strcmp(argv[1], "filter")) { - filter = 1; - } else { - img_name = argv[1]; - } - } - if(!img_name && argc > 2) { - img_name = argv[2]; - } - if(img_name) { - img = MEM_ALLOC(&allocator, 3 * IMG_WIDTH * IMG_HEIGHT); - NCHECK(img, NULL); + if(argc > 1 && !strcmp(argv[1], "filter")) { + filter = 1; } - CHECK(s3d_device_create(NULL, &allocator, 1, &dev), RES_OK); + image_init(&allocator, &img); + CHECK(image_setup + (&img, IMG_WIDTH, IMG_HEIGHT, IMG_WIDTH*3, IMAGE_RGB8, NULL), RES_OK); + + CHECK(s3d_device_create(NULL, &allocator, 0, &dev), RES_OK); CHECK(s3d_scene_create(dev, &scn), RES_OK); attribs.usage = S3D_POSITION; @@ -293,9 +285,9 @@ main(int argc, char** argv) (scnview, org, dir, range, (void*)(uintptr_t)0xDEADBEEF, &hit), RES_OK); if(S3D_HIT_NONE(&hit)) { - if(img) { - img[ipix+0] = img[ipix+1] = img[ipix+2] = 0; - } + ((uint8_t*)img.pixels)[ipix+0] = 0; + ((uint8_t*)img.pixels)[ipix+1] = 0; + ((uint8_t*)img.pixels)[ipix+2] = 0; } else { float N[3], len, dot, col[3] = { 1.f, 1.f, 1.f }; struct s3d_attrib attr; @@ -333,9 +325,6 @@ main(int argc, char** argv) CHECK(hit.prim.scene_prim_id >= hit.prim.prim_id, 1); CHECK(hit.prim.scene_prim_id < 30, 1); - if(!img) - continue; - if(hit.prim.geom_id == walls_id) { if(hit.prim.prim_id == 4 || hit.prim.prim_id == 5) { col[0] = 1.f, col[1] = 0.f, col[2] = 0.f; @@ -348,21 +337,17 @@ main(int argc, char** argv) if(dot < 0.f) dot = f3_dot(f3_minus(N, N), dir); - img[ipix+0] = (unsigned char)(dot * col[0] * 255.f); - img[ipix+1] = (unsigned char)(dot * col[1] * 255.f); - img[ipix+2] = (unsigned char)(dot * col[2] * 255.f); + ((uint8_t*)img.pixels)[ipix+0] = (unsigned char)(dot * col[0] * 255.f); + ((uint8_t*)img.pixels)[ipix+1] = (unsigned char)(dot * col[1] * 255.f); + ((uint8_t*)img.pixels)[ipix+2] = (unsigned char)(dot * col[2] * 255.f); } } } CHECK(s3d_scene_view_ref_put(scnview), RES_OK); - if(img_name) { - CHECK(image_ppm_write(img_name, IMG_WIDTH, IMG_HEIGHT, 3, img), RES_OK); - } - - if(img) - MEM_RM(&allocator, img); - + CHECK(image_write_ppm_stream(&img, 0, stdout), RES_OK); + image_release(&img); + CHECK(s3d_device_ref_put(dev), RES_OK); CHECK(s3d_shape_ref_put(inst), RES_OK); CHECK(s3d_shape_ref_put(short_block), RES_OK); diff --git a/src/test_s3d_trace_ray_instance.c b/src/test_s3d_trace_ray_instance.c @@ -181,8 +181,9 @@ test_quad(struct s3d_device* dev) } static void -test_cbox(struct s3d_device* dev, const char* filename) +test_cbox(struct s3d_device* dev) { + struct image img; struct camera cam; struct cbox_desc cbox_desc; struct s3d_scene* scn; @@ -195,11 +196,10 @@ test_cbox(struct s3d_device* dev, const char* filename) float pos[3], tgt[3], up[3]; float org[3], dir[3], range[2]; float proj_ratio; - unsigned char* img = NULL; unsigned walls_id; - const int img_sz[2] = { 640, 480 }; - const int N = 8; - int x, y; + const size_t img_sz[2] = { 640, 480 }; + const size_t N = 8; + size_t x, y; CHECK(s3d_scene_create(dev, &cbox), RES_OK); CHECK(s3d_scene_create(dev, &scn), RES_OK); @@ -266,11 +266,9 @@ test_cbox(struct s3d_device* dev, const char* filename) proj_ratio = (float)img_sz[0] / (float)img_sz[1]; camera_init(&cam, pos, tgt, up, (float)PI*0.5f, proj_ratio); - /* Create the output image */ - if(filename) { - img = mem_alloc((size_t)(3*img_sz[0]*img_sz[1])); - NCHECK(img, NULL); - } + image_init(NULL, &img); + CHECK(image_setup + (&img, img_sz[0], img_sz[1], img_sz[0]*3, IMAGE_RGB8, NULL), RES_OK); /* Trace rays */ CHECK(s3d_scene_view_create(scn, S3D_TRACE, &view), RES_OK); @@ -280,18 +278,17 @@ test_cbox(struct s3d_device* dev, const char* filename) float pixel[2]; pixel[1] = (float)y / (float)img_sz[1]; FOR_EACH(x, 0, img_sz[0]) { - const int ipix = (y*img_sz[0] + x)*3/*RGB*/; + const size_t ipix = (y*img_sz[0] + x)*3/*RGB*/; struct s3d_hit hit; pixel[0] = (float)x/(float)img_sz[0]; camera_ray(&cam, pixel, org, dir); CHECK(s3d_scene_view_trace_ray(view, org, dir, range, NULL, &hit), RES_OK); - if(!img) continue; if(S3D_HIT_NONE(&hit)) { - img[ipix+0] = 0; - img[ipix+1] = 0; - img[ipix+2] = 0; + ((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 col[3], dot; @@ -308,19 +305,17 @@ test_cbox(struct s3d_device* dev, const char* filename) f3_normalize(normal, hit.normal); dot = absf(f3_dot(normal, dir)); - img[ipix+0] = (unsigned char)(dot * col[0] * 255.f); - img[ipix+1] = (unsigned char)(dot * col[1] * 255.f); - img[ipix+2] = (unsigned char)(dot * col[2] * 255.f); + ((uint8_t*)img.pixels)[ipix+0] = (uint8_t)(dot * col[0] * 255.f); + ((uint8_t*)img.pixels)[ipix+1] = (uint8_t)(dot * col[1] * 255.f); + ((uint8_t*)img.pixels)[ipix+2] = (uint8_t)(dot * col[2] * 255.f); } } } CHECK(s3d_scene_view_ref_put(view), RES_OK); /* Write image */ - if(img) { - CHECK(image_ppm_write(filename, img_sz[0], img_sz[1], 3, img), RES_OK); - mem_rm(img); - } + CHECK(image_write_ppm_stream(&img, 0, stdout), RES_OK); + image_release(&img); /* Release data */ CHECK(s3d_scene_ref_put(cbox), RES_OK); @@ -335,10 +330,10 @@ main(int argc, char** argv) (void)argc, (void)argv; mem_init_proxy_allocator(&allocator, &mem_default_allocator); - CHECK(s3d_device_create(NULL, &allocator, 1, &dev), RES_OK); + CHECK(s3d_device_create(NULL, &allocator, 0, &dev), RES_OK); test_quad(dev); - test_cbox(dev, argc > 1 ? argv[1] : NULL); + test_cbox(dev); CHECK(s3d_device_ref_put(dev), RES_OK);