# H3DU.MatrixStack

### H3DU.MatrixStack()

H3DU.MatrixStack is a class that implements a stack of 4x4 transformation matrices.

When the constructor is called, it will create a stack whose only element is the identity matrix.

This class is considered a supplementary class to the Public Domain HTML 3D Library and is not considered part of that library.

To use this class, you must include the script "extras/matrixstack.js"; the class is not included in the "h3du_min.js" file which makes up the HTML 3D Library. Example:

```
<script type="text/javascript" src="extras/matrixstack.js"></script>
```

### Methods

- frustum

Modifies the matrix at the top of this stack by multiplying it by a frustum matrix. - get

Gets a copy of the matrix at the top of this stack. - loadIdentity

Modifies the matrix at the top of this stack by replacing it with the identity matrix. - loadMatrix

Modifies the matrix at the top of this stack by replacing it with the given matrix. - loadTransposeMatrix

Modifies the matrix at the top of this stack by replacing it with the transpose of the given matrix. - lookAt

Modifies the matrix at the top of this stack by multiplying it by a matrix representing a camera view. - multMatrix

Modifies the matrix at the top of this stack by multiplying it by another matrix. - multTransposeMatrix
**Deprecated: Use**`multMatrix(H3DU.Math.mat4transpose(mat))`

instead. - ortho

Modifies the matrix at the top of this stack by multiplying it by an orthographic projection. - ortho2d

Modifies the matrix at the top of this stack by multiplying it by a 2D orthographic projection. - perspective

Modifies the matrix at the top of this stack by multiplying it by a matrix that defines a perspective projection. - pickMatrix

Modifies the matrix at the top of this stack by multiplying it by a matrix that transforms the view to a portion of the viewport. - popMatrix

Removes the matrix at the top of this stack, making the matrix beneath it the new top matrix. - pushMatrix

Makes a copy of the matrix at the top of this stack and puts the copy on top of the stack. - rotate

Modifies the matrix at the top of this stack by multiplying it by a rotation transformation. - scale

Modifies the matrix at the top of this stack by multiplying it by a scaling transformation. - translate

Modifies the matrix at the top of this stack by multiplying it by a translation transformation.

### H3DU.MatrixStack#frustum(l, r, b, t, n, f)

Modifies the matrix at the top of this stack by multiplying it by a frustum matrix. This method is designed for enabling a right-handed coordinate system.

#### Parameters

`l`

(Type: number)

X coordinate of the point where the left clipping plane meets the near clipping plane.`r`

(Type: number)

X coordinate of the point where the right clipping plane meets the near clipping plane.`b`

(Type: number)

Y coordinate of the point where the bottom clipping plane meets the near clipping plane.`t`

(Type: number)

Y coordinate of the point where the top clipping plane meets the near clipping plane.`n`

(Type: number)

The distance from the camera to the near clipping plane. Objects closer than this distance won't be seen. This should be slightly greater than 0.`f`

(Type: number)

The distance from the camera to the far clipping plane. Objects beyond this distance will be too far to be seen.

#### Return Value

This object. (Type: H3DU.MatrixStack)

### H3DU.MatrixStack#get()

Gets a copy of the matrix at the top of this stack.

#### Return Value

. (Type: Array.<number>)

### H3DU.MatrixStack#loadIdentity()

Modifies the matrix at the top of this stack by replacing it with the identity matrix.

#### Return Value

This object. (Type: H3DU.MatrixStack)

### H3DU.MatrixStack#loadMatrix(mat)

Modifies the matrix at the top of this stack by replacing it with the given matrix.

#### Parameters

`mat`

(Type: Array.<number>)

A matrix to replace the top of the stack with.

#### Return Value

This object. (Type: H3DU.MatrixStack)

### H3DU.MatrixStack#loadTransposeMatrix(mat)

Modifies the matrix at the top of this stack by replacing it with the transpose of the given matrix.

#### Parameters

`mat`

(Type: Array.<number>)

A matrix whose transpose will replace the top of the stack.

#### Return Value

This object. (Type: H3DU.MatrixStack)

### H3DU.MatrixStack#lookAt(ex, ey, ez, cx, cy, cz, ux, uy, uz)

Modifies the matrix at the top of this stack by multiplying it by a matrix representing a camera view. This method is designed for enabling a right-handed coordinate system.

#### Parameters

`ex`

(Type: number)

X coordinate of the camera position in world space.`ey`

(Type: number)

Y coordinate of the camera position.`ez`

(Type: number)

Z coordinate of the camera position.`cx`

(Type: number)

X coordinate of the position in world space that the camera is looking at.`cy`

(Type: number)

Y coordinate of the position looked at.`cz`

(Type: number)

Z coordinate of the position looked at.`ux`

(Type: number)

X coordinate of the up direction vector. This vector must not point in the same or opposite direction as the camera's view direction.`uy`

(Type: number)

Y coordinate of the up vector.`uz`

(Type: number)

Z coordinate of the up vector.

#### Return Value

This object. (Type: H3DU.MatrixStack)

### H3DU.MatrixStack#multMatrix(mat)

Modifies the matrix at the top of this stack by multiplying it by another matrix. The matrices are multiplied such that the transformations they describe happen in reverse order. For example, if the matrix at the top of the stack describes a translation and the matrix passed to this method describes a scaling, the multiplied matrix will describe the effect of scaling then translation.

#### Parameters

`mat`

(Type: Array.<number>)

A matrix to multiply the current one by.

