diff options
| author | ladevieq <ladevie.quentin@gmail.com> | 2021-09-26 23:57:26 +0200 |
|---|---|---|
| committer | ladevieq <ladevie.quentin@gmail.com> | 2021-09-26 23:57:26 +0200 |
| commit | c5119193ffaedf4ac94fecfb434a3292a4746259 (patch) | |
| tree | 398031c5d1e920fec21e53feacd1d55ba8a79dad /shaders/include/math.h | |
| parent | bbba71cfe5a9545f1a87c70eb3cab3b6b08b1b25 (diff) | |
Refactor gltf loading and triangles attributes
Diffstat (limited to 'shaders/include/math.h')
| -rw-r--r-- | shaders/include/math.h | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/shaders/include/math.h b/shaders/include/math.h new file mode 100644 index 0000000..cc4e2f4 --- /dev/null +++ b/shaders/include/math.h @@ -0,0 +1,45 @@ +const float PI = 3.141592653589793f;
+const float TWO_PI = PI * 2.0f;
+
+vec3 less_than(vec3 vec, float value)
+{
+ return vec3(
+ (vec.x < value) ? 1.0f : 0.0f,
+ (vec.y < value) ? 1.0f : 0.0f,
+ (vec.z < value) ? 1.0f : 0.0f);
+}
+
+vec3 sample_hemisphere(vec2 randoms) {
+ float radius = sqrt(randoms[0]);
+ float theta = 2.0 * PI * randoms[1];
+
+ return vec3(cos(theta) * radius, sin(theta) * radius, sqrt(1 - randoms[0]));
+}
+
+vec2 disk_vec(vec2 randoms) {
+ float z = randoms[0] * 2.0 - 1.0;
+ float a = 2.0 * PI * randoms[1];
+ float r = sqrt(1.0 - z * z);
+
+ return vec2(cos(a) * r, sin(a) * r);
+}
+
+// Calculates rotation quaternion from axis vector to the vector (0, 0, 1)
+// Input vector must be normalized!
+vec4 rotation_to_z_axis(vec3 axis) {
+ // Handle special case when axis is exact or near opposite of (0, 0, 1)
+ if (axis.z < -0.99999f) return vec4(1.0f, 0.0f, 0.0f, 0.0f);
+
+ return normalize(vec4(axis.y, -axis.x, 0.0f, 1.0f + axis.z));
+}
+
+vec4 invert_rotation(vec4 q) {
+ return vec4(-q.xyz, q.w);
+}
+
+// Optimized point rotation using quaternion
+// Source: https://gamedev.stackexchange.com/questions/28395/rotating-vector3-by-a-quaternion
+vec3 rotate_point(vec4 q, vec3 v) {
+ const vec3 q_axis = q.xyz;
+ return 2.0f * dot(q_axis, v) * q_axis + (q.w * q.w - dot(q_axis, q_axis)) * v + 2.0f * q.w * cross(q_axis, v);
+}
|
