vec4

Header file: include/vec4.h

vec4 is a vector with 4 scalars, x, y, z and w.

\(vec4 = \begin{bmatrix} x \\ y \\ z \\ w \end{bmatrix}\)

Class variables

Type

Name

Description

float

x

The first element of the vec4.

float

y

The second element of the vec4.

float

z

The third element of the vec4.

float

w

The fourth element of the vec4.

Class functions

Constructors

Name

Description

vec4()

Construct a (0.0, 0.0, 0.0, 0.0) vector.

vec4(float _value)

Construct a (_value, _value, _value, _value) vector.

vec4(float _x, float _y, float _z, float _w)

Construct a (_x, _y, _z, _w) vector.

vec4(float _x, const nml::vec3& _yzw)

Construct a (_x, _yzw.x, _yzw.y, _yzw.z) vector.

vec4(const nml::vec3& _xyz, float _w)

Construct a (_xyz.x, _xyz.y, _xyz.z, _w) vector.

vec4(float _x, float _y, const nml::vec2& _zw)

Construct a (_x, _y, _zw.x, _zw.y) vector.

vec4(float _x, const nml::vec2& _yz, float _w)

Construct a (_x, _yz.x, _yz.y, _w) vector.

vec4(const nml::vec2& _xy, float _z, float _w)

Construct a (_xy.x, _xy.y, _z, _w) vector.

vec4(const nml::vec2& _xy, const nml::vec2& _zw)

Construct a (_xy.x, _xy.y, _zw.x, _zw.y) vector.

vec4(const float* _ptr)

Construct a vector from a pointer.

Operators

Name

Description

nml::vec4& operator+=(const nml::vec4& other)

Add a vec4 to the current vec4.

nml::vec4& operator-=(const nml::vec4& other)

Substract a vec4 from the current vec4.

nml::vec4& operator*=(const float other)

Multiply the current vec4 by a scalar.

nml::vec4& operator/=(const float other)

Divide the current vec4 by a scalar.

nml::vec4 operator-()

Return the negative of the current vec4.

float& operator[](size_t index)

Return a reference to x if index is 0, a reference to y if index is 1, a reference to z if index is 2 or a reference to w if index is 3, else, throw an exception.

const float operator[](size_t index) const

Return the value of x if index is 0, the value of y if index is 1, the value of z if index is 2 or the value of w if index is 3, else, throw an exception.

Functions

Name

Description

float length()

Return the length of the vector.

float* data()

Return a pointer to the vector’s elements.

Namespace functions

Operators

Name

Description

nml::vec4 operator+(nml::vec4 lhs, const nml::vec4& rhs)

Return a vec4 that is the sum between two vec4.

nml::vec4 operator-(nml::vec4 lhs, const nml::vec4& rhs)

Return a vec4 that is the difference between two vec4.

nml::vec4 operator*(nml::vec4 lhs, const float rhs)

Return a vec4 that is the product between a vec4 and a scalar.

nml::vec4 operator*(float lhs, const nml::vec4& rhs)

Return a vec4 that is the product between a scalar and a vec4.

nml::vec4 operator/(nml::vec4 lhs, const float rhs)

Return a vec4 that is the quotient between a vec4 and a scalar.

bool operator==(const nml::vec4& lhs, const nml::vec4& rhs)

Return true if the two vec4 are identical, else, return false.

bool operator!=(const nml::vec4& lhs, const nml::vec4& rhs)

Return true if the two vec4 are different, else, return false.

Functions

Name

Description

nml::vec4 nml::normalize(const nml::vec4& vec)

Return a vec4 with the same direction as vec but with length 1.

float nml::dot(const nml::vec4& a, const nml::vec4& b)

Return the dot product between two vec4.

nml::vec4 nml::reflect(const nml::vec4& i, const nml::vec4& n)

Return the reflected direction between the incident vector i and the normal n. n should be normalized.

nml::vec4 nml::refract(const nml::vec4& i, const nml::vec4& n, float ior)

Return the refracted direction between the incident vector i, the normal n and the ratio of indices of refraction ior. n should be normalized.

std::string nml::to_string(const nml::vec4& vec)

Return a vec4 as a string under the format “[x, y, z, w]”.