Mercurial > love-experiments
annotate src/common/Matrix.cpp @ 1448:108ee2e81f65 default tip
Fix a case typo, imageData.h -> ImageData.h
| author | Radomir Dopieralski <sheep-devel@sheep.art.pl> |
|---|---|
| date | Sat, 18 Jan 2014 13:57:44 +0100 |
| parents | b7f097053d74 |
| children | a2af6d2406ed |
| rev | line source |
|---|---|
| 1096 | 1 /** |
|
1282
b7f097053d74
Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents:
1096
diff
changeset
|
2 * Copyright (c) 2006-2013 LOVE Development Team |
| 1096 | 3 * |
| 4 * This software is provided 'as-is', without any express or implied | |
| 5 * warranty. In no event will the authors be held liable for any damages | |
| 6 * arising from the use of this software. | |
| 7 * | |
| 8 * Permission is granted to anyone to use this software for any purpose, | |
| 9 * including commercial applications, and to alter it and redistribute it | |
| 10 * freely, subject to the following restrictions: | |
| 11 * | |
| 12 * 1. The origin of this software must not be misrepresented; you must not | |
| 13 * claim that you wrote the original software. If you use this software | |
| 14 * in a product, an acknowledgment in the product documentation would be | |
| 15 * appreciated but is not required. | |
| 16 * 2. Altered source versions must be plainly marked as such, and must not be | |
| 17 * misrepresented as being the original software. | |
| 18 * 3. This notice may not be removed or altered from any source distribution. | |
| 19 **/ | |
| 20 | |
| 21 #include "Matrix.h" | |
| 22 | |
| 23 // STD | |
| 24 #include <cstring> // memcpy | |
| 25 #include <cmath> | |
| 26 | |
| 27 namespace love | |
| 28 { | |
| 29 | |
| 30 // | e0 e4 e8 e12 | | |
| 31 // | e1 e5 e9 e13 | | |
| 32 // | e2 e6 e10 e14 | | |
| 33 // | e3 e7 e11 e15 | | |
| 34 | |
| 35 Matrix::Matrix() | |
| 36 { | |
| 37 setIdentity(); | |
| 38 } | |
| 39 | |
| 40 Matrix::~Matrix() | |
| 41 { | |
| 42 } | |
| 43 | |
| 44 // | e0 e4 e8 e12 | | |
| 45 // | e1 e5 e9 e13 | | |
| 46 // | e2 e6 e10 e14 | | |
| 47 // | e3 e7 e11 e15 | | |
| 48 // | e0 e4 e8 e12 | | |
| 49 // | e1 e5 e9 e13 | | |
| 50 // | e2 e6 e10 e14 | | |
| 51 // | e3 e7 e11 e15 | | |
| 52 | |
| 53 Matrix Matrix::operator * (const Matrix &m) const | |
| 54 { | |
| 55 Matrix t; | |
| 56 | |
| 57 t.e[0] = (e[0]*m.e[0]) + (e[4]*m.e[1]) + (e[8]*m.e[2]) + (e[12]*m.e[3]); | |
| 58 t.e[4] = (e[0]*m.e[4]) + (e[4]*m.e[5]) + (e[8]*m.e[6]) + (e[12]*m.e[7]); | |
| 59 t.e[8] = (e[0]*m.e[8]) + (e[4]*m.e[9]) + (e[8]*m.e[10]) + (e[12]*m.e[11]); | |
| 60 t.e[12] = (e[0]*m.e[12]) + (e[4]*m.e[13]) + (e[8]*m.e[14]) + (e[12]*m.e[15]); | |
| 61 | |
| 62 t.e[1] = (e[1]*m.e[0]) + (e[5]*m.e[1]) + (e[9]*m.e[2]) + (e[13]*m.e[3]); | |
| 63 t.e[5] = (e[1]*m.e[4]) + (e[5]*m.e[5]) + (e[9]*m.e[6]) + (e[13]*m.e[7]); | |
| 64 t.e[9] = (e[1]*m.e[8]) + (e[5]*m.e[9]) + (e[9]*m.e[10]) + (e[13]*m.e[11]); | |
| 65 t.e[13] = (e[1]*m.e[12]) + (e[5]*m.e[13]) + (e[9]*m.e[14]) + (e[13]*m.e[15]); | |
| 66 | |
| 67 t.e[2] = (e[2]*m.e[0]) + (e[6]*m.e[1]) + (e[10]*m.e[2]) + (e[14]*m.e[3]); | |
| 68 t.e[6] = (e[2]*m.e[4]) + (e[6]*m.e[5]) + (e[10]*m.e[6]) + (e[14]*m.e[7]); | |
| 69 t.e[10] = (e[2]*m.e[8]) + (e[6]*m.e[9]) + (e[10]*m.e[10]) + (e[14]*m.e[11]); | |
| 70 t.e[14] = (e[2]*m.e[12]) + (e[6]*m.e[13]) + (e[10]*m.e[14]) + (e[14]*m.e[15]); | |
| 71 | |
| 72 t.e[3] = (e[3]*m.e[0]) + (e[7]*m.e[1]) + (e[11]*m.e[2]) + (e[15]*m.e[3]); | |
| 73 t.e[7] = (e[3]*m.e[4]) + (e[7]*m.e[5]) + (e[11]*m.e[6]) + (e[15]*m.e[7]); | |
| 74 t.e[11] = (e[3]*m.e[8]) + (e[7]*m.e[9]) + (e[11]*m.e[10]) + (e[15]*m.e[11]); | |
| 75 t.e[15] = (e[3]*m.e[12]) + (e[7]*m.e[13]) + (e[11]*m.e[14]) + (e[15]*m.e[15]); | |
| 76 | |
| 77 return t; | |
| 78 } | |
| 79 | |
| 80 void Matrix::operator *= (const Matrix &m) | |
| 81 { | |
| 82 Matrix t = (*this) * m; | |
| 83 memcpy((void *)this->e, (void *)t.e, sizeof(float)*16); | |
| 84 } | |
| 85 | |
| 86 const float *Matrix::getElements() const | |
| 87 { | |
| 88 return e; | |
| 89 } | |
| 90 | |
| 91 void Matrix::setIdentity() | |
| 92 { | |
| 93 memset(e, 0, sizeof(float)*16); | |
| 94 e[0] = e[5] = e[10] = e[15] = 1; | |
| 95 } | |
| 96 | |
| 97 void Matrix::setTranslation(float x, float y) | |
| 98 { | |
| 99 setIdentity(); | |
| 100 e[12] = x; | |
| 101 e[13] = y; | |
| 102 } | |
| 103 | |
| 104 void Matrix::setRotation(float rad) | |
| 105 { | |
| 106 setIdentity(); | |
| 107 float c = cos(rad), s = sin(rad); | |
| 108 e[0] = c; | |
| 109 e[4] = -s; | |
| 110 e[1] = s; | |
| 111 e[5] = c; | |
| 112 } | |
| 113 | |
| 114 void Matrix::setScale(float sx, float sy) | |
| 115 { | |
| 116 setIdentity(); | |
| 117 e[0] = sx; | |
| 118 e[5] = sy; | |
| 119 } | |
| 120 | |
| 121 void Matrix::setShear(float kx, float ky) | |
| 122 { | |
| 123 setIdentity(); | |
| 124 e[1] = ky; | |
| 125 e[4] = kx; | |
| 126 } | |
| 127 | |
| 128 void Matrix::setTransformation(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) | |
| 129 { | |
| 130 memset(e, 0, sizeof(float)*16); // zero out matrix | |
| 131 float c = cos(angle), s = sin(angle); | |
| 132 // matrix multiplication carried out on paper: | |
| 133 // |1 x| |c -s | |sx | | 1 ky | |1 -ox| | |
| 134 // | 1 y| |s c | | sy | |kx 1 | | 1 -oy| | |
| 135 // | 1 | | 1 | | 1 | | 1 | | 1 | | |
| 136 // | 1| | 1| | 1| | 1| | 1 | | |
| 137 // move rotate scale skew origin | |
| 138 e[10] = e[15] = 1.0f; | |
| 139 e[0] = c * sx - ky * s * sy; // = a | |
| 140 e[1] = s * sx + ky * c * sy; // = b | |
| 141 e[4] = kx * c * sx - s * sy; // = c | |
| 142 e[5] = kx * s * sx + c * sy; // = d | |
| 143 e[12] = x - ox * e[0] - oy * e[4]; | |
| 144 e[13] = y - ox * e[1] - oy * e[5]; | |
| 145 } | |
| 146 | |
| 147 void Matrix::translate(float x, float y) | |
| 148 { | |
| 149 Matrix t; | |
| 150 t.setTranslation(x, y); | |
| 151 this->operator *=(t); | |
| 152 } | |
| 153 | |
| 154 void Matrix::rotate(float rad) | |
| 155 { | |
| 156 Matrix t; | |
| 157 t.setRotation(rad); | |
| 158 this->operator *=(t); | |
| 159 } | |
| 160 | |
| 161 void Matrix::scale(float sx, float sy) | |
| 162 { | |
| 163 Matrix t; | |
| 164 t.setScale(sx, sy); | |
| 165 this->operator *=(t); | |
| 166 } | |
| 167 | |
| 168 void Matrix::shear(float kx, float ky) | |
| 169 { | |
| 170 Matrix t; | |
| 171 t.setShear(kx,ky); | |
| 172 this->operator *=(t); | |
| 173 } | |
| 174 | |
| 175 // | x | | |
| 176 // | y | | |
| 177 // | 0 | | |
| 178 // | 1 | | |
| 179 // | e0 e4 e8 e12 | | |
| 180 // | e1 e5 e9 e13 | | |
| 181 // | e2 e6 e10 e14 | | |
| 182 // | e3 e7 e11 e15 | | |
| 183 | |
| 184 void Matrix::transform(vertex *dst, const vertex *src, int size) const | |
| 185 { | |
| 186 for (int i = 0; i<size; i++) | |
| 187 { | |
| 188 // Store in temp variables in case src = dst | |
| 189 float x = (e[0]*src[i].x) + (e[4]*src[i].y) + (0) + (e[12]); | |
| 190 float y = (e[1]*src[i].x) + (e[5]*src[i].y) + (0) + (e[13]); | |
| 191 | |
| 192 dst[i].x = x; | |
| 193 dst[i].y = y; | |
| 194 } | |
| 195 } | |
| 196 | |
| 197 | |
| 198 } // love |
