# vec3

Header: cglm/vec3.h

> Important: cglm was used glm_vec_ namespace for vec3 functions until v0.5.0, since v0.5.0 cglm uses glm_vec3_ namespace for vec3.

> Also glm_vec3_flipsign has been renamed to glm_vec3_negate

We mostly use vectors in graphics math, to make writing code faster and easy to read, some vec3 functions are aliased in global namespace. For instance `glm_dot()` is alias of `glm_vec3_dot()`, alias means inline wrapper here. There is no call version of alias functions

There are also functions for rotating vec3 vector. _m4, _m3 prefixes rotate vec3 with matrix.

## Table of contents (click to go):

Macros:

1. glm_vec3_dup(v, dest)

2. GLM_VEC3_ONE_INIT

3. GLM_VEC3_ZERO_INIT

4. GLM_VEC3_ONE

5. GLM_VEC3_ZERO

6. GLM_YUP

7. GLM_ZUP

8. GLM_XUP

Functions:

1. `glm_vec3()`

2. `glm_vec3_copy()`

3. `glm_vec3_zero()`

4. `glm_vec3_one()`

5. `glm_vec3_dot()`

6. `glm_vec3_norm2()`

7. `glm_vec3_norm()`

8. `glm_vec3_add()`

9. `glm_vec3_adds()`

10. `glm_vec3_sub()`

11. `glm_vec3_subs()`

12. `glm_vec3_mul()`

13. `glm_vec3_scale()`

14. `glm_vec3_scale_as()`

15. `glm_vec3_div()`

16. `glm_vec3_divs()`

17. `glm_vec3_addadd()`

18. `glm_vec3_subadd()`

19. `glm_vec3_muladd()`

20. `glm_vec3_muladds()`

21. `glm_vec3_maxadd()`

22. `glm_vec3_minadd()`

23. `glm_vec3_flipsign()`

24. `glm_vec3_flipsign_to()`

25. `glm_vec3_inv()`

26. `glm_vec3_inv_to()`

27. `glm_vec3_negate()`

28. `glm_vec3_negate_to()`

29. `glm_vec3_normalize()`

30. `glm_vec3_normalize_to()`

31. `glm_vec3_cross()`

32. `glm_vec3_crossn()`

33. `glm_vec3_distance2()`

34. `glm_vec3_distance()`

35. `glm_vec3_angle()`

36. `glm_vec3_rotate()`

37. `glm_vec3_rotate_m4()`

38. `glm_vec3_rotate_m3()`

39. `glm_vec3_proj()`

40. `glm_vec3_center()`

41. `glm_vec3_maxv()`

42. `glm_vec3_minv()`

43. `glm_vec3_ortho()`

44. `glm_vec3_clamp()`

45. `glm_vec3_lerp()`

46. `glm_vec3_make()`

47. `glm_vec3_faceforward()`

48. `glm_vec3_reflect()`

49. `glm_vec3_refract()`

## Functions documentation

void glm_vec3(vec4 v4, vec3 dest)

  init vec3 using vec4

  Parameters:
    [in] v4 vector4
    [out] dest destination

void glm_vec3_copy(vec3 a, vec3 dest)

  copy all members of [a] to [dest]

  Parameters:
    [in] a source
    [out] dest destination

void glm_vec3_zero(vec3 v)

  makes all members 0.0f (zero)

  Parameters:
    [in, out] v vector

void glm_vec3_one(vec3 v)

  makes all members 1.0f (one)

  Parameters:
    [in, out] v vector

float glm_vec3_dot(vec3 a, vec3 b)

  dot product of vec3

  Parameters:
    [in] a vector1
    [in] b vector2
  Returns:

    dot product

void glm_vec3_cross(vec3 a, vec3 b, vec3 d)

  cross product of two vector (RH)

  Parameters:
    [in] a vector 1
    [in] b vector 2
    [out] dest destination

void glm_vec3_crossn(vec3 a, vec3 b, vec3 dest)

  cross product of two vector (RH) and normalize the result

  Parameters:
    [in] a vector 1
    [in] b vector 2
    [out] dest destination

float glm_vec3_norm2(vec3 v)

  norm * norm (magnitude) of vector

  we can use this func instead of calling norm * norm, because it would call sqrtf function twice but with this func we can avoid func call, maybe this is not good name for this func

  Parameters:
    [in] v vector
  Returns:

    square of norm / magnitude

float glm_vec3_norm(vec3 vec)
  euclidean norm (magnitude), also called L2 norm
  this will give magnitude of vector in euclidean space
  Parameters:
    [in] vec vector

