test_s3d_sphere_box.c (4375B)
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_camera.h" 18 #include "test_s3d_cbox.h" 19 #include "test_s3d_utils.h" 20 21 #include <rsys/image.h> 22 #include <rsys/float3.h> 23 24 int 25 main(int argc, char** argv) 26 { 27 struct mem_allocator allocator; 28 struct s3d_device* dev; 29 struct s3d_shape* box; 30 struct s3d_shape* sphere; 31 struct s3d_scene* scn; 32 struct s3d_scene_view* view; 33 struct s3d_vertex_data vdata; 34 struct cbox_desc desc; 35 struct image img; 36 struct camera cam; 37 const size_t img_sz[2] = {640, 480}; 38 float pos[3] = {0, 0, 0}; 39 float tgt[3] = {0, 0, 0}; 40 float up[3] = {0, 1, 0}; 41 float tmp[3]; 42 float lower[3]; 43 float upper[3]; 44 float proj_ratio; 45 size_t x, y; 46 float center[3]; 47 (void)argc, (void)argv; 48 49 CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK); 50 CHK(s3d_device_create(NULL, &allocator, 0, &dev) == RES_OK); 51 CHK(s3d_scene_create(dev, &scn) == RES_OK); 52 53 CHK(s3d_shape_create_sphere(dev, &sphere) == RES_OK); 54 CHK(s3d_sphere_setup(sphere, f3(center, 150, 200, 90), 90) == RES_OK); 55 CHK(s3d_scene_attach_shape(scn, sphere) == RES_OK); 56 CHK(s3d_shape_ref_put(sphere) == RES_OK); 57 58 CHK(s3d_shape_create_sphere(dev, &sphere) == RES_OK); 59 CHK(s3d_sphere_setup(sphere, f3(center, 400, 200, 90), 90) == RES_OK); 60 CHK(s3d_scene_attach_shape(scn, sphere) == RES_OK); 61 CHK(s3d_shape_ref_put(sphere) == RES_OK); 62 63 desc.vertices = cbox_walls; 64 desc.indices = cbox_walls_ids; 65 vdata.usage = S3D_POSITION; 66 vdata.type = S3D_FLOAT3; 67 vdata.get = cbox_get_position; 68 CHK(s3d_shape_create_mesh(dev, &box) == RES_OK); 69 CHK(s3d_mesh_setup_indexed_vertices(box, cbox_walls_ntris, cbox_get_ids, 70 cbox_walls_nverts, &vdata, 1, &desc) == RES_OK); 71 CHK(s3d_scene_attach_shape(scn, box) == RES_OK); 72 CHK(s3d_shape_ref_put(box) == RES_OK); 73 74 CHK(s3d_scene_view_create(scn, S3D_TRACE, &view) == RES_OK); 75 CHK(s3d_scene_view_get_aabb(view, lower, upper) == RES_OK); 76 CHK(f3_eq(lower, f3(tmp, 0, 0, 0))); 77 CHK(f3_eq(upper, f3(tmp, 552, 559, 548))); 78 79 image_init(NULL, &img); 80 CHK(image_setup 81 (&img, img_sz[0], img_sz[1], img_sz[0]*3, IMAGE_RGB8, NULL) == RES_OK); 82 83 f3(pos, 278.f, -1000.f, 273.f); 84 f3(tgt, 278.f, 0.f, 273.f); 85 f3(up, 0, 0, 1); 86 proj_ratio = (float)img_sz[0] / (float)img_sz[1]; 87 camera_init(&cam, pos, tgt, up, (float)PI*0.25f, proj_ratio); 88 89 FOR_EACH(y, 0, img_sz[1]) { 90 float pixel[2]; 91 pixel[1] = (float)y / (float)img_sz[1]; 92 FOR_EACH(x, 0, img_sz[0]) { 93 const size_t ipix = (y*img_sz[0] + x)*3/*RGB*/; 94 struct s3d_hit hit; 95 const float range[2] = {0, FLT_MAX}; 96 float org[3]; 97 float dir[3]; 98 99 pixel[0] = (float)x/(float)img_sz[0]; 100 camera_ray(&cam, pixel, org, dir); 101 CHK(s3d_scene_view_trace_ray(view, org, dir, range, NULL, &hit) == RES_OK); 102 if(S3D_HIT_NONE(&hit)) { 103 ((uint8_t*)img.pixels)[ipix+0] = 0; 104 ((uint8_t*)img.pixels)[ipix+1] = 0; 105 ((uint8_t*)img.pixels)[ipix+2] = 0; 106 } else { 107 float normal[3] = {0.f, 0.f, 0.f}; 108 float dot; 109 f3_normalize(normal, hit.normal); 110 dot = absf(f3_dot(normal, dir)); 111 ((uint8_t*)img.pixels)[ipix+0] = (uint8_t)(dot*255.f); 112 ((uint8_t*)img.pixels)[ipix+1] = (uint8_t)(dot*255.f); 113 ((uint8_t*)img.pixels)[ipix+2] = (uint8_t)(dot*255.f); 114 } 115 } 116 } 117 118 /* Write image */ 119 CHK(image_write_ppm_stream(&img, 0, stdout) == RES_OK); 120 image_release(&img); 121 122 CHK(s3d_device_ref_put(dev) == RES_OK); 123 CHK(s3d_scene_ref_put(scn) == RES_OK); 124 CHK(s3d_scene_view_ref_put(view) == RES_OK); 125 126 check_memory_allocator(&allocator); 127 mem_shutdown_proxy_allocator(&allocator); 128 CHK(mem_allocated_size() == 0); 129 return 0; 130 }