#### Return Value

This object. (Type: H3DU.MatrixStack)

### H3DU.MatrixStack#multTransposeMatrix(mat)

**Deprecated: Use multMatrix(H3DU.Math.mat4transpose(mat)) instead.**

Modifies the matrix at the top of this stack by multiplying it by the transpose of another matrix.

#### Parameters

`mat`

(Type: Array.<number>)

A matrix whose transpose the current matrix will be multiplied by.

#### Return Value

This object. (Type: H3DU.MatrixStack)

### H3DU.MatrixStack#ortho(l, r, b, t, n, f)

Modifies the matrix at the top of this stack by multiplying it by an orthographic projection. In this projection, the left clipping plane is parallel to the right clipping plane and the top to the bottom.

This method is designed for enabling a right-handed coordinate system.

#### Parameters

`l`

(Type: number)

Leftmost coordinate of the 3D view.`r`

(Type: number)

Rightmost coordinate of the 3D view. (Note that r can be greater than l or vice versa.)`b`

(Type: number)

Bottommost coordinate of the 3D view.`t`

(Type: number)

Topmost coordinate of the 3D view. (Note that t can be greater than b or vice versa.)`n`

(Type: number)

Distance from the camera to the near clipping plane. A positive value means the plane is in front of the viewer.`f`

(Type: number)

Distance from the camera to the far clipping plane. A positive value means the plane is in front of the viewer.

#### Return Value

This object. (Type: H3DU.MatrixStack)

### H3DU.MatrixStack#ortho2d(l, r, b, t)

Modifies the matrix at the top of this stack by multiplying it by a 2D orthographic projection. This method is designed for enabling a right-handed coordinate system.

#### Parameters

`l`

(Type: number)

Leftmost coordinate of the 2D view.`r`

(Type: number)

Rightmost coordinate of the 2D view. (Note that r can be greater than l or vice versa.)`b`

(Type: number)

Bottommost coordinate of the 2D view.`t`

(Type: number)

Topmost coordinate of the 2D view. (Note that t can be greater than b or vice versa.)

#### Return Value

This object. (Type: H3DU.MatrixStack)

### H3DU.MatrixStack#perspective(fov, aspect, n, f)

Modifies the matrix at the top of this stack by multiplying it by a matrix that defines a perspective projection.

This method is designed for enabling a right-handed coordinate system.

#### Parameters

`fov`

(Type: number)

Vertical field of view, in degrees. Should be less than 180 degrees. (The smaller this number, the bigger close objects appear to be. As a result, zoom can be implemented by multiplying field of view by an additional factor.)`aspect`

(Type: number)

The ratio of width to height of the viewport, usually the scene's aspect ratio.`n`

(Type: number)

The distance from the camera to the near clipping plane. Objects closer than this distance won't be seen. This should be slightly greater than 0.`f`

(Type: number)

The distance from the camera to the far clipping plane. Objects beyond this distance will be too far to be seen.

#### Return Value

This object. (Type: H3DU.MatrixStack)

### H3DU.MatrixStack#pickMatrix(wx, wy, ww, wh, vp)

Modifies the matrix at the top of this stack by multiplying it by a matrix that transforms the view to a portion of the viewport.

#### Parameters

`wx`

(Type: number)

X coordinate of the center of the desired viewport portion.`wy`

(Type: number)

Y coordinate of the center of the desired viewport portion.`ww`

(Type: number)

Width of the desired viewport portion.`wh`

(Type: number)

Height of the desired viewport portion.`vp`

(Type: Array.<number>)

A 4-element array giving the X and Y coordinates of the lower left corner followed by the width and height of a rectangle indicating the current viewport.

#### Return Value

This object. (Type: H3DU.MatrixStack)

### H3DU.MatrixStack#popMatrix()

Removes the matrix at the top of this stack, making the matrix beneath it the new top matrix. Has no effect if the stack has only one matrix.

#### Return Value

This object. (Type: H3DU.MatrixStack)

### H3DU.MatrixStack#pushMatrix()

Makes a copy of the matrix at the top of this stack and puts the copy on top of the stack.

#### Return Value

This object. (Type: H3DU.MatrixStack)

### H3DU.MatrixStack#rotate(angle, x, y, z)

Modifies the matrix at the top of this stack by multiplying it by a rotation transformation.

#### Parameters

`angle`

(Type: number)

The desired angle to rotate in degrees. If the axis of rotation points toward the viewer, the angle's value is increasing in a counterclockwise direction.`x`

(Type: number)

X-component of the axis of rotation.`y`

(Type: number)

Y-component of the axis of rotation.`z`

(Type: number)

Z-component of the axis of rotation.

#### Return Value

This object. (Type: H3DU.MatrixStack)

### H3DU.MatrixStack#scale(x, y, z)

Modifies the matrix at the top of this stack by multiplying it by a scaling transformation.

#### Parameters

`x`

(Type: number)

Scale factor along the X axis.`y`

(Type: number)

Scale factor along the Y axis.`z`

(Type: number)

Scale factor along the Z axis.

#### Return Value

This object. (Type: H3DU.MatrixStack)

### H3DU.MatrixStack#translate(x, y, z)

Modifies the matrix at the top of this stack by multiplying it by a translation transformation.

#### Parameters

`x`

(Type: number)

Translation along the X axis.`y`

(Type: number)

Translation along the Y axis.`z`

(Type: number)

Translation along the Z axis.

#### Return Value

This object. (Type: H3DU.MatrixStack)