summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnsis Brammanis <brammanis@gmail.com>2015-08-13 15:34:08 +0300
committerAnsis Brammanis <brammanis@gmail.com>2015-08-24 18:41:49 -0400
commitd5139107123b3cadcce2c80d4aea0ed3a6256683 (patch)
treeaf4f97c7f9a5c8d85fef28e9e6d564fb8ec33c25
parent5636eb86c1b2e5fe9c8533fd795b4dfd107bdd30 (diff)
downloadqtlocation-mapboxgl-d5139107123b3cadcce2c80d4aea0ed3a6256683.tar.gz
port mat4.perspective and mat4.rotate_x
-rw-r--r--include/mbgl/util/mat4.hpp2
-rw-r--r--src/mbgl/util/mat4.cpp55
2 files changed, 57 insertions, 0 deletions
diff --git a/include/mbgl/util/mat4.hpp b/include/mbgl/util/mat4.hpp
index 4279113d21..e59712ed0d 100644
--- a/include/mbgl/util/mat4.hpp
+++ b/include/mbgl/util/mat4.hpp
@@ -33,8 +33,10 @@ namespace matrix {
void identity(mat4& out);
void ortho(mat4& out, float left, float right, float bottom, float top, float near, float far);
+void perspective(mat4& out, float fovy, float aspect, float near, float far);
void copy(mat4& out, const mat4& a);
void translate(mat4& out, const mat4& a, float x, float y, float z);
+void rotate_x(mat4& out, const mat4& a, float rad);
void rotate_z(mat4& out, const mat4& a, float rad);
void scale(mat4& out, const mat4& a, float x, float y, float z);
void multiply(mat4& out, const mat4& a, const mat4& b);
diff --git a/src/mbgl/util/mat4.cpp b/src/mbgl/util/mat4.cpp
index cabd8e2842..d48558dc22 100644
--- a/src/mbgl/util/mat4.cpp
+++ b/src/mbgl/util/mat4.cpp
@@ -67,6 +67,27 @@ void matrix::ortho(mat4& out, float left, float right, float bottom, float top,
out[15] = 1.0f;
}
+void matrix::perspective(mat4& out, float fovy, float aspect, float near, float far) {
+ float f = 1.0f / std::tan(fovy / 2.0f),
+ nf = 1.0f / (near - far);
+ out[0] = f / aspect;
+ out[1] = 0.0f;
+ out[2] = 0.0f;
+ out[3] = 0.0f;
+ out[4] = 0.0f;
+ out[5] = f;
+ out[6] = 0.0f;
+ out[7] = 0.0f;
+ out[8] = 0.0f;
+ out[9] = 0.0f;
+ out[10] = (far + near) * nf;
+ out[11] = -1.0f;
+ out[12] = 0.0f;
+ out[13] = 0.0f;
+ out[14] = (2.0f * far * near) * nf;
+ out[15] = 0.0f;
+}
+
void matrix::copy(mat4& out, const mat4& a) {
out[0] = a[0];
out[1] = a[1];
@@ -112,6 +133,40 @@ void matrix::translate(mat4& out, const mat4& a, float x, float y, float z) {
}
}
+void matrix::rotate_x(mat4& out, const mat4& a, float rad) {
+ float s = std::sin(rad),
+ c = std::cos(rad),
+ a10 = a[4],
+ a11 = a[5],
+ a12 = a[6],
+ a13 = a[7],
+ a20 = a[8],
+ a21 = a[9],
+ a22 = a[10],
+ a23 = a[11];
+
+ if (&a != &out) { // If the source and destination differ, copy the unchanged rows
+ out[0] = a[0];
+ out[1] = a[1];
+ out[2] = a[2];
+ out[3] = a[3];
+ out[12] = a[12];
+ out[13] = a[13];
+ out[14] = a[14];
+ out[15] = a[15];
+ }
+
+ // Perform axis-specific matrix multiplication
+ out[4] = a10 * c + a20 * s;
+ out[5] = a11 * c + a21 * s;
+ out[6] = a12 * c + a22 * s;
+ out[7] = a13 * c + a23 * s;
+ out[8] = a20 * c - a10 * s;
+ out[9] = a21 * c - a11 * s;
+ out[10] = a22 * c - a12 * s;
+ out[11] = a23 * c - a13 * s;
+}
+
void matrix::rotate_z(mat4& out, const mat4& a, float rad) {
float s = std::sin(rad),
c = std::cos(rad),