star-3d

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

test_s3d_sphere_box.c (4402B)


      1 /* Copyright (C) 2015-2023, 2026 |Méso|Star> (contact@meso-star.com)
      2  *
      3  * This file is part of Star-3D.
      4  *
      5  * Star-3D is free software: you can redistribute it and/or modify
      6  * it under the terms of the GNU General Public License as published by
      7  * the Free Software Foundation, either version 3 of the License, or
      8  * (at your option) any later version.
      9  *
     10  * Star-3D is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     13  * GNU General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU General Public License
     16  * along with Star-3D. If not, see <http://www.gnu.org/licenses/>. */
     17 
     18 #include "s3d.h"
     19 #include "test_s3d_camera.h"
     20 #include "test_s3d_cbox.h"
     21 #include "test_s3d_utils.h"
     22 
     23 #include <rsys/image.h>
     24 #include <rsys/float3.h>
     25 
     26 int
     27 main(int argc, char** argv)
     28 {
     29   struct mem_allocator allocator;
     30   struct s3d_device* dev;
     31   struct s3d_shape* box;
     32   struct s3d_shape* sphere;
     33   struct s3d_scene* scn;
     34   struct s3d_scene_view* view;
     35   struct s3d_vertex_data vdata;
     36   struct cbox_desc desc;
     37   struct image img;
     38   struct camera cam;
     39   const size_t img_sz[2] = {640, 480};
     40   float pos[3] = {0, 0, 0};
     41   float tgt[3] = {0, 0, 0};
     42   float up[3] = {0, 1, 0};
     43   float tmp[3];
     44   float lower[3];
     45   float upper[3];
     46   float proj_ratio;
     47   size_t x, y;
     48   float center[3];
     49   (void)argc, (void)argv;
     50 
     51   CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK);
     52   CHK(s3d_device_create(NULL, &allocator, 0, &dev) == RES_OK);
     53   CHK(s3d_scene_create(dev, &scn) == RES_OK);
     54 
     55   CHK(s3d_shape_create_sphere(dev, &sphere) == RES_OK);
     56   CHK(s3d_sphere_setup(sphere, f3(center, 150, 200, 90), 90) == RES_OK);
     57   CHK(s3d_scene_attach_shape(scn, sphere) == RES_OK);
     58   CHK(s3d_shape_ref_put(sphere) == RES_OK);
     59 
     60   CHK(s3d_shape_create_sphere(dev, &sphere) == RES_OK);
     61   CHK(s3d_sphere_setup(sphere, f3(center, 400, 200, 90), 90) == RES_OK);
     62   CHK(s3d_scene_attach_shape(scn, sphere) == RES_OK);
     63   CHK(s3d_shape_ref_put(sphere) == RES_OK);
     64 
     65   desc.vertices = cbox_walls;
     66   desc.indices = cbox_walls_ids;
     67   vdata.usage = S3D_POSITION;
     68   vdata.type = S3D_FLOAT3;
     69   vdata.get = cbox_get_position;
     70   CHK(s3d_shape_create_mesh(dev, &box) == RES_OK);
     71   CHK(s3d_mesh_setup_indexed_vertices(box, cbox_walls_ntris, cbox_get_ids,
     72     cbox_walls_nverts, &vdata, 1, &desc) == RES_OK);
     73   CHK(s3d_scene_attach_shape(scn, box) == RES_OK);
     74   CHK(s3d_shape_ref_put(box) == RES_OK);
     75 
     76   CHK(s3d_scene_view_create(scn, S3D_TRACE, &view) == RES_OK);
     77   CHK(s3d_scene_view_get_aabb(view, lower, upper) == RES_OK);
     78   CHK(f3_eq(lower, f3(tmp, 0, 0, 0)));
     79   CHK(f3_eq(upper, f3(tmp, 552, 559, 548)));
     80 
     81   image_init(NULL, &img);
     82   CHK(image_setup
     83     (&img, img_sz[0], img_sz[1], img_sz[0]*3, IMAGE_RGB8, NULL) == RES_OK);
     84 
     85   f3(pos, 278.f, -1000.f, 273.f);
     86   f3(tgt, 278.f, 0.f, 273.f);
     87   f3(up, 0, 0, 1);
     88   proj_ratio = (float)img_sz[0] / (float)img_sz[1];
     89   camera_init(&cam, pos, tgt, up, (float)PI*0.25f, proj_ratio);
     90 
     91   FOR_EACH(y, 0, img_sz[1]) {
     92     float pixel[2];
     93     pixel[1] = (float)y / (float)img_sz[1];
     94     FOR_EACH(x, 0, img_sz[0]) {
     95       const size_t ipix = (y*img_sz[0] + x)*3/*RGB*/;
     96       struct s3d_hit hit;
     97       const float range[2] = {0, FLT_MAX};
     98       float org[3];
     99       float dir[3];
    100 
    101       pixel[0] = (float)x/(float)img_sz[0];
    102       camera_ray(&cam, pixel, org, dir);
    103       CHK(s3d_scene_view_trace_ray(view, org, dir, range, NULL, &hit) == RES_OK);
    104       if(S3D_HIT_NONE(&hit)) {
    105         ((uint8_t*)img.pixels)[ipix+0] = 0;
    106         ((uint8_t*)img.pixels)[ipix+1] = 0;
    107         ((uint8_t*)img.pixels)[ipix+2] = 0;
    108       } else {
    109         float normal[3] = {0.f, 0.f, 0.f};
    110         float dot;
    111         f3_normalize(normal, hit.normal);
    112         dot = absf(f3_dot(normal, dir));
    113         ((uint8_t*)img.pixels)[ipix+0] = (uint8_t)(dot*255.f);
    114         ((uint8_t*)img.pixels)[ipix+1] = (uint8_t)(dot*255.f);
    115         ((uint8_t*)img.pixels)[ipix+2] = (uint8_t)(dot*255.f);
    116       }
    117     }
    118   }
    119 
    120   /* Write image */
    121   CHK(image_write_ppm_stream(&img, 0, stdout) == RES_OK);
    122   image_release(&img);
    123 
    124   CHK(s3d_device_ref_put(dev) == RES_OK);
    125   CHK(s3d_scene_ref_put(scn) == RES_OK);
    126   CHK(s3d_scene_view_ref_put(view) == RES_OK);
    127 
    128   check_memory_allocator(&allocator);
    129   mem_shutdown_proxy_allocator(&allocator);
    130   CHK(mem_allocated_size() == 0);
    131   return 0;
    132 }