test_s3d_trace_ray_sphere.c (4361B)
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_utils.h" 21 22 #include <rsys/image.h> 23 #include <rsys/math.h> 24 25 int 26 main(int argc, char** argv) 27 { 28 struct mem_allocator allocator; 29 struct image img; 30 struct camera cam; 31 struct s3d_hit hit; 32 struct s3d_device* dev; 33 struct s3d_scene* scn; 34 struct s3d_shape* shape; 35 struct s3d_scene_view* view; 36 const size_t img_sz[2] = {640, 480}; 37 const float radius = 2; 38 const float center[3] = {1.0, 1.0, 0}; 39 float pos[3] = {0, 0, 0}; 40 float tgt[3] = {0, 0, 0}; 41 float up[3] = {0, 1, 0}; 42 const float range[2] = {0, FLT_MAX}; 43 float org[3]; 44 float dir[3]; 45 46 float proj_ratio; 47 size_t x, y; 48 int hit_something = 0; 49 (void)argc, (void)argv; 50 51 CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK); 52 image_init(&allocator, &img); 53 CHK(image_setup 54 (&img, img_sz[0], img_sz[1], img_sz[0]*3, IMAGE_RGB8, NULL) == RES_OK); 55 56 CHK(s3d_device_create(NULL, &allocator, 0, &dev) == RES_OK); 57 CHK(s3d_scene_create(dev, &scn) == RES_OK); 58 CHK(s3d_shape_create_sphere(dev, &shape) == RES_OK); 59 CHK(s3d_sphere_setup(shape, center, radius) == RES_OK); 60 CHK(s3d_scene_attach_shape(scn, shape) == RES_OK); 61 CHK(s3d_scene_view_create(scn, S3D_TRACE, &view) == RES_OK); 62 63 f3(org, 1.0, 1.0, -4); 64 f3(dir, 0.0, 0.0, 1); 65 CHK(s3d_scene_view_trace_ray(view, org, dir, range, NULL, &hit) == RES_OK); 66 CHK(!S3D_HIT_NONE(&hit)); 67 CHK(eq_epsf(hit.distance, 2, 1.e-6f)); 68 69 f3(pos, 0, 0, -10); 70 f3(tgt, 0, 0, 0); 71 f3(up, 0, 1, 0); 72 proj_ratio = (float)img_sz[0] / (float)img_sz[1]; 73 camera_init(&cam, pos, tgt, up, (float)PI*0.25f, proj_ratio); 74 75 FOR_EACH(y, 0, img_sz[1]) { 76 float pixel[2]; 77 pixel[1] = (float)y / (float)img_sz[1]; 78 FOR_EACH(x, 0, img_sz[0]) { 79 const size_t ipix = (y*img_sz[0] + x)*3/*RGB*/; 80 81 pixel[0] = (float)x/(float)img_sz[0]; 82 camera_ray(&cam, pixel, org, dir); 83 CHK(s3d_scene_view_trace_ray(view, org, dir, range, NULL, &hit) == RES_OK); 84 if(S3D_HIT_NONE(&hit)) { 85 ((uint8_t*)img.pixels)[ipix+0] = 0; 86 ((uint8_t*)img.pixels)[ipix+1] = 0; 87 ((uint8_t*)img.pixels)[ipix+2] = 0; 88 } else { 89 struct s3d_attrib attr; 90 float normal[3] = {0.f, 0.f, 0.f}; 91 float tmp[3]; 92 float len; 93 float dot; 94 95 f3_normalize(normal, hit.normal); 96 CHK(s3d_primitive_get_attrib 97 (&hit.prim, S3D_GEOMETRY_NORMAL, hit.uv, &attr) == RES_OK); 98 f3_normalize(attr.value, attr.value); 99 CHK(f3_eq_eps(normal, attr.value, 1.e-3f)); 100 101 f3_add(pos, org, f3_mulf(pos, dir, hit.distance)); 102 CHK(s3d_primitive_get_attrib 103 (&hit.prim, S3D_POSITION, hit.uv, &attr) == RES_OK); 104 CHK(f3_eq_eps(pos, attr.value, 1.e-3f)); 105 106 len = f3_len(f3_sub(pos, pos, center)); 107 CHK(eq_epsf(len, radius, 1.e-3f)); 108 CHK(f3_eq_eps(f3_mulf(tmp, normal, radius), pos, 1.e-3f)); 109 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 hit_something = 1; 115 } 116 } 117 } 118 CHK(hit_something == 1); 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_shape_ref_put(shape) == RES_OK); 127 CHK(s3d_scene_view_ref_put(view) == RES_OK); 128 129 check_memory_allocator(&allocator); 130 mem_shutdown_proxy_allocator(&allocator); 131 CHK(mem_allocated_size() == 0); 132 return 0; 133 } 134