17#define SBGL_ALIGN(n) __declspec(align(n))
19#define SBGL_ALIGN(n) __attribute__((aligned(n)))
22#define SBGL_PI 3.14159265358979323846f
40 __extension__
struct {
51 __extension__
struct {
63 __extension__
struct {
74 __extension__
struct {
120 float xhalf = 0.5f * x;
122 pun.i = 0x5f3759df - (pun.i >> 1);
123 pun.f = pun.f * (1.5f - xhalf * pun.f * pun.f);
125 pun.f = pun.f * (1.5f - xhalf * pun.f * pun.f);
143 return (
sbgl_Vec3){ { a.
x * s, a.
y * s, a.
z * s, 0.0f } };
148 return a.
x * b.
x + a.
y * b.
y + a.
z * b.
z;
154 { a.
y * b.
z - a.
z * b.
y, a.
z * b.
x - a.
x * b.
z, a.
x * b.
y - a.
y * b.
x, 0.0f }
164 if (len_sq < 0.000001f)
176 return (
sbgl_Vec4){ { a.
x * s, a.
y * s, a.
z * s, a.
w * s } };
187 a.
w * b.
y + a.
y * b.
w + a.
z * b.
x - a.
x * b.
z,
188 a.
w * b.
z + a.
z * b.
w + a.
x * b.
y - a.
y * b.
x,
189 a.
w * b.
w - a.
x * b.
x - a.
y * b.
y - a.
z * b.
z } };
194 float s = sinf(angle_rad / 2.0f);
196 return (
sbgl_Quat){ { n.
x * s, n.
y * s, n.
z * s, cosf(angle_rad / 2.0f) } };
212 float xx = q.
x * q.
x;
213 float yy = q.
y * q.
y;
214 float zz = q.
z * q.
z;
215 float xy = q.
x * q.
y;
216 float xz = q.
x * q.
z;
217 float yz = q.
y * q.
z;
218 float wx = q.
w * q.
x;
219 float wy = q.
w * q.
y;
220 float wz = q.
w * q.
z;
222 res.
m[0][0] = 1.0f - 2.0f * (yy + zz);
223 res.
m[0][1] = 2.0f * (xy + wz);
224 res.
m[0][2] = 2.0f * (xz - wy);
226 res.
m[1][0] = 2.0f * (xy - wz);
227 res.
m[1][1] = 1.0f - 2.0f * (xx + zz);
228 res.
m[1][2] = 2.0f * (yz + wx);
230 res.
m[2][0] = 2.0f * (xz + wy);
231 res.
m[2][1] = 2.0f * (yz - wx);
232 res.
m[2][2] = 1.0f - 2.0f * (xx + yy);
242 for (
int c = 0; c < 4; ++c) {
243 for (
int r = 0; r < 4; ++r) {
244 res.
m[c][r] = a.
m[0][r] * b.
m[c][0] + a.
m[1][r] * b.
m[c][1] + a.
m[2][r] * b.
m[c][2] +
245 a.
m[3][r] * b.
m[c][3];
253 return (
sbgl_Vec4){ { m.
m[0][0] * v.
x + m.
m[1][0] * v.
y + m.
m[2][0] * v.
z + m.
m[3][0] * v.
w,
254 m.
m[0][1] * v.
x + m.
m[1][1] * v.
y + m.
m[2][1] * v.
z + m.
m[3][1] * v.
w,
255 m.
m[0][2] * v.
x + m.
m[1][2] * v.
y + m.
m[2][2] * v.
z + m.
m[3][2] * v.
w,
256 m.
m[0][3] * v.
x + m.
m[1][3] * v.
y + m.
m[2][3] * v.
z + m.
m[3][3] * v.
w } };
279 float f = 1.0f / tanf(fov_y_rad / 2.0f);
281 res.
m[0][0] = f / aspect;
283 res.
m[2][2] = far / (near - far);
285 res.
m[3][2] = (far * near) / (near - far);
static sbgl_Mat4 sbgl_Mat4Orthographic(sbgl_OrthoParams p)
Creates an orthographic projection matrix.
static float sbgl_Vec3Dot(sbgl_Vec3 a, sbgl_Vec3 b)
Computes the dot product of two Vec3 vectors.
static sbgl_Mat4 sbgl_QuatToMat4(sbgl_Quat q)
Converts a quaternion to a rotation matrix.
static sbgl_Mat4 sbgl_Mat4Scale(sbgl_Vec3 v)
Creates a scaling matrix.
static sbgl_Vec4 sbgl_Mat4MulVec4(sbgl_Mat4 m, sbgl_Vec4 v)
Multiplies a matrix by a Vec4.
static sbgl_Quat sbgl_QuatSet(float x, float y, float z, float w)
Creates a Quat.
static sbgl_Vec4 sbgl_Vec4Add(sbgl_Vec4 a, sbgl_Vec4 b)
Adds two Vec4 vectors.
static sbgl_Mat4 sbgl_Mat4Identity(void)
Returns an identity matrix.
static sbgl_Quat sbgl_QuatMul(sbgl_Quat a, sbgl_Quat b)
Multiplies two quaternions.
static sbgl_Quat sbgl_QuatFromAxisAngle(sbgl_Vec3 axis, float angle_rad)
Creates a quaternion from an axis and an angle (in radians).
static sbgl_Vec3 sbgl_Vec3Set(float x, float y, float z)
Creates a Vec3, correctly padded.
static sbgl_Quat sbgl_QuatIdentity(void)
Returns an identity quaternion.
static sbgl_Vec3 sbgl_Vec3Add(sbgl_Vec3 a, sbgl_Vec3 b)
Adds two Vec3 vectors.
static sbgl_Mat4 sbgl_Mat4Perspective(float fov_y_rad, float aspect, float near, float far)
Creates a perspective projection matrix.
static sbgl_Vec3 sbgl_Vec3Mul(sbgl_Vec3 a, float s)
Multiplies a Vec3 by a scalar.
static sbgl_Mat4 sbgl_Mat4Translate(sbgl_Vec3 v)
Creates a translation matrix.
static sbgl_Vec4 sbgl_Vec4Mul(sbgl_Vec4 a, float s)
Multiplies a Vec4 by a scalar.
static float sbgl_InvSqrt(float x)
Approximate Inverse Square Root algorithm.
static sbgl_Mat4 sbgl_Mat4Rotate(float angle_rad, sbgl_Vec3 axis)
Creates a rotation matrix from axis and angle.
static float sbgl_Vec3Length(sbgl_Vec3 v)
Returns the length of a Vec3.
static sbgl_Mat4 sbgl_Mat4Mul(sbgl_Mat4 a, sbgl_Mat4 b)
Multiplies two matrices.
static sbgl_Vec2 sbgl_Vec2Set(float x, float y)
Creates a Vec2.
static sbgl_Mat4 sbgl_Mat4LookAt(sbgl_Vec3 eye, sbgl_Vec3 center, sbgl_Vec3 up)
Creates a look-at view matrix.
static sbgl_Vec3 sbgl_Vec3Cross(sbgl_Vec3 a, sbgl_Vec3 b)
Computes the cross product of two Vec3 vectors.
static sbgl_Vec3 sbgl_Vec3Normalize(sbgl_Vec3 v)
Normalizes a Vec3.
static sbgl_Vec4 sbgl_Vec4Set(float x, float y, float z, float w)
Creates a Vec4.
static sbgl_Vec3 sbgl_Vec3Sub(sbgl_Vec3 a, sbgl_Vec3 b)
Subtracts b from a.
4x4 Matrix, 16-byte aligned, column-major.
Parameters for orthographic projection.
Quaternion, 16-byte aligned.
3D Vector, 16-byte aligned and padded for SIMD safety.
4D Vector, 16-byte aligned.