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