I am trying to build a model matrix where I apply some common transformations like scaling, rotation and translation. I was following a tutorial here: https://solarianprogrammer.com/2013/05/22/opengl-101-matrices-projection-view-model/. I know that matrixes are not commutative so I think I paid attention to get the correct order of transformations.
But when I try to translate my model it gets stretched into the corresponding axis instead of translating. Here is how it looks:
I would appreciate any help. :)
Vertex shader
# version 330
in vec3 in_pos;
in vec3 in_col;
in vec3 in_norm;
// uniform wie eine konstante, kann einmal per frame geaender werden und gillt fuer alle vertices
uniform mat4 mvp; // mat4 = 4x4 Matrix
// out stream to fragment shader
out vec4 frag_pos;
out vec4 frag_col;
out vec4 frag_norm;
// column major
void main() {
gl_Position = mvp * vec4(in_pos, 1.0);
frag_pos = vec4(in_pos,1.0);
// transparent frag_col = vec4(in_col,0.5);
frag_col = vec4(in_col,1);
frag_norm = vec4(in_norm,1.0);
}
Java transformations
float aspectRatio = (float) HEIGHT /(float)WIDTH;
float[][] rotX = new float[][] {
{1.0f,0.0f,0.0f,0.0f},
{0.0f,(float)Math.cos(thetaX),(float)-Math.sin(thetaX),0.0f},
{0.0f,(float)Math.sin(thetaX),(float)Math.cos(thetaX),0.0f},
{0.0f,0.0f,0.0f,1.0f}
};
float[][] rotY = new float[][] {
{(float)Math.cos(thetaY),0.0f,(float)Math.sin(thetaY),0.0f},
{0.0f,1.0f,0.0f,0.0f},
{(float)-Math.sin(thetaY),0.0f,(float)Math.cos(thetaY),0.0f},
{0.0f,0.0f,0.0f,1.0f}
};
float[][] rotZ = new float[][] {
{(float)Math.cos(thetaZ),(float)-Math.sin(thetaZ),0.0f,0.0f},
{(float)Math.sin(thetaZ),(float)Math.cos(thetaZ),0.0f,0.0f},
{0.0f,0.0f,1.0f,0.0f},
{0.0f,0.0f,0.0f,1.0f}
};
float[][] translation = new float[][] {
{1.0f,0.0f,0.0f,transX},
{0.0f,1.0f,0.0f,transY},
{0.0f,0.0f,1.0f,transZ},
{0.0f,0.0f,0.0f,1.0f}
};
float[][] scaleMatrix = new float[][] {
{scale,0.0f,0.0f,0.0f},
{0.0f,scale,0.0f,0.0f},
{0.0f,0.0f,scale,0.0f},
{0.0f,0.0f,0.0f,1.0f}
};
float[][] aspect = new float[][] {
{aspectRatio,0.0f,0.0f,0.0f},
{0.0f,1.0f,0.0f,0.0f},
{0.0f,0.0f,1.0f,0.0f},
{0.0f,0.0f,0.0f,1.0f}
};
float[][] rotationMatrix = Matrix.matMult(rotZ,Matrix.matMult(rotY,rotX));
float[][] matrix = Matrix.matMult(translation,Matrix.matMult(rotationMatrix,scaleMatrix));
float[] model = Matrix.matrixToFloatVector(Matrix.matMult(matrix,aspect));
FloatBuffer fb = BufferUtils.createFloatBuffer(16);
fb.put(model);
fb.flip(); // Reset pointer
// "upload" to graphics card
glUniformMatrix4fv(uLocMVP, false, fb);
See The OpenGL Shading Language 4.6, 5.4.2 Vector and Matrix Constructors, page 110:
To initialize a matrix by specifying vectors or scalars, the components are assigned to the matrix elements in column-major order.
mat4(float, float, float, float, // first column float, float, float, float, // second column float, float, float, float, // third column float, float, float, float); // fourth column
This means you have to transpose the matrix before setting the uniform variable of type mat4
.
This can be done automatically by setting the 3rd parameter of glUniformMatrix4fv
true
:
glUniformMatrix4fv(uLocMVP, true, fb)
Instead you can transpose the initialization and swap the matrix multiplications (Matrix.matMult
), too.