92 cam.
fov_y = fov_y_rad;
161 for (uint32_t i = 0; i < count; ++i) {
166 float discriminant = b * b - 4 * a * c;
168 results[i].
hit =
false;
169 if (discriminant >= 0) {
170 float sqrt_d = sqrtf(discriminant);
171 float t0 = (-b - sqrt_d) / (2.0f * a);
172 float t1 = (-b + sqrt_d) / (2.0f * a);
179 results[i].
hit =
true;
205 for (uint32_t i = 0; i < count; ++i) {
208 float tmin = fminf(t1, t2);
209 float tmax = fmaxf(t1, t2);
213 tmin = fmaxf(tmin, fminf(t1, t2));
214 tmax = fminf(tmax, fmaxf(t1, t2));
218 tmin = fmaxf(tmin, fminf(t1, t2));
219 tmax = fminf(tmax, fmaxf(t1, t2));
221 results[i].
hit = (tmax >= tmin && tmax > 0);
222 if (results[i].hit) {
223 results[i].
distance = tmin > 0 ? tmin : tmax;
231 float epsilon = 0.0001f;
234 if (fabsf(p.
x) >= fabsf(d.
x) - epsilon)
235 results[i].
normal.
x = p.
x > 0 ? 1.0f : -1.0f;
236 else if (fabsf(p.
y) >= fabsf(d.
y) - epsilon)
237 results[i].
normal.
y = p.
y > 0 ? 1.0f : -1.0f;
238 else if (fabsf(p.
z) >= fabsf(d.
z) - epsilon)
239 results[i].
normal.
z = p.
z > 0 ? 1.0f : -1.0f;
static sbgl_Camera sbgl_CameraOrthographic(sbgl_OrthoParams p)
Initializes a camera with orthographic projection.
static void sbgl_RayAABBIntersectBatch(sbgl_Ray ray, const sbgl_AABB *boxes, sbgl_HitResult *results, uint32_t count)
Performs a batch ray-AABB intersection test.
static void sbgl_RaySphereIntersectBatch(sbgl_Ray ray, const sbgl_Sphere *spheres, sbgl_HitResult *results, uint32_t count)
Performs a batch ray-sphere intersection test.
static sbgl_Camera sbgl_CameraPerspective(float fov_y_rad, float aspect, float near_p, float far_p)
Initializes a camera with perspective projection.
static sbgl_Mat4 sbgl_CameraGetProjection(const sbgl_Camera *cam)
Computes the projection matrix for the given camera.
sbgl_CameraType
Camera projection types.
@ SBGL_CAMERA_ORTHOGRAPHIC
@ SBGL_CAMERA_PERSPECTIVE
static sbgl_Mat4 sbgl_CameraGetView(const sbgl_Camera *cam)
Computes the view matrix for the given camera.
Single-header math library for SBgl.
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_Vec3 sbgl_Vec3Set(float x, float y, float z)
Creates a Vec3, correctly padded.
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_Mat4LookAt(sbgl_Vec3 eye, sbgl_Vec3 center, sbgl_Vec3 up)
Creates a look-at view matrix.
static sbgl_Vec3 sbgl_Vec3Normalize(sbgl_Vec3 v)
Normalizes a Vec3.
static sbgl_Vec3 sbgl_Vec3Sub(sbgl_Vec3 a, sbgl_Vec3 b)
Subtracts b from a.
Axis-Aligned Bounding Box (AABB).
Stateful camera representation.
Intersection result for batch testing.
4x4 Matrix, 16-byte aligned, column-major.
Parameters for orthographic projection.
3D Vector, 16-byte aligned and padded for SIMD safety.