Projection

package
v0.0.0-...-ae8aae0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 30, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package Projection provides a 4×4 matrix for 3D projective transformations.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Identity projection with no transformation defined. When applied to other data
	// structures, no transformation is performed.
	Identity = XYZW{
		X: Vector4.XYZW{X: 1, Y: 0, Z: 0, W: 0},
		Y: Vector4.XYZW{X: 0, Y: 1, Z: 0, W: 0},
		Z: Vector4.XYZW{X: 0, Y: 0, Z: 1, W: 0},
		W: Vector4.XYZW{X: 0, Y: 0, Z: 0, W: 1},
	}

	// Zero projection with all values initialized to 0. When applied to other data
	// structures, they will be zeroed.
	Zero = XYZW{}
)

Functions

func AspectRatio

func AspectRatio(p XYZW) Float.X

AspectRatio returns the X:Y aspect ratio of this Projection's viewport.

func Determinant

func Determinant(p XYZW) Float.X

Determinant returns a scalar value that is the signed factor by which areas are scaled by this matrix. If the sign is negative, the matrix flips the orientation of the area.

The determinant can be used to calculate the invertibility of a matrix or solve linear systems of equations involving the matrix, among other applications.

func FarPlaneHalfExtents

func FarPlaneHalfExtents(p XYZW) Vector2.XY

FarPlaneHalfExtents returns the dimensions of the far clipping plane of the projection, divided by two.

func FarZ

func FarZ(p XYZW) Float.X

FarZ returns the distance for this Projection beyond which positions are clipped.

func FieldOfView

func FieldOfView(p XYZW) Angle.Degrees

FieldOfView returns the horizontal field of view of the projection (in degrees).

func Fovy

func Fovy[X Float.Any](hfov Angle.Degrees, aspect X) Angle.Degrees

Fovy returns the vertical field of view of the projection (in degrees) associated with the given horizontal field of view (in degrees) and aspect ratio.

func IsOrthogonal

func IsOrthogonal(p XYZW) bool

IsOrthogonal returns true if this Projection performs an orthogonal projection.

func LevelOfDetailMultiplier

func LevelOfDetailMultiplier(p XYZW) Float.X

LevelOfDetailMultiplier returns the factor by which the visible level of detail is scaled by this Projection.

func NearZ

func NearZ(p XYZW) Float.X

NearZ returns the distance for this Projection before which positions are clipped.

func PixelsPerMeter

func PixelsPerMeter(p XYZW, pixel_width int) int

PixelsPerMeter returns the number of pixels with the given pixel width displayed per meter, after this Projection is applied.

func Transform

func Transform(v Vector4.XYZW, p XYZW) Vector4.XYZW

Transform transforms (multiplies) the [Vector4.XYZW] by the given projection's transformation matrix.

func ViewportHalfExtents

func ViewportHalfExtents(p XYZW) Vector2.XY

ViewportHalfExtents returns the dimensions of the viewport plane that this Projection projects positions onto, divided by two.

Types

type ClippingPlane

type ClippingPlane int
const (
	PlaneNear ClippingPlane = iota
	PlaneFar
	PlaneLeft
	PlaneTop
	PlaneRight
	PlaneBottom
)

func (ClippingPlane) For

func (index ClippingPlane) For(p XYZW) (new_plane Plane.NormalD)

For returns the clipping plane of this Projection whose index is given by plane.

plane should be equal to one of PLANE_NEAR, PLANE_FAR, PLANE_LEFT, PLANE_TOP, PLANE_RIGHT, or PLANE_BOTTOM.

type XYZW

type XYZW = struct {
	X Vector4.XYZW // The projection matrix's X vector (column 0). Equivalent to array index 0.
	Y Vector4.XYZW // The projection matrix's Y vector (column 1). Equivalent to array index 1.
	Z Vector4.XYZW // The projection matrix's Z vector (column 2). Equivalent to array index 2.
	W Vector4.XYZW // The projection matrix's W vector (column 3). Equivalent to array index 3.
}

XYZW is a 4×4 matrix used for 3D projective transformations. It can represent transformations such as translation, rotation, scaling, shearing, and perspective division. It consists of four Vector4 columns.

