test_s3d_camera.h (1816B)
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 #ifndef TEST_S3D_CAMERA_H 17 #define TEST_S3D_CAMERA_H 18 19 #include <rsys/float3.h> 20 21 struct camera { 22 float pos[3]; 23 float x[3], y[3], z[3]; /* Basis */ 24 }; 25 26 static INLINE void 27 camera_init 28 (struct camera* cam, 29 const float pos[3], 30 const float tgt[3], 31 const float up[3], 32 const float fov_x, 33 const float proj_ratio) 34 { 35 float f = 0.f; 36 ASSERT(cam); 37 38 f3_set(cam->pos, pos); 39 f = f3_normalize(cam->z, f3_sub(cam->z, tgt, pos)); CHK(f != 0); 40 f = f3_normalize(cam->x, f3_cross(cam->x, cam->z, up)); CHK(f != 0); 41 f = f3_normalize(cam->y, f3_cross(cam->y, cam->z, cam->x)); CHK(f != 0); 42 f3_divf(cam->z, cam->z, (float)tan(fov_x*0.5f)); 43 f3_divf(cam->y, cam->y, proj_ratio); 44 } 45 46 static INLINE void 47 camera_ray 48 (const struct camera* cam, 49 const float pixel[2], 50 float org[3], 51 float dir[3]) 52 { 53 float x[3], y[3], f; 54 ASSERT(cam && pixel && org && dir); 55 56 f3_mulf(x, cam->x, pixel[0]*2.f - 1.f); 57 f3_mulf(y, cam->y, pixel[1]*2.f - 1.f); 58 f3_add(dir, f3_add(dir, x, y), cam->z); 59 f = f3_normalize(dir, dir); CHK(f != 0); 60 f3_set(org, cam->pos); 61 } 62 63 #endif /* TEST_S3D_CAMERA_H */ 64