|
@@ -127,57 +127,34 @@ void matrix_3x3::set_to_identity()
|
127
|
127
|
matrix[6] = 0; matrix[7] = 0; matrix[8] = 1;
|
128
|
128
|
}
|
129
|
129
|
|
130
|
|
-matrix_3x3 matrix_3x3::create_look_at(vector_3 target, vector_3 up)
|
131
|
|
-{
|
132
|
|
- // There are lots of examples of look at code on the internet that don't do all these noramize and also find the position
|
133
|
|
- // through several dot products. The problem with them is that they have a bit of error in that all the vectors arn't normal and need to be.
|
134
|
|
- vector_3 z_row = vector_3(-target.x, -target.y, -target.z).get_normal();
|
135
|
|
- vector_3 x_row = vector_3::cross(up, z_row).get_normal();
|
136
|
|
- vector_3 y_row = vector_3::cross(z_row, x_row).get_normal();
|
137
|
|
-
|
138
|
|
- //x_row.debug("x_row");
|
139
|
|
- //y_row.debug("y_row");
|
140
|
|
- //z_row.debug("z_row");
|
141
|
|
-
|
142
|
|
- matrix_3x3 rot = matrix_3x3::create_from_rows(vector_3(x_row.x, y_row.x, z_row.x),
|
143
|
|
- vector_3(x_row.y, y_row.y, z_row.y),
|
144
|
|
- vector_3(x_row.z, y_row.z, z_row.z));
|
145
|
|
-
|
146
|
|
- //rot.debug("rot");
|
|
130
|
+matrix_3x3 matrix_3x3::create_look_at(vector_3 target)
|
|
131
|
+{
|
|
132
|
+ vector_3 z_row = vector_3(target.x, target.y, target.z).get_normal();
|
|
133
|
+ vector_3 x_row = vector_3(1, 0, -target.x/target.z).get_normal();
|
|
134
|
+ vector_3 y_row = vector_3(0, 1, -target.y/target.z).get_normal();
|
|
135
|
+
|
|
136
|
+ // x_row.debug("x_row");
|
|
137
|
+ // y_row.debug("y_row");
|
|
138
|
+ // z_row.debug("z_row");
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+ // create the matrix already correctly transposed
|
|
142
|
+ matrix_3x3 rot = matrix_3x3::create_from_rows(vector_3(x_row.x, x_row.y, x_row.z),
|
|
143
|
+ vector_3(y_row.x, y_row.y, y_row.z),
|
|
144
|
+ vector_3(z_row.x, z_row.y, z_row.z));
|
|
145
|
+
|
|
146
|
+ // rot.debug("rot");
|
147
|
147
|
return rot;
|
148
|
148
|
}
|
149
|
149
|
|
150
|
|
-matrix_3x3 matrix_3x3::create_inverse(matrix_3x3 original)
|
151
|
|
-{
|
152
|
|
- //original.debug("original");
|
153
|
|
- float* A = original.matrix;
|
154
|
|
- float determinant =
|
155
|
|
- + A[0 * 3 + 0] * (A[1 * 3 + 1] * A[2 * 3 + 2] - A[2 * 3 + 1] * A[1 * 3 + 2])
|
156
|
|
- - A[0 * 3 + 1] * (A[1 * 3 + 0] * A[2 * 3 + 2] - A[1 * 3 + 2] * A[2 * 3 + 0])
|
157
|
|
- + A[0 * 3 + 2] * (A[1 * 3 + 0] * A[2 * 3 + 1] - A[1 * 3 + 1] * A[2 * 3 + 0]);
|
158
|
|
- matrix_3x3 inverse;
|
159
|
|
- inverse.matrix[0 * 3 + 0] = +(A[1 * 3 + 1] * A[2 * 3 + 2] - A[2 * 3 + 1] * A[1 * 3 + 2]) / determinant;
|
160
|
|
- inverse.matrix[0 * 3 + 1] = -(A[0 * 3 + 1] * A[2 * 3 + 2] - A[0 * 3 + 2] * A[2 * 3 + 1]) / determinant;
|
161
|
|
- inverse.matrix[0 * 3 + 2] = +(A[0 * 3 + 1] * A[1 * 3 + 2] - A[0 * 3 + 2] * A[1 * 3 + 1]) / determinant;
|
162
|
|
- inverse.matrix[1 * 3 + 0] = -(A[1 * 3 + 0] * A[2 * 3 + 2] - A[1 * 3 + 2] * A[2 * 3 + 0]) / determinant;
|
163
|
|
- inverse.matrix[1 * 3 + 1] = +(A[0 * 3 + 0] * A[2 * 3 + 2] - A[0 * 3 + 2] * A[2 * 3 + 0]) / determinant;
|
164
|
|
- inverse.matrix[1 * 3 + 2] = -(A[0 * 3 + 0] * A[1 * 3 + 2] - A[1 * 3 + 0] * A[0 * 3 + 2]) / determinant;
|
165
|
|
- inverse.matrix[2 * 3 + 0] = +(A[1 * 3 + 0] * A[2 * 3 + 1] - A[2 * 3 + 0] * A[1 * 3 + 1]) / determinant;
|
166
|
|
- inverse.matrix[2 * 3 + 1] = -(A[0 * 3 + 0] * A[2 * 3 + 1] - A[2 * 3 + 0] * A[0 * 3 + 1]) / determinant;
|
167
|
|
- inverse.matrix[2 * 3 + 2] = +(A[0 * 3 + 0] * A[1 * 3 + 1] - A[1 * 3 + 0] * A[0 * 3 + 1]) / determinant;
|
168
|
|
-
|
169
|
|
- vector_3 row0 = vector_3(inverse.matrix[0 * 3 + 0], inverse.matrix[0 * 3 + 1], inverse.matrix[0 * 3 + 2]);
|
170
|
|
- vector_3 row1 = vector_3(inverse.matrix[1 * 3 + 0], inverse.matrix[1 * 3 + 1], inverse.matrix[1 * 3 + 2]);
|
171
|
|
- vector_3 row2 = vector_3(inverse.matrix[2 * 3 + 0], inverse.matrix[2 * 3 + 1], inverse.matrix[2 * 3 + 2]);
|
172
|
|
-
|
173
|
|
- row0.normalize();
|
174
|
|
- row1.normalize();
|
175
|
|
- row2.normalize();
|
176
|
|
-
|
177
|
|
- inverse = matrix_3x3::create_from_rows(row0, row1, row2);
|
178
|
|
-
|
179
|
|
- //inverse.debug("inverse");
|
180
|
|
- return inverse;
|
|
150
|
+
|
|
151
|
+matrix_3x3 matrix_3x3::transpose(matrix_3x3 original)
|
|
152
|
+{
|
|
153
|
+ matrix_3x3 new_matrix;
|
|
154
|
+ new_matrix.matrix[0] = original.matrix[0]; new_matrix.matrix[1] = original.matrix[3]; new_matrix.matrix[2] = original.matrix[6];
|
|
155
|
+ new_matrix.matrix[3] = original.matrix[1]; new_matrix.matrix[4] = original.matrix[4]; new_matrix.matrix[5] = original.matrix[7];
|
|
156
|
+ new_matrix.matrix[6] = original.matrix[2]; new_matrix.matrix[7] = original.matrix[5]; new_matrix.matrix[8] = original.matrix[8];
|
|
157
|
+ return new_matrix;
|
181
|
158
|
}
|
182
|
159
|
|
183
|
160
|
void matrix_3x3::debug(char* title)
|