star-3d

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

test_s3d_sampler.c (8205B)


      1 /* Copyright (C) 2015-2023 |Méso|Star> (contact@meso-star.com)
      2  *
      3  * This program is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published by
      5  * the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * This program is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #include "s3d.h"
     17 #include "test_s3d_cbox.h"
     18 #include "test_s3d_utils.h"
     19 
     20 #include <rsys/float3.h>
     21 
     22 #define NSAMPS 4096
     23 
     24 int
     25 main(int argc, char** argv)
     26 {
     27   struct mem_allocator allocator;
     28   struct s3d_device* dev;
     29   struct s3d_scene* scn;
     30   struct s3d_scene* scn2;
     31   struct s3d_scene_view* scnview;
     32   struct s3d_shape* cbox;
     33   struct s3d_shape* walls;
     34   struct s3d_shape* short_block;
     35   struct s3d_shape* tall_block;
     36   struct s3d_vertex_data attribs;
     37   struct s3d_primitive prim;
     38   struct s3d_primitive prim1;
     39   struct s3d_attrib attr0, attr1;
     40   struct cbox_desc desc;
     41   float uv[2];
     42   size_t i;
     43   unsigned ntris, nverts;
     44   unsigned cbox_id;
     45   unsigned walls_id;
     46   unsigned short_block_id;
     47   unsigned tall_block_id;
     48   (void)argc, (void)argv;
     49 
     50   mem_init_proxy_allocator(&allocator, &mem_default_allocator);
     51 
     52   CHK(s3d_device_create(NULL, &allocator, 1, &dev) == RES_OK);
     53   CHK(s3d_scene_create(dev, &scn) == RES_OK);
     54   CHK(s3d_scene_create(dev, &scn2) == RES_OK);
     55   CHK(s3d_scene_instantiate(scn, &cbox) == RES_OK);
     56   CHK(s3d_scene_attach_shape(scn2, cbox) == RES_OK);
     57   CHK(s3d_shape_create_mesh(dev, &walls) == RES_OK);
     58   CHK(s3d_shape_create_mesh(dev, &short_block) == RES_OK);
     59   CHK(s3d_shape_create_mesh(dev, &tall_block) == RES_OK);
     60 
     61   CHK(s3d_shape_get_id(cbox, &cbox_id) == RES_OK);
     62   CHK(s3d_shape_get_id(walls, &walls_id) == RES_OK);
     63   CHK(s3d_shape_get_id(short_block, &short_block_id) == RES_OK);
     64   CHK(s3d_shape_get_id(tall_block, &tall_block_id) == RES_OK);
     65 
     66   CHK(s3d_scene_view_create(scn, S3D_SAMPLE, &scnview) == RES_OK);
     67   CHK(s3d_scene_view_sample(NULL, 0, 0, 0, NULL, NULL) == RES_BAD_ARG);
     68   CHK(s3d_scene_view_sample(scnview, 0, 0, 0, NULL, NULL) == RES_BAD_ARG);
     69   CHK(s3d_scene_view_sample(NULL, 0, 0, 0, &prim, NULL) == RES_BAD_ARG);
     70   CHK(s3d_scene_view_sample(scnview, 0, 0, 0, &prim, NULL) == RES_BAD_ARG);
     71   CHK(s3d_scene_view_sample(NULL, 0, 0, 0, NULL, uv) == RES_BAD_ARG);
     72   CHK(s3d_scene_view_sample(scnview, 0, 0, 0, NULL, uv) == RES_BAD_ARG);
     73   CHK(s3d_scene_view_sample(NULL, 0, 0, 0, &prim, uv) == RES_BAD_ARG);
     74   CHK(s3d_scene_view_sample(scnview, 0, 0, 0, &prim, uv) == RES_OK);
     75   CHK(s3d_scene_view_sample(scnview, -1, 0, 0, &prim, uv) == RES_BAD_ARG);
     76   CHK(s3d_scene_view_sample(scnview, 0, -1, 0, &prim, uv) == RES_BAD_ARG);
     77   CHK(s3d_scene_view_sample(scnview, 0, 0, -1, &prim, uv) == RES_BAD_ARG);
     78   CHK(s3d_scene_view_sample(scnview, 1, 0, 0, &prim, uv) == RES_BAD_ARG);
     79   CHK(s3d_scene_view_sample(scnview, 0, 1, 0, &prim, uv) == RES_BAD_ARG);
     80   CHK(s3d_scene_view_sample(scnview, 0, 0, 1, &prim, uv) == RES_BAD_ARG);
     81   CHK(s3d_scene_view_sample(scnview, 0.5f, 0.5f, 0.5f, &prim, uv) == RES_OK);
     82   CHK(S3D_PRIMITIVE_EQ(&prim, &S3D_PRIMITIVE_NULL) == 1);
     83   CHK(s3d_scene_view_ref_put(scnview) == RES_OK);
     84 
     85   attribs.usage = S3D_POSITION;
     86   attribs.type = S3D_FLOAT3;
     87   attribs.get = cbox_get_position;
     88 
     89   ntris = cbox_walls_ntris;
     90   nverts = cbox_walls_nverts;
     91   desc.vertices = cbox_walls;
     92   desc.indices = cbox_walls_ids;
     93   CHK(s3d_mesh_setup_indexed_vertices
     94     (walls, ntris, cbox_get_ids, nverts, &attribs, 1, &desc) == RES_OK);
     95 
     96   CHK(s3d_scene_attach_shape(scn, walls) == RES_OK);
     97 
     98   CHK(s3d_scene_view_create(scn, S3D_SAMPLE, &scnview) == RES_OK);
     99 
    100   CHK(s3d_scene_view_sample(scnview, 0.5f, 0.5f, 0.5f, &prim, uv) == RES_OK);
    101   CHK(s3d_primitive_get_attrib(&prim, S3D_POSITION, uv, &attr0) == RES_OK);
    102   CHK(s3d_scene_view_sample(scnview, 0.5f, 0.5f, 0.5f, &prim, uv) == RES_OK);
    103   CHK(s3d_primitive_get_attrib(&prim, S3D_POSITION, uv, &attr1) == RES_OK);
    104 
    105   prim1 = prim;
    106   CHK(S3D_PRIMITIVE_EQ(&prim, &prim1) == 1);
    107   prim1.inst_id = prim.inst_id + 1;
    108   CHK(S3D_PRIMITIVE_EQ(&prim, &prim1) == 0);
    109   prim1.inst_id = prim.inst_id;
    110   CHK(S3D_PRIMITIVE_EQ(&prim, &prim1) == 1);
    111   prim1.prim_id = S3D_INVALID_ID;
    112   CHK(S3D_PRIMITIVE_EQ(&prim, &prim1) == 0);
    113   prim1.prim_id = prim.prim_id;
    114   prim1.geom_id = S3D_INVALID_ID;
    115   CHK(S3D_PRIMITIVE_EQ(&prim, &prim1) == 0);
    116 
    117   CHK(attr0.type == S3D_FLOAT3);
    118   CHK(attr1.type == S3D_FLOAT3);
    119   CHK(f3_eq_eps(attr0.value, attr1.value, 1.e-6f) == 1);
    120 
    121   CHK(s3d_scene_view_sample(scnview, 0.3f, 0.1f, 0.2f, &prim, uv) == RES_OK);
    122   CHK(s3d_primitive_get_attrib(&prim, S3D_POSITION, uv, &attr1) == RES_OK);
    123   CHK(f3_eq_eps(attr0.value, attr1.value, 1.e-6f) != 1);
    124 
    125   CHK(s3d_scene_view_ref_put(scnview) == RES_OK);
    126 
    127   CHK(s3d_shape_enable(walls, 0) == RES_OK);
    128   CHK(s3d_scene_view_create(scn, S3D_SAMPLE, &scnview) == RES_OK);
    129   CHK(s3d_scene_view_sample(scnview, 0.5f, 0.5f, 0.5f, &prim, uv) == RES_OK);
    130   CHK(S3D_PRIMITIVE_EQ(&prim, &S3D_PRIMITIVE_NULL) == 1);
    131   CHK(s3d_scene_view_ref_put(scnview) == RES_OK);
    132   CHK(s3d_shape_enable(walls, 1) == RES_OK);
    133 
    134   ntris = cbox_block_ntris;
    135   nverts = cbox_block_nverts;
    136   desc.vertices = cbox_short_block;
    137   desc.indices = cbox_block_ids;
    138   CHK(s3d_mesh_setup_indexed_vertices
    139     (short_block, ntris, cbox_get_ids, nverts, &attribs, 1, &desc) == RES_OK);
    140 
    141   CHK(s3d_scene_attach_shape(scn, short_block) == RES_OK);
    142   CHK(s3d_scene_attach_shape(scn, tall_block) == RES_OK);
    143 
    144   CHK(s3d_scene_view_create(scn, S3D_SAMPLE, &scnview) == RES_OK);
    145   CHK(s3d_scene_view_sample(scnview, 0.5f, 0.5f, 0.5f, &prim, uv) == RES_OK);
    146   CHK(s3d_primitive_get_attrib(&prim, S3D_POSITION, uv, &attr0) == RES_OK);
    147   desc.vertices = cbox_tall_block;
    148   CHK(s3d_mesh_setup_indexed_vertices
    149     (tall_block, ntris, cbox_get_ids, nverts, &attribs, 1, &desc) == RES_OK);
    150   CHK(s3d_scene_view_sample(scnview, 0.5f, 0.5f, 0.5f, &prim, uv) == RES_OK);
    151   CHK(s3d_primitive_get_attrib(&prim, S3D_POSITION, uv, &attr1) == RES_OK);
    152   CHK(f3_eq_eps(attr0.value, attr1.value, 1.e-6f) == 1);
    153   CHK(s3d_scene_view_ref_put(scnview) == RES_OK);
    154 
    155   CHK(s3d_scene_view_create(scn, S3D_SAMPLE, &scnview) == RES_OK);
    156   CHK(s3d_scene_view_sample(scnview, 0.5f, 0.5f, 0.5f, &prim, uv) == RES_OK);
    157   CHK(s3d_primitive_get_attrib(&prim, S3D_POSITION, uv, &attr1) == RES_OK);
    158   CHK(f3_eq_eps(attr0.value, attr1.value, 1.e-6f) != 1);
    159   CHK(s3d_scene_view_ref_put(scnview) == RES_OK);
    160 
    161   CHK(s3d_shape_enable(cbox, 0) == RES_OK);
    162   CHK(s3d_scene_view_create(scn2, S3D_SAMPLE|S3D_TRACE, &scnview) == RES_OK);
    163   CHK(s3d_scene_view_sample(scnview, 0.5f, 0.5f, 0.5f, &prim, uv) == RES_OK);
    164   CHK(S3D_PRIMITIVE_EQ(&prim, &S3D_PRIMITIVE_NULL) == 1);
    165   CHK(s3d_scene_view_ref_put(scnview) == RES_OK);
    166   CHK(s3d_shape_enable(cbox, 1) == RES_OK);
    167   CHK(s3d_scene_view_create(scn2, S3D_SAMPLE|S3D_TRACE, &scnview) == RES_OK);
    168   FOR_EACH(i, 0, NSAMPS) {
    169     const float u = rand_canonic();
    170     const float v = rand_canonic();
    171     const float w = rand_canonic();
    172     CHK(s3d_scene_view_sample(scnview, u, v, w, &prim, uv) == RES_OK);
    173     CHK(s3d_primitive_get_attrib(&prim, S3D_POSITION, uv, &attr0) == RES_OK);
    174 
    175     CHK(prim.inst_id == cbox_id);
    176     CHK(prim.geom_id == walls_id
    177      || prim.geom_id == tall_block_id
    178      || prim.geom_id == short_block_id);
    179     CHK(prim.prim_id < 10);
    180     CHK(prim.scene_prim_id >= prim.prim_id);
    181     CHK(prim.scene_prim_id < 30);
    182     printf("%f %f %f\n", SPLIT3(attr0.value));
    183   }
    184   CHK(s3d_scene_view_ref_put(scnview) == RES_OK);
    185 
    186   CHK(s3d_device_ref_put(dev) == RES_OK);
    187   CHK(s3d_scene_ref_put(scn) == RES_OK);
    188   CHK(s3d_scene_ref_put(scn2) == RES_OK);
    189   CHK(s3d_shape_ref_put(cbox) == RES_OK);
    190   CHK(s3d_shape_ref_put(walls) == RES_OK);
    191   CHK(s3d_shape_ref_put(short_block) == RES_OK);
    192   CHK(s3d_shape_ref_put(tall_block) == RES_OK);
    193 
    194   check_memory_allocator(&allocator);
    195   mem_shutdown_proxy_allocator(&allocator);
    196   CHK(mem_allocated_size() == 0);
    197   return 0;
    198 }
    199