Skip to content

cf_ortho

Category: math
GitHub: cute_math3d.h


Builds a right-handed orthographic projection matrix.

CF_M4x4 cf_ortho(float left, float right, float bottom, float top, float znear, float zfar)
Parameters Description
left The left clip plane.
right The right clip plane.
bottom The bottom clip plane.
top The top clip plane.
znear Distance to the near clip plane. Positive, measured along -z.
zfar Distance to the far clip plane. Positive, measured along -z.

Return Value

Returns a matrix mapping the view volume to clip space.

Remarks

Right-handed: the camera looks down -z, so visible geometry has negative view-space z while znear and zfar are given as positive distances.

The z convention matters. SDL_GPU normalizes clip-space z to [0, 1] on every backend (the D3D/Metal/Vulkan convention), not OpenGL's [-1, 1]. This matrix maps znear to 0 and zfar to 1, which pairs with CF_COMPARE_FUNCTION_LESS_THAN and a depth clear of 1.0. The textbook glOrtho formula compiles and produces a plausible image, but sends half the depth range to negative NDC z where the GPU clips it away -- the classic "my model has holes and I can see inside it" bug.

Depth testing also requires a canvas created with depth_stencil_enable set to true; cf_canvas_defaults leaves it off, and without it all depth render state is ignored.

CF_M4x4
cf_perspective
cf_look_at
cf_ortho_2d
cf_mul