Use native Matrix4 instead of float[16]
This commit is contained in:
@@ -17,8 +17,6 @@ package org.oscim.utils;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
|
||||
import org.oscim.renderer.GLRenderer;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.opengl.GLES20;
|
||||
import android.opengl.GLUtils;
|
||||
@@ -204,37 +202,33 @@ public class GlUtils {
|
||||
return oom;
|
||||
}
|
||||
|
||||
// this is save as it can only be called from glThread
|
||||
private static float[] tmpColor = new float[4];
|
||||
|
||||
public static void setBlendColors(int handle, float[] c1, float[] c2, float mix) {
|
||||
if (mix <= 0f)
|
||||
GLES20.glUniform4fv(handle, 1, c1, 0);
|
||||
else if (mix >= 1f)
|
||||
GLES20.glUniform4fv(handle, 1, c2, 0);
|
||||
else {
|
||||
tmpColor[0] = c1[0] * (1 - mix) + c2[0] * mix;
|
||||
tmpColor[1] = c1[1] * (1 - mix) + c2[1] * mix;
|
||||
tmpColor[2] = c1[2] * (1 - mix) + c2[2] * mix;
|
||||
tmpColor[3] = c1[3] * (1 - mix) + c2[3] * mix;
|
||||
GLES20.glUniform4fv(handle, 1, tmpColor, 0);
|
||||
GLES20.glUniform4f(handle,
|
||||
c1[0] * (1 - mix) + c2[0] * mix,
|
||||
c1[1] * (1 - mix) + c2[1] * mix,
|
||||
c1[2] * (1 - mix) + c2[2] * mix,
|
||||
c1[3] * (1 - mix) + c2[3] * mix);
|
||||
}
|
||||
}
|
||||
|
||||
public static void setColor(int handle, float[] c, float alpha) {
|
||||
if (alpha >= 1) {
|
||||
GLES20.glUniform4fv(handle, 1, c, 0);
|
||||
GLES20.glUniform4f(handle, c[0], c[1], c[2], c[3]);
|
||||
} else {
|
||||
if (alpha < 0) {
|
||||
Log.d(TAG, "setColor: " + alpha);
|
||||
alpha = 0;
|
||||
GLES20.glUniform4f(handle, 0, 0, 0, 0);
|
||||
}
|
||||
tmpColor[0] = c[0] * alpha;
|
||||
tmpColor[1] = c[1] * alpha;
|
||||
tmpColor[2] = c[2] * alpha;
|
||||
tmpColor[3] = c[3] * alpha;
|
||||
|
||||
GLES20.glUniform4fv(handle, 1, tmpColor, 0);
|
||||
GLES20.glUniform4f(handle,
|
||||
c[0] * alpha, c[1] * alpha,
|
||||
c[2] * alpha, c[3] * alpha);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -280,56 +274,56 @@ public class GlUtils {
|
||||
color[2] = FastMath.clampN((float) (p + (b - p) * change));
|
||||
}
|
||||
|
||||
private final static float[] mIdentity = {
|
||||
1, 0, 0, 0,
|
||||
0, 1, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 1 };
|
||||
|
||||
public static void setTileMatrix(float[] matrix, float tx, float ty, float s) {
|
||||
System.arraycopy(mIdentity, 0, matrix, 0, 16);
|
||||
// scale tile relative to map scale
|
||||
matrix[0] = matrix[5] = s / GLRenderer.COORD_SCALE;
|
||||
// translate relative to map center
|
||||
matrix[12] = tx * s;
|
||||
matrix[13] = ty * s;
|
||||
}
|
||||
|
||||
public static void setTranslation(float[] matrix, float x, float y, float z) {
|
||||
System.arraycopy(mIdentity, 0, matrix, 0, 16);
|
||||
matrix[12] = x;
|
||||
matrix[13] = y;
|
||||
matrix[14] = z;
|
||||
}
|
||||
|
||||
public static void setMatrix(float[] matrix, float tx, float ty, float scale) {
|
||||
System.arraycopy(mIdentity, 0, matrix, 0, 16);
|
||||
matrix[12] = tx;
|
||||
matrix[13] = ty;
|
||||
matrix[0] = scale;
|
||||
matrix[5] = scale;
|
||||
//matrix[10] = scale;
|
||||
}
|
||||
|
||||
public static void setIdentity(float[] matrix) {
|
||||
System.arraycopy(mIdentity, 0, matrix, 0, 16);
|
||||
}
|
||||
|
||||
public static void setScaleM(float[] matrix, float sx, float sy, float sz) {
|
||||
System.arraycopy(mIdentity, 0, matrix, 0, 16);
|
||||
matrix[0] = sx;
|
||||
matrix[5] = sy;
|
||||
matrix[10] = sz;
|
||||
}
|
||||
|
||||
public static void addOffsetM(float[] matrix, int delta) {
|
||||
// from http://www.mathfor3dgameprogramming.com/code/Listing9.1.cpp
|
||||
// float n = MapViewPosition.VIEW_NEAR;
|
||||
// float f = MapViewPosition.VIEW_FAR;
|
||||
// float pz = 1;
|
||||
// float epsilon = -2.0f * f * n * delta / ((f + n) * pz * (pz + delta));
|
||||
float epsilon = 1.0f / (1 << 11);
|
||||
|
||||
matrix[10] *= 1.0f + epsilon * delta;
|
||||
}
|
||||
// private final static float[] mIdentity = {
|
||||
// 1, 0, 0, 0,
|
||||
// 0, 1, 0, 0,
|
||||
// 0, 0, 1, 0,
|
||||
// 0, 0, 0, 1 };
|
||||
//
|
||||
// public static void setTileMatrix(float[] matrix, float tx, float ty, float s) {
|
||||
// System.arraycopy(mIdentity, 0, matrix, 0, 16);
|
||||
// // scale tile relative to map scale
|
||||
// matrix[0] = matrix[5] = s / GLRenderer.COORD_SCALE;
|
||||
// // translate relative to map center
|
||||
// matrix[12] = tx * s;
|
||||
// matrix[13] = ty * s;
|
||||
// }
|
||||
//
|
||||
// public static void setTranslation(float[] matrix, float x, float y, float z) {
|
||||
// System.arraycopy(mIdentity, 0, matrix, 0, 16);
|
||||
// matrix[12] = x;
|
||||
// matrix[13] = y;
|
||||
// matrix[14] = z;
|
||||
// }
|
||||
//
|
||||
// public static void setMatrix(float[] matrix, float tx, float ty, float scale) {
|
||||
// System.arraycopy(mIdentity, 0, matrix, 0, 16);
|
||||
// matrix[12] = tx;
|
||||
// matrix[13] = ty;
|
||||
// matrix[0] = scale;
|
||||
// matrix[5] = scale;
|
||||
// //matrix[10] = scale;
|
||||
// }
|
||||
//
|
||||
// public static void setIdentity(float[] matrix) {
|
||||
// System.arraycopy(mIdentity, 0, matrix, 0, 16);
|
||||
// }
|
||||
//
|
||||
// public static void setScaleM(float[] matrix, float sx, float sy, float sz) {
|
||||
// System.arraycopy(mIdentity, 0, matrix, 0, 16);
|
||||
// matrix[0] = sx;
|
||||
// matrix[5] = sy;
|
||||
// matrix[10] = sz;
|
||||
// }
|
||||
//
|
||||
// public static void addOffsetM(float[] matrix, int delta) {
|
||||
// // from http://www.mathfor3dgameprogramming.com/code/Listing9.1.cpp
|
||||
// // float n = MapViewPosition.VIEW_NEAR;
|
||||
// // float f = MapViewPosition.VIEW_FAR;
|
||||
// // float pz = 1;
|
||||
// // float epsilon = -2.0f * f * n * delta / ((f + n) * pz * (pz + delta));
|
||||
// float epsilon = 1.0f / (1 << 11);
|
||||
//
|
||||
// matrix[10] *= 1.0f + epsilon * delta;
|
||||
// }
|
||||
}
|
||||
|
||||
226
src/org/oscim/utils/Matrix4.java
Normal file
226
src/org/oscim/utils/Matrix4.java
Normal file
@@ -0,0 +1,226 @@
|
||||
/*
|
||||
* Copyright 2013 Hannes Janetzek
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under the
|
||||
* terms of the GNU Lesser General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.oscim.utils;
|
||||
|
||||
public class Matrix4 {
|
||||
|
||||
static {
|
||||
System.loadLibrary("glutils");
|
||||
}
|
||||
|
||||
private final static String TAG = Matrix4.class.getName();
|
||||
private final static boolean dbg = false;
|
||||
|
||||
private native static long alloc();
|
||||
|
||||
private native static void delete(long self);
|
||||
|
||||
private native static void set(long self, float[] m);
|
||||
|
||||
private native static void copy(long self, long other);
|
||||
|
||||
private native static void identity(long self);
|
||||
|
||||
private native static void get(long self, float[] m);
|
||||
|
||||
private native static void mul(long self, long lhs_ptr);
|
||||
|
||||
private native static void smul(long self, long rhs_ptr, long lhs_ptr);
|
||||
|
||||
private native static void strans(long self, long rhs_ptr);
|
||||
|
||||
private native static void prj(long self, float[] vec3);
|
||||
|
||||
private native static void setRotation(long self, float a, float x, float y, float z);
|
||||
|
||||
private native static void setScale(long self, float x, float y, float z);
|
||||
|
||||
private native static void setTranslation(long self, float x, float y, float z);
|
||||
|
||||
private native static void setTransScale(long self, float tx, float ty, float scale);
|
||||
|
||||
private native static void setAsUniform(long self, int handle);
|
||||
|
||||
private native void setValueAt(long self, int pos, float value);
|
||||
|
||||
private native void addDepthOffset(long self, int delta);
|
||||
|
||||
private final long pointer;
|
||||
|
||||
private final static String INVALID_INPUT = "Bad Array!";
|
||||
|
||||
public Matrix4() {
|
||||
pointer = alloc();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Matrix from float array
|
||||
*
|
||||
* @param m float array to copy
|
||||
*/
|
||||
public void set(float[] m) {
|
||||
if (m == null || m.length != 16)
|
||||
throw new IllegalArgumentException(INVALID_INPUT);
|
||||
|
||||
set(pointer, m);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Matrix as float array
|
||||
*
|
||||
* @param m float array to store Matrix
|
||||
*/
|
||||
public void get(float[] m) {
|
||||
if (m == null || m.length != 16)
|
||||
throw new IllegalArgumentException(INVALID_INPUT);
|
||||
|
||||
get(pointer, m);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy values from mat
|
||||
*
|
||||
* @param mat Matrix to copy
|
||||
*/
|
||||
public void copy(Matrix4 mat) {
|
||||
copy(pointer, mat.pointer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Project Vector with Matrix
|
||||
*
|
||||
* @param vec3 Vector to project
|
||||
*/
|
||||
public void prj(float[] vec3) {
|
||||
if (vec3 == null || vec3.length < 3)
|
||||
throw new IllegalArgumentException(INVALID_INPUT);
|
||||
|
||||
prj(pointer, vec3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply rhs onto Matrix
|
||||
*
|
||||
* @param rhs right hand side
|
||||
*/
|
||||
public void multiplyMM(Matrix4 rhs) {
|
||||
smul(pointer, pointer, rhs.pointer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply rhs onto lhs and store result in Matrix
|
||||
*
|
||||
* @param lhs left hand side
|
||||
* @param rhs right hand side
|
||||
*/
|
||||
public void multiplyMM(Matrix4 lhs, Matrix4 rhs) {
|
||||
smul(pointer, lhs.pointer, rhs.pointer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transpose mat and store result in Matrix
|
||||
*
|
||||
* @param mat to transpose
|
||||
*/
|
||||
public void transposeM(Matrix4 mat) {
|
||||
strans(pointer, mat.pointer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set rotation
|
||||
*
|
||||
* @param a angle in degree
|
||||
* @param x around x-axis
|
||||
* @param y around y-axis
|
||||
* @param z around z-axis
|
||||
*/
|
||||
public void setRotation(float a, float x, float y, float z) {
|
||||
setRotation(pointer, a, x, y, z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set translation
|
||||
*
|
||||
* @param x along x-axis
|
||||
* @param y along y-axis
|
||||
* @param z along z-axis
|
||||
*/
|
||||
public void setTranslation(float x, float y, float z) {
|
||||
setTranslation(pointer, x, y, z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set scale factor
|
||||
*
|
||||
* @param x axis
|
||||
* @param y axis
|
||||
* @param z axis
|
||||
*/
|
||||
public void setScale(float x, float y, float z) {
|
||||
setScale(pointer, x, y, z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set translation and x,y scale
|
||||
*
|
||||
* @param tx translate x
|
||||
* @param ty translate y
|
||||
* @param scale factor x,y
|
||||
*/
|
||||
public void setTransScale(float tx, float ty, float scale) {
|
||||
setTransScale(pointer, tx, ty, scale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Matrix with glUniformMatrix
|
||||
*
|
||||
* @param location GL location id
|
||||
*/
|
||||
public void setAsUniform(int location) {
|
||||
setAsUniform(pointer, location);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set single value
|
||||
*
|
||||
* @param pos at position
|
||||
* @param value value to set
|
||||
*/
|
||||
public void setValue(int pos, float value) {
|
||||
setValueAt(pointer, pos, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* add some offset (similar to glDepthOffset)
|
||||
*
|
||||
* @param delta offset
|
||||
*/
|
||||
public void addDepthOffset(int delta) {
|
||||
addDepthOffset(pointer, delta);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set identity matrix
|
||||
*/
|
||||
public void setIdentity() {
|
||||
identity(pointer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finalize() {
|
||||
if (pointer != 0)
|
||||
delete(pointer);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user