mirror of https://github.com/bjornbytes/lovr.git
Move math helpers into lib;
They aren't really part of the math module since they are shared across several modules, more "util"-y.
This commit is contained in:
parent
2238f51338
commit
005d4b93f2
|
@ -1,8 +1,6 @@
|
||||||
#include "api.h"
|
#include "api.h"
|
||||||
#include "api/math.h"
|
#include "api/math.h"
|
||||||
#include "math/math.h"
|
#include "math/math.h"
|
||||||
#include "math/mat4.h"
|
|
||||||
#include "math/quat.h"
|
|
||||||
#include "math/curve.h"
|
#include "math/curve.h"
|
||||||
#include "math/randomGenerator.h"
|
#include "math/randomGenerator.h"
|
||||||
#include "math/transform.h"
|
#include "math/transform.h"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#include "math/mat4.h"
|
#include "lib/math.h"
|
||||||
#include "math/randomGenerator.h"
|
#include "math/randomGenerator.h"
|
||||||
|
|
||||||
int luax_readtransform(lua_State* L, int index, mat4 transform, int scaleComponents);
|
int luax_readtransform(lua_State* L, int index, mat4 transform, int scaleComponents);
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
#include "api.h"
|
#include "api.h"
|
||||||
#include "data/modelData.h"
|
#include "data/modelData.h"
|
||||||
#include "math/transform.h"
|
#include "math/transform.h"
|
||||||
#include "math/mat4.h"
|
|
||||||
|
|
||||||
int l_lovrModelDataGetVertexData(lua_State* L) {
|
int l_lovrModelDataGetVertexData(lua_State* L) {
|
||||||
ModelData* modelData = luax_checktype(L, 1, ModelData);
|
ModelData* modelData = luax_checktype(L, 1, ModelData);
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
#include "api.h"
|
#include "api.h"
|
||||||
#include "math/mat4.h"
|
|
||||||
#include "math/transform.h"
|
#include "math/transform.h"
|
||||||
|
|
||||||
int luax_readtransform(lua_State* L, int index, mat4 m, int scaleComponents) {
|
int luax_readtransform(lua_State* L, int index, mat4 m, int scaleComponents) {
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
#include "audio/audio.h"
|
#include "audio/audio.h"
|
||||||
#include "math/vec3.h"
|
#include "lib/math.h"
|
||||||
#include "math/quat.h"
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
#include "data/modelData.h"
|
#include "data/modelData.h"
|
||||||
#include "filesystem/filesystem.h"
|
#include "filesystem/filesystem.h"
|
||||||
#include "filesystem/file.h"
|
#include "filesystem/file.h"
|
||||||
#include "math/mat4.h"
|
#include "lib/math.h"
|
||||||
#include "math/quat.h"
|
|
||||||
#include "math/vec3.h"
|
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
|
@ -1,7 +1,4 @@
|
||||||
#include "graphics/animator.h"
|
#include "graphics/animator.h"
|
||||||
#include "math/mat4.h"
|
|
||||||
#include "math/vec3.h"
|
|
||||||
#include "math/quat.h"
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#include "data/modelData.h"
|
#include "data/modelData.h"
|
||||||
#include "math/mat4.h"
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
#include "lib/math.h"
|
||||||
#include "lib/map/map.h"
|
#include "lib/map/map.h"
|
||||||
#include "lib/vec/vec.h"
|
#include "lib/vec/vec.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
|
@ -3,10 +3,9 @@
|
||||||
#include "data/rasterizer.h"
|
#include "data/rasterizer.h"
|
||||||
#include "event/event.h"
|
#include "event/event.h"
|
||||||
#include "filesystem/filesystem.h"
|
#include "filesystem/filesystem.h"
|
||||||
#include "math/mat4.h"
|
|
||||||
#include "math/vec3.h"
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "lib/glfw.h"
|
#include "lib/glfw.h"
|
||||||
|
#include "lib/math.h"
|
||||||
#include "lib/stb/stb_image.h"
|
#include "lib/stb/stb_image.h"
|
||||||
#define _USE_MATH_DEFINES
|
#define _USE_MATH_DEFINES
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
|
@ -4,7 +4,6 @@
|
||||||
#include "data/blob.h"
|
#include "data/blob.h"
|
||||||
#include "data/textureData.h"
|
#include "data/textureData.h"
|
||||||
#include "data/vertexData.h"
|
#include "data/vertexData.h"
|
||||||
#include "math/mat4.h"
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
#include "graphics/material.h"
|
#include "graphics/material.h"
|
||||||
#include "graphics/mesh.h"
|
#include "graphics/mesh.h"
|
||||||
#include "graphics/texture.h"
|
#include "graphics/texture.h"
|
||||||
#include "math/mat4.h"
|
#include "lib/math.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
#include "graphics/texture.h"
|
#include "graphics/texture.h"
|
||||||
#include "resources/shaders.h"
|
#include "resources/shaders.h"
|
||||||
#include "data/modelData.h"
|
#include "data/modelData.h"
|
||||||
#include "math/mat4.h"
|
#include "lib/math.h"
|
||||||
#include "lib/vec/vec.h"
|
#include "lib/vec/vec.h"
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
#include "event/event.h"
|
#include "event/event.h"
|
||||||
#include "graphics/graphics.h"
|
#include "graphics/graphics.h"
|
||||||
#include "math/mat4.h"
|
#include "lib/math.h"
|
||||||
#include "math/vec3.h"
|
|
||||||
#include "math/quat.h"
|
|
||||||
#include "lib/glfw.h"
|
#include "lib/glfw.h"
|
||||||
#define _USE_MATH_DEFINES
|
#define _USE_MATH_DEFINES
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
#include "event/event.h"
|
#include "event/event.h"
|
||||||
#include "graphics/graphics.h"
|
#include "graphics/graphics.h"
|
||||||
#include "graphics/canvas.h"
|
#include "graphics/canvas.h"
|
||||||
#include "math/mat4.h"
|
|
||||||
#include "math/quat.h"
|
|
||||||
#include "platform.h"
|
#include "platform.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
#include "lib/math.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
|
@ -1,16 +1,191 @@
|
||||||
#include "math/mat4.h"
|
#include "math.h"
|
||||||
#include "math/quat.h"
|
|
||||||
#include "math/vec3.h"
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#ifdef LOVR_USE_SSE
|
#ifdef LOVR_USE_SSE
|
||||||
#include <xmmintrin.h>
|
#include <xmmintrin.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// m0 m4 m8 m12
|
// vec3
|
||||||
// m1 m5 m9 m13
|
|
||||||
// m2 m6 m10 m14
|
vec3 vec3_init(vec3 v, vec3 u) {
|
||||||
// m3 m7 m11 m15
|
return vec3_set(v, u[0], u[1], u[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
vec3 vec3_set(vec3 v, float x, float y, float z) {
|
||||||
|
v[0] = x;
|
||||||
|
v[1] = y;
|
||||||
|
v[2] = z;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
vec3 vec3_add(vec3 v, vec3 u) {
|
||||||
|
v[0] += u[0];
|
||||||
|
v[1] += u[1];
|
||||||
|
v[2] += u[2];
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
vec3 vec3_scale(vec3 v, float s) {
|
||||||
|
v[0] *= s;
|
||||||
|
v[1] *= s;
|
||||||
|
v[2] *= s;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
vec3 vec3_normalize(vec3 v) {
|
||||||
|
float len = vec3_length(v);
|
||||||
|
return len == 0 ? v : vec3_scale(v, 1 / len);
|
||||||
|
}
|
||||||
|
|
||||||
|
float vec3_length(vec3 v) {
|
||||||
|
return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
float vec3_dot(vec3 v, vec3 u) {
|
||||||
|
return v[0] * u[0] + v[1] * u[1] + v[2] * u[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
vec3 vec3_cross(vec3 v, vec3 u) {
|
||||||
|
return vec3_set(v,
|
||||||
|
v[1] * u[2] - v[2] * u[1],
|
||||||
|
v[2] * u[0] - v[0] * u[2],
|
||||||
|
v[0] * u[1] - v[1] * u[0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
vec3 vec3_lerp(vec3 v, vec3 u, float t) {
|
||||||
|
v[0] = v[0] + (u[0] - v[0]) * t;
|
||||||
|
v[1] = v[1] + (u[1] - v[1]) * t;
|
||||||
|
v[2] = v[2] + (u[2] - v[2]) * t;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
// quat
|
||||||
|
|
||||||
|
quat quat_init(quat q, quat r) {
|
||||||
|
return quat_set(q, r[0], r[1], r[2], r[3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
quat quat_set(quat q, float x, float y, float z, float w) {
|
||||||
|
q[0] = x;
|
||||||
|
q[1] = y;
|
||||||
|
q[2] = z;
|
||||||
|
q[3] = w;
|
||||||
|
return q;
|
||||||
|
}
|
||||||
|
|
||||||
|
quat quat_fromAngleAxis(quat q, float angle, vec3 axis) {
|
||||||
|
vec3_normalize(axis);
|
||||||
|
float s = sin(angle * .5f);
|
||||||
|
float c = cos(angle * .5f);
|
||||||
|
q[0] = s * axis[0];
|
||||||
|
q[1] = s * axis[1];
|
||||||
|
q[2] = s * axis[2];
|
||||||
|
q[3] = c;
|
||||||
|
return q;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef MAX
|
||||||
|
#define MAX(a, b) (a > b ? a : b)
|
||||||
|
#endif
|
||||||
|
quat quat_fromMat4(quat q, mat4 m) {
|
||||||
|
float x = sqrt(MAX(0, 1 + m[0] - m[5] - m[10])) / 2;
|
||||||
|
float y = sqrt(MAX(0, 1 - m[0] + m[5] - m[10])) / 2;
|
||||||
|
float z = sqrt(MAX(0, 1 - m[0] - m[5] + m[10])) / 2;
|
||||||
|
float w = sqrt(MAX(0, 1 + m[0] + m[5] + m[10])) / 2;
|
||||||
|
x = (m[9] - m[6]) > 0 ? -x : x;
|
||||||
|
y = (m[2] - m[8]) > 0 ? -y : y;
|
||||||
|
z = (m[4] - m[1]) > 0 ? -z : z;
|
||||||
|
q[0] = x;
|
||||||
|
q[1] = y;
|
||||||
|
q[2] = z;
|
||||||
|
q[3] = w;
|
||||||
|
return q;
|
||||||
|
}
|
||||||
|
|
||||||
|
quat quat_normalize(quat q) {
|
||||||
|
float len = quat_length(q);
|
||||||
|
if (len == 0) {
|
||||||
|
return q;
|
||||||
|
}
|
||||||
|
|
||||||
|
len = 1 / len;
|
||||||
|
q[0] *= len;
|
||||||
|
q[1] *= len;
|
||||||
|
q[2] *= len;
|
||||||
|
q[3] *= len;
|
||||||
|
return q;
|
||||||
|
}
|
||||||
|
|
||||||
|
float quat_length(quat q) {
|
||||||
|
return sqrt(q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
quat quat_slerp(quat q, quat r, float t) {
|
||||||
|
float dot = q[0] * r[0] + q[1] * r[1] + q[2] * r[2] + q[3] * r[3];
|
||||||
|
if (fabs(dot) >= 1.f) {
|
||||||
|
return q;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dot < 0) {
|
||||||
|
q[0] *= -1;
|
||||||
|
q[1] *= -1;
|
||||||
|
q[2] *= -1;
|
||||||
|
q[3] *= -1;
|
||||||
|
dot *= -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
float halfTheta = acos(dot);
|
||||||
|
float sinHalfTheta = sqrt(1.f - dot * dot);
|
||||||
|
|
||||||
|
if (fabs(sinHalfTheta) < .001) {
|
||||||
|
q[0] = q[0] * .5 + r[0] * .5;
|
||||||
|
q[1] = q[1] * .5 + r[1] * .5;
|
||||||
|
q[2] = q[2] * .5 + r[2] * .5;
|
||||||
|
q[3] = q[3] * .5 + r[3] * .5;
|
||||||
|
return q;
|
||||||
|
}
|
||||||
|
|
||||||
|
float a = sin((1 - t) * halfTheta) / sinHalfTheta;
|
||||||
|
float b = sin(t * halfTheta) / sinHalfTheta;
|
||||||
|
|
||||||
|
q[0] = q[0] * a + r[0] * b;
|
||||||
|
q[1] = q[1] * a + r[1] * b;
|
||||||
|
q[2] = q[2] * a + r[2] * b;
|
||||||
|
q[3] = q[3] * a + r[3] * b;
|
||||||
|
|
||||||
|
return q;
|
||||||
|
}
|
||||||
|
|
||||||
|
void quat_rotate(quat q, vec3 v) {
|
||||||
|
float s = q[3];
|
||||||
|
float u[3];
|
||||||
|
float c[3];
|
||||||
|
vec3_init(u, q);
|
||||||
|
vec3_cross(vec3_init(c, u), v);
|
||||||
|
float uu = vec3_dot(u, u);
|
||||||
|
float uv = vec3_dot(u, v);
|
||||||
|
vec3_scale(u, 2 * uv);
|
||||||
|
vec3_scale(v, s * s - uu);
|
||||||
|
vec3_scale(c, 2 * s);
|
||||||
|
vec3_add(v, vec3_add(u, c));
|
||||||
|
}
|
||||||
|
|
||||||
|
void quat_getAngleAxis(quat q, float* angle, float* x, float* y, float* z) {
|
||||||
|
if (q[3] > 1 || q[3] < -1) {
|
||||||
|
quat_normalize(q);
|
||||||
|
}
|
||||||
|
|
||||||
|
float qw = q[3];
|
||||||
|
float s = sqrt(1 - qw * qw);
|
||||||
|
s = s < .0001 ? 1 : 1 / s;
|
||||||
|
*angle = 2 * acos(qw);
|
||||||
|
*x = q[0] * s;
|
||||||
|
*y = q[1] * s;
|
||||||
|
*z = q[2] * s;
|
||||||
|
}
|
||||||
|
|
||||||
|
// mat4
|
||||||
|
|
||||||
mat4 mat4_set(mat4 m, mat4 n) {
|
mat4 mat4_set(mat4 m, mat4 n) {
|
||||||
return memcpy(m, n, 16 * sizeof(float));
|
return memcpy(m, n, 16 * sizeof(float));
|
|
@ -1,9 +1,33 @@
|
||||||
#include "math/math.h"
|
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define MAT4_IDENTITY { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
|
typedef float* vec3;
|
||||||
|
typedef float* quat;
|
||||||
|
typedef float* mat4;
|
||||||
|
|
||||||
|
// vec3
|
||||||
|
vec3 vec3_init(vec3 v, vec3 u);
|
||||||
|
vec3 vec3_set(vec3 v, float x, float y, float z);
|
||||||
|
vec3 vec3_add(vec3 v, vec3 u);
|
||||||
|
vec3 vec3_scale(vec3 v, float s);
|
||||||
|
vec3 vec3_normalize(vec3 v);
|
||||||
|
float vec3_length(vec3 v);
|
||||||
|
float vec3_dot(vec3 v, vec3 u);
|
||||||
|
vec3 vec3_cross(vec3 v, vec3 u);
|
||||||
|
vec3 vec3_lerp(vec3 v, vec3 u, float t);
|
||||||
|
|
||||||
|
// quat
|
||||||
|
quat quat_init(quat q, quat r);
|
||||||
|
quat quat_set(quat q, float x, float y, float z, float w);
|
||||||
|
quat quat_fromAngleAxis(quat q, float angle, vec3 axis);
|
||||||
|
quat quat_fromMat4(quat q, mat4 m);
|
||||||
|
quat quat_normalize(quat q);
|
||||||
|
float quat_length(quat q);
|
||||||
|
quat quat_slerp(quat q, quat r, float t);
|
||||||
|
void quat_rotate(quat q, vec3 v);
|
||||||
|
void quat_getAngleAxis(quat q, float* angle, float* x, float* y, float* z);
|
||||||
|
|
||||||
|
// mat4
|
||||||
|
#define MAT4_IDENTITY { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
|
||||||
#define mat4_init mat4_set
|
#define mat4_init mat4_set
|
||||||
mat4 mat4_set(mat4 m, mat4 n);
|
mat4 mat4_set(mat4 m, mat4 n);
|
||||||
mat4 mat4_fromMat34(mat4 m, float (*n)[4]);
|
mat4 mat4_fromMat34(mat4 m, float (*n)[4]);
|
|
@ -1,5 +1,5 @@
|
||||||
#include "math/vec3.h"
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
#include "lib/math.h"
|
||||||
#include "lib/vec/vec.h"
|
#include "lib/vec/vec.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
|
@ -1,14 +1,9 @@
|
||||||
#include "lib/vec/vec.h"
|
|
||||||
#include "math/randomGenerator.h"
|
#include "math/randomGenerator.h"
|
||||||
|
#include "lib/math.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
typedef float* vec3;
|
|
||||||
typedef float* quat;
|
|
||||||
typedef float* mat4;
|
|
||||||
typedef vec_t(mat4) vec_mat4_t;
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
bool initialized;
|
bool initialized;
|
||||||
RandomGenerator* generator;
|
RandomGenerator* generator;
|
||||||
|
|
124
src/math/quat.c
124
src/math/quat.c
|
@ -1,124 +0,0 @@
|
||||||
#include "math/quat.h"
|
|
||||||
#include "math/vec3.h"
|
|
||||||
#include "util.h"
|
|
||||||
#include <math.h>
|
|
||||||
|
|
||||||
quat quat_init(quat q, quat r) {
|
|
||||||
return quat_set(q, r[0], r[1], r[2], r[3]);
|
|
||||||
}
|
|
||||||
|
|
||||||
quat quat_set(quat q, float x, float y, float z, float w) {
|
|
||||||
q[0] = x;
|
|
||||||
q[1] = y;
|
|
||||||
q[2] = z;
|
|
||||||
q[3] = w;
|
|
||||||
return q;
|
|
||||||
}
|
|
||||||
|
|
||||||
quat quat_fromAngleAxis(quat q, float angle, vec3 axis) {
|
|
||||||
vec3_normalize(axis);
|
|
||||||
float s = sin(angle * .5f);
|
|
||||||
float c = cos(angle * .5f);
|
|
||||||
q[0] = s * axis[0];
|
|
||||||
q[1] = s * axis[1];
|
|
||||||
q[2] = s * axis[2];
|
|
||||||
q[3] = c;
|
|
||||||
return q;
|
|
||||||
}
|
|
||||||
|
|
||||||
quat quat_fromMat4(quat q, mat4 m) {
|
|
||||||
float x = sqrt(MAX(0, 1 + m[0] - m[5] - m[10])) / 2;
|
|
||||||
float y = sqrt(MAX(0, 1 - m[0] + m[5] - m[10])) / 2;
|
|
||||||
float z = sqrt(MAX(0, 1 - m[0] - m[5] + m[10])) / 2;
|
|
||||||
float w = sqrt(MAX(0, 1 + m[0] + m[5] + m[10])) / 2;
|
|
||||||
x = (m[9] - m[6]) > 0 ? -x : x;
|
|
||||||
y = (m[2] - m[8]) > 0 ? -y : y;
|
|
||||||
z = (m[4] - m[1]) > 0 ? -z : z;
|
|
||||||
q[0] = x;
|
|
||||||
q[1] = y;
|
|
||||||
q[2] = z;
|
|
||||||
q[3] = w;
|
|
||||||
return q;
|
|
||||||
}
|
|
||||||
|
|
||||||
quat quat_normalize(quat q) {
|
|
||||||
float len = quat_length(q);
|
|
||||||
if (len == 0) {
|
|
||||||
return q;
|
|
||||||
}
|
|
||||||
|
|
||||||
len = 1 / len;
|
|
||||||
q[0] *= len;
|
|
||||||
q[1] *= len;
|
|
||||||
q[2] *= len;
|
|
||||||
q[3] *= len;
|
|
||||||
return q;
|
|
||||||
}
|
|
||||||
|
|
||||||
float quat_length(quat q) {
|
|
||||||
return sqrt(q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]);
|
|
||||||
}
|
|
||||||
|
|
||||||
quat quat_slerp(quat q, quat r, float t) {
|
|
||||||
float dot = q[0] * r[0] + q[1] * r[1] + q[2] * r[2] + q[3] * r[3];
|
|
||||||
if (fabs(dot) >= 1.f) {
|
|
||||||
return q;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dot < 0) {
|
|
||||||
q[0] *= -1;
|
|
||||||
q[1] *= -1;
|
|
||||||
q[2] *= -1;
|
|
||||||
q[3] *= -1;
|
|
||||||
dot *= -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
float halfTheta = acos(dot);
|
|
||||||
float sinHalfTheta = sqrt(1.f - dot * dot);
|
|
||||||
|
|
||||||
if (fabs(sinHalfTheta) < .001) {
|
|
||||||
q[0] = q[0] * .5 + r[0] * .5;
|
|
||||||
q[1] = q[1] * .5 + r[1] * .5;
|
|
||||||
q[2] = q[2] * .5 + r[2] * .5;
|
|
||||||
q[3] = q[3] * .5 + r[3] * .5;
|
|
||||||
return q;
|
|
||||||
}
|
|
||||||
|
|
||||||
float a = sin((1 - t) * halfTheta) / sinHalfTheta;
|
|
||||||
float b = sin(t * halfTheta) / sinHalfTheta;
|
|
||||||
|
|
||||||
q[0] = q[0] * a + r[0] * b;
|
|
||||||
q[1] = q[1] * a + r[1] * b;
|
|
||||||
q[2] = q[2] * a + r[2] * b;
|
|
||||||
q[3] = q[3] * a + r[3] * b;
|
|
||||||
|
|
||||||
return q;
|
|
||||||
}
|
|
||||||
|
|
||||||
void quat_rotate(quat q, vec3 v) {
|
|
||||||
float s = q[3];
|
|
||||||
float u[3];
|
|
||||||
float c[3];
|
|
||||||
vec3_init(u, q);
|
|
||||||
vec3_cross(vec3_init(c, u), v);
|
|
||||||
float uu = vec3_dot(u, u);
|
|
||||||
float uv = vec3_dot(u, v);
|
|
||||||
vec3_scale(u, 2 * uv);
|
|
||||||
vec3_scale(v, s * s - uu);
|
|
||||||
vec3_scale(c, 2 * s);
|
|
||||||
vec3_add(v, vec3_add(u, c));
|
|
||||||
}
|
|
||||||
|
|
||||||
void quat_getAngleAxis(quat q, float* angle, float* x, float* y, float* z) {
|
|
||||||
if (q[3] > 1 || q[3] < -1) {
|
|
||||||
quat_normalize(q);
|
|
||||||
}
|
|
||||||
|
|
||||||
float qw = q[3];
|
|
||||||
float s = sqrt(1 - qw * qw);
|
|
||||||
s = s < .0001 ? 1 : 1 / s;
|
|
||||||
*angle = 2 * acos(qw);
|
|
||||||
*x = q[0] * s;
|
|
||||||
*y = q[1] * s;
|
|
||||||
*z = q[2] * s;
|
|
||||||
}
|
|
|
@ -1,13 +0,0 @@
|
||||||
#include "math/math.h"
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
quat quat_init(quat q, quat r);
|
|
||||||
quat quat_set(quat q, float x, float y, float z, float w);
|
|
||||||
quat quat_fromAngleAxis(quat q, float angle, vec3 axis);
|
|
||||||
quat quat_fromMat4(quat q, mat4 m);
|
|
||||||
quat quat_normalize(quat q);
|
|
||||||
float quat_length(quat q);
|
|
||||||
quat quat_slerp(quat q, quat r, float t);
|
|
||||||
void quat_rotate(quat q, vec3 v);
|
|
||||||
void quat_getAngleAxis(quat q, float* angle, float* x, float* y, float* z);
|
|
|
@ -1,5 +1,4 @@
|
||||||
#include "transform.h"
|
#include "transform.h"
|
||||||
#include "math/mat4.h"
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
Transform* lovrTransformCreate(mat4 transfrom) {
|
Transform* lovrTransformCreate(mat4 transfrom) {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "math/math.h"
|
#include "lib/math.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
|
@ -1,55 +0,0 @@
|
||||||
#include "math/vec3.h"
|
|
||||||
#include <math.h>
|
|
||||||
|
|
||||||
vec3 vec3_init(vec3 v, vec3 u) {
|
|
||||||
return vec3_set(v, u[0], u[1], u[2]);
|
|
||||||
}
|
|
||||||
|
|
||||||
vec3 vec3_set(vec3 v, float x, float y, float z) {
|
|
||||||
v[0] = x;
|
|
||||||
v[1] = y;
|
|
||||||
v[2] = z;
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
vec3 vec3_add(vec3 v, vec3 u) {
|
|
||||||
v[0] += u[0];
|
|
||||||
v[1] += u[1];
|
|
||||||
v[2] += u[2];
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
vec3 vec3_scale(vec3 v, float s) {
|
|
||||||
v[0] *= s;
|
|
||||||
v[1] *= s;
|
|
||||||
v[2] *= s;
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
vec3 vec3_normalize(vec3 v) {
|
|
||||||
float len = vec3_length(v);
|
|
||||||
return len == 0 ? v : vec3_scale(v, 1 / len);
|
|
||||||
}
|
|
||||||
|
|
||||||
float vec3_length(vec3 v) {
|
|
||||||
return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
|
|
||||||
}
|
|
||||||
|
|
||||||
float vec3_dot(vec3 v, vec3 u) {
|
|
||||||
return v[0] * u[0] + v[1] * u[1] + v[2] * u[2];
|
|
||||||
}
|
|
||||||
|
|
||||||
vec3 vec3_cross(vec3 v, vec3 u) {
|
|
||||||
return vec3_set(v,
|
|
||||||
v[1] * u[2] - v[2] * u[1],
|
|
||||||
v[2] * u[0] - v[0] * u[2],
|
|
||||||
v[0] * u[1] - v[1] * u[0]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
vec3 vec3_lerp(vec3 v, vec3 u, float t) {
|
|
||||||
v[0] = v[0] + (u[0] - v[0]) * t;
|
|
||||||
v[1] = v[1] + (u[1] - v[1]) * t;
|
|
||||||
v[2] = v[2] + (u[2] - v[2]) * t;
|
|
||||||
return v;
|
|
||||||
}
|
|
|
@ -1,13 +0,0 @@
|
||||||
#include "math/math.h"
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
vec3 vec3_init(vec3 v, vec3 u);
|
|
||||||
vec3 vec3_set(vec3 v, float x, float y, float z);
|
|
||||||
vec3 vec3_add(vec3 v, vec3 u);
|
|
||||||
vec3 vec3_scale(vec3 v, float s);
|
|
||||||
vec3 vec3_normalize(vec3 v);
|
|
||||||
float vec3_length(vec3 v);
|
|
||||||
float vec3_dot(vec3 v, vec3 u);
|
|
||||||
vec3 vec3_cross(vec3 v, vec3 u);
|
|
||||||
vec3 vec3_lerp(vec3 v, vec3 u, float t);
|
|
|
@ -1,5 +1,5 @@
|
||||||
#include "physics.h"
|
#include "physics.h"
|
||||||
#include "math/quat.h"
|
#include "lib/math.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue