Jean-Luc Brouillet | c5184e2 | 2015-03-13 13:51:24 -0700 | [diff] [blame^] | 1 | # |
| 2 | # Copyright (C) 2015 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | |
| 17 | header: |
| 18 | summary: Matrix functions |
| 19 | description: |
| 20 | These functions let you manipulate square matrices of rank 2x2, 3x3, and 4x4. |
| 21 | They are particularly useful for graphical transformations and are |
| 22 | compatible with OpenGL. |
| 23 | |
| 24 | We use a zero-based index for rows and columns. E.g. the last element of |
| 25 | a @rs_matrix4x4 is found at (3, 3). |
| 26 | |
| 27 | RenderScript uses column-major matrices and column-based vectors. |
| 28 | Transforming a vector is done by postmultiplying the vector, |
| 29 | e.g. <code>(matrix * vector)</code>, as provided by @rsMatrixMultiply(). |
| 30 | |
| 31 | To create a transformation matrix that performs two transformations at |
| 32 | once, multiply the two source matrices, with the first transformation as the |
| 33 | right argument. E.g. to create a transformation matrix that applies the |
| 34 | transformation s1 followed by s2, call <code>rsMatrixLoadMultiply(&combined, &s2, &s1)</code>. |
| 35 | This derives from <code>s2 * (s1 * v)</code>, which is <code>(s2 * s1) * v</code>. |
| 36 | |
| 37 | We have two style of functions to create transformation matrices: |
| 38 | rsMatrixLoad<i>Transformation</i> and rsMatrix<i>Transformation</i>. The |
| 39 | former style simply stores the transformation matrix in the first argument. |
| 40 | The latter modifies a pre-existing transformation matrix so that the new |
| 41 | transformation happens first. E.g. if you call @rsMatrixTranslate() |
| 42 | on a matrix that already does a scaling, the resulting matrix when applied |
| 43 | to a vector will first do the translation then the scaling. |
| 44 | end: |
| 45 | |
| 46 | function: rsMatrixGet |
| 47 | t: rs_matrix4x4, rs_matrix3x3, rs_matrix2x2 |
| 48 | ret: float |
| 49 | arg: const #1* m, "The matrix to extract the element from." |
| 50 | arg: uint32_t col, "The zero-based column of the element to be extracted." |
| 51 | arg: uint32_t row, "The zero-based row of the element to extracted." |
| 52 | summary: Get one element |
| 53 | description: |
| 54 | Returns one element of a matrix. |
| 55 | |
| 56 | <b>Warning:</b> The order of the column and row parameters may be unexpected. |
| 57 | test: none |
| 58 | end: |
| 59 | |
| 60 | function: rsMatrixInverse |
| 61 | ret: bool |
| 62 | arg: rs_matrix4x4* m, "The matrix to invert." |
| 63 | summary: Inverts a matrix in place |
| 64 | description: |
| 65 | Returns true if the matrix was successfully inverted. |
| 66 | test: none |
| 67 | end: |
| 68 | |
| 69 | |
| 70 | function: rsMatrixInverseTranspose |
| 71 | ret: bool |
| 72 | arg: rs_matrix4x4* m, "The matrix to modify." |
| 73 | summary: Inverts and transpose a matrix in place |
| 74 | description: |
| 75 | The matrix is first inverted then transposed. |
| 76 | Returns true if the matrix was successfully inverted. |
| 77 | test: none |
| 78 | end: |
| 79 | |
| 80 | |
| 81 | function: rsMatrixLoad |
| 82 | t: rs_matrix4x4, rs_matrix3x3, rs_matrix2x2 |
| 83 | ret: void |
| 84 | arg: #1* destination, "The matrix to set." |
| 85 | arg: const float* array, "The array of values to set the matrix to. These arrays should be 4, 9, or 16 floats long, depending on the matrix size." |
| 86 | summary: Load or copy a matrix |
| 87 | description: |
| 88 | Set the elements of a matrix from an array of floats or from another matrix. |
| 89 | |
| 90 | If loading from an array, the floats should be in row-major order, i.e. the element a |
| 91 | <code>row 0, column 0</code> should be first, followed by the element at |
| 92 | <code>row 0, column 1</code>, etc. |
| 93 | |
| 94 | If loading from a matrix and the source is smaller than the destination, the rest of the |
| 95 | destination is filled with elements of the identity matrix. E.g. |
| 96 | loading a rs_matrix2x2 into a rs_matrix4x4 will give: |
| 97 | <table style="max-width:300px"> |
| 98 | <tr><td>m00</td> <td>m01</td> <td>0.0</td> <td>0.0</td></tr> |
| 99 | <tr><td>m10</td> <td>m11</td> <td>0.0</td> <td>0.0</td></tr> |
| 100 | <tr><td>0.0</td> <td>0.0</td> <td>1.0</td> <td>0.0</td></tr> |
| 101 | <tr><td>0.0</td> <td>0.0</td> <td>0.0</td> <td>1.0</td></tr> |
| 102 | </table> |
| 103 | test: none |
| 104 | end: |
| 105 | |
| 106 | function: rsMatrixLoad |
| 107 | t: rs_matrix4x4, rs_matrix3x3, rs_matrix2x2 |
| 108 | ret: void |
| 109 | arg: #1* destination |
| 110 | arg: const #1* source, "The source matrix." |
| 111 | test: none |
| 112 | end: |
| 113 | |
| 114 | function: rsMatrixLoad |
| 115 | t: rs_matrix3x3, rs_matrix2x2 |
| 116 | ret: void |
| 117 | arg: rs_matrix4x4* destination |
| 118 | arg: const #1* source |
| 119 | test: none |
| 120 | end: |
| 121 | |
| 122 | function: rsMatrixLoadFrustum |
| 123 | ret: void |
| 124 | arg: rs_matrix4x4* m, "The matrix to set." |
| 125 | arg: float left |
| 126 | arg: float right |
| 127 | arg: float bottom |
| 128 | arg: float top |
| 129 | arg: float near |
| 130 | arg: float far |
| 131 | summary: Load a frustum projection matrix |
| 132 | description: |
| 133 | Constructs a frustum projection matrix, transforming the box |
| 134 | identified by the six clipping planes <code>left, right, bottom, top, |
| 135 | near, far</code>. |
| 136 | |
| 137 | To apply this projection to a vector, multiply the vector by the |
| 138 | created matrix using @rsMatrixMultiply(). |
| 139 | test: none |
| 140 | end: |
| 141 | |
| 142 | function: rsMatrixLoadIdentity |
| 143 | t: rs_matrix4x4, rs_matrix3x3, rs_matrix2x2 |
| 144 | ret: void |
| 145 | arg: #1* m, "The matrix to set." |
| 146 | summary: Load identity matrix |
| 147 | description: |
| 148 | Set the elements of a matrix to the identity matrix. |
| 149 | test: none |
| 150 | end: |
| 151 | |
| 152 | function: rsMatrixLoadMultiply |
| 153 | t: rs_matrix4x4, rs_matrix3x3, rs_matrix2x2 |
| 154 | ret: void |
| 155 | arg: #1* m, "The matrix to set." |
| 156 | arg: const #1* lhs, "The left matrix of the product." |
| 157 | arg: const #1* rhs, "The right matrix of the product." |
| 158 | summary: Multiply two matrices |
| 159 | description: |
| 160 | Sets m to the matrix product of <code>lhs * rhs</code>. |
| 161 | |
| 162 | To combine two 4x4 transformaton matrices, multiply the second transformation matrix |
| 163 | by the first transformation matrix. E.g. to create a transformation matrix that applies |
| 164 | the transformation s1 followed by s2, call |
| 165 | <code>rsMatrixLoadMultiply(&combined, &s2, &s1)</code>. |
| 166 | |
| 167 | <b>Warning:</b> Prior to version 21, storing the result back into right matrix is not supported and |
| 168 | will result in undefined behavior. Use rsMatrixMulitply instead. E.g. instead of doing |
| 169 | rsMatrixLoadMultiply (&m2r, &m2r, &m2l), use rsMatrixMultiply (&m2r, &m2l). |
| 170 | rsMatrixLoadMultiply (&m2l, &m2r, &m2l) works as expected. |
| 171 | test: none |
| 172 | end: |
| 173 | |
| 174 | function: rsMatrixLoadOrtho |
| 175 | ret: void |
| 176 | arg: rs_matrix4x4* m, "The matrix to set." |
| 177 | arg: float left |
| 178 | arg: float right |
| 179 | arg: float bottom |
| 180 | arg: float top |
| 181 | arg: float near |
| 182 | arg: float far |
| 183 | summary: Load an orthographic projection matrix |
| 184 | description: |
| 185 | Constructs an orthographic projection matrix, transforming the box |
| 186 | identified by the six clipping planes <code>left, right, bottom, top, |
| 187 | near, far</code> into a unit cube with a corner at |
| 188 | <code>(-1, -1, -1)</code> and the opposite at <code>(1, 1, 1)</code>. |
| 189 | |
| 190 | To apply this projection to a vector, multiply the vector by the |
| 191 | created matrix using @rsMatrixMultiply(). |
| 192 | |
| 193 | See https://en.wikipedia.org/wiki/Orthographic_projection . |
| 194 | test: none |
| 195 | end: |
| 196 | |
| 197 | function: rsMatrixLoadPerspective |
| 198 | ret: void |
| 199 | arg: rs_matrix4x4* m, "The matrix to set." |
| 200 | arg: float fovy, "Field of view, in degrees along the Y axis." |
| 201 | arg: float aspect, "Ratio of x / y." |
| 202 | arg: float near, "The near clipping plane." |
| 203 | arg: float far, "The far clipping plane." |
| 204 | summary: Load a perspective projection matrix |
| 205 | description: |
| 206 | Constructs a perspective projection matrix, assuming a symmetrical field of view. |
| 207 | |
| 208 | To apply this projection to a vector, multiply the vector by the |
| 209 | created matrix using @rsMatrixMultiply(). |
| 210 | test: none |
| 211 | end: |
| 212 | |
| 213 | function: rsMatrixLoadRotate |
| 214 | ret: void |
| 215 | arg: rs_matrix4x4* m, "The matrix to set." |
| 216 | arg: float rot, "How much rotation to do, in degrees." |
| 217 | arg: float x, "The x component of the vector that is the axis of rotation." |
| 218 | arg: float y, "The y component of the vector that is the axis of rotation." |
| 219 | arg: float z, "The z component of the vector that is the axis of rotation." |
| 220 | summary: Load a rotation matrix |
| 221 | description: |
| 222 | This function creates a rotation matrix. The axis of rotation is the |
| 223 | <code>(x, y, z)</code> vector. |
| 224 | |
| 225 | To rotate a vector, multiply the vector by the created matrix |
| 226 | using @rsMatrixMultiply(). |
| 227 | |
| 228 | See http://en.wikipedia.org/wiki/Rotation_matrix . |
| 229 | test: none |
| 230 | end: |
| 231 | |
| 232 | function: rsMatrixLoadScale |
| 233 | ret: void |
| 234 | arg: rs_matrix4x4* m, "The matrix to set." |
| 235 | arg: float x, "The multiple to scale the x components by." |
| 236 | arg: float y, "The multiple to scale the y components by." |
| 237 | arg: float z, "The multiple to scale the z components by." |
| 238 | summary: Load a scaling matrix |
| 239 | description: |
| 240 | This function creates a scaling matrix, where each component of a |
| 241 | vector is multiplied by a number. This number can be negative. |
| 242 | |
| 243 | To scale a vector, multiply the vector by the created matrix |
| 244 | using @rsMatrixMultiply(). |
| 245 | test: none |
| 246 | end: |
| 247 | |
| 248 | function: rsMatrixLoadTranslate |
| 249 | ret: void |
| 250 | arg: rs_matrix4x4* m, "The matrix to set." |
| 251 | arg: float x, "The number to add to each x component." |
| 252 | arg: float y, "The number to add to each y component." |
| 253 | arg: float z, "The number to add to each z component." |
| 254 | summary: Load a translation matrix |
| 255 | description: |
| 256 | This function creates a translation matrix, where a |
| 257 | number is added to each element of a vector. |
| 258 | |
| 259 | To translate a vector, multiply the vector by the created matrix |
| 260 | using @rsMatrixMultiply(). |
| 261 | test: none |
| 262 | end: |
| 263 | |
| 264 | function: rsMatrixMultiply |
| 265 | t: rs_matrix4x4, rs_matrix3x3, rs_matrix2x2 |
| 266 | ret: void |
| 267 | arg: #1* m, "The left matrix of the product and the matrix to be set." |
| 268 | arg: const #1* rhs, "The right matrix of the product." |
| 269 | summary: Multiply a matrix by a vector or another matrix |
| 270 | description: |
| 271 | For the matrix by matrix variant, sets m to the matrix product <code>m * rhs</code>. |
| 272 | |
| 273 | When combining two 4x4 transformation matrices using this function, the resulting |
| 274 | matrix will correspond to performing the rhs transformation first followed by |
| 275 | the original m transformation. |
| 276 | |
| 277 | For the matrix by vector variant, returns the post-multiplication of the vector |
| 278 | by the matrix, ie. <code>m * in</code>. |
| 279 | |
| 280 | When multiplying a float3 to a @rs_matrix4x4, the vector is expanded with (1). |
| 281 | |
| 282 | When multiplying a float2 to a @rs_matrix4x4, the vector is expanded with (0, 1). |
| 283 | |
| 284 | When multiplying a float2 to a @rs_matrix3x3, the vector is expanded with (0). |
| 285 | |
| 286 | Starting with API 14, this function takes a const matrix as the first argument. |
| 287 | test: none |
| 288 | end: |
| 289 | |
| 290 | function: rsMatrixMultiply |
| 291 | version: 9 13 |
| 292 | ret: float4 |
| 293 | arg: rs_matrix4x4* m |
| 294 | arg: float4 in |
| 295 | test: none |
| 296 | end: |
| 297 | |
| 298 | function: rsMatrixMultiply |
| 299 | version: 9 13 |
| 300 | ret: float4 |
| 301 | arg: rs_matrix4x4* m |
| 302 | arg: float3 in |
| 303 | test: none |
| 304 | end: |
| 305 | |
| 306 | function: rsMatrixMultiply |
| 307 | version: 9 13 |
| 308 | ret: float4 |
| 309 | arg: rs_matrix4x4* m |
| 310 | arg: float2 in |
| 311 | test: none |
| 312 | end: |
| 313 | |
| 314 | function: rsMatrixMultiply |
| 315 | version: 9 13 |
| 316 | ret: float3 |
| 317 | arg: rs_matrix3x3* m |
| 318 | arg: float3 in |
| 319 | test: none |
| 320 | end: |
| 321 | |
| 322 | function: rsMatrixMultiply |
| 323 | version: 9 13 |
| 324 | ret: float3 |
| 325 | arg: rs_matrix3x3* m |
| 326 | arg: float2 in |
| 327 | test: none |
| 328 | end: |
| 329 | |
| 330 | function: rsMatrixMultiply |
| 331 | version: 9 13 |
| 332 | ret: float2 |
| 333 | arg: rs_matrix2x2* m |
| 334 | arg: float2 in |
| 335 | test: none |
| 336 | end: |
| 337 | |
| 338 | function: rsMatrixMultiply |
| 339 | version: 14 |
| 340 | ret: float4 |
| 341 | arg: const rs_matrix4x4* m |
| 342 | arg: float4 in |
| 343 | test: none |
| 344 | end: |
| 345 | |
| 346 | function: rsMatrixMultiply |
| 347 | version: 14 |
| 348 | ret: float4 |
| 349 | arg: const rs_matrix4x4* m |
| 350 | arg: float3 in |
| 351 | test: none |
| 352 | end: |
| 353 | |
| 354 | function: rsMatrixMultiply |
| 355 | version: 14 |
| 356 | ret: float4 |
| 357 | arg: const rs_matrix4x4* m |
| 358 | arg: float2 in |
| 359 | test: none |
| 360 | end: |
| 361 | |
| 362 | function: rsMatrixMultiply |
| 363 | version: 14 |
| 364 | ret: float3 |
| 365 | arg: const rs_matrix3x3* m |
| 366 | arg: float3 in |
| 367 | test: none |
| 368 | end: |
| 369 | |
| 370 | function: rsMatrixMultiply |
| 371 | version: 14 |
| 372 | ret: float3 |
| 373 | arg: const rs_matrix3x3* m |
| 374 | arg: float2 in |
| 375 | test: none |
| 376 | end: |
| 377 | |
| 378 | function: rsMatrixMultiply |
| 379 | version: 14 |
| 380 | ret: float2 |
| 381 | arg: const rs_matrix2x2* m |
| 382 | arg: float2 in |
| 383 | test: none |
| 384 | end: |
| 385 | |
| 386 | function: rsMatrixRotate |
| 387 | ret: void |
| 388 | arg: rs_matrix4x4* m, "The matrix to modify." |
| 389 | arg: float rot, "How much rotation to do, in degrees." |
| 390 | arg: float x, "The x component of the vector that is the axis of rotation." |
| 391 | arg: float y, "The y component of the vector that is the axis of rotation." |
| 392 | arg: float z, "The z component of the vector that is the axis of rotation." |
| 393 | summary: Apply a rotation to a transformation matrix |
| 394 | description: |
| 395 | Multiply the matrix m with a rotation matrix. |
| 396 | |
| 397 | This function modifies a transformation matrix to first do a rotation. |
| 398 | The axis of rotation is the <code>(x, y, z)</code> vector. |
| 399 | |
| 400 | To apply this combined transformation to a vector, multiply |
| 401 | the vector by the created matrix using @rsMatrixMultiply(). |
| 402 | test: none |
| 403 | end: |
| 404 | |
| 405 | function: rsMatrixScale |
| 406 | ret: void |
| 407 | arg: rs_matrix4x4* m, "The matrix to modify." |
| 408 | arg: float x, "The multiple to scale the x components by." |
| 409 | arg: float y, "The multiple to scale the y components by." |
| 410 | arg: float z, "The multiple to scale the z components by." |
| 411 | summary: Apply a scaling to a transformation matrix |
| 412 | description: |
| 413 | Multiply the matrix m with a scaling matrix. |
| 414 | |
| 415 | This function modifies a transformation matrix to first do a scaling. |
| 416 | When scaling, each component of a vector is multiplied by a number. |
| 417 | This number can be negative. |
| 418 | |
| 419 | To apply this combined transformation to a vector, multiply |
| 420 | the vector by the created matrix using @rsMatrixMultiply(). |
| 421 | test: none |
| 422 | end: |
| 423 | |
| 424 | function: rsMatrixSet |
| 425 | t: rs_matrix4x4, rs_matrix3x3, rs_matrix2x2 |
| 426 | ret: void |
| 427 | arg: #1* m, "The matrix that will be modified." |
| 428 | arg: uint32_t col, "The zero-based column of the element to be set." |
| 429 | arg: uint32_t row, "The zero-based row of the element to be set." |
| 430 | arg: float v, "The value to set." |
| 431 | summary: Set one element |
| 432 | description: |
| 433 | Set an element of a matrix. |
| 434 | |
| 435 | <b>Warning:</b> The order of the column and row parameters may be unexpected. |
| 436 | test: none |
| 437 | end: |
| 438 | |
| 439 | function: rsMatrixTranslate |
| 440 | ret: void |
| 441 | arg: rs_matrix4x4* m, "The matrix to modify." |
| 442 | arg: float x, "The number to add to each x component." |
| 443 | arg: float y, "The number to add to each y component." |
| 444 | arg: float z, "The number to add to each z component." |
| 445 | summary: Apply a translation to a transformation matrix |
| 446 | description: |
| 447 | Multiply the matrix m with a translation matrix. |
| 448 | |
| 449 | This function modifies a transformation matrix to first |
| 450 | do a translation. When translating, a number is added |
| 451 | to each component of a vector. |
| 452 | |
| 453 | To apply this combined transformation to a vector, multiply |
| 454 | the vector by the created matrix using @rsMatrixMultiply(). |
| 455 | test: none |
| 456 | end: |
| 457 | |
| 458 | function: rsMatrixTranspose |
| 459 | t: rs_matrix4x4*, rs_matrix3x3*, rs_matrix2x2* |
| 460 | ret: void |
| 461 | arg: #1 m, "The matrix to transpose." |
| 462 | summary: Transpose a matrix place |
| 463 | description: |
| 464 | Transpose the matrix m in place. |
| 465 | test: none |
| 466 | end: |