OpenGL Normal Matrix - android

I've been trying to create simple diffuse lighting shader for Android using OpenGL ES 2.0 and getting knowledge from OpenGL 4.0 Shading language cookbook but it doesn't tell much about normal matrix and I am pretty sure the problem comes from it because the "model" I've been using is working perfectly in WebGL where I can use that nice glMatrix lib that I can't find for Java.
I am not sure how can I get normal matrix from model view matrix but I read its just transpose of inverted 3x3 piece of modelview matrix, too bad Android Matrix class only lets you work with 4x4 matrices(right?) so I've been splitting the matrix up in the shader which is probably where I go wrong.
So what I do is simply this:
float[] nMatrix = new float[4 * 4];
Matrix.invertM(nMatrix, 0, mvMatrix, 0);
Matrix.transposeM(nMatrix, 0, nMatrix, 0);
glUniformMatrix4fv(shader.getUniformPointer("nMatrix"), 1, false, nMatrix, 0);
and then at my vertex shader I do this:
tNorm = normalize(mat3(nMatrix) * vNormal).xyz;
and the rest of the code is basically from the book and the results are below
As you can see some sides of the cube are compeltely dark, and I am sure I got all the normals even thought I don't know any Android GL debugger, but if you know one, feel free to tell me about it.
So the question is, how can I properly get the normal matrix from my modelview matrix?

I don't where you could find a Matrix library in Java to help you with this.
But, as long as your modelView matrix does not contain non-uniform scales, you can safely use your modelView matrix instead of the normalMatrix.
This could help you get started and make sure that your problem is not hidden elsewhere.

This might be useful
Port of some useful mat3 functions of glm library
package com.CosmicCreations;
public class Mat3x3 {
public static float determinant(float []m){
return
+ m[0] * (m[4] * m[8] - m[7] * m[5])
- m[3] * (m[1] * m[8] - m[7] * m[2])
+ m[6] * (m[1] * m[5] - m[4] * m[2]);
}
public static void Mat3(float []m4, float[]m){
m[0]=m4[0]; m[1]=m4[1]; m[2]=m4[2];
m[3]=m4[4]; m[4]=m4[5]; m[5]=m4[6];
m[6]=m4[8]; m[7]=m4[9]; m[8]=m4[10];
}
/*
Inverse[0][0] = + (m[1][1] * m[2][2] - m[2][1] * m[1][2]);
Inverse[1][0] = - (m[1][0] * m[2][2] - m[2][0] * m[1][2]);
Inverse[2][0] = + (m[1][0] * m[2][1] - m[2][0] * m[1][1]);
Inverse[0][1] = - (m[0][1] * m[2][2] - m[2][1] * m[0][2]);
Inverse[1][1] = + (m[0][0] * m[2][2] - m[2][0] * m[0][2]);
Inverse[2][1] = - (m[0][0] * m[2][1] - m[2][0] * m[0][1]);
Inverse[0][2] = + (m[0][1] * m[1][2] - m[1][1] * m[0][2]);
Inverse[1][2] = - (m[0][0] * m[1][2] - m[1][0] * m[0][2]);
Inverse[2][2] = + (m[0][0] * m[1][1] - m[1][0] * m[0][1]);
Inverse /= Determinant;
*/
public static void inverse(float []m, float[] Inverse, int offset){
float Determinant = Mat3x3.determinant(m);
Inverse[offset+0] = + (m[4] * m[8] - m[7] * m[5])/ Determinant;
Inverse[offset+3] = - (m[3] * m[8] - m[6] * m[5])/ Determinant;
Inverse[offset+6] = + (m[3] * m[7] - m[6] * m[4])/ Determinant;
Inverse[offset+1] = - (m[1] * m[8] - m[7] * m[2])/ Determinant;
Inverse[offset+4] = + (m[0] * m[8] - m[6] * m[2])/ Determinant;
Inverse[offset+7] = - (m[0] * m[7] - m[6] * m[1])/ Determinant;
Inverse[offset+2] = + (m[1] * m[5] - m[4] * m[2])/ Determinant;
Inverse[offset+5] = - (m[0] * m[5] - m[3] * m[2])/ Determinant;
Inverse[offset+8] = + (m[0] * m[4] - m[3] * m[1])/ Determinant;
}
public static void transpose(float []m, int offset, float[]result){
result[0] = m[offset+0];
result[1] = m[offset+3];
result[2] = m[offset+6];
result[3] = m[offset+1];
result[4] = m[offset+4];
result[5] = m[offset+7];
result[6] = m[offset+2];
result[7] = m[offset+5];
result[8] = m[offset+8];
}
}
It should be used like this -
// Invert + transpose of mvmatrix
float []temp = new float[18];
Mat3x3.Mat3(mMVMatrix, temp);
Mat3x3.inverse(temp, temp, 9);
Mat3x3.transpose(temp, 9, normalMatrix);

