From c4d0c4af57f3486651ae66811008618bdb9b65d6 Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Sat, 11 May 2013 17:28:09 +0200 Subject: [PATCH] fix Matrix functions to use temporary array when lhs or rhs are same as result --- jni/gl/utils.c | 23 +++++++++++++++++++++++ src/org/oscim/utils/Matrix4.java | 26 ++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/jni/gl/utils.c b/jni/gl/utils.c index 02e54a95..7e3734d7 100644 --- a/jni/gl/utils.c +++ b/jni/gl/utils.c @@ -186,6 +186,29 @@ void JNI(smul)(JNIEnv* env, jclass* clazz, jlong ptr_r, jlong ptr_a, jlong ptr_b multiplyMM(matr, mata, matb); } +void JNI(smulrhs)(JNIEnv* env, jclass* clazz, jlong ptr_r, jlong ptr_rhs) +{ + float* matr = CAST(ptr_r); + float* mata = alloca(16 * sizeof(float)); + float* matb = CAST(ptr_rhs); + + memcpy(mata, matr, 16 * sizeof(float)); + + multiplyMM(matr, mata, matb); +} + +void JNI(smullhs)(JNIEnv* env, jclass* clazz, jlong ptr_r, jlong ptr_lhs) +{ + float* matr = CAST(ptr_r); + float* mata = CAST(ptr_lhs); + float* matb = alloca(16 * sizeof(float)); + + memcpy(matb, matr, 16 * sizeof(float)); + + multiplyMM(matr, mata, matb); +} + + void JNI(strans)(JNIEnv* env, jclass* clazz, jlong ptr_r, jlong ptr_a) { float* matr = CAST(ptr_r); diff --git a/src/org/oscim/utils/Matrix4.java b/src/org/oscim/utils/Matrix4.java index 95712116..39cf394f 100644 --- a/src/org/oscim/utils/Matrix4.java +++ b/src/org/oscim/utils/Matrix4.java @@ -39,6 +39,10 @@ public class Matrix4 { private native static void smul(long self, long rhs_ptr, long lhs_ptr); + private native static void smulrhs(long self, long rhs_ptr); + + private native static void smullhs(long self, long lhs_ptr); + private native static void strans(long self, long rhs_ptr); private native static void prj(long self, float[] vec3); @@ -111,16 +115,30 @@ public class Matrix4 { } /** - * Multiply rhs onto Matrix + * Multiply rhs onto Matrix. * * @param rhs right hand side */ - public void multiplyMM(Matrix4 rhs) { - smul(pointer, pointer, rhs.pointer); + public void multiplyRhs(Matrix4 rhs) { + smulrhs(pointer, rhs.pointer); } /** - * Multiply rhs onto lhs and store result in Matrix + * Use this matrix as rhs, multiply it on lhs and store result. + * + * @param lhs right hand side + */ + public void multiplyLhs(Matrix4 lhs) { + smullhs(pointer, lhs.pointer); + } + + /** + * Multiply rhs onto lhs and store result in Matrix. + * + * This matrix MUST be different from lhs and rhs! + * + * As you know, when combining matrices for vector projection + * this has the same effect first as applying rhs then lhs. * * @param lhs left hand side * @param rhs right hand side