0
votes

I really don't understand what I am doing wrong in the perspective matrix creation code:

public static Matrix4f perspective(float fov, float width, float height,
            float Near, float Far) {
        Matrix4f projection = new Matrix4f(0);

        float a = width / height;
        float h = 1.0f/(float) Math.tan(Math.toRadians(fov / 2));
        float depth = Near - Far;

        // x&y
        projection.values[0][0] = h/a;
        projection.values[1][1] = h;
        projection.values[2][2] = ((Far + Near)/depth);
        projection.values[2][3] = 2.0f *(Far * Near) / depth;
        projection.values[3][2] = -1.0f;

        return projection;
    }

Then when I send it to the shader I multiply it before with the modelview matrix like this:

math.mul(camera.getMatrix(), mesh.transform.getMatrix());

this means

P*M

in other languages that let you override operators, since I don't have a View Matrix from the camera.

What happens is that the 3d model is flat and when I rotate it, it gets really distorted. I tried the whole day to figure out what is wrong, but I had no luck.

What am I doing wrong ? Is there something I forgot about ?

EDIT:

Maybe there is something wrong with my multiplication code:

Multiplication code:

public static Matrix4f mul(Matrix4f m1, Matrix4f m2) {
        Matrix4f result = new Matrix4f();
        for (int j = 0; j < 4; j++) {
            for (int i = 0; i < 4; i++) {
                float sum = 0;

                for (int n = 0; n < 4; n++) {
                    sum += m1.values[n][i] * m2.values[j][n];
                }

                result.values[j][i] = sum;
            }
        }
        return result;
    }

I even tried to swap the multiplication order from P * M to M * P but it still don't solve the problem.

I tried to transpose the matrix after multiplication:

Transpose code:

 public static Matrix4f transpose(Matrix4f data)
        {
            Matrix4f result = new Matrix4f();
            for(int y = 0; y < 4; y++)
            {
                for(int x = 0; x < 4; x++)
                {
                    result.values[x][y] = data.values[y][x];
                }
            }
            return result;
        }

But that makes even worse distorsions.

EDIT:

Here are the raw printed memory contents of the projection matrix:

matrix[0][0] = 1.071111
matrix[0][1] = 0.0
matrix[0][2] = 0.0
matrix[0][3] = 0.0
matrix[1][0] = 0.0
matrix[1][1] = 1.428148
matrix[1][2] = 0.0
matrix[1][3] = 0.0
matrix[2][0] = 0.0
matrix[2][1] = 0.0
matrix[2][2] = -1.00002
matrix[2][3] = -0.0200002
matrix[3][0] = 0.0
matrix[3][1] = 0.0
matrix[3][2] = -1.0
matrix[3][3] = 0.0

Here is the ModelView Matrix (transformation)(The model position = 0, 0, 5):

model[0][0] = 1.0
model[0][1] = 0.0
model[0][2] = 0.0
model[0][3] = 0.0
model[1][0] = 0.0
model[1][1] = 1.0
model[1][2] = 0.0
model[1][3] = 0.0
model[2][0] = 0.0
model[2][1] = 0.0
model[2][2] = 1.0
model[2][3] = 5.0
model[3][0] = 0.0
model[3][1] = 0.0
model[3][2] = 0.0
model[3][3] = 1.0

Here is M*P:

matrix[0][0] = 1.071111
matrix[0][1] = 0.0
matrix[0][2] = 0.0
matrix[0][3] = 0.0
matrix[1][0] = 0.0
matrix[1][1] = 1.428148
matrix[1][2] = 0.0
matrix[1][3] = 0.0
matrix[2][0] = 0.0
matrix[2][1] = 0.0
matrix[2][2] = -1.00002
matrix[2][3] = -5.0201
matrix[3][0] = 0.0
matrix[3][1] = 0.0
matrix[3][2] = -1.0
matrix[3][3] = -5.0

Here is P*M:

matrix[0][0] = 1.071111
matrix[0][1] = 0.0
matrix[0][2] = 0.0
matrix[0][3] = 0.0
matrix[1][0] = 0.0
matrix[1][1] = 1.428148
matrix[1][2] = 0.0
matrix[1][3] = 0.0
matrix[2][0] = 0.0
matrix[2][1] = 0.0
matrix[2][2] = -6.00002
matrix[2][3] = -0.0200002
matrix[3][0] = 0.0
matrix[3][1] = 0.0
matrix[3][2] = -1.0
matrix[3][3] = 0.0
1
From what I can tell of the Java documentation, Matrix4f is row-major. You should consider swapping those operands (M * P).Andon M. Coleman
The Matrix4f is not the java one, its a class made by me. I will still try to swap the multiplication.user5819
@user5819: can you print the raw memory contents of the final matrix, just as 16 floats, in the order the are stored in memory?derhass
I added them. Maybe someone can give me a matrix so I have something to compare with.user5819
@user5819: Well. I actually meant the final multiplied P * M or M * P matrix. With your projection matrix so far, on thing still is weird: your code sets matrix[3[2] = -1, but that elements appaers to be 1 now.derhass

1 Answers

0
votes

The answer belong to @derhass:

"From the numbers given, it looks like M*P is the correct result. However, you store the matrices transposed to what GL (somewhat) defaults to. So either you use vec4 * mat4 in the shader, and it should work, or you have to transpose the result for the GL and can use the default mat4 * vec4 convetion"