Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | /** @file rs_matrix.rsh |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 18 | * \brief Matrix functions. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 19 | * |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 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 | * A few general notes: |
| 25 | * |
| 26 | * \li We use a zero-based index for rows and columns. E.g. the last element of |
| 27 | * a \ref rs_matrix4x4 is found at (3, 3). |
| 28 | * |
| 29 | * \li RenderScript uses column-based vectors. Transforming a vector is done by |
| 30 | * postmultiplying the vector, e.g. <em>(matrix * vector)</em>, as provided by |
| 31 | * \ref rsMatrixMultiply. |
| 32 | * |
| 33 | * \li To create a transformation matrix that performs two transformations at |
| 34 | * once, multiply the two source matrices, with the first transformation as the |
| 35 | * right argument. E.g. to create a transformation matrix that applies the |
| 36 | * transformation \e s1 followed by \e s2, call |
Jean-Luc Brouillet | 462e62c | 2014-12-12 13:42:24 -0800 | [diff] [blame^] | 37 | * <c>rsMatrixLoadMultiply(&combined, &s2, &s1)</c>. |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 38 | * This derives from <em>s2 * (s1 * v)</em>, which is <em>(s2 * s1) * v</em>. |
| 39 | * |
| 40 | * \li We have two style of functions to create transformation matrices: |
| 41 | * rsMatrixLoad<em>Transformation</em> and rsMatrix<em>Transformation</em>. The |
| 42 | * former style simply stores the transformation matrix in the first argument. |
| 43 | * The latter modifies a pre-existing transformation matrix so that the new |
| 44 | * transformation happens first. E.g. if you call \ref rsMatrixTranslate |
| 45 | * on a matrix that already does a scaling, the resulting matrix when applied |
| 46 | * to a vector will first do the translation then the scaling. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 47 | * |
| 48 | */ |
| 49 | |
| 50 | #ifndef __RS_MATRIX_RSH__ |
| 51 | #define __RS_MATRIX_RSH__ |
| 52 | |
| 53 | /** |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 54 | * Set an element of a matrix. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 55 | * |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 56 | * @param m The matrix that will be modified. |
| 57 | * @param col The zero-based column of the element to be set. |
| 58 | * @param row The zero-based row of the element to be set. |
| 59 | * @param v The value to set. |
| 60 | * |
| 61 | * \warning The order of the column and row parameters may be |
| 62 | * unexpected. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 63 | */ |
| 64 | _RS_RUNTIME void __attribute__((overloadable)) |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 65 | rsMatrixSet(rs_matrix4x4 *m, uint32_t col, uint32_t row, float v); |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 66 | /** |
| 67 | * \overload |
| 68 | */ |
| 69 | _RS_RUNTIME void __attribute__((overloadable)) |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 70 | rsMatrixSet(rs_matrix3x3 *m, uint32_t col, uint32_t row, float v); |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 71 | /** |
| 72 | * \overload |
| 73 | */ |
| 74 | _RS_RUNTIME void __attribute__((overloadable)) |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 75 | rsMatrixSet(rs_matrix2x2 *m, uint32_t col, uint32_t row, float v); |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 76 | |
| 77 | /** |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 78 | * Returns one element of a matrix. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 79 | * |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 80 | * @param m The matrix to extract the element from. |
| 81 | * @param col The zero-based column of the element to be extracted. |
| 82 | * @param row The zero-based row of the element to extracted. |
| 83 | * |
| 84 | * \warning The order of the column and row parameters may be |
| 85 | * unexpected. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 86 | * |
| 87 | * @return float |
| 88 | */ |
| 89 | _RS_RUNTIME float __attribute__((overloadable)) |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 90 | rsMatrixGet(const rs_matrix4x4 *m, uint32_t col, uint32_t row); |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 91 | /** |
| 92 | * \overload |
| 93 | */ |
| 94 | _RS_RUNTIME float __attribute__((overloadable)) |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 95 | rsMatrixGet(const rs_matrix3x3 *m, uint32_t col, uint32_t row); |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 96 | /** |
| 97 | * \overload |
| 98 | */ |
| 99 | _RS_RUNTIME float __attribute__((overloadable)) |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 100 | rsMatrixGet(const rs_matrix2x2 *m, uint32_t col, uint32_t row); |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * Set the elements of a matrix to the identity matrix. |
| 104 | * |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 105 | * @param m The matrix to set. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 106 | */ |
| 107 | extern void __attribute__((overloadable)) rsMatrixLoadIdentity(rs_matrix4x4 *m); |
| 108 | /** |
| 109 | * \overload |
| 110 | */ |
| 111 | extern void __attribute__((overloadable)) rsMatrixLoadIdentity(rs_matrix3x3 *m); |
| 112 | /** |
| 113 | * \overload |
| 114 | */ |
| 115 | extern void __attribute__((overloadable)) rsMatrixLoadIdentity(rs_matrix2x2 *m); |
| 116 | |
| 117 | /** |
| 118 | * Set the elements of a matrix from an array of floats. |
| 119 | * |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 120 | * The array of floats should be in row-major order, i.e. the element a |
| 121 | * <em>row 0, column 0</em> should be first, followed by the element at |
| 122 | * <em>row 0, column 1</em>, etc. |
| 123 | * |
| 124 | * @param m The matrix to set. |
| 125 | * @param v The array of values to set the matrix to. These arrays should be |
| 126 | * 4, 9, or 16 floats long, depending on the matrix size. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 127 | */ |
| 128 | extern void __attribute__((overloadable)) rsMatrixLoad(rs_matrix4x4 *m, const float *v); |
| 129 | /** |
| 130 | * \overload |
| 131 | */ |
| 132 | extern void __attribute__((overloadable)) rsMatrixLoad(rs_matrix3x3 *m, const float *v); |
| 133 | /** |
| 134 | * \overload |
| 135 | */ |
| 136 | extern void __attribute__((overloadable)) rsMatrixLoad(rs_matrix2x2 *m, const float *v); |
| 137 | /** |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 138 | * Set the elements of a matrix from another matrix. |
| 139 | * |
| 140 | * If the source matrix is smaller than the destination, the rest of the |
| 141 | * destination is filled with elements of the identity matrix. E.g. |
| 142 | * loading a rs_matrix2x2 into a rs_matrix4x4 will give: |
| 143 | * |
| 144 | * \htmlonly<table> |
| 145 | * <tr><td>m00</td><td>m01</td><td>0.0</td><td>0.0</td></tr> |
| 146 | * <tr><td>m10</td><td>m11</td><td>0.0</td><td>0.0</td></tr> |
| 147 | * <tr><td>0.0</td><td>0.0</td><td>1.0</td><td>0.0</td></tr> |
| 148 | * <tr><td>0.0</td><td>0.0</td><td>0.0</td><td>1.0</td></tr> |
| 149 | * </table>\endhtmlonly |
| 150 | * |
| 151 | * @param m The matrix to set. |
| 152 | * @param v The source matrix. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 153 | */ |
| 154 | extern void __attribute__((overloadable)) rsMatrixLoad(rs_matrix4x4 *m, const rs_matrix4x4 *v); |
| 155 | /** |
| 156 | * \overload |
| 157 | */ |
| 158 | extern void __attribute__((overloadable)) rsMatrixLoad(rs_matrix4x4 *m, const rs_matrix3x3 *v); |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 159 | /** |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 160 | * \overload |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 161 | */ |
| 162 | extern void __attribute__((overloadable)) rsMatrixLoad(rs_matrix4x4 *m, const rs_matrix2x2 *v); |
| 163 | /** |
| 164 | * \overload |
| 165 | */ |
| 166 | extern void __attribute__((overloadable)) rsMatrixLoad(rs_matrix3x3 *m, const rs_matrix3x3 *v); |
| 167 | /** |
| 168 | * \overload |
| 169 | */ |
| 170 | extern void __attribute__((overloadable)) rsMatrixLoad(rs_matrix2x2 *m, const rs_matrix2x2 *v); |
| 171 | |
| 172 | /** |
| 173 | * Load a rotation matrix. |
| 174 | * |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 175 | * This function creates a rotation matrix. The axis of rotation is the |
| 176 | * <em>(x, y, z)</em> vector. |
| 177 | * |
| 178 | * To rotate a vector, multiply the vector by the created matrix |
| 179 | * using \ref rsMatrixMultiply. |
| 180 | * |
| 181 | * See http://en.wikipedia.org/wiki/Rotation_matrix . |
| 182 | * |
| 183 | * @param m The matrix to set. |
| 184 | * @param rot How much rotation to do, in degrees. |
| 185 | * @param x The x component of the vector that is the axis of rotation. |
| 186 | * @param y The y component of the vector that is the axis of rotation. |
| 187 | * @param z The z component of the vector that is the axis of rotation. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 188 | */ |
| 189 | extern void __attribute__((overloadable)) |
| 190 | rsMatrixLoadRotate(rs_matrix4x4 *m, float rot, float x, float y, float z); |
| 191 | |
| 192 | /** |
| 193 | * Load a scale matrix. |
| 194 | * |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 195 | * This function creates a scaling matrix, where each component of a |
| 196 | * vector is multiplied by a number. This number can be negative. |
| 197 | * |
| 198 | * To scale a vector, multiply the vector by the created matrix |
| 199 | * using \ref rsMatrixMultiply. |
| 200 | * |
| 201 | * @param m The matrix to set. |
| 202 | * @param x The multiple to scale the x components by. |
| 203 | * @param y The multiple to scale the y components by. |
| 204 | * @param z The multiple to scale the z components by. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 205 | */ |
| 206 | extern void __attribute__((overloadable)) |
| 207 | rsMatrixLoadScale(rs_matrix4x4 *m, float x, float y, float z); |
| 208 | |
| 209 | /** |
| 210 | * Load a translation matrix. |
| 211 | * |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 212 | * This function creates a translation matrix, where a |
| 213 | * number is added to each element of a vector. |
| 214 | * |
| 215 | * To translate a vector, multiply the vector by the created matrix |
| 216 | * using \ref rsMatrixMultiply. |
| 217 | * |
| 218 | * @param m The matrix to set. |
| 219 | * @param x The number to add to each x component. |
| 220 | * @param y The number to add to each y component. |
| 221 | * @param z The number to add to each z component. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 222 | */ |
| 223 | extern void __attribute__((overloadable)) |
| 224 | rsMatrixLoadTranslate(rs_matrix4x4 *m, float x, float y, float z); |
| 225 | |
| 226 | /** |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 227 | * Multiply two matrices. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 228 | * |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 229 | * Sets \e m to the matrix product of <em>lhs * rhs</em>. |
| 230 | * |
| 231 | * To combine two 4x4 transformaton matrices, multiply the second transformation matrix |
| 232 | * by the first transformation matrix. E.g. to create a transformation matrix that applies |
| 233 | * the transformation \e s1 followed by \e s2, call |
Jean-Luc Brouillet | 462e62c | 2014-12-12 13:42:24 -0800 | [diff] [blame^] | 234 | * <c>rsMatrixLoadMultiply(&combined, &s2, &s1)</c>. |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 235 | * |
| 236 | * \warning Prior to version 21, storing the result back into right matrix is not supported and |
| 237 | * will result in undefined behavior. Use rsMatrixMulitply instead. E.g. instead of doing |
| 238 | * rsMatrixLoadMultiply (&m2r, &m2r, &m2l), use rsMatrixMultiply (&m2r, &m2l). |
| 239 | * rsMatrixLoadMultiply (&m2l, &m2r, &m2l) works as expected. |
| 240 | * |
| 241 | * @param m The matrix to set. |
| 242 | * @param lhs The left matrix of the product. |
| 243 | * @param rhs The right matrix of the product. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 244 | */ |
| 245 | extern void __attribute__((overloadable)) |
| 246 | rsMatrixLoadMultiply(rs_matrix4x4 *m, const rs_matrix4x4 *lhs, const rs_matrix4x4 *rhs); |
| 247 | /** |
| 248 | * \overload |
| 249 | */ |
| 250 | extern void __attribute__((overloadable)) |
| 251 | rsMatrixLoadMultiply(rs_matrix3x3 *m, const rs_matrix3x3 *lhs, const rs_matrix3x3 *rhs); |
| 252 | /** |
| 253 | * \overload |
| 254 | */ |
| 255 | extern void __attribute__((overloadable)) |
| 256 | rsMatrixLoadMultiply(rs_matrix2x2 *m, const rs_matrix2x2 *lhs, const rs_matrix2x2 *rhs); |
| 257 | |
| 258 | /** |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 259 | * Multiply a matrix into another one. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 260 | * |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 261 | * Sets \e m to the matrix product <em>m * rhs</em>. |
| 262 | * |
| 263 | * When combining two 4x4 transformation matrices using this function, the resulting |
| 264 | * matrix will correspond to performing the \e rhs transformation first followed by |
| 265 | * the original \e m transformation. |
| 266 | * |
| 267 | * @param m The left matrix of the product and the matrix to be set. |
| 268 | * @param rhs The right matrix of the product. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 269 | */ |
| 270 | extern void __attribute__((overloadable)) |
| 271 | rsMatrixMultiply(rs_matrix4x4 *m, const rs_matrix4x4 *rhs); |
| 272 | /** |
| 273 | * \overload |
| 274 | */ |
| 275 | extern void __attribute__((overloadable)) |
| 276 | rsMatrixMultiply(rs_matrix3x3 *m, const rs_matrix3x3 *rhs); |
| 277 | /** |
| 278 | * \overload |
| 279 | */ |
| 280 | extern void __attribute__((overloadable)) |
| 281 | rsMatrixMultiply(rs_matrix2x2 *m, const rs_matrix2x2 *rhs); |
| 282 | |
| 283 | /** |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 284 | * Multiply the matrix \e m with a rotation matrix. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 285 | * |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 286 | * This function modifies a transformation matrix to first do a rotation. |
| 287 | * The axis of rotation is the <em>(x, y, z)</em> vector. |
| 288 | * |
| 289 | * To apply this combined transformation to a vector, multiply |
| 290 | * the vector by the created matrix using \ref rsMatrixMultiply. |
| 291 | * |
| 292 | * @param m The matrix to modify. |
| 293 | * @param rot How much rotation to do, in degrees. |
| 294 | * @param x The x component of the vector that is the axis of rotation. |
| 295 | * @param y The y component of the vector that is the axis of rotation. |
| 296 | * @param z The z component of the vector that is the axis of rotation. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 297 | */ |
| 298 | extern void __attribute__((overloadable)) |
| 299 | rsMatrixRotate(rs_matrix4x4 *m, float rot, float x, float y, float z); |
| 300 | |
| 301 | /** |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 302 | * Multiply the matrix \e m with a scaling matrix. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 303 | * |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 304 | * This function modifies a transformation matrix to first do a scaling. |
| 305 | * When scaling, each component of a vector is multiplied by a number. |
| 306 | * This number can be negative. |
| 307 | * |
| 308 | * To apply this combined transformation to a vector, multiply |
| 309 | * the vector by the created matrix using \ref rsMatrixMultiply. |
| 310 | * |
| 311 | * @param m The matrix to modify. |
| 312 | * @param x The multiple to scale the x components by. |
| 313 | * @param y The multiple to scale the y components by. |
| 314 | * @param z The multiple to scale the z components by. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 315 | */ |
| 316 | extern void __attribute__((overloadable)) |
| 317 | rsMatrixScale(rs_matrix4x4 *m, float x, float y, float z); |
| 318 | |
| 319 | /** |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 320 | * Multiply the matrix \e m with a translation matrix. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 321 | * |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 322 | * This function modifies a transformation matrix to first |
| 323 | * do a translation. When translating, a number is added |
| 324 | * to each component of a vector. |
| 325 | * |
| 326 | * To apply this combined transformation to a vector, multiply |
| 327 | * the vector by the created matrix using \ref rsMatrixMultiply. |
| 328 | * |
| 329 | * @param m The matrix to modify. |
| 330 | * @param x The number to add to each x component. |
| 331 | * @param y The number to add to each y component. |
| 332 | * @param z The number to add to each z component. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 333 | */ |
| 334 | extern void __attribute__((overloadable)) |
| 335 | rsMatrixTranslate(rs_matrix4x4 *m, float x, float y, float z); |
| 336 | |
| 337 | /** |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 338 | * Load an orthographic projection matrix. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 339 | * |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 340 | * Constructs an orthographic projection matrix, transforming the box |
| 341 | * identified by the six clipping planes <em>left, right, bottom, top, |
| 342 | * near, far</em> into a unit cube with a corner at |
| 343 | * <em>(-1, -1, -1)</em> and the opposite at <em>(1, 1, 1)</em>. |
| 344 | * |
| 345 | * To apply this projection to a vector, multiply the vector by the |
| 346 | * created matrix using \ref rsMatrixMultiply. |
| 347 | * |
| 348 | * See https://en.wikipedia.org/wiki/Orthographic_projection . |
| 349 | * |
| 350 | * @param m The matrix to set. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 351 | * @param left |
| 352 | * @param right |
| 353 | * @param bottom |
| 354 | * @param top |
| 355 | * @param near |
| 356 | * @param far |
| 357 | */ |
| 358 | extern void __attribute__((overloadable)) |
| 359 | rsMatrixLoadOrtho(rs_matrix4x4 *m, float left, float right, float bottom, float top, float near, float far); |
| 360 | |
| 361 | /** |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 362 | * Load a frustum projection matrix. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 363 | * |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 364 | * Constructs a frustum projection matrix, transforming the box |
| 365 | * identified by the six clipping planes <em>left, right, bottom, top, |
| 366 | * near, far</em>. |
| 367 | * |
| 368 | * To apply this projection to a vector, multiply the vector by the |
| 369 | * created matrix using \ref rsMatrixMultiply. |
| 370 | * |
| 371 | * @param m The matrix to set. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 372 | * @param left |
| 373 | * @param right |
| 374 | * @param bottom |
| 375 | * @param top |
| 376 | * @param near |
| 377 | * @param far |
| 378 | */ |
| 379 | extern void __attribute__((overloadable)) |
| 380 | rsMatrixLoadFrustum(rs_matrix4x4 *m, float left, float right, float bottom, float top, float near, float far); |
| 381 | |
| 382 | /** |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 383 | * Load a perspective projection matrix. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 384 | * |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 385 | * Constructs a perspective projection matrix, assuming a symmetrical field of view. |
| 386 | * |
| 387 | * To apply this projection to a vector, multiply the vector by the |
| 388 | * created matrix using \ref rsMatrixMultiply. |
| 389 | * |
| 390 | * @param m The matrix to set. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 391 | * @param fovy Field of view, in degrees along the Y axis. |
| 392 | * @param aspect Ratio of x / y. |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 393 | * @param near The near clipping plane. |
| 394 | * @param far The far clipping plane. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 395 | */ |
| 396 | extern void __attribute__((overloadable)) |
| 397 | rsMatrixLoadPerspective(rs_matrix4x4* m, float fovy, float aspect, float near, float far); |
| 398 | |
| 399 | #if !defined(RS_VERSION) || (RS_VERSION < 14) |
| 400 | /** |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 401 | * Multiply a vector by a matrix. |
| 402 | * |
| 403 | * Returns the post-multiplication of the vector by the matrix, ie. <em>m * in</em>. |
| 404 | * |
| 405 | * When multiplying a \e float3 to a \e rs_matrix4x4, the vector is expanded with (1). |
| 406 | * |
| 407 | * When multiplying a \e float2 to a \e rs_matrix4x4, the vector is expanded with (0, 1). |
| 408 | * |
| 409 | * When multiplying a \e float2 to a \e rs_matrix3x3, the vector is expanded with (0). |
| 410 | * |
| 411 | * This function is available in API version 10-13. Starting with API 14, |
| 412 | * the function takes a const matrix as the first argument. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 413 | */ |
| 414 | _RS_RUNTIME float4 __attribute__((overloadable)) |
| 415 | rsMatrixMultiply(rs_matrix4x4 *m, float4 in); |
| 416 | |
| 417 | /** |
| 418 | * \overload |
| 419 | */ |
| 420 | _RS_RUNTIME float4 __attribute__((overloadable)) |
| 421 | rsMatrixMultiply(rs_matrix4x4 *m, float3 in); |
| 422 | |
| 423 | /** |
| 424 | * \overload |
| 425 | */ |
| 426 | _RS_RUNTIME float4 __attribute__((overloadable)) |
| 427 | rsMatrixMultiply(rs_matrix4x4 *m, float2 in); |
| 428 | |
| 429 | /** |
| 430 | * \overload |
| 431 | */ |
| 432 | _RS_RUNTIME float3 __attribute__((overloadable)) |
| 433 | rsMatrixMultiply(rs_matrix3x3 *m, float3 in); |
| 434 | |
| 435 | /** |
| 436 | * \overload |
| 437 | */ |
| 438 | _RS_RUNTIME float3 __attribute__((overloadable)) |
| 439 | rsMatrixMultiply(rs_matrix3x3 *m, float2 in); |
| 440 | |
| 441 | /** |
| 442 | * \overload |
| 443 | */ |
| 444 | _RS_RUNTIME float2 __attribute__((overloadable)) |
| 445 | rsMatrixMultiply(rs_matrix2x2 *m, float2 in); |
| 446 | #else |
| 447 | /** |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 448 | * Multiply a vector by a matrix. |
| 449 | * |
| 450 | * Returns the post-multiplication of the vector of the matrix, i.e. <em>m * in</em>. |
| 451 | * |
| 452 | * When multiplying a \e float3 to a \e rs_matrix4x4, the vector is expanded with (1). |
| 453 | * |
| 454 | * When multiplying a \e float2 to a \e rs_matrix4x4, the vector is expanded with (0, 1). |
| 455 | * |
| 456 | * When multiplying a \e float2 to a \e rs_matrix3x3, the vector is expanded with (0). |
| 457 | * |
| 458 | * This function is available starting with API version 14. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 459 | */ |
| 460 | _RS_RUNTIME float4 __attribute__((overloadable)) |
| 461 | rsMatrixMultiply(const rs_matrix4x4 *m, float4 in); |
| 462 | |
| 463 | /** |
| 464 | * \overload |
| 465 | */ |
| 466 | _RS_RUNTIME float4 __attribute__((overloadable)) |
| 467 | rsMatrixMultiply(const rs_matrix4x4 *m, float3 in); |
| 468 | |
| 469 | /** |
| 470 | * \overload |
| 471 | */ |
| 472 | _RS_RUNTIME float4 __attribute__((overloadable)) |
| 473 | rsMatrixMultiply(const rs_matrix4x4 *m, float2 in); |
| 474 | |
| 475 | /** |
| 476 | * \overload |
| 477 | */ |
| 478 | _RS_RUNTIME float3 __attribute__((overloadable)) |
| 479 | rsMatrixMultiply(const rs_matrix3x3 *m, float3 in); |
| 480 | |
| 481 | /** |
| 482 | * \overload |
| 483 | */ |
| 484 | _RS_RUNTIME float3 __attribute__((overloadable)) |
| 485 | rsMatrixMultiply(const rs_matrix3x3 *m, float2 in); |
| 486 | |
| 487 | /** |
| 488 | * \overload |
| 489 | */ |
| 490 | _RS_RUNTIME float2 __attribute__((overloadable)) |
| 491 | rsMatrixMultiply(const rs_matrix2x2 *m, float2 in); |
| 492 | #endif |
| 493 | |
| 494 | |
| 495 | /** |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 496 | * Inverts a matrix in place. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 497 | * |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 498 | * Returns true if the matrix was successfully inverted. |
| 499 | * |
| 500 | * @param m The matrix to invert. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 501 | */ |
| 502 | extern bool __attribute__((overloadable)) rsMatrixInverse(rs_matrix4x4 *m); |
| 503 | |
| 504 | /** |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 505 | * Inverts and transpose a matrix in place. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 506 | * |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 507 | * The matrix is first inverted then transposed. |
| 508 | * Returns true if the matrix was successfully inverted. |
| 509 | * |
| 510 | * @param m The matrix to modify. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 511 | */ |
| 512 | extern bool __attribute__((overloadable)) rsMatrixInverseTranspose(rs_matrix4x4 *m); |
| 513 | |
| 514 | /** |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 515 | * Transpose the matrix m in place. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 516 | * |
Jean-Luc Brouillet | 1bb2eed | 2014-09-05 17:44:48 -0700 | [diff] [blame] | 517 | * @param m The matrix to transpose. |
Jason Sams | 044e2ee | 2011-08-08 16:52:30 -0700 | [diff] [blame] | 518 | */ |
| 519 | extern void __attribute__((overloadable)) rsMatrixTranspose(rs_matrix4x4 *m); |
| 520 | /** |
| 521 | * \overload |
| 522 | */ |
| 523 | extern void __attribute__((overloadable)) rsMatrixTranspose(rs_matrix3x3 *m); |
| 524 | /** |
| 525 | * \overload |
| 526 | */ |
| 527 | extern void __attribute__((overloadable)) rsMatrixTranspose(rs_matrix2x2 *m); |
| 528 | |
| 529 | |
| 530 | #endif |