Geosearchef
Geosearchef

Reputation: 648

Can't give Matrix to Shader as uniform

I'm currently implementing matrices in my enigne. With standard glTranslate and glRotate and then ftransform() in the shader it works. Done manually not.

How i give Matrix to the Shader:

public static void loadMatrix(int location, Matrix4f matrix)
{
    FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
    matrix.store(buffer);
    buffer.flip();
    glUniformMatrix4(location, false, buffer);
}

Sending viewMatrix:

shaderEngine.loadMatrix(glGetUniformLocation(shaderEngine.standard, "viewMatrix"), camera.viewMatrix);
    shaderEngine.loadMatrix(glGetUniformLocation(shaderEngine.obj, "viewMatrix"), camera.viewMatrix);
    System.out.println(camera.viewMatrix.toString());

In the shader i get it with:

uniform mat4 viewMatrix;

and in the shaders main i set the frag color:

gl_FragColor = vec4(viewMatrix[0][3] / -256,0,0,1);

Which is BLACK (so viewMatrix[0][3] == 0) while my matrix output in java looks like this:

1.0 0.0 0.0 -128.0
0.0 1.0 0.0 -22.75
0.0 0.0 1.0 -128.0
0.0 0.0 0.0 1.0

Upvotes: 0

Views: 245

Answers (2)

Geosearchef
Geosearchef

Reputation: 648

My problem was that i was giving the matrices to the shader when the used program was 0.

Upvotes: 0

Andon M. Coleman
Andon M. Coleman

Reputation: 43369

Your confusion seems to come from how array subscripts address the elements of a matrix in GLSL. The first subscript is the column and the second is the row.

  Thus, unless you transpose your matrix, column 1 row 4 == 0.0.

  If you transpose your matrix or swap the subscripts, you will get -128.0.

That second parameter in the call to glUniformMatrix4 (...) allows you to transpose the matrix before GLSL gets its hands on it, by the way. This will allow you to treat everything as row-major if that is more natural to you.

Upvotes: 1

Related Questions