void glm_vec3_add(vec3 a, vec3 b, vec3 dest)

  add a vector to b vector store result in dest

  Parameters:
    [in] a vector1
    [in] b vector2
    [out] dest destination vector

void glm_vec3_adds(vec3 a, float s, vec3 dest)

  add scalar to v vector store result in dest (d = v + vec(s))

  Parameters:
    [in] v vector
    [in] s scalar
    [out] dest destination vector

void glm_vec3_sub(vec3 v1, vec3 v2, vec3 dest)

  subtract b vector from a vector store result in dest (d = v1 - v2)

  Parameters:
    [in] a vector1
    [in] b vector2
    [out] dest destination vector

void glm_vec3_subs(vec3 v, float s, vec3 dest)

  subtract scalar from v vector store result in dest (d = v - vec(s))

  Parameters:
    [in] v vector
    [in] s scalar
    [out] dest destination vector

void glm_vec3_mul(vec3 a, vec3 b, vec3 d)

  multiply two vector (component-wise multiplication)

  Parameters:
    [in] a vector
    [in] b scalar
    [out] d result = (a[0] * b[0], a[1] * b[1], a[2] * b[2])

void glm_vec3_scale(vec3 v, float s, vec3 dest)

  > multiply/scale vec3 vector with scalar: result = v * s

  Parameters:
    [in] v vector
    [in] s scalar
    [out] dest destination vector

void glm_vec3_scale_as(vec3 v, float s, vec3 dest)

  make vec3 vector scale as specified: result = unit(v) * s

  Parameters:
    [in] v vector
    [in] s scalar
    [out] dest destination vector

void glm_vec3_div(vec3 a, vec3 b, vec3 dest)

  div vector with another component-wise division: d = a / b

  Parameters:
    [in] a vector 1
    [in] b vector 2
    [out] dest result = (a[0] / b[0], a[1] / b[1], a[2] / b[2])

void glm_vec3_divs(vec3 v, float s, vec3 dest)

  div vector with scalar: d = v / s

  Parameters:
    [in] v vector
    [in] s scalar
    [out] dest result = (a[0] / s, a[1] / s, a[2] / s])

void glm_vec3_addadd(vec3 a, vec3 b, vec3 dest)
  add two vectors and add result to sum
  it applies += operator so dest must be initialized
  Parameters:
    [in] a vector 1
    [in] b vector 2
    [out] dest dest += (a + b)

void glm_vec3_subadd(vec3 a, vec3 b, vec3 dest)
  sub two vectors and add result to sum
  it applies += operator so dest must be initialized
  Parameters:
    [in] a vector 1
    [in] b vector 2
    [out] dest dest += (a - b)

void glm_vec3_muladd(vec3 a, vec3 b, vec3 dest)
  mul two vectors and add result to sum
  it applies += operator so dest must be initialized
  Parameters:
    [in] a vector 1
    [in] b vector 2
    [out] dest dest += (a * b)

void glm_vec3_muladds(vec3 a, float s, vec3 dest)
  mul vector with scalar and add result to sum
  it applies += operator so dest must be initialized
  Parameters:
    [in] a vector
    [in] s scalar
    [out] dest dest += (a * b)

void glm_vec3_maxadd(vec3 a, vec3 b, vec3 dest)
  add max of two vector to result/dest
  it applies += operator so dest must be initialized
  Parameters:
    [in] a vector 1
    [in] b vector 2
    [out] dest dest += (a * b)

void glm_vec3_minadd(vec3 a, vec3 b, vec3 dest)
  add min of two vector to result/dest
  it applies += operator so dest must be initialized
  Parameters:
    [in] a vector 1
    [in] b vector 2
    [out] dest dest += (a * b)

void glm_vec3_flipsign(vec3 v)

  DEPRECATED!

  use `glm_vec3_negate()`

  Parameters:
    [in, out] v vector

void glm_vec3_flipsign_to(vec3 v, vec3 dest)

  DEPRECATED!

  use `glm_vec3_negate_to()`

  Parameters:
    [in] v vector
    [out] dest negated vector

void glm_vec3_inv(vec3 v)

  DEPRECATED!

  use `glm_vec3_negate()`

  Parameters:
    [in, out] v vector

void glm_vec3_inv_to(vec3 v, vec3 dest)

  DEPRECATED!

  use `glm_vec3_negate_to()`

  Parameters:
    [in] v source
    [out] dest destination

void glm_vec3_negate(vec3 v)

  negate vector components

  Parameters:
    [in, out] v vector

void glm_vec3_negate_to(vec3 v, vec3 dest)

  negate vector components and store result in dest

  Parameters:
    [in] v vector
    [out] dest negated vector

void glm_vec3_normalize(vec3 v)

  normalize vec3 and store result in same vec

  Parameters:
    [in, out] v vector

void glm_vec3_normalize_to(vec3 vec, vec3 dest)

  > normalize vec3 to dest

  Parameters:
    [in] vec source
    [out] dest destination

float glm_vec3_angle(vec3 v1, vec3 v2)

  angle between two vector

  Parameters:
    [in] v1 vector1
    [in] v2 vector2
  Return:
    angle as radians

void glm_vec3_rotate(vec3 v, float angle, vec3 axis)

  > rotate vec3 around axis by angle using Rodrigues’ rotation formula

  Parameters:
    [in, out] v vector
    [in] axis axis vector (will be normalized)
    [in] angle angle (radians)

void glm_vec3_rotate_m4(mat4 m, vec3 v, vec3 dest)

  apply rotation matrix to vector

  Parameters:
    [in] m affine matrix or rot matrix
    [in] v vector
    [out] dest rotated vector

void glm_vec3_rotate_m3(mat3 m, vec3 v, vec3 dest)

  apply rotation matrix to vector

  Parameters:
    [in] m affine matrix or rot matrix
    [in] v vector
    [out] dest rotated vector

void glm_vec3_proj(vec3 a, vec3 b, vec3 dest)

  project a vector onto b vector

  Parameters:
    [in] a vector1
    [in] b vector2
    [out] dest projected vector

void glm_vec3_center(vec3 v1, vec3 v2, vec3 dest)

  find center point of two vector

  Parameters:
    [in] v1 vector1
    [in] v2 vector2
    [out] dest center point

float glm_vec3_distance2(vec3 v1, vec3 v2)

  squared distance between two vectors

  Parameters:
    [in] v1 vector1
    [in] v2 vector2
  Returns:
    squared distance (distance * distance)

float glm_vec3_distance(vec3 v1, vec3 v2)

  distance between two vectors

  Parameters:
    [in] v1 vector1
    [in] v2 vector2
  Returns:
    distance

void glm_vec3_maxv(vec3 v1, vec3 v2, vec3 dest)

  max values of vectors

  Parameters:
    [in] v1 vector1
    [in] v2 vector2
    [out] dest destination

void glm_vec3_minv(vec3 v1, vec3 v2, vec3 dest)

  min values of vectors

  Parameters:
    [in] v1 vector1
    [in] v2 vector2
    [out] dest destination

void glm_vec3_ortho(vec3 v, vec3 dest)

  possible orthogonal/perpendicular vector

  References:

    - On picking an orthogonal vector (and combing coconuts)

  Parameters:
    [in] v vector
    [out] dest orthogonal/perpendicular vector

void glm_vec3_clamp(vec3 v, float minVal, float maxVal)

  constrain a value to lie between two further values

  Parameters:
    [in, out] v vector
    [in] minVal minimum value
    [in] maxVal maximum value

void glm_vec3_lerp(vec3 from, vec3 to, float t, vec3 dest)

  linear interpolation between two vector

  formula: from + s * (to - from)
  Parameters:
    [in] from from value
    [in] to to value
    [in] t interpolant (amount) clamped between 0 and 1
    [out] dest destination

void glm_vec3_make(const float *__restrict src, vec3 dest)

  Create three dimensional vector from pointer

  Parameters:
    [in] src pointer to an array of floats
    [out] dest destination vector

void glm_vec3_faceforward(vec3 n, vec3 v, vec3 nref, vec3 dest)

  A vector pointing in the same direction as another

  Parameters:
    [in] n vector to orient
    [in] v incident vector
    [in] nref reference vector
    [out] dest destination: oriented vector, pointing away from the surface.

void glm_vec3_reflect(vec3 v, vec3 n, vec3 dest)

  Reflection vector using an incident ray and a surface normal

  Parameters:
    [in] v incident vector
    [in] n ❗️ normalized ❗️ normal vector
    [out] dest destination: reflection result

bool glm_vec3_refract(vec3 v, vec3 n, float eta, vec3 dest)

  Computes refraction vector for an incident vector and a surface normal.

  Calculates the refraction vector based on Snell’s law. If total internal reflection occurs (angle too great given eta), dest is set to zero and returns false. Otherwise, computes refraction vector, stores it in dest, and returns true.

  Parameters:
    [in] v ❗️ normalized ❗️ incident vector
    [in] n ❗️ normalized ❗️ normal vector
    [in] eta ratio of indices of refraction (incident/transmitted)
    [out] dest refraction vector if refraction occurs; zero vector otherwise
  Returns:

    returns true if refraction occurs; false if total internal reflection occurs.