vec3.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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 version 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. #. :c:func:`glm_vec3_make`
  72. #. :c:func:`glm_vec3_faceforward`
  73. #. :c:func:`glm_vec3_reflect`
  74. #. :c:func:`glm_vec3_refract`
  75. Functions documentation
  76. ~~~~~~~~~~~~~~~~~~~~~~~
  77. .. c:function:: void glm_vec3(vec4 v4, vec3 dest)
  78. init vec3 using vec4
  79. Parameters:
  80. | *[in]* **v4** vector4
  81. | *[out]* **dest** destination
  82. .. c:function:: void glm_vec3_copy(vec3 a, vec3 dest)
  83. copy all members of [a] to [dest]
  84. Parameters:
  85. | *[in]* **a** source
  86. | *[out]* **dest** destination
  87. .. c:function:: void glm_vec3_zero(vec3 v)
  88. makes all members 0.0f (zero)
  89. Parameters:
  90. | *[in, out]* **v** vector
  91. .. c:function:: void glm_vec3_one(vec3 v)
  92. makes all members 1.0f (one)
  93. Parameters:
  94. | *[in, out]* **v** vector
  95. .. c:function:: float glm_vec3_dot(vec3 a, vec3 b)
  96. dot product of vec3
  97. Parameters:
  98. | *[in]* **a** vector1
  99. | *[in]* **b** vector2
  100. Returns:
  101. dot product
  102. .. c:function:: void glm_vec3_cross(vec3 a, vec3 b, vec3 d)
  103. cross product of two vector (RH)
  104. Parameters:
  105. | *[in]* **a** vector 1
  106. | *[in]* **b** vector 2
  107. | *[out]* **dest** destination
  108. .. c:function:: void glm_vec3_crossn(vec3 a, vec3 b, vec3 dest)
  109. cross product of two vector (RH) and normalize the result
  110. Parameters:
  111. | *[in]* **a** vector 1
  112. | *[in]* **b** vector 2
  113. | *[out]* **dest** destination
  114. .. c:function:: float glm_vec3_norm2(vec3 v)
  115. norm * norm (magnitude) of vector
  116. we can use this func instead of calling norm * norm, because it would call
  117. sqrtf function twice but with this func we can avoid func call, maybe this is
  118. not good name for this func
  119. Parameters:
  120. | *[in]* **v** vector
  121. Returns:
  122. square of norm / magnitude
  123. .. c:function:: float glm_vec3_norm(vec3 vec)
  124. | euclidean norm (magnitude), also called L2 norm
  125. | this will give magnitude of vector in euclidean space
  126. Parameters:
  127. | *[in]* **vec** vector
  128. .. c:function:: void glm_vec3_add(vec3 a, vec3 b, vec3 dest)
  129. add a vector to b vector store result in dest
  130. Parameters:
  131. | *[in]* **a** vector1
  132. | *[in]* **b** vector2
  133. | *[out]* **dest** destination vector
  134. .. c:function:: void glm_vec3_adds(vec3 a, float s, vec3 dest)
  135. add scalar to v vector store result in dest (d = v + vec(s))
  136. Parameters:
  137. | *[in]* **v** vector
  138. | *[in]* **s** scalar
  139. | *[out]* **dest** destination vector
  140. .. c:function:: void glm_vec3_sub(vec3 v1, vec3 v2, vec3 dest)
  141. subtract b vector from a vector store result in dest (d = v1 - v2)
  142. Parameters:
  143. | *[in]* **a** vector1
  144. | *[in]* **b** vector2
  145. | *[out]* **dest** destination vector
  146. .. c:function:: void glm_vec3_subs(vec3 v, float s, vec3 dest)
  147. subtract scalar from v vector store result in dest (d = v - vec(s))
  148. Parameters:
  149. | *[in]* **v** vector
  150. | *[in]* **s** scalar
  151. | *[out]* **dest** destination vector
  152. .. c:function:: void glm_vec3_mul(vec3 a, vec3 b, vec3 d)
  153. multiply two vector (component-wise multiplication)
  154. Parameters:
  155. | *[in]* **a** vector
  156. | *[in]* **b** scalar
  157. | *[out]* **d** result = (a[0] * b[0], a[1] * b[1], a[2] * b[2])
  158. .. c:function:: void glm_vec3_scale(vec3 v, float s, vec3 dest)
  159. multiply/scale vec3 vector with scalar: result = v * s
  160. Parameters:
  161. | *[in]* **v** vector
  162. | *[in]* **s** scalar
  163. | *[out]* **dest** destination vector
  164. .. c:function:: void glm_vec3_scale_as(vec3 v, float s, vec3 dest)
  165. make vec3 vector scale as specified: result = unit(v) * s
  166. Parameters:
  167. | *[in]* **v** vector
  168. | *[in]* **s** scalar
  169. | *[out]* **dest** destination vector
  170. .. c:function:: void glm_vec3_div(vec3 a, vec3 b, vec3 dest)
  171. div vector with another component-wise division: d = a / b
  172. Parameters:
  173. | *[in]* **a** vector 1
  174. | *[in]* **b** vector 2
  175. | *[out]* **dest** result = (a[0] / b[0], a[1] / b[1], a[2] / b[2])
  176. .. c:function:: void glm_vec3_divs(vec3 v, float s, vec3 dest)
  177. div vector with scalar: d = v / s
  178. Parameters:
  179. | *[in]* **v** vector
  180. | *[in]* **s** scalar
  181. | *[out]* **dest** result = (a[0] / s, a[1] / s, a[2] / s])
  182. .. c:function:: void glm_vec3_addadd(vec3 a, vec3 b, vec3 dest)
  183. | add two vectors and add result to sum
  184. | it applies += operator so dest must be initialized
  185. Parameters:
  186. | *[in]* **a** vector 1
  187. | *[in]* **b** vector 2
  188. | *[out]* **dest** dest += (a + b)
  189. .. c:function:: void glm_vec3_subadd(vec3 a, vec3 b, vec3 dest)
  190. | sub two vectors and add result to sum
  191. | it applies += operator so dest must be initialized
  192. Parameters:
  193. | *[in]* **a** vector 1
  194. | *[in]* **b** vector 2
  195. | *[out]* **dest** dest += (a - b)
  196. .. c:function:: void glm_vec3_muladd(vec3 a, vec3 b, vec3 dest)
  197. | mul two vectors and add result to sum
  198. | it applies += operator so dest must be initialized
  199. Parameters:
  200. | *[in]* **a** vector 1
  201. | *[in]* **b** vector 2
  202. | *[out]* **dest** dest += (a * b)
  203. .. c:function:: void glm_vec3_muladds(vec3 a, float s, vec3 dest)
  204. | mul vector with scalar and add result to sum
  205. | it applies += operator so dest must be initialized
  206. Parameters:
  207. | *[in]* **a** vector
  208. | *[in]* **s** scalar
  209. | *[out]* **dest** dest += (a * b)
  210. .. c:function:: void glm_vec3_maxadd(vec3 a, vec3 b, vec3 dest)
  211. | add max of two vector to result/dest
  212. | it applies += operator so dest must be initialized
  213. Parameters:
  214. | *[in]* **a** vector 1
  215. | *[in]* **b** vector 2
  216. | *[out]* **dest** dest += (a * b)
  217. .. c:function:: void glm_vec3_minadd(vec3 a, vec3 b, vec3 dest)
  218. | add min of two vector to result/dest
  219. | it applies += operator so dest must be initialized
  220. Parameters:
  221. | *[in]* **a** vector 1
  222. | *[in]* **b** vector 2
  223. | *[out]* **dest** dest += (a * b)
  224. .. c:function:: void glm_vec3_flipsign(vec3 v)
  225. **DEPRECATED!**
  226. use :c:func:`glm_vec3_negate`
  227. Parameters:
  228. | *[in, out]* **v** vector
  229. .. c:function:: void glm_vec3_flipsign_to(vec3 v, vec3 dest)
  230. **DEPRECATED!**
  231. use :c:func:`glm_vec3_negate_to`
  232. Parameters:
  233. | *[in]* **v** vector
  234. | *[out]* **dest** negated vector
  235. .. c:function:: void glm_vec3_inv(vec3 v)
  236. **DEPRECATED!**
  237. use :c:func:`glm_vec3_negate`
  238. Parameters:
  239. | *[in, out]* **v** vector
  240. .. c:function:: void glm_vec3_inv_to(vec3 v, vec3 dest)
  241. **DEPRECATED!**
  242. use :c:func:`glm_vec3_negate_to`
  243. Parameters:
  244. | *[in]* **v** source
  245. | *[out]* **dest** destination
  246. .. c:function:: void glm_vec3_negate(vec3 v)
  247. negate vector components
  248. Parameters:
  249. | *[in, out]* **v** vector
  250. .. c:function:: void glm_vec3_negate_to(vec3 v, vec3 dest)
  251. negate vector components and store result in dest
  252. Parameters:
  253. | *[in]* **v** vector
  254. | *[out]* **dest** negated vector
  255. .. c:function:: void glm_vec3_normalize(vec3 v)
  256. normalize vec3 and store result in same vec
  257. Parameters:
  258. | *[in, out]* **v** vector
  259. .. c:function:: void glm_vec3_normalize_to(vec3 vec, vec3 dest)
  260. normalize vec3 to dest
  261. Parameters:
  262. | *[in]* **vec** source
  263. | *[out]* **dest** destination
  264. .. c:function:: float glm_vec3_angle(vec3 v1, vec3 v2)
  265. angle between two vector
  266. Parameters:
  267. | *[in]* **v1** vector1
  268. | *[in]* **v2** vector2
  269. Return:
  270. | angle as radians
  271. .. c:function:: void glm_vec3_rotate(vec3 v, float angle, vec3 axis)
  272. rotate vec3 around axis by angle using Rodrigues' rotation formula
  273. Parameters:
  274. | *[in, out]* **v** vector
  275. | *[in]* **axis** axis vector (will be normalized)
  276. | *[in]* **angle** angle (radians)
  277. .. c:function:: void glm_vec3_rotate_m4(mat4 m, vec3 v, vec3 dest)
  278. apply rotation matrix to vector
  279. Parameters:
  280. | *[in]* **m** affine matrix or rot matrix
  281. | *[in]* **v** vector
  282. | *[out]* **dest** rotated vector
  283. .. c:function:: void glm_vec3_rotate_m3(mat3 m, vec3 v, vec3 dest)
  284. apply rotation matrix to vector
  285. Parameters:
  286. | *[in]* **m** affine matrix or rot matrix
  287. | *[in]* **v** vector
  288. | *[out]* **dest** rotated vector
  289. .. c:function:: void glm_vec3_proj(vec3 a, vec3 b, vec3 dest)
  290. project a vector onto b vector
  291. Parameters:
  292. | *[in]* **a** vector1
  293. | *[in]* **b** vector2
  294. | *[out]* **dest** projected vector
  295. .. c:function:: void glm_vec3_center(vec3 v1, vec3 v2, vec3 dest)
  296. find center point of two vector
  297. Parameters:
  298. | *[in]* **v1** vector1
  299. | *[in]* **v2** vector2
  300. | *[out]* **dest** center point
  301. .. c:function:: float glm_vec3_distance2(vec3 v1, vec3 v2)
  302. squared distance between two vectors
  303. Parameters:
  304. | *[in]* **v1** vector1
  305. | *[in]* **v2** vector2
  306. Returns:
  307. | squared distance (distance * distance)
  308. .. c:function:: float glm_vec3_distance(vec3 v1, vec3 v2)
  309. distance between two vectors
  310. Parameters:
  311. | *[in]* **v1** vector1
  312. | *[in]* **v2** vector2
  313. Returns:
  314. | distance
  315. .. c:function:: void glm_vec3_maxv(vec3 v1, vec3 v2, vec3 dest)
  316. max values of vectors
  317. Parameters:
  318. | *[in]* **v1** vector1
  319. | *[in]* **v2** vector2
  320. | *[out]* **dest** destination
  321. .. c:function:: void glm_vec3_minv(vec3 v1, vec3 v2, vec3 dest)
  322. min values of vectors
  323. Parameters:
  324. | *[in]* **v1** vector1
  325. | *[in]* **v2** vector2
  326. | *[out]* **dest** destination
  327. .. c:function:: void glm_vec3_ortho(vec3 v, vec3 dest)
  328. possible orthogonal/perpendicular vector
  329. References:
  330. * `On picking an orthogonal vector (and combing coconuts) <http://lolengine.net/blog/2013/09/21/picking-orthogonal-vector-combing-coconuts>`_
  331. Parameters:
  332. | *[in]* **v** vector
  333. | *[out]* **dest** orthogonal/perpendicular vector
  334. .. c:function:: void glm_vec3_clamp(vec3 v, float minVal, float maxVal)
  335. constrain a value to lie between two further values
  336. Parameters:
  337. | *[in, out]* **v** vector
  338. | *[in]* **minVal** minimum value
  339. | *[in]* **maxVal** maximum value
  340. .. c:function:: void glm_vec3_lerp(vec3 from, vec3 to, float t, vec3 dest)
  341. linear interpolation between two vector
  342. | formula: from + s * (to - from)
  343. Parameters:
  344. | *[in]* **from** from value
  345. | *[in]* **to** to value
  346. | *[in]* **t** interpolant (amount) clamped between 0 and 1
  347. | *[out]* **dest** destination
  348. .. c:function:: void glm_vec3_make(const float * __restrict src, vec3 dest)
  349. Create three dimensional vector from pointer
  350. .. note::: **@src** must contain at least 3 elements.
  351. Parameters:
  352. | *[in]* **src** pointer to an array of floats
  353. | *[out]* **dest** destination vector
  354. .. c:function:: void glm_vec3_faceforward(vec3 N, vec3 I, vec3 Nref, vec3 dest)
  355. A vector pointing in the same direction as another
  356. Parameters:
  357. | *[in]* **N** vector to orient
  358. | *[in]* **I** incident vector
  359. | *[in]* **Nref** reference vector
  360. | *[out]* **dest** destination: oriented vector, pointing away from the surface.
  361. .. c:function:: void glm_vec3_reflect(vec3 I, vec3 N, vec3 dest)
  362. Reflection vector using an incident ray and a surface normal
  363. Parameters:
  364. | *[in]* **I** incident vector
  365. | *[in]* **N** *❗️ normalized ❗️* normal vector
  366. | *[out]* **dest** destination: reflection result
  367. .. c:function:: bool glm_vec3_refract(vec3 I, vec3 N, float eta, vec3 dest)
  368. Computes refraction vector for an incident vector and a surface normal.
  369. Calculates the refraction vector based on Snell's law. If total internal reflection
  370. occurs (angle too great given eta), dest is set to zero and returns false.
  371. Otherwise, computes refraction vector, stores it in dest, and returns true.
  372. Parameters:
  373. | *[in]* **I** *❗️ normalized ❗️* incident vector
  374. | *[in]* **N** *❗️ normalized ❗️* normal vector
  375. | *[in]* **eta** ratio of indices of refraction (incident/transmitted)
  376. | *[out]* **dest** refraction vector if refraction occurs; zero vector otherwise
  377. Returns:
  378. returns true if refraction occurs; false if total internal reflection occurs.