3dphotoshoot

annotate libs/vmath/matrix.inl @ 10:c71c477521ca

converting to GLES2 and C++
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 31 May 2015 00:40:26 +0300
parents
children
rev   line source
nuclear@10 1 /*
nuclear@10 2 libvmath - a vector math library
nuclear@10 3 Copyright (C) 2004-2015 John Tsiombikas <nuclear@member.fsf.org>
nuclear@10 4
nuclear@10 5 This program is free software: you can redistribute it and/or modify
nuclear@10 6 it under the terms of the GNU Lesser General Public License as published
nuclear@10 7 by the Free Software Foundation, either version 3 of the License, or
nuclear@10 8 (at your option) any later version.
nuclear@10 9
nuclear@10 10 This program is distributed in the hope that it will be useful,
nuclear@10 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@10 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@10 13 GNU Lesser General Public License for more details.
nuclear@10 14
nuclear@10 15 You should have received a copy of the GNU Lesser General Public License
nuclear@10 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
nuclear@10 17 */
nuclear@10 18
nuclear@10 19 #include <string.h>
nuclear@10 20
nuclear@10 21 #ifdef __cplusplus
nuclear@10 22 extern "C" {
nuclear@10 23 #endif /* __cplusplus */
nuclear@10 24
nuclear@10 25 /* C matrix 3x3 functions */
nuclear@10 26 static inline void m3_identity(mat3_t m)
nuclear@10 27 {
nuclear@10 28 static const mat3_t id = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
nuclear@10 29 memcpy(m, id, sizeof id);
nuclear@10 30 }
nuclear@10 31
nuclear@10 32 static inline void m3_cons(mat3_t m,
nuclear@10 33 scalar_t m11, scalar_t m12, scalar_t m13,
nuclear@10 34 scalar_t m21, scalar_t m22, scalar_t m23,
nuclear@10 35 scalar_t m31, scalar_t m32, scalar_t m33)
nuclear@10 36 {
nuclear@10 37 m[0][0] = m11; m[0][1] = m12; m[0][2] = m13;
nuclear@10 38 m[1][0] = m21; m[1][1] = m22; m[1][2] = m23;
nuclear@10 39 m[2][0] = m31; m[2][1] = m32; m[2][2] = m33;
nuclear@10 40 }
nuclear@10 41
nuclear@10 42 static inline void m3_copy(mat3_t dest, mat3_t src)
nuclear@10 43 {
nuclear@10 44 memcpy(dest, src, sizeof(mat3_t));
nuclear@10 45 }
nuclear@10 46
nuclear@10 47
nuclear@10 48 /* C matrix 4x4 functions */
nuclear@10 49 static inline void m4_identity(mat4_t m)
nuclear@10 50 {
nuclear@10 51 static const mat4_t id = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
nuclear@10 52 memcpy(m, id, sizeof id);
nuclear@10 53 }
nuclear@10 54
nuclear@10 55 static inline void m4_cons(mat4_t m,
nuclear@10 56 scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14,
nuclear@10 57 scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24,
nuclear@10 58 scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34,
nuclear@10 59 scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44)
nuclear@10 60 {
nuclear@10 61 m[0][0] = m11; m[0][1] = m12; m[0][2] = m13; m[0][3] = m14;
nuclear@10 62 m[1][0] = m21; m[1][1] = m22; m[1][2] = m23; m[1][3] = m24;
nuclear@10 63 m[2][0] = m31; m[2][1] = m32; m[2][2] = m33; m[2][3] = m34;
nuclear@10 64 m[3][0] = m41; m[3][1] = m42; m[3][2] = m43; m[3][3] = m44;
nuclear@10 65 }
nuclear@10 66
nuclear@10 67 static inline void m4_copy(mat4_t dest, mat4_t src)
nuclear@10 68 {
nuclear@10 69 memcpy(dest, src, sizeof(mat4_t));
nuclear@10 70 }
nuclear@10 71
nuclear@10 72 static inline void m4_mult(mat4_t res, mat4_t m1, mat4_t m2)
nuclear@10 73 {
nuclear@10 74 mat4_t tmp;
nuclear@10 75
nuclear@10 76 /*
nuclear@10 77 int i, j;
nuclear@10 78 for(i=0; i<4; i++) {
nuclear@10 79 for(j=0; j<4; j++) {
nuclear@10 80 tmp[i][j] = m1[i][0] * m2[0][j] + m1[i][1] * m2[1][j] + m1[i][2] * m2[2][j] + m1[i][3] * m2[3][j];
nuclear@10 81 }
nuclear@10 82 }
nuclear@10 83 */
nuclear@10 84
nuclear@10 85 tmp[0][0] = m1[0][0] * m2[0][0] + m1[0][1] * m2[1][0] + m1[0][2] * m2[2][0] + m1[0][3] * m2[3][0];
nuclear@10 86 tmp[0][1] = m1[0][0] * m2[0][1] + m1[0][1] * m2[1][1] + m1[0][2] * m2[2][1] + m1[0][3] * m2[3][1];
nuclear@10 87 tmp[0][2] = m1[0][0] * m2[0][2] + m1[0][1] * m2[1][2] + m1[0][2] * m2[2][2] + m1[0][3] * m2[3][2];
nuclear@10 88 tmp[0][3] = m1[0][0] * m2[0][3] + m1[0][1] * m2[1][3] + m1[0][2] * m2[2][3] + m1[0][3] * m2[3][3];
nuclear@10 89
nuclear@10 90 tmp[1][0] = m1[1][0] * m2[0][0] + m1[1][1] * m2[1][0] + m1[1][2] * m2[2][0] + m1[1][3] * m2[3][0];
nuclear@10 91 tmp[1][1] = m1[1][0] * m2[0][1] + m1[1][1] * m2[1][1] + m1[1][2] * m2[2][1] + m1[1][3] * m2[3][1];
nuclear@10 92 tmp[1][2] = m1[1][0] * m2[0][2] + m1[1][1] * m2[1][2] + m1[1][2] * m2[2][2] + m1[1][3] * m2[3][2];
nuclear@10 93 tmp[1][3] = m1[1][0] * m2[0][3] + m1[1][1] * m2[1][3] + m1[1][2] * m2[2][3] + m1[1][3] * m2[3][3];
nuclear@10 94
nuclear@10 95 tmp[2][0] = m1[2][0] * m2[0][0] + m1[2][1] * m2[1][0] + m1[2][2] * m2[2][0] + m1[2][3] * m2[3][0];
nuclear@10 96 tmp[2][1] = m1[2][0] * m2[0][1] + m1[2][1] * m2[1][1] + m1[2][2] * m2[2][1] + m1[2][3] * m2[3][1];
nuclear@10 97 tmp[2][2] = m1[2][0] * m2[0][2] + m1[2][1] * m2[1][2] + m1[2][2] * m2[2][2] + m1[2][3] * m2[3][2];
nuclear@10 98 tmp[2][3] = m1[2][0] * m2[0][3] + m1[2][1] * m2[1][3] + m1[2][2] * m2[2][3] + m1[2][3] * m2[3][3];
nuclear@10 99
nuclear@10 100 tmp[3][0] = m1[3][0] * m2[0][0] + m1[3][1] * m2[1][0] + m1[3][2] * m2[2][0] + m1[3][3] * m2[3][0];
nuclear@10 101 tmp[3][1] = m1[3][0] * m2[0][1] + m1[3][1] * m2[1][1] + m1[3][2] * m2[2][1] + m1[3][3] * m2[3][1];
nuclear@10 102 tmp[3][2] = m1[3][0] * m2[0][2] + m1[3][1] * m2[1][2] + m1[3][2] * m2[2][2] + m1[3][3] * m2[3][2];
nuclear@10 103 tmp[3][3] = m1[3][0] * m2[0][3] + m1[3][1] * m2[1][3] + m1[3][2] * m2[2][3] + m1[3][3] * m2[3][3];
nuclear@10 104
nuclear@10 105 m4_copy(res, tmp);
nuclear@10 106 }
nuclear@10 107
nuclear@10 108 static inline void m4_set_column(mat4_t m, vec4_t v, int idx)
nuclear@10 109 {
nuclear@10 110 m[0][idx] = v.x;
nuclear@10 111 m[1][idx] = v.y;
nuclear@10 112 m[2][idx] = v.z;
nuclear@10 113 m[3][idx] = v.w;
nuclear@10 114 }
nuclear@10 115
nuclear@10 116 static inline void m4_set_row(mat4_t m, vec4_t v, int idx)
nuclear@10 117 {
nuclear@10 118 m[idx][0] = v.x;
nuclear@10 119 m[idx][1] = v.y;
nuclear@10 120 m[idx][2] = v.z;
nuclear@10 121 m[idx][3] = v.w;
nuclear@10 122 }
nuclear@10 123
nuclear@10 124 #ifdef __cplusplus
nuclear@10 125 } /* extern "C" */
nuclear@10 126
nuclear@10 127
nuclear@10 128 /* unrolled to hell and inline */
nuclear@10 129 inline Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2)
nuclear@10 130 {
nuclear@10 131 Matrix4x4 res;
nuclear@10 132
nuclear@10 133 /*
nuclear@10 134 for(i=0; i<4; i++) {
nuclear@10 135 for(j=0; j<4; j++) {
nuclear@10 136 res.m[i][j] = m1.m[i][0] * m2.m[0][j] + m1.m[i][1] * m2.m[1][j] + m1.m[i][2] * m2.m[2][j] + m1.m[i][3] * m2.m[3][j];
nuclear@10 137 }
nuclear@10 138 }
nuclear@10 139 */
nuclear@10 140
nuclear@10 141 res.m[0][0] = m1.m[0][0] * m2.m[0][0] + m1.m[0][1] * m2.m[1][0] + m1.m[0][2] * m2.m[2][0] + m1.m[0][3] * m2.m[3][0];
nuclear@10 142 res.m[0][1] = m1.m[0][0] * m2.m[0][1] + m1.m[0][1] * m2.m[1][1] + m1.m[0][2] * m2.m[2][1] + m1.m[0][3] * m2.m[3][1];
nuclear@10 143 res.m[0][2] = m1.m[0][0] * m2.m[0][2] + m1.m[0][1] * m2.m[1][2] + m1.m[0][2] * m2.m[2][2] + m1.m[0][3] * m2.m[3][2];
nuclear@10 144 res.m[0][3] = m1.m[0][0] * m2.m[0][3] + m1.m[0][1] * m2.m[1][3] + m1.m[0][2] * m2.m[2][3] + m1.m[0][3] * m2.m[3][3];
nuclear@10 145
nuclear@10 146 res.m[1][0] = m1.m[1][0] * m2.m[0][0] + m1.m[1][1] * m2.m[1][0] + m1.m[1][2] * m2.m[2][0] + m1.m[1][3] * m2.m[3][0];
nuclear@10 147 res.m[1][1] = m1.m[1][0] * m2.m[0][1] + m1.m[1][1] * m2.m[1][1] + m1.m[1][2] * m2.m[2][1] + m1.m[1][3] * m2.m[3][1];
nuclear@10 148 res.m[1][2] = m1.m[1][0] * m2.m[0][2] + m1.m[1][1] * m2.m[1][2] + m1.m[1][2] * m2.m[2][2] + m1.m[1][3] * m2.m[3][2];
nuclear@10 149 res.m[1][3] = m1.m[1][0] * m2.m[0][3] + m1.m[1][1] * m2.m[1][3] + m1.m[1][2] * m2.m[2][3] + m1.m[1][3] * m2.m[3][3];
nuclear@10 150
nuclear@10 151 res.m[2][0] = m1.m[2][0] * m2.m[0][0] + m1.m[2][1] * m2.m[1][0] + m1.m[2][2] * m2.m[2][0] + m1.m[2][3] * m2.m[3][0];
nuclear@10 152 res.m[2][1] = m1.m[2][0] * m2.m[0][1] + m1.m[2][1] * m2.m[1][1] + m1.m[2][2] * m2.m[2][1] + m1.m[2][3] * m2.m[3][1];
nuclear@10 153 res.m[2][2] = m1.m[2][0] * m2.m[0][2] + m1.m[2][1] * m2.m[1][2] + m1.m[2][2] * m2.m[2][2] + m1.m[2][3] * m2.m[3][2];
nuclear@10 154 res.m[2][3] = m1.m[2][0] * m2.m[0][3] + m1.m[2][1] * m2.m[1][3] + m1.m[2][2] * m2.m[2][3] + m1.m[2][3] * m2.m[3][3];
nuclear@10 155
nuclear@10 156 res.m[3][0] = m1.m[3][0] * m2.m[0][0] + m1.m[3][1] * m2.m[1][0] + m1.m[3][2] * m2.m[2][0] + m1.m[3][3] * m2.m[3][0];
nuclear@10 157 res.m[3][1] = m1.m[3][0] * m2.m[0][1] + m1.m[3][1] * m2.m[1][1] + m1.m[3][2] * m2.m[2][1] + m1.m[3][3] * m2.m[3][1];
nuclear@10 158 res.m[3][2] = m1.m[3][0] * m2.m[0][2] + m1.m[3][1] * m2.m[1][2] + m1.m[3][2] * m2.m[2][2] + m1.m[3][3] * m2.m[3][2];
nuclear@10 159 res.m[3][3] = m1.m[3][0] * m2.m[0][3] + m1.m[3][1] * m2.m[1][3] + m1.m[3][2] * m2.m[2][3] + m1.m[3][3] * m2.m[3][3];
nuclear@10 160
nuclear@10 161 return res;
nuclear@10 162 }
nuclear@10 163
nuclear@10 164 inline void operator *=(Matrix4x4 &m1, const Matrix4x4 &m2)
nuclear@10 165 {
nuclear@10 166 Matrix4x4 res = m1 * m2;
nuclear@10 167 m1 = res;
nuclear@10 168 }
nuclear@10 169
nuclear@10 170
nuclear@10 171 inline scalar_t *Matrix3x3::operator [](int index)
nuclear@10 172 {
nuclear@10 173 return m[index];
nuclear@10 174 }
nuclear@10 175
nuclear@10 176 inline const scalar_t *Matrix3x3::operator [](int index) const
nuclear@10 177 {
nuclear@10 178 return m[index];
nuclear@10 179 }
nuclear@10 180
nuclear@10 181 inline void Matrix3x3::reset_identity()
nuclear@10 182 {
nuclear@10 183 *this = identity;
nuclear@10 184 }
nuclear@10 185
nuclear@10 186 inline scalar_t *Matrix4x4::operator [](int index)
nuclear@10 187 {
nuclear@10 188 return m[index];
nuclear@10 189 }
nuclear@10 190
nuclear@10 191 inline const scalar_t *Matrix4x4::operator [](int index) const
nuclear@10 192 {
nuclear@10 193 return m[index];
nuclear@10 194 }
nuclear@10 195
nuclear@10 196 inline void Matrix4x4::reset_identity()
nuclear@10 197 {
nuclear@10 198 *this = identity;
nuclear@10 199 }
nuclear@10 200 #endif /* __cplusplus */