2
\$\begingroup\$

I am having an issue with my rotations in my opengl shaders. My program works fine before I add in my rotation matrices. Here is an example, at YouTube. After I add rotations to my shaders, there are issues. Here is an example.


Here is my vertex shader:

    #version 130

in vec3 position;
in vec4 color;
in vec2 texcoord;
uniform mat4 view_array;
uniform mat4 end_array;
uniform mat4 projection_matrix;
uniform mat4 trans_matrix;
uniform mat4 x_rotation_matrix;
uniform mat4 y_rotation_matrix;
uniform mat4 z_rotation_matrix;

out vec4 pos;
out vec2 texture_coordinate;
//out vec4 color_value;
void main()
{
     mat4 vw_matrix = mat4(1.0);
     mat4 rotation_matrix = x_rotation_matrix * y_rotation_matrix;
     mat4 model_matrix =  trans_matrix * rotation_matrix;//
     pos = projection_matrix * vw_matrix * model_matrix *  vec4(position, 1.0);
     gl_Position = pos;
     texture_coordinate = texcoord;
     //color_value = color
}

Here are the values for my matrices:

// rotation matrices

x_rotation_matrix[0] = 1;
x_rotation_matrix[1] = 0;
x_rotation_matrix[2] = 0;
x_rotation_matrix[3] = 0;
x_rotation_matrix[4] = (float) cos(-yaw * (PI / 180));
x_rotation_matrix[5] = (float) -sin(-yaw * (PI / 180));
x_rotation_matrix[6] = 0;
x_rotation_matrix[7] = (float) sin(-yaw * (PI / 180));
x_rotation_matrix[8] = (float) cos(-yaw * (PI / 180));
x_rotation_matrix[9] = 0;
x_rotation_matrix[10] = 1;
x_rotation_matrix[11] = 0;
x_rotation_matrix[12] = 0;
x_rotation_matrix[13] = 0;
x_rotation_matrix[14] = 0;
x_rotation_matrix[15] = 1;

y_rotation_matrix[0] = (float) cos(-pitch * (PI / 180));
y_rotation_matrix[1] = 0;
y_rotation_matrix[2] = (float) sin(-pitch * (PI / 180));
y_rotation_matrix[3] = 0;
y_rotation_matrix[4] = 0;
y_rotation_matrix[5] = 1;
y_rotation_matrix[6] = (float) -sin(-pitch * (PI / 180));
y_rotation_matrix[7] = 0;
y_rotation_matrix[8] = (float) cos(-pitch * (PI / 180));
y_rotation_matrix[9] = 0;
y_rotation_matrix[10] = 1;
y_rotation_matrix[11] = 0;
y_rotation_matrix[12] = 0;
y_rotation_matrix[13] = 0;
y_rotation_matrix[14] = 0;
y_rotation_matrix[15] = 1;

z_rotation_matrix[0] = (float) cos(-pitch * (PI / 180));
z_rotation_matrix[1] = (float) -sin(-pitch * (PI / 180));
z_rotation_matrix[2] = 0;
z_rotation_matrix[3] = (float) sin(-pitch * (PI / 180));
z_rotation_matrix[4] = (float) cos(-pitch * (PI / 180));
z_rotation_matrix[5] = 1;
z_rotation_matrix[6] = 0;
z_rotation_matrix[7] = 0;
z_rotation_matrix[8] = 0;
z_rotation_matrix[9] = 0;
z_rotation_matrix[10] = 1;
z_rotation_matrix[11] = 0;
z_rotation_matrix[12] = 0;
z_rotation_matrix[13] = 0;
z_rotation_matrix[14] = 0;
z_rotation_matrix[15] = 1;

GL20.glUniformMatrix4fv(
    GL20.glGetUniformLocation(programID, "x_rotation_matrix"), true, x_rotation_matrix);
GL20.glUniformMatrix4fv(
    GL20.glGetUniformLocation(programID, "y_rotation_matrix"), true, y_rotation_matrix);
GL20.glUniformMatrix4fv(
    GL20.glGetUniformLocation(programID, "z_rotation_matrix"), true, z_rotation_matrix);

Here is my transformation matrix:

trans_matrix[0] = 1;
trans_matrix[5] = 1;
trans_matrix[10] = 1;
trans_matrix[12] = x;
trans_matrix[13] = y;
trans_matrix[14] = z;
trans_matrix[15] = 1;

What am I doing wrong?

\$\endgroup\$
4
\$\begingroup\$

You're off by 1 in your matrix initialization. The matrix rows are 4 entries wide (4x4 matrix) but you're initializing them like they're 3 entries wide.

z_rotation_matrix[0] = (float) cos(-pitch * (PI / 180));// * (PI / 180));
z_rotation_matrix[1] = (float) -sin(-pitch * (PI / 180));// * (PI / 180));
z_rotation_matrix[2] = 0;
z_rotation_matrix[3] = (float) sin(-pitch * (PI / 180));// * (PI / 180));
z_rotation_matrix[4] = (float) cos(-pitch * (PI / 180));// * (PI / 180));
z_rotation_matrix[5] = 1;
z_rotation_matrix[6] = 0;

Should be

z_rotation_matrix[0] = (float) cos(-pitch * (PI / 180));// * (PI / 180));
z_rotation_matrix[1] = (float) -sin(-pitch * (PI / 180));// * (PI / 180));
z_rotation_matrix[2] = 0;
z_rotation_matrix[3] = 0;
z_rotation_matrix[4] = (float) sin(-pitch * (PI / 180));// * (PI / 180));
z_rotation_matrix[5] = (float) cos(-pitch * (PI / 180));// * (PI / 180));
z_rotation_matrix[6] = 0;

And so on.

x_rotation_matrix[4] = (float) cos(-yaw * (PI / 180));//);// * (PI / 180)
x_rotation_matrix[5] = (float) -sin(-yaw * (PI / 180));// * (PI / 180));
x_rotation_matrix[6] = 0;
x_rotation_matrix[7] = (float) sin(-yaw * (PI / 180));// * (PI / 180));
x_rotation_matrix[8] = (float) cos(-yaw * (PI / 180));// * (PI / 180));
x_rotation_matrix[9] = 0;
x_rotation_matrix[10] = 1;

Should be

x_rotation_matrix[4] = 0;
x_rotation_matrix[5] = (float) cos(-yaw * (PI / 180));//);// * (PI / 180)
x_rotation_matrix[6] = (float) -sin(-yaw * (PI / 180));// * (PI / 180));
x_rotation_matrix[7] = 0;
x_rotation_matrix[8] = 0;
x_rotation_matrix[9] = (float) sin(-yaw * (PI / 180));// * (PI / 180));
x_rotation_matrix[10] = (float) cos(-yaw * (PI / 180));// * (PI / 180));
x_rotation_matrix[11] = 0;
x_rotation_matrix[12] = 0;

Same issue with y_rotation_matrix.

\$\endgroup\$
  • \$\begingroup\$ Is this what you mean? [link] (pastebin.com/VXKYA6tG) \$\endgroup\$ – Robert Jan 24 '17 at 2:41
  • \$\begingroup\$ Y rotation is still off. change [6] to [8] and [8] to [10]. But X and Z seem fine now. \$\endgroup\$ – Stephane Hockenhull Jan 25 '17 at 2:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.