For purely linear transformations (translation, rotation, and scale), it is recommended to use Transform3D, as it is more performant and requires less memory.

func DepthCorrection

func DepthCorrection(fliy bool) XYZW

DepthCorrection creates a new Projection that projects positions from a depth range of -1 to 1 to one that ranges from 0 to 1, and flips the projected positions vertically, according to fliy.

func FitAABB

func FitAABB(aabb AABB.PositionSize) XYZW

FitAABB creates a new Projection that scales a given projection to fit around a given AABB in projection space.

func FlippedY

func FlippedY(p XYZW) XYZW

FlippedY returns a copy of this Projection with the signs of the values of the Y column flipped.

func Frustum

func Frustum[X Float.Any](left, right, bottom, top, z_near, z_far X) XYZW

Frustum creates a new Projection that projects positions in a frustum with the given clipping planes.

func FrustumAspectRatio

func FrustumAspectRatio[X Float.Any](size, aspect X, offset Vector2.XY, z_near, z_far X, flip_fov bool) XYZW

FrustumAspectRatio creates a new Projection that projects positions in a frustum with the given size, X:Y aspect ratio, offset, and clipping planes.

flip_fov determines whether the projection's field of view is flipped over its diagonal.

func HeadMountedDisplay

func HeadMountedDisplay[X Float.Any, I Int.Any](eye I, aspect, intraocular_dist, display_width, display_to_lens, oversample, z_near, z_far Float.X) XYZW

HeadMountedDisplay creates a new Projection for projecting positions onto a head-mounted display with the given X:Y aspect ratio, distance between eyes, display width, distance to lens, oversampling factor, and depth clipping planes.

eye creates the projection for the left eye when set to 1, or the right eye when set to 2.

func HeadMountedDisplayPerspective

func HeadMountedDisplayPerspective(fovy Angle.Degrees, aspect, z_near, z_far Float.X, flip_fov bool, eye int, intraocular_dist, convergence_dist Float.X) XYZW

HeadMountedDisplayPerspective creates a new Projection that projects positions using a perspective projection with the given Y-axis field of view (in degrees), X:Y aspect ratio, and clipping distances. The projection is adjusted for a head-mounted display with the given distance between eyes and distance to a point that can be focused on.

eye creates the projection for the left eye when set to 1, or the right eye when set to 2.

flip_fov determines whether the projection's field of view is flipped over its diagonal

func IntoRect2

func IntoRect2(rect Rect2.PositionSize) XYZW

IntoRect2 creates a new Projection that projects positions into the given Rect2.

func Inverse

func Inverse(proj XYZW) XYZW

Inverse returns a Projection that performs the inverse of this Projection's projective transformation.

func JitterOffseted

func JitterOffseted(p XYZW, jitter Vector2.XY) XYZW

JitterOffseted returns a Projection with the X and Y values from the given Vector2 added to the first and second values of the final column respectively.

func Mul

func Mul(a, b XYZW) XYZW

func New

func New() XYZW

New creates a new projection set to Identity.

func Orthogonal

func Orthogonal[X Float.Any](left, right, bottom, top, z_near, z_far X) XYZW

Orthogonal creates a new Projection that projects positions using an orthogonal projection with the given clipping planes.

func OrthogonalAspectRatio

func OrthogonalAspectRatio[X Float.Any](size, aspect, z_near, z_far X, flip_fov bool) XYZW

OrthogonalAspectRatio creates a new Projection that projects positions using an orthogonal projection with the given size, X:Y aspect ratio, and clipping planes.

flip_fov determines whether the projection's field of view is flipped over its diagonal.

func Perspective

func Perspective[X Float.Any](fovy Angle.Degrees, aspect, z_near, z_far X, flip_fov bool) XYZW

Perspective creates a new Projection that projects positions using a perspective projection with the given Y-axis field of view (in degrees), X:Y aspect ratio, and clipping planes.

flip_fov determines whether the projection's field of view is flipped over its diagonal.

func PerspectiveAdjustedNearZ

func PerspectiveAdjustedNearZ(p XYZW, new_near_z Float.X) XYZW

PerspectiveAdjustedNearZ returns a Projection with the near clipping distance adjusted to be new_znear.

Note: The original Projection must be a perspective projection.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL