vec3.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. .. default-domain:: C
  2. vec3
  3. ====
  4. Header: cglm/vec3.h
  5. **Important:** *cglm* was used **glm_vec_** namespace for vec3 functions until
  6. **v0.5.0**, since **v0.5.0** cglm uses **glm_vec3_** namespace for vec3.
  7. Also `glm_vec3_flipsign` has been renamed to `glm_vec3_negate`
  8. We mostly use vectors in graphics math, to make writing code faster
  9. and easy to read, some *vec3* functions are aliased in global namespace.
  10. For instance :c:func:`glm_dot` is alias of :c:func:`glm_vec3_dot`,
  11. alias means inline wrapper here. There is no call verison of alias functions
  12. There are also functions for rotating *vec3* vector. **_m4**, **_m3** prefixes
  13. rotate *vec3* with matrix.
  14. Table of contents (click to go):
  15. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  16. Macros:
  17. 1. glm_vec3_dup(v, dest)
  18. #. GLM_VEC3_ONE_INIT
  19. #. GLM_VEC3_ZERO_INIT
  20. #. GLM_VEC3_ONE
  21. #. GLM_VEC3_ZERO
  22. #. GLM_YUP
  23. #. GLM_ZUP
  24. #. GLM_XUP
  25. Functions:
  26. 1. :c:func:`glm_vec3`
  27. #. :c:func:`glm_vec3_copy`
  28. #. :c:func:`glm_vec3_zero`
  29. #. :c:func:`glm_vec3_one`
  30. #. :c:func:`glm_vec3_dot`
  31. #. :c:func:`glm_vec3_norm2`
  32. #. :c:func:`glm_vec3_norm`
  33. #. :c:func:`glm_vec3_add`
  34. #. :c:func:`glm_vec3_adds`
  35. #. :c:func:`glm_vec3_sub`
  36. #. :c:func:`glm_vec3_subs`
  37. #. :c:func:`glm_vec3_mul`
  38. #. :c:func:`glm_vec3_scale`
  39. #. :c:func:`glm_vec3_scale_as`
  40. #. :c:func:`glm_vec3_div`
  41. #. :c:func:`glm_vec3_divs`
  42. #. :c:func:`glm_vec3_addadd`
  43. #. :c:func:`glm_vec3_subadd`
  44. #. :c:func:`glm_vec3_muladd`
  45. #. :c:func:`glm_vec3_muladds`
  46. #. :c:func:`glm_vec3_maxadd`
  47. #. :c:func:`glm_vec3_minadd`
  48. #. :c:func:`glm_vec3_flipsign`
  49. #. :c:func:`glm_vec3_flipsign_to`
  50. #. :c:func:`glm_vec3_inv`
  51. #. :c:func:`glm_vec3_inv_to`
  52. #. :c:func:`glm_vec3_negate`
  53. #. :c:func:`glm_vec3_negate_to`
  54. #. :c:func:`glm_vec3_normalize`
  55. #. :c:func:`glm_vec3_normalize_to`
  56. #. :c:func:`glm_vec3_cross`
  57. #. :c:func:`glm_vec3_crossn`
  58. #. :c:func:`glm_vec3_distance2`
  59. #. :c:func:`glm_vec3_distance`
  60. #. :c:func:`glm_vec3_angle`
  61. #. :c:func:`glm_vec3_rotate`
  62. #. :c:func:`glm_vec3_rotate_m4`
  63. #. :c:func:`glm_vec3_rotate_m3`
  64. #. :c:func:`glm_vec3_proj`
  65. #. :c:func:`glm_vec3_center`
  66. #. :c:func:`glm_vec3_maxv`
  67. #. :c:func:`glm_vec3_minv`
  68. #. :c:func:`glm_vec3_ortho`
  69. #. :c:func:`glm_vec3_clamp`
  70. #. :c:func:`glm_vec3_lerp`
  71. Functions documentation
  72. ~~~~~~~~~~~~~~~~~~~~~~~
  73. .. c:function:: void glm_vec3(vec4 v4, vec3 dest)
  74. init vec3 using vec4
  75. Parameters:
  76. | *[in]* **v4** vector4
  77. | *[out]* **dest** destination
  78. .. c:function:: void glm_vec3_copy(vec3 a, vec3 dest)
  79. copy all members of [a] to [dest]
  80. Parameters:
  81. | *[in]* **a** source
  82. | *[out]* **dest** destination
  83. .. c:function:: void glm_vec3_zero(vec3 v)
  84. makes all members 0.0f (zero)
  85. Parameters:
  86. | *[in, out]* **v** vector
  87. .. c:function:: void glm_vec3_one(vec3 v)
  88. makes all members 1.0f (one)
  89. Parameters:
  90. | *[in, out]* **v** vector
  91. .. c:function:: float glm_vec3_dot(vec3 a, vec3 b)
  92. dot product of vec3
  93. Parameters:
  94. | *[in]* **a** vector1
  95. | *[in]* **b** vector2
  96. Returns:
  97. dot product
  98. .. c:function:: void glm_vec3_cross(vec3 a, vec3 b, vec3 d)
  99. cross product of two vector (RH)
  100. Parameters:
  101. | *[in]* **a** vector 1
  102. | *[in]* **b** vector 2
  103. | *[out]* **dest** destination
  104. .. c:function:: void glm_vec3_crossn(vec3 a, vec3 b, vec3 dest)
  105. cross product of two vector (RH) and normalize the result
  106. Parameters:
  107. | *[in]* **a** vector 1
  108. | *[in]* **b** vector 2
  109. | *[out]* **dest** destination
  110. .. c:function:: float glm_vec3_norm2(vec3 v)
  111. norm * norm (magnitude) of vector
  112. we can use this func instead of calling norm * norm, because it would call
  113. sqrtf fuction twice but with this func we can avoid func call, maybe this is
  114. not good name for this func
  115. Parameters:
  116. | *[in]* **v** vector
  117. Returns:
  118. square of norm / magnitude
  119. .. c:function:: float glm_vec3_norm(vec3 vec)
  120. | euclidean norm (magnitude), also called L2 norm
  121. | this will give magnitude of vector in euclidean space
  122. Parameters:
  123. | *[in]* **vec** vector
  124. .. c:function:: void glm_vec3_add(vec3 a, vec3 b, vec3 dest)
  125. add a vector to b vector store result in dest
  126. Parameters:
  127. | *[in]* **a** vector1
  128. | *[in]* **b** vector2
  129. | *[out]* **dest** destination vector
  130. .. c:function:: void glm_vec3_adds(vec3 a, float s, vec3 dest)
  131. add scalar to v vector store result in dest (d = v + vec(s))
  132. Parameters:
  133. | *[in]* **v** vector
  134. | *[in]* **s** scalar
  135. | *[out]* **dest** destination vector
  136. .. c:function:: void glm_vec3_sub(vec3 v1, vec3 v2, vec3 dest)
  137. subtract b vector from a vector store result in dest (d = v1 - v2)
  138. Parameters:
  139. | *[in]* **a** vector1
  140. | *[in]* **b** vector2
  141. | *[out]* **dest** destination vector
  142. .. c:function:: void glm_vec3_subs(vec3 v, float s, vec3 dest)
  143. subtract scalar from v vector store result in dest (d = v - vec(s))
  144. Parameters:
  145. | *[in]* **v** vector
  146. | *[in]* **s** scalar
  147. | *[out]* **dest** destination vector
  148. .. c:function:: void glm_vec3_mul(vec3 a, vec3 b, vec3 d)
  149. multiply two vector (component-wise multiplication)
  150. Parameters:
  151. | *[in]* **a** vector
  152. | *[in]* **b** scalar
  153. | *[out]* **d** result = (a[0] * b[0], a[1] * b[1], a[2] * b[2])
  154. .. c:function:: void glm_vec3_scale(vec3 v, float s, vec3 dest)
  155. multiply/scale vec3 vector with scalar: result = v * s
  156. Parameters:
  157. | *[in]* **v** vector
  158. | *[in]* **s** scalar
  159. | *[out]* **dest** destination vector
  160. .. c:function:: void glm_vec3_scale_as(vec3 v, float s, vec3 dest)
  161. make vec3 vector scale as specified: result = unit(v) * s
  162. Parameters:
  163. | *[in]* **v** vector
  164. | *[in]* **s** scalar
  165. | *[out]* **dest** destination vector
  166. .. c:function:: void glm_vec3_div(vec3 a, vec3 b, vec3 dest)
  167. div vector with another component-wise division: d = a / b
  168. Parameters:
  169. | *[in]* **a** vector 1
  170. | *[in]* **b** vector 2
  171. | *[out]* **dest** result = (a[0] / b[0], a[1] / b[1], a[2] / b[2])
  172. .. c:function:: void glm_vec3_divs(vec3 v, float s, vec3 dest)
  173. div vector with scalar: d = v / s
  174. Parameters:
  175. | *[in]* **v** vector
  176. | *[in]* **s** scalar
  177. | *[out]* **dest** result = (a[0] / s, a[1] / s, a[2] / s])
  178. .. c:function:: void glm_vec3_addadd(vec3 a, vec3 b, vec3 dest)
  179. | add two vectors and add result to sum
  180. | it applies += operator so dest must be initialized
  181. Parameters:
  182. | *[in]* **a** vector 1
  183. | *[in]* **b** vector 2
  184. | *[out]* **dest** dest += (a + b)
  185. .. c:function:: void glm_vec3_subadd(vec3 a, vec3 b, vec3 dest)
  186. | sub two vectors and add result to sum
  187. | it applies += operator so dest must be initialized
  188. Parameters:
  189. | *[in]* **a** vector 1
  190. | *[in]* **b** vector 2
  191. | *[out]* **dest** dest += (a - b)
  192. .. c:function:: void glm_vec3_muladd(vec3 a, vec3 b, vec3 dest)
  193. | mul two vectors and add result to sum
  194. | it applies += operator so dest must be initialized
  195. Parameters:
  196. | *[in]* **a** vector 1
  197. | *[in]* **b** vector 2
  198. | *[out]* **dest** dest += (a * b)
  199. .. c:function:: void glm_vec3_muladds(vec3 a, float s, vec3 dest)
  200. | mul vector with scalar and add result to sum
  201. | it applies += operator so dest must be initialized
  202. Parameters:
  203. | *[in]* **a** vector
  204. | *[in]* **s** scalar
  205. | *[out]* **dest** dest += (a * b)
  206. .. c:function:: void glm_vec3_maxadd(vec3 a, vec3 b, vec3 dest)
  207. | add max of two vector to result/dest
  208. | it applies += operator so dest must be initialized
  209. Parameters:
  210. | *[in]* **a** vector 1
  211. | *[in]* **b** vector 2
  212. | *[out]* **dest** dest += (a * b)
  213. .. c:function:: void glm_vec3_minadd(vec3 a, vec3 b, vec3 dest)
  214. | add min of two vector to result/dest
  215. | it applies += operator so dest must be initialized
  216. Parameters:
  217. | *[in]* **a** vector 1
  218. | *[in]* **b** vector 2
  219. | *[out]* **dest** dest += (a * b)
  220. .. c:function:: void glm_vec3_flipsign(vec3 v)
  221. **DEPRACATED!**
  222. use :c:func:`glm_vec3_negate`
  223. Parameters:
  224. | *[in, out]* **v** vector
  225. .. c:function:: void glm_vec3_flipsign_to(vec3 v, vec3 dest)
  226. **DEPRACATED!**
  227. use :c:func:`glm_vec3_negate_to`
  228. Parameters:
  229. | *[in]* **v** vector
  230. | *[out]* **dest** negated vector
  231. .. c:function:: void glm_vec3_inv(vec3 v)
  232. **DEPRACATED!**
  233. use :c:func:`glm_vec3_negate`
  234. Parameters:
  235. | *[in, out]* **v** vector
  236. .. c:function:: void glm_vec3_inv_to(vec3 v, vec3 dest)
  237. **DEPRACATED!**
  238. use :c:func:`glm_vec3_negate_to`
  239. Parameters:
  240. | *[in]* **v** source
  241. | *[out]* **dest** destination
  242. .. c:function:: void glm_vec3_negate(vec3 v)
  243. negate vector components
  244. Parameters:
  245. | *[in, out]* **v** vector
  246. .. c:function:: void glm_vec3_negate_to(vec3 v, vec3 dest)
  247. negate vector components and store result in dest
  248. Parameters:
  249. | *[in]* **v** vector
  250. | *[out]* **dest** negated vector
  251. .. c:function:: void glm_vec3_normalize(vec3 v)
  252. normalize vec3 and store result in same vec
  253. Parameters:
  254. | *[in, out]* **v** vector
  255. .. c:function:: void glm_vec3_normalize_to(vec3 vec, vec3 dest)
  256. normalize vec3 to dest
  257. Parameters:
  258. | *[in]* **vec** source
  259. | *[out]* **dest** destination
  260. .. c:function:: float glm_vec3_angle(vec3 v1, vec3 v2)
  261. angle betwen two vector
  262. Parameters:
  263. | *[in]* **v1** vector1
  264. | *[in]* **v2** vector2
  265. Return:
  266. | angle as radians
  267. .. c:function:: void glm_vec3_rotate(vec3 v, float angle, vec3 axis)
  268. rotate vec3 around axis by angle using Rodrigues' rotation formula
  269. Parameters:
  270. | *[in, out]* **v** vector
  271. | *[in]* **axis** axis vector (will be normalized)
  272. | *[in]* **angle** angle (radians)
  273. .. c:function:: void glm_vec3_rotate_m4(mat4 m, vec3 v, vec3 dest)
  274. apply rotation matrix to vector
  275. Parameters:
  276. | *[in]* **m** affine matrix or rot matrix
  277. | *[in]* **v** vector
  278. | *[out]* **dest** rotated vector
  279. .. c:function:: void glm_vec3_rotate_m3(mat3 m, vec3 v, vec3 dest)
  280. apply rotation matrix to vector
  281. Parameters:
  282. | *[in]* **m** affine matrix or rot matrix
  283. | *[in]* **v** vector
  284. | *[out]* **dest** rotated vector
  285. .. c:function:: void glm_vec3_proj(vec3 a, vec3 b, vec3 dest)
  286. project a vector onto b vector
  287. Parameters:
  288. | *[in]* **a** vector1
  289. | *[in]* **b** vector2
  290. | *[out]* **dest** projected vector
  291. .. c:function:: void glm_vec3_center(vec3 v1, vec3 v2, vec3 dest)
  292. find center point of two vector
  293. Parameters:
  294. | *[in]* **v1** vector1
  295. | *[in]* **v2** vector2
  296. | *[out]* **dest** center point
  297. .. c:function:: float glm_vec3_distance2(vec3 v1, vec3 v2)
  298. squared distance between two vectors
  299. Parameters:
  300. | *[in]* **v1** vector1
  301. | *[in]* **v2** vector2
  302. Returns:
  303. | squared distance (distance * distance)
  304. .. c:function:: float glm_vec3_distance(vec3 v1, vec3 v2)
  305. distance between two vectors
  306. Parameters:
  307. | *[in]* **v1** vector1
  308. | *[in]* **v2** vector2
  309. Returns:
  310. | distance
  311. .. c:function:: void glm_vec3_maxv(vec3 v1, vec3 v2, vec3 dest)
  312. max values of vectors
  313. Parameters:
  314. | *[in]* **v1** vector1
  315. | *[in]* **v2** vector2
  316. | *[out]* **dest** destination
  317. .. c:function:: void glm_vec3_minv(vec3 v1, vec3 v2, vec3 dest)
  318. min values of vectors
  319. Parameters:
  320. | *[in]* **v1** vector1
  321. | *[in]* **v2** vector2
  322. | *[out]* **dest** destination
  323. .. c:function:: void glm_vec3_ortho(vec3 v, vec3 dest)
  324. possible orthogonal/perpendicular vector
  325. Parameters:
  326. | *[in]* **v** vector
  327. | *[out]* **dest** orthogonal/perpendicular vector
  328. .. c:function:: void glm_vec3_clamp(vec3 v, float minVal, float maxVal)
  329. constrain a value to lie between two further values
  330. Parameters:
  331. | *[in, out]* **v** vector
  332. | *[in]* **minVal** minimum value
  333. | *[in]* **maxVal** maximum value
  334. .. c:function:: void glm_vec3_lerp(vec3 from, vec3 to, float t, vec3 dest)
  335. linear interpolation between two vector
  336. | formula: from + s * (to - from)
  337. Parameters:
  338. | *[in]* **from** from value
  339. | *[in]* **to** to value
  340. | *[in]* **t** interpolant (amount) clamped between 0 and 1
  341. | *[out]* **dest** destination