vec3.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. norm (magnitude) of vec3
  121. Parameters:
  122. | *[in]* **vec** vector
  123. .. c:function:: void glm_vec3_add(vec3 a, vec3 b, vec3 dest)
  124. add a vector to b vector store result in dest
  125. Parameters:
  126. | *[in]* **a** vector1
  127. | *[in]* **b** vector2
  128. | *[out]* **dest** destination vector
  129. .. c:function:: void glm_vec3_adds(vec3 a, float s, vec3 dest)
  130. add scalar to v vector store result in dest (d = v + vec(s))
  131. Parameters:
  132. | *[in]* **v** vector
  133. | *[in]* **s** scalar
  134. | *[out]* **dest** destination vector
  135. .. c:function:: void glm_vec3_sub(vec3 v1, vec3 v2, vec3 dest)
  136. subtract b vector from a vector store result in dest (d = v1 - v2)
  137. Parameters:
  138. | *[in]* **a** vector1
  139. | *[in]* **b** vector2
  140. | *[out]* **dest** destination vector
  141. .. c:function:: void glm_vec3_subs(vec3 v, float s, vec3 dest)
  142. subtract scalar from v vector store result in dest (d = v - vec(s))
  143. Parameters:
  144. | *[in]* **v** vector
  145. | *[in]* **s** scalar
  146. | *[out]* **dest** destination vector
  147. .. c:function:: void glm_vec3_mul(vec3 a, vec3 b, vec3 d)
  148. multiply two vector (component-wise multiplication)
  149. Parameters:
  150. | *[in]* **a** vector
  151. | *[in]* **b** scalar
  152. | *[out]* **d** result = (a[0] * b[0], a[1] * b[1], a[2] * b[2])
  153. .. c:function:: void glm_vec3_scale(vec3 v, float s, vec3 dest)
  154. multiply/scale vec3 vector with scalar: result = v * s
  155. Parameters:
  156. | *[in]* **v** vector
  157. | *[in]* **s** scalar
  158. | *[out]* **dest** destination vector
  159. .. c:function:: void glm_vec3_scale_as(vec3 v, float s, vec3 dest)
  160. make vec3 vector scale as specified: result = unit(v) * s
  161. Parameters:
  162. | *[in]* **v** vector
  163. | *[in]* **s** scalar
  164. | *[out]* **dest** destination vector
  165. .. c:function:: void glm_vec3_div(vec3 a, vec3 b, vec3 dest)
  166. div vector with another component-wise division: d = a / b
  167. Parameters:
  168. | *[in]* **a** vector 1
  169. | *[in]* **b** vector 2
  170. | *[out]* **dest** result = (a[0] / b[0], a[1] / b[1], a[2] / b[2])
  171. .. c:function:: void glm_vec3_divs(vec3 v, float s, vec3 dest)
  172. div vector with scalar: d = v / s
  173. Parameters:
  174. | *[in]* **v** vector
  175. | *[in]* **s** scalar
  176. | *[out]* **dest** result = (a[0] / s, a[1] / s, a[2] / s])
  177. .. c:function:: void glm_vec3_addadd(vec3 a, vec3 b, vec3 dest)
  178. | add two vectors and add result to sum
  179. | it applies += operator so dest must be initialized
  180. Parameters:
  181. | *[in]* **a** vector 1
  182. | *[in]* **b** vector 2
  183. | *[out]* **dest** dest += (a + b)
  184. .. c:function:: void glm_vec3_subadd(vec3 a, vec3 b, vec3 dest)
  185. | sub two vectors and add result to sum
  186. | it applies += operator so dest must be initialized
  187. Parameters:
  188. | *[in]* **a** vector 1
  189. | *[in]* **b** vector 2
  190. | *[out]* **dest** dest += (a - b)
  191. .. c:function:: void glm_vec3_muladd(vec3 a, vec3 b, vec3 dest)
  192. | mul two vectors and add result to sum
  193. | it applies += operator so dest must be initialized
  194. Parameters:
  195. | *[in]* **a** vector 1
  196. | *[in]* **b** vector 2
  197. | *[out]* **dest** dest += (a * b)
  198. .. c:function:: void glm_vec3_muladds(vec3 a, float s, vec3 dest)
  199. | mul vector with scalar and add result to sum
  200. | it applies += operator so dest must be initialized
  201. Parameters:
  202. | *[in]* **a** vector
  203. | *[in]* **s** scalar
  204. | *[out]* **dest** dest += (a * b)
  205. .. c:function:: void glm_vec3_maxadd(vec3 a, vec3 b, vec3 dest)
  206. | add max of two vector to result/dest
  207. | it applies += operator so dest must be initialized
  208. Parameters:
  209. | *[in]* **a** vector 1
  210. | *[in]* **b** vector 2
  211. | *[out]* **dest** dest += (a * b)
  212. .. c:function:: void glm_vec3_minadd(vec3 a, vec3 b, vec3 dest)
  213. | add min of two vector to result/dest
  214. | it applies += operator so dest must be initialized
  215. Parameters:
  216. | *[in]* **a** vector 1
  217. | *[in]* **b** vector 2
  218. | *[out]* **dest** dest += (a * b)
  219. .. c:function:: void glm_vec3_flipsign(vec3 v)
  220. **DEPRACATED!**
  221. use :c:func:`glm_vec3_negate`
  222. Parameters:
  223. | *[in, out]* **v** vector
  224. .. c:function:: void glm_vec3_flipsign_to(vec3 v, vec3 dest)
  225. **DEPRACATED!**
  226. use :c:func:`glm_vec3_negate_to`
  227. Parameters:
  228. | *[in]* **v** vector
  229. | *[out]* **dest** negated vector
  230. .. c:function:: void glm_vec3_inv(vec3 v)
  231. **DEPRACATED!**
  232. use :c:func:`glm_vec3_negate`
  233. Parameters:
  234. | *[in, out]* **v** vector
  235. .. c:function:: void glm_vec3_inv_to(vec3 v, vec3 dest)
  236. **DEPRACATED!**
  237. use :c:func:`glm_vec3_negate_to`
  238. Parameters:
  239. | *[in]* **v** source
  240. | *[out]* **dest** destination
  241. .. c:function:: void glm_vec3_negate(vec3 v)
  242. negate vector components
  243. Parameters:
  244. | *[in, out]* **v** vector
  245. .. c:function:: void glm_vec3_negate_to(vec3 v, vec3 dest)
  246. negate vector components and store result in dest
  247. Parameters:
  248. | *[in]* **v** vector
  249. | *[out]* **dest** negated vector
  250. .. c:function:: void glm_vec3_normalize(vec3 v)
  251. normalize vec3 and store result in same vec
  252. Parameters:
  253. | *[in, out]* **v** vector
  254. .. c:function:: void glm_vec3_normalize_to(vec3 vec, vec3 dest)
  255. normalize vec3 to dest
  256. Parameters:
  257. | *[in]* **vec** source
  258. | *[out]* **dest** destination
  259. .. c:function:: float glm_vec3_angle(vec3 v1, vec3 v2)
  260. angle betwen two vector
  261. Parameters:
  262. | *[in]* **v1** vector1
  263. | *[in]* **v2** vector2
  264. Return:
  265. | angle as radians
  266. .. c:function:: void glm_vec3_rotate(vec3 v, float angle, vec3 axis)
  267. rotate vec3 around axis by angle using Rodrigues' rotation formula
  268. Parameters:
  269. | *[in, out]* **v** vector
  270. | *[in]* **axis** axis vector (will be normalized)
  271. | *[out]* **angle** angle (radians)
  272. .. c:function:: void glm_vec3_rotate_m4(mat4 m, vec3 v, vec3 dest)
  273. apply rotation matrix to vector
  274. Parameters:
  275. | *[in]* **m** affine matrix or rot matrix
  276. | *[in]* **v** vector
  277. | *[out]* **dest** rotated vector
  278. .. c:function:: void glm_vec3_rotate_m3(mat3 m, vec3 v, vec3 dest)
  279. apply rotation matrix to vector
  280. Parameters:
  281. | *[in]* **m** affine matrix or rot matrix
  282. | *[in]* **v** vector
  283. | *[out]* **dest** rotated vector
  284. .. c:function:: void glm_vec3_proj(vec3 a, vec3 b, vec3 dest)
  285. project a vector onto b vector
  286. Parameters:
  287. | *[in]* **a** vector1
  288. | *[in]* **b** vector2
  289. | *[out]* **dest** projected vector
  290. .. c:function:: void glm_vec3_center(vec3 v1, vec3 v2, vec3 dest)
  291. find center point of two vector
  292. Parameters:
  293. | *[in]* **v1** vector1
  294. | *[in]* **v2** vector2
  295. | *[out]* **dest** center point
  296. .. c:function:: float glm_vec3_distance2(vec3 v1, vec3 v2)
  297. squared distance between two vectors
  298. Parameters:
  299. | *[in]* **mat** vector1
  300. | *[in]* **row1** vector2
  301. Returns:
  302. | squared distance (distance * distance)
  303. .. c:function:: float glm_vec3_distance(vec3 v1, vec3 v2)
  304. distance between two vectors
  305. Parameters:
  306. | *[in]* **mat** vector1
  307. | *[in]* **row1** vector2
  308. Returns:
  309. | distance
  310. .. c:function:: void glm_vec3_maxv(vec3 v1, vec3 v2, vec3 dest)
  311. max values of vectors
  312. Parameters:
  313. | *[in]* **v1** vector1
  314. | *[in]* **v2** vector2
  315. | *[out]* **dest** destination
  316. .. c:function:: void glm_vec3_minv(vec3 v1, vec3 v2, vec3 dest)
  317. min values of vectors
  318. Parameters:
  319. | *[in]* **v1** vector1
  320. | *[in]* **v2** vector2
  321. | *[out]* **dest** destination
  322. .. c:function:: void glm_vec3_ortho(vec3 v, vec3 dest)
  323. possible orthogonal/perpendicular vector
  324. Parameters:
  325. | *[in]* **mat** vector
  326. | *[out]* **dest** orthogonal/perpendicular vector
  327. .. c:function:: void glm_vec3_clamp(vec3 v, float minVal, float maxVal)
  328. constrain a value to lie between two further values
  329. Parameters:
  330. | *[in, out]* **v** vector
  331. | *[in]* **minVal** minimum value
  332. | *[in]* **maxVal** maximum value
  333. .. c:function:: void glm_vec3_lerp(vec3 from, vec3 to, float t, vec3 dest)
  334. linear interpolation between two vector
  335. | formula: from + s * (to - from)
  336. Parameters:
  337. | *[in]* **from** from value
  338. | *[in]* **to** to value
  339. | *[in]* **t** interpolant (amount) clamped between 0 and 1
  340. | *[out]* **dest** destination