takluiper - 1 year ago 40

Android Question

I have managed to do an small app using opengl2 in android. It consists on a sphere moving over a rectangle, both with a texture. It's just a test in which I'm testing my first steps in opengl.

Now I'm trying to move the camera over it. I multiplied my view matrix by a rotation matrix on y axis. And the result is ok, it works properly. But then I wanted to add a translation to the camera, so I applied a translation matrix and I think in this step is where I messed things up, maybe is not that simple. The code is: (I think multiplications are in the correct order, but not sure)

`double x = 0.01;`

float[] matRotationY = {(float)Math.cos(x), 0, (float)Math.sin(x), 0,

0,1,0,0,

(float)-Math.sin(x), 0, (float)Math.cos(x), 0,

0,0,0,1};

float[] matTranslationY = {1,0,0,0,

0,1,0,-0.01f,

0,0,1,0,

0,0,0,1};

Matrix.multiplyMM(mViewMatrix, 0, matRotationY, 0, mViewMatrix, 0);

Matrix.multiplyMM(mViewMatrix, 0, matTranslationY, 0, mViewMatrix, 0);

If you need more code, tell me and I'll edit the question. Sorry if the question is a little stupid, I'm beginning with it, and some advice would be great.

Answer Source

Matrices can be interprited in two different ways: Column major and Row major. Consider the following matrix:

```
[ 1, 5 ]
[ 0, 1 ]
```

Putting it in a `float[]`

as you did stores it in memory as such:

```
{ 1, 5, 0, 1 }
```

So essentially, every two sequential numbers form one *row* of the matrix - this is storing it in row-major format.

However, the multiplication functions that you use, namely `Matrix.multiplyMM`

uses every set of sequential numbers as *columns* of a matrix - so { 1, 5, 0, 1 } becomes

```
[ 1, 0 ]
[ 5, 1 ]
```

Notice it is a mirrored version of the original example matrix.

So, specifically in your case,

```
[ 1, 0, 0, 0 ]
[ 0, 1, 0, -0.01 ]
[ 0, 0, 1, 0 ]
[ 0, 0, 0, 1 ]
```

is understood by `Matrix.multiplyMM`

as

```
[ 1, 0, 0, 0 ]
[ 0, 1, 0, 0 ]
[ 0, 0, 1, 0 ]
[ 0, -0.01, 0, 1 ]
```