test_ssol_scene.c (10299B)
1 /* Copyright (C) 2018-2026 |Meso|Star> (contact@meso-star.com) 2 * Copyright (C) 2016, 2018 CNRS 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 16 17 #include "ssol.h" 18 #include "test_ssol_utils.h" 19 #include "test_ssol_geometries.h" 20 21 #include <rsys/float3.h> 22 23 struct scene_ctx { 24 struct ssol_instance* instance; 25 struct ssol_instance* instance2; 26 int instance_found; 27 int instance2_found; 28 }; 29 30 static res_T 31 instance_func(struct ssol_instance* inst, void* context) 32 { 33 struct scene_ctx* ctx = context; 34 CHK(inst != NULL); 35 if(!ctx) return RES_BAD_ARG; 36 37 if(inst == ctx->instance) { 38 CHK(ctx->instance_found == 0); 39 ctx->instance_found = 1; 40 } else if(inst == ctx->instance2) { 41 CHK(ctx->instance2_found == 0); 42 ctx->instance2_found = 1; 43 } else { 44 FATAL("Unreachable code.\n"); 45 } 46 return RES_OK; 47 } 48 49 int 50 main(int argc, char** argv) 51 { 52 const float tall_block[] = { 53 423.f, 247.f, 0.f, 54 265.f, 296.f, 0.f, 55 314.f, 456.f, 0.f, 56 472.f, 406.f, 0.f, 57 423.f, 247.f, 330.f, 58 265.f, 296.f, 330.f, 59 314.f, 456.f, 330.f, 60 472.f, 406.f, 330.f 61 }; 62 const unsigned block_ids[] = { 63 4, 5, 6, 6, 7, 4, 64 1, 2, 6, 6, 5, 1, 65 0, 3, 7, 7, 4, 0, 66 2, 3, 7, 7, 6, 2, 67 0, 1, 5, 5, 4, 0 68 }; 69 struct mem_allocator allocator; 70 struct ssol_device* dev; 71 struct ssol_shape* shape; 72 struct ssol_material* material; 73 struct ssol_object* object; 74 struct ssol_instance* instance; 75 struct ssol_instance* instance2; 76 struct ssol_sun* sun; 77 struct ssol_sun* sun2; 78 struct ssol_scene* scene; 79 struct ssol_scene* scene2; 80 struct ssol_atmosphere* atm; 81 struct ssol_atmosphere* atm2; 82 struct ssol_data extinction; 83 struct ssol_vertex_data vdata; 84 struct scene_ctx ctx; 85 struct desc desc; 86 double transform[12]; 87 float lower[3], upper[3], tmp[3]; 88 (void) argc, (void) argv; 89 90 mem_init_proxy_allocator(&allocator, &mem_default_allocator); 91 92 CHK(ssol_device_create 93 (NULL, &allocator, SSOL_NTHREADS_DEFAULT, 0, &dev) == RES_OK); 94 95 CHK(ssol_material_create_virtual(dev, &material) == RES_OK); 96 97 CHK(ssol_shape_create_punched_surface(dev, &shape) == RES_OK); 98 CHK(ssol_object_create(dev, &object) == RES_OK); 99 CHK(ssol_object_add_shaded_shape(object, shape, material, material) == RES_OK); 100 CHK(ssol_object_instantiate(object, &instance) == RES_OK); 101 CHK(ssol_object_instantiate(object, &instance2) == RES_OK); 102 CHK(ssol_instance_set_transform(instance, transform) == RES_OK); 103 CHK(ssol_sun_create_directional(dev, &sun) == RES_OK); 104 CHK(ssol_sun_create_directional(dev, &sun2) == RES_OK); 105 106 CHK(ssol_scene_create(dev, &scene) == RES_OK); 107 CHK(ssol_scene_create(dev, &scene2) == RES_OK); 108 109 CHK(ssol_scene_ref_get(NULL) == RES_BAD_ARG); 110 CHK(ssol_scene_ref_get(scene) == RES_OK); 111 112 CHK(ssol_scene_ref_put(NULL) == RES_BAD_ARG); 113 CHK(ssol_scene_ref_put(scene) == RES_OK); 114 115 CHK(ssol_scene_clear(NULL) == RES_BAD_ARG); 116 CHK(ssol_scene_clear(scene) == RES_OK); 117 118 CHK(ssol_scene_attach_instance(NULL, instance) == RES_BAD_ARG); 119 CHK(ssol_scene_attach_instance(scene, NULL) == RES_BAD_ARG); 120 CHK(ssol_scene_attach_instance(scene, instance) == RES_OK); 121 CHK(ssol_scene_attach_instance(scene, instance) == RES_OK); 122 CHK(ssol_scene_attach_instance(scene, instance2) == RES_OK); 123 124 ctx.instance = instance; 125 ctx.instance2 = instance2; 126 ctx.instance_found = 0; 127 ctx.instance2_found = 0; 128 CHK(ssol_scene_for_each_instance(NULL, NULL, NULL) == RES_BAD_ARG); 129 CHK(ssol_scene_for_each_instance(scene, NULL, NULL) == RES_BAD_ARG); 130 CHK(ssol_scene_for_each_instance(NULL, instance_func, NULL) == RES_BAD_ARG); 131 CHK(ssol_scene_for_each_instance(scene, instance_func, NULL) == RES_BAD_ARG); 132 CHK(ssol_scene_for_each_instance(NULL, NULL, &ctx) == RES_BAD_ARG); 133 CHK(ssol_scene_for_each_instance(scene, NULL, &ctx) == RES_BAD_ARG); 134 CHK(ssol_scene_for_each_instance(NULL, instance_func, &ctx) == RES_BAD_ARG); 135 CHK(ssol_scene_for_each_instance(scene, instance_func, &ctx) == RES_OK); 136 CHK(ctx.instance_found == 1); 137 CHK(ctx.instance2_found == 1); 138 139 CHK(ssol_scene_detach_instance(NULL, instance) == RES_BAD_ARG); 140 CHK(ssol_scene_detach_instance(scene, NULL) == RES_BAD_ARG); 141 CHK(ssol_scene_detach_instance(scene, instance) == RES_OK); 142 CHK(ssol_scene_detach_instance(scene, instance) == RES_BAD_ARG); 143 CHK(ssol_scene_detach_instance(scene, instance2) == RES_OK); 144 145 CHK(ssol_scene_attach_instance(scene, instance) == RES_OK); 146 CHK(ssol_scene_attach_instance(scene2, instance) == RES_OK); 147 CHK(ssol_scene_detach_instance(scene2, instance) == RES_OK); 148 CHK(ssol_scene_detach_instance(scene, instance) == RES_OK); 149 CHK(ssol_scene_attach_instance(scene, instance) == RES_OK); 150 CHK(ssol_scene_detach_instance(scene2, instance) == RES_BAD_ARG); 151 CHK(ssol_scene_detach_instance(scene, instance) == RES_OK); 152 153 CHK(ssol_scene_attach_sun(NULL, sun) == RES_BAD_ARG); 154 CHK(ssol_scene_attach_sun(scene, NULL) == RES_BAD_ARG); 155 CHK(ssol_scene_attach_sun(scene, sun) == RES_OK); 156 CHK(ssol_scene_attach_sun(scene, sun) == RES_OK); 157 CHK(ssol_scene_attach_sun(scene, sun2) == RES_BAD_ARG); 158 159 CHK(ssol_scene_detach_sun(NULL, sun) == RES_BAD_ARG); 160 CHK(ssol_scene_detach_sun(scene, NULL) == RES_BAD_ARG); 161 CHK(ssol_scene_detach_sun(scene, sun) == RES_OK); 162 CHK(ssol_scene_detach_sun(scene, sun) == RES_BAD_ARG); 163 164 CHK(ssol_scene_attach_sun(scene, sun) == RES_OK); 165 CHK(ssol_scene_attach_sun(scene2, sun) == RES_BAD_ARG); 166 CHK(ssol_scene_detach_sun(scene2, sun) == RES_BAD_ARG); 167 CHK(ssol_scene_detach_sun(scene, sun) == RES_OK); 168 CHK(ssol_scene_attach_sun(scene2, sun) == RES_OK); 169 CHK(ssol_scene_detach_sun(scene, sun) == RES_BAD_ARG); 170 CHK(ssol_scene_detach_sun(scene2, sun) == RES_OK); 171 172 CHK(ssol_atmosphere_create(dev, &atm) == RES_OK); 173 extinction.type = SSOL_DATA_REAL; 174 extinction.value.real = 0.1; 175 CHK(ssol_atmosphere_set_extinction(atm, &extinction) == RES_OK); 176 CHK(ssol_atmosphere_create(dev, &atm2) == RES_OK); 177 CHK(ssol_atmosphere_set_extinction(atm2, &extinction) == RES_OK); 178 179 CHK(ssol_scene_attach_atmosphere(NULL, atm) == RES_BAD_ARG); 180 CHK(ssol_scene_attach_atmosphere(scene, NULL) == RES_BAD_ARG); 181 CHK(ssol_scene_attach_atmosphere(scene, atm) == RES_OK); 182 CHK(ssol_scene_attach_atmosphere(scene, atm) == RES_OK); 183 CHK(ssol_scene_attach_atmosphere(scene, atm2) == RES_BAD_ARG); 184 185 CHK(ssol_scene_detach_atmosphere(NULL, atm) == RES_BAD_ARG); 186 CHK(ssol_scene_detach_atmosphere(scene, NULL) == RES_BAD_ARG); 187 CHK(ssol_scene_detach_atmosphere(scene, atm) == RES_OK); 188 CHK(ssol_scene_detach_atmosphere(scene, atm) == RES_BAD_ARG); 189 190 CHK(ssol_scene_attach_atmosphere(scene, atm) == RES_OK); 191 CHK(ssol_scene_attach_atmosphere(scene2, atm) == RES_BAD_ARG); 192 CHK(ssol_scene_detach_atmosphere(scene2, atm) == RES_BAD_ARG); 193 CHK(ssol_scene_detach_atmosphere(scene, atm) == RES_OK); 194 CHK(ssol_scene_attach_atmosphere(scene2, atm) == RES_OK); 195 CHK(ssol_scene_detach_atmosphere(scene, atm) == RES_BAD_ARG); 196 CHK(ssol_scene_detach_atmosphere(scene2, atm) == RES_OK); 197 198 CHK(ssol_scene_detach_sun(scene, sun2) == RES_BAD_ARG); 199 CHK(ssol_sun_ref_put(sun2) == RES_OK); 200 201 CHK(ssol_scene_attach_instance(scene, instance) == RES_OK); 202 CHK(ssol_scene_attach_sun(scene, sun) == RES_OK); 203 CHK(ssol_scene_attach_atmosphere(scene, atm) == RES_OK); 204 CHK(ssol_scene_clear(scene) == RES_OK); 205 206 CHK(ssol_scene_ref_put(scene) == RES_OK); 207 CHK(ssol_scene_ref_put(scene2) == RES_OK); 208 CHK(ssol_shape_ref_put(shape) == RES_OK); 209 CHK(ssol_object_ref_put(object) == RES_OK); 210 CHK(ssol_instance_ref_put(instance) == RES_OK); 211 212 CHK(ssol_scene_create(dev, &scene) == RES_OK); 213 CHK(ssol_shape_create_mesh(dev, &shape) == RES_OK); 214 CHK(ssol_object_create(dev, &object) == RES_OK); 215 216 vdata.usage = SSOL_POSITION; 217 vdata.get = get_position; 218 desc.vertices = tall_block;; 219 desc.indices = block_ids; 220 221 CHK(ssol_mesh_setup(shape, 10, get_ids, 8, &vdata, 1, &desc) == RES_OK); 222 CHK(ssol_object_add_shaded_shape(object, shape, material, material) == RES_OK); 223 CHK(ssol_object_instantiate(object, &instance) == RES_OK); 224 225 CHK(ssol_scene_compute_aabb(NULL, NULL, NULL) == RES_BAD_ARG); 226 CHK(ssol_scene_compute_aabb(scene, NULL, NULL) == RES_BAD_ARG); 227 CHK(ssol_scene_compute_aabb(NULL, lower, NULL) == RES_BAD_ARG); 228 CHK(ssol_scene_compute_aabb(scene, lower, NULL) == RES_BAD_ARG); 229 CHK(ssol_scene_compute_aabb(NULL, NULL, upper) == RES_BAD_ARG); 230 CHK(ssol_scene_compute_aabb(scene, NULL, upper) == RES_BAD_ARG); 231 CHK(ssol_scene_compute_aabb(NULL, lower, upper) == RES_BAD_ARG); 232 CHK(ssol_scene_compute_aabb(scene, lower, upper) == RES_OK); 233 234 /* Empty scene */ 235 CHK(lower[0] > upper[0]); 236 CHK(lower[1] > upper[1]); 237 CHK(lower[2] > upper[2]); 238 239 CHK(ssol_scene_attach_instance(scene, instance) == RES_OK); 240 CHK(ssol_scene_compute_aabb(scene, lower, upper) == RES_OK); 241 CHK(f3_eq_eps(lower, f3(tmp, 265.f, 247.f, 0.f), 1.e-6f) == 1); 242 CHK(f3_eq_eps(upper, f3(tmp, 472.f, 456.f, 330.f), 1.e-6f) == 1); 243 244 CHK(ssol_scene_clear(scene) == RES_OK); 245 CHK(ssol_scene_compute_aabb(scene, lower, upper) == RES_OK); 246 247 /* Empty scene */ 248 CHK(lower[0] > upper[0]); 249 CHK(lower[1] > upper[1]); 250 CHK(lower[2] > upper[2]); 251 252 CHK(ssol_scene_ref_put(scene) == RES_OK); 253 254 CHK(ssol_instance_ref_put(instance) == RES_OK); 255 CHK(ssol_instance_ref_put(instance2) == RES_OK); 256 CHK(ssol_object_ref_put(object) == RES_OK); 257 CHK(ssol_shape_ref_put(shape) == RES_OK); 258 CHK(ssol_sun_ref_put(sun) == RES_OK); 259 CHK(ssol_atmosphere_ref_put(atm) == RES_OK); 260 CHK(ssol_atmosphere_ref_put(atm2) == RES_OK); 261 CHK(ssol_material_ref_put(material) == RES_OK); 262 263 CHK(ssol_device_ref_put(dev) == RES_OK); 264 265 check_memory_allocator(&allocator); 266 mem_shutdown_proxy_allocator(&allocator); 267 CHK(mem_allocated_size() == 0); 268 269 return 0; 270 }