1 /* gph-cmath - C graphics math library
2 * Copyright (C) 2018 John Tsiombikas <nuclear@member.fsf.org>
4 * This program is free software. Feel free to use, modify, and/or redistribute
5 * it under the terms of the MIT/X11 license. See LICENSE for details.
6 * If you intend to redistribute parts of the code without the LICENSE file
7 * replace this paragraph with the full contents of the LICENSE file.
11 static inline float cgm_deg_to_rad(float deg)
13 return M_PI * deg / 180.0f;
16 static inline float cgm_rad_to_deg(float rad)
18 return 180.0f * rad / M_PI;
21 static inline float cgm_smoothstep(float a, float b, float x)
23 if(x < a) return 0.0f;
24 if(x >= b) return 1.0f;
26 x = (x - a) / (b - a);
27 return x * x * (3.0f - 2.0f * x);
30 static inline float cgm_lerp(float a, float b, float t)
32 return a + (b - a) * t;
35 static inline float cgm_bezier(float a, float b, float c, float d, float t)
37 float omt, omt3, t3, f;
40 omt3 = omt * omt * omt;
43 return (a * omt3) + (b * f * omt) + (c * f * t) + (d * t3);
46 static inline void cgm_discrand(cgm_vec3 *pt, float rad)
48 float theta = 2.0f * M_PI * (float)rand() / RAND_MAX;
49 float r = sqrt((float)rand() / RAND_MAX) * rad;
50 pt->x = cos(theta) * r;
51 pt->y = sin(theta) * r;
55 static inline void cgm_sphrand(cgm_vec3 *pt, float rad)
57 float u, v, theta, phi;
59 u = (float)rand() / RAND_MAX;
60 v = (float)rand() / RAND_MAX;
62 theta = 2.0f * M_PI * u;
63 phi = acos(2.0f * v - 1.0f);
65 pt->x = cos(theta) * sin(phi) * rad;
66 pt->y = sin(theta) * sin(phi) * rad;
67 pt->z = cos(phi) * rad;
70 static inline void cgm_unproject(cgm_vec3 *res, const cgm_vec3 *norm_scrpos,
71 const float *inv_viewproj)
75 pos.x = 2.0f * norm_scrpos->x - 1.0f;
76 pos.y = 2.0f * norm_scrpos->y - 1.0f;
77 pos.z = 2.0f * norm_scrpos->z - 1.0f;
80 cgm_wmul_m4v4(&pos, inv_viewproj);
82 res->x = pos.x / pos.w;
83 res->y = pos.y / pos.w;
84 res->z = pos.z / pos.w;
87 static inline void cgm_glu_unproject(float winx, float winy, float winz,
88 const float *view, const float *proj, const int *vp,
89 float *objx, float *objy, float *objz)
94 cgm_mcopy(inv_pv, proj);
95 cgm_mmul(inv_pv, view);
97 npos.x = (winx - vp[0]) / vp[2];
98 npos.y = (winy - vp[1]) / vp[4];
101 cgm_unproject(&res, &npos, inv_pv);
108 static inline void cgm_pick_ray(cgm_ray *ray, float nx, float ny,
109 const float *viewmat, const float *projmat)
111 cgm_vec3 npos, farpt;
114 cgm_mcopy(inv_pv, projmat);
115 cgm_mmul(inv_pv, viewmat);
117 cgm_vcons(&npos, nx, ny, 0.0f);
118 cgm_unproject(&ray->origin, &npos, inv_pv);
120 cgm_unproject(&farpt, &npos, inv_pv);
122 ray->dir.x = farpt.x - ray->origin.x;
123 ray->dir.y = farpt.y - ray->origin.y;
124 ray->dir.z = farpt.z - ray->origin.z;
127 static inline void cgm_raypos(cgm_vec3 *p, const cgm_ray *ray, float t)
129 p->x = ray->origin.x + ray->dir.x * t;
130 p->y = ray->origin.y + ray->dir.y * t;
131 p->z = ray->origin.z + ray->dir.z * t;
134 static inline void cgm_bary(cgm_vec3 *bary, const cgm_vec3 *a,
135 const cgm_vec3 *b, const cgm_vec3 *c, const cgm_vec3 *pt)
137 float d00, d01, d11, d20, d21, denom;
138 cgm_vec3 v0 = *b, v1 = *c, v2 = *pt;
144 d00 = cgm_vdot(&v0, &v0);
145 d01 = cgm_vdot(&v0, &v1);
146 d11 = cgm_vdot(&v1, &v1);
147 d20 = cgm_vdot(&v2, &v0);
148 d21 = cgm_vdot(&v2, &v1);
149 denom = d00 * d11 - d01 * d01;
151 bary->y = (d11 * d20 - d01 * d21) / denom;
152 bary->z = (d00 * d21 - d01 * d20) / denom;
153 bary->x = 1.0f - bary->y - bary->z;