Related

How to get the angle from quartnenion value?

Using android phone, I am able to get quarternion value x,y,z from TYPE_ROTATION_VECTOR
However, I do not know how to get the angle.
Anyone have any idea on this? Thank you.
Taken from here
/**
* Returns the heading, attitude and bank of this quaternion as euler angles in the double array respectively
*
* #return An array of size 3 containing the euler angles for this quaternion
*/
public double[] toEulerAngles() {
double[] ret = new double[3];
ret[0] = Math.atan2(2 * points[1] * getW() - 2 * points[0] * points[2], 1 - 2 * (points[1] * points[1]) - 2
* (points[2] * points[2])); // atan2(2*qy*qw-2*qx*qz , 1 - 2*qy2 - 2*qz2)
ret[1] = Math.asin(2 * points[0] * points[1] + 2 * points[2] * getW()); // asin(2*qx*qy + 2*qz*qw)
ret[2] = Math.atan2(2 * points[0] * getW() - 2 * points[1] * points[2], 1 - 2 * (points[0] * points[0]) - 2
* (points[2] * points[2])); // atan2(2*qx*qw-2*qy*qz , 1 - 2*qx2 - 2*qz2)
return ret;
}

Visualize sound to show in Bar Graph

I have created sound visualization using felixpalmer library. Now i have to modify his code to look like this.How could i achieve empty spaces between bars. like
I will handle the color scheme but don't know how to add empty spaces in bars and shadow effect.
BarGraphRender
public class BarGraphRenderer extends Renderer
{
private int mDivisions;
private Paint mPaint;
private boolean mTop;
/**
* Renders the FFT data as a series of lines, in histogram form
* #param divisions - must be a power of 2. Controls how many lines to draw
* #param paint - Paint to draw lines with
* #param top - whether to draw the lines at the top of the canvas, or the bottom
*/
public BarGraphRenderer(int divisions , Paint paint , boolean top)
{
super();
mDivisions = divisions;
mPaint = paint;
mTop = top;
}
#Override
public void onRender(Canvas canvas, AudioData data, Rect rect)
{
// Do nothing, we only display FFT data
}
#Override
public void onRender(Canvas canvas, FFTData data, Rect rect)
{
for (int i = 0; i < data.bytes.length / mDivisions; i++)
{
mFFTPoints[i * 4] = i * 4 * mDivisions;
mFFTPoints[i * 4 + 2] = i * 4 * mDivisions;
byte rfk = data.bytes[mDivisions * i];
byte ifk = data.bytes[mDivisions * i + 1];
float magnitude = (rfk * rfk + ifk * ifk);
int dbValue = (int) (10 * Math.log10(magnitude));
if(mTop)
{
mFFTPoints[i * 4 + 1] = 0;
mFFTPoints[i * 4 + 3] = (dbValue * 2 - 10);
}
else
{
mFFTPoints[i * 4 + 1] = rect.height();
mFFTPoints[i * 4 + 3] = rect.height() - (dbValue * 2 - 10);
}
}
canvas.drawLines(mFFTPoints, mPaint);
}
}

Android Google maps Spatial Coordinates: From GCJ-02

