star-3d

Surface structuring for efficient 3D geometric queries
git clone git://git.meso-star.com/star-3d.git
Log | Files | Refs | README | LICENSE

test_s3d_camera.h (1843B)


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