Skip to content
modkitv0.2

math/project.h

#include <modkit/math/project.h>8 functions

Coordinate space transformations and MVP pipeline.

Coordinate spaces (right-hand system): Local/Object -> Model matrix -> World World -> View matrix -> View/Eye View/Eye -> Projection -> Clip (NDC after perspective divide) Clip -> Viewport -> Screen The typical rendering pipeline uses: MVP = Projection * View * Model The GPU backend computes modelViewProj automatically from:mk_view_transform(view, projection)mk_encoder_set_transform(model)

Functions

mk_compute_model_viewfunction

void mk_compute_model_view(const mk_mat4 *model, const mk_mat4 *view, mk_mat4 *dest)

Compute model-view matrix: result = view * model.

ParameterTypeDescription
modelconst mk_mat4 *Value for model.
viewconst mk_mat4 *Value for view.
destmk_mat4 *Value for dest.

mk_compute_mvpfunction

void mk_compute_mvp(const mk_mat4 *model, const mk_mat4 *view, const mk_mat4 *projection, mk_mat4 *dest)

Compute MVP matrix: result = projection * view * model.

ParameterTypeDescription
modelconst mk_mat4 *Value for model.
viewconst mk_mat4 *Value for view.
projectionconst mk_mat4 *Value for projection.
destmk_mat4 *Value for dest.

mk_compute_view_projectionfunction

void mk_compute_view_projection(const mk_mat4 *view, const mk_mat4 *projection, mk_mat4 *dest)

Compute view-projection matrix: result = projection * view.

ParameterTypeDescription
viewconst mk_mat4 *Value for view.
projectionconst mk_mat4 *Value for projection.
destmk_mat4 *Value for dest.

mk_project_world_to_screenfunction

void mk_project_world_to_screen(const mk_vec3 *world_pos, const mk_mat4 *mvp, const mk_vec4 *viewport, mk_vec3 *screen_pos)

Project world coordinate to screen coordinate.

ParameterTypeDescription
world_posconst mk_vec3 *Position in world space
mvpconst mk_mat4 *Model-View-Projection matrix
viewportconst mk_vec4 *Viewport as [x, y, width, height]
screen_posmk_vec3 *Output screen position (z = normalized depth)

mk_screen_to_rayfunction

void mk_screen_to_ray(float screen_x, float screen_y, const mk_camera *camera, const mk_vec4 *viewport, mk_vec3 *ray_origin, mk_vec3 *ray_dir)

Create ray from screen position (for picking/raycasting).

Screen coordinates are top-left origin (y increases downward), matching mk_mouse_position(); the Y flip to clip space is handled internally.

ParameterTypeDescription
screen_xfloatScreen X coordinate
screen_yfloatScreen Y coordinate
cameraconst mk_camera *Camera to create ray from
viewportconst mk_vec4 *Viewport as [x, y, width, height]
ray_originmk_vec3 *Output ray origin (camera position)
ray_dirmk_vec3 *Output ray direction (normalized)

mk_unproject_screen_to_worldfunction

void mk_unproject_screen_to_world(const mk_vec3 *screen_pos, const mk_mat4 *inv_mvp, const mk_vec4 *viewport, mk_vec3 *world_pos)

Unproject screen coordinate to world coordinate.

ParameterTypeDescription
screen_posconst mk_vec3 *Screen position (z = normalized depth 0-1)
inv_mvpconst mk_mat4 *Inverse of MVP matrix
viewportconst mk_vec4 *Viewport as [x, y, width, height]
world_posmk_vec3 *Output world position

mk_viewportfunction

mk_vec4 mk_viewport(float x, float y, float width, float height)

Create viewport vec4 from dimensions.

ParameterTypeDescription
xfloatValue for x.
yfloatValue for y.
widthfloatValue for width.
heightfloatValue for height.

Returns The resulting value.

mk_viewport_fullfunction

mk_vec4 mk_viewport_full(float width, float height)

Create viewport from 0,0 with given dimensions.

ParameterTypeDescription
widthfloatValue for width.
heightfloatValue for height.

Returns The resulting value.