I am provided a LatLng that belongs to Spatial Reference GCJ-02.
Of course it does not show up properly in google maps, since i beleive, correct me if i am wrong, google maps uses WGS-84.
Does the google map service v2 for android provide a way to display or transform GCJ-02 ?
Is my only option using geo tool? I do not want to bring in such huge library for just spatial transformation.
Thanks for all your help,
Kev
Around the time the question was asked, the only way to convert between GCJ-02 and WGS-84 was to use an interpolation method with coordinates based on regression from a data set of Google China and satellite imagery coordinates. There is an Objective-C library on GitHub at https://github.com/maxime/ChinaMapDeviation.
However, since the question was asked, the GCJ-02 encryption code was leaked and can be found in many places online, the most popular being the EvilTransform repo.
Further reading:
What causes the GPS shift in China?
Restrictions on geographic data in China
public static LatLng wgs_gcj_encrypts(double wgLat, double wgLon) {
LatLng point;
if (outOfChina(wgLat, wgLon)) {
Log.e("Baidu", "outOfChina");
point = new LatLng(wgLat,wgLon);
return point;
}
double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
double dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
double radLat = wgLat / 180.0 * pi;
double magic = Math.sin(radLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
double lat = wgLat + dLat;
double lon = wgLon + dLon;
point = new LatLng(lat,lon);
return point;
}
private static boolean outOfChina(double lat, double lon) {
if (lon < 72.004 || lon > 137.8347)
return true;
if (lat < 0.8293 || lat > 55.8271)
return true;
return false;
}
public static double transformLat(double x, double y) {
double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
return ret;
}
public static double transformLon(double x, double y) {
double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;
return ret;
}
Google maps will accept 4326 but if you really want to be correct - read this post
https://gis.stackexchange.com/questions/253/what-is-the-current-web-mercator-projection-code
You can use proj4 or store your data in PostGIS ( It can do the transform for you). I don't know of any library on the android client that could do this but perhaps this could the trick for you:
http://www.jhlabs.com/java/maps/proj/

Animation Along a path with Auto Rotation based on the path in android

I have been trying to animate an image of a fly which moves in a path like the following image(which i have added for a clear idea) in android version 2.2
Well,this can be done in a very simple manner in the iphone as they have a property forsetting this auto rotation after the path is drawn using
animation.rotationMode = kCAAnimationRotateAuto;
which i believe would rotate the object based on the path`
I am able to animate ma fly through this path using the nineoldandroid library using the methods
path.moveTo(float x, float y);
path.lineTo(float x, float y);
path.curveTo(float c0X, float c0Y, float c1X, float c1Y, float x, float y);
Such that the curves are drawn through cubic B�zier curve.
Now what i have been trying is to implement something that would allow my fly to rotate itself along the path and i just cant seem to reach anywhere.
Please Help Me out with some ideas!!! :( :(
You have to download the demo and the lib of nineoldandroids and these 4 java files if you want to use my solution
That was easy, I modified the evaluator in the demo of nineoldandroids.
It's too much to post here:
Just to get the idea:
I extend the PathPoint with the field angle.
Then write all calculated Points in a stack (a simple float[][])
After the first calculation the angle can be calculated by the atan and the last 2 points in the stack.
If you don't want to use a stack you can modify the timeparam and look forward to where the next point will be drawn and calculate the angle out of these.
Just think about:
Do you first watch where you are walking to and then walk or do you just walk and then chose the angle for the destination. It's not neccessary since we have display densities that high and calculating the angle for each pixel.
Here's the PathEvaluator
public class PathEvaluatorAngle implements TypeEvaluator<PathPointAngle> {
private static final int POINT_COUNT = 5000;
private float[][] stack = new float[POINT_COUNT][2];
private int stackC = 0;
#Override
public PathPointAngle evaluate(float t, PathPointAngle startValue, PathPointAngle endValue) {
float x, y;
if (endValue.mOperation == PathPointAngle.CURVE) {
float oneMinusT = 1 - t;
x = oneMinusT * oneMinusT * oneMinusT * startValue.mX +
3 * oneMinusT * oneMinusT * t * endValue.mControl0X +
3 * oneMinusT * t * t * endValue.mControl1X +
t * t * t * endValue.mX;
y = oneMinusT * oneMinusT * oneMinusT * startValue.mY +
3 * oneMinusT * oneMinusT * t * endValue.mControl0Y +
3 * oneMinusT * t * t * endValue.mControl1Y +
t * t * t * endValue.mY;
} else if (endValue.mOperation == PathPointAngle.LINE) {
x = startValue.mX + t * (endValue.mX - startValue.mX);
y = startValue.mY + t * (endValue.mY - startValue.mY);
} else {
x = endValue.mX;
y = endValue.mY;
}
stack[stackC][0] = x;
stack[stackC][1] = y;
double angle;
if (stackC == 0){
angle = 0;
} else if (stackC >= POINT_COUNT){
throw new IllegalStateException("set the stack POINT_COUNT higher!");
} else {
angle = Math.atan(
(stack[stackC][1] - stack[stackC-1][1]) /
(stack[stackC][0] - stack[stackC-1][0])
) * 180d/Math.PI;
}
stackC++;
return PathPointAngle.moveTo(x, y, angle);
}
}
Please check the below link.Hope it will help.
https://github.com/JakeWharton/NineOldAndroids

Calculation of proper Rotation Values from Gyroscope Sensor in Android

I am working on rotating the rectangle using the orientation values from gyroscope sensors in Android 3.1 device.
I have to rotate my device very fast to get values 1.0 and more.
Here is the code
final float currentRotVector[] = { 1, 0, 0, 0 };
if (timestamp != 0)
{
final float dT = (event.timestamp - timestamp) * NS2S;
// Axis of the rotation sample, not normalized yet.
// Calculate the angular speed of the sample
float omegaMagnitude = (float) Math.sqrt(X * X + Y * Y + Z * Z);
// Normalize the rotation vector if it's big enough to get the axis
if (omegaMagnitude > EPSILON)
{
X /= omegaMagnitude;
Y /= omegaMagnitude;
Z /= omegaMagnitude;
}
// Integrate around this axis with the angular speed by the timestep
// in order to get a delta rotation from this sample over the timestep
// We will convert this axis-angle representation of the delta rotation
// into a quaternion before turning it into the rotation matrix.
float thetaOverTwo = dT * omegaMagnitude / 2.0f;
float sinThetaOverTwo = (float) Math.sin(thetaOverTwo);
float cosThetaOverTwo = (float) Math.cos(thetaOverTwo);
deltaRotationVector[0] = cosThetaOverTwo;
deltaRotationVector[1] = sinThetaOverTwo * X;
deltaRotationVector[2] = sinThetaOverTwo * Y;
deltaRotationVector[3] = sinThetaOverTwo * Z;
/* quaternion multiplication
Reference: http://www.cprogramming.com/tutorial/3d/quaternions.html
*/
currentRotVector[0] = deltaRotationVector[0] * currentRotVector[0] -
deltaRotationVector[1] * currentRotVector[1] -
deltaRotationVector[2] * currentRotVector[2] -
deltaRotationVector[3] * currentRotVector[3];
currentRotVector[1] = deltaRotationVector[0] * currentRotVector[1] +
deltaRotationVector[1] * currentRotVector[0] +
deltaRotationVector[2] * currentRotVector[3] -
deltaRotationVector[3] * currentRotVector[2];
currentRotVector[2] = deltaRotationVector[0] * currentRotVector[2] -
deltaRotationVector[1] * currentRotVector[3] +
deltaRotationVector[2] * currentRotVector[0] +
deltaRotationVector[3] * currentRotVector[1];
currentRotVector[3] = deltaRotationVector[0] * currentRotVector[3] +
deltaRotationVector[1] * currentRotVector[2] -
deltaRotationVector[2] * currentRotVector[1] +
deltaRotationVector[3] * currentRotVector[0];
final float rad2deg = (float) (180.0f / Math.PI);
RotAngle = currentRotVector[0] * rad2deg;
axisX = currentRotVector[1];
axisY = currentRotVector[2];
axisZ = currentRotVector[3];
Log.i("Sensor Orientation GyroScope", "axisX: " + axisX + //
" axisY: " + axisY + //
" axisZ: " + axisZ + //
" RotAngle: " + RotAngle);
}
timestamp = event.timestamp;
I am getting some outputs like
axisX: 0.69363713 axisY: 0.18359372 axisZ: 0.0228636 RotAngle: 36.7191
And because of the axis values, the output rectangle looked tweaked when the device is lay down on the table.
Is there any problem in the above code?
The values are measeured in rad/s. This has been standarized starting from Android 2.3
To get values of about 1.0 you have to turn at a speed of almost 60 deg/s
Some devices having previous Android versions return (returned) values in degrees/s, but these are just a few. As an example, the LG Optimus Black (P970) with android 2.2 is one of these devices returning deg/s, but this is not the common case.

Categories

Resources