OpenGL旋转matricies和着色器的问题

我在我的opengl着色器中遇到了旋转问题。 我的程序工作正常之前,我添加我的旋转matrix。 这里是一个例子,在YouTube 。 在给我的着色器添加旋转之后,会出现问题。 这是一个例子 。


这是我的顶点着色器:

#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 } 

以下是我的matrix的值:

 // 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); 

这是我的转换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; 

我究竟做错了什么?

你在matrix初始化中偏离了1。 matrix行是4个条目宽(4x4matrix),但你正在初始化他们,就像他们3条目宽。

 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; 

应该

 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; 

等等。

 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; 

应该

 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; 

y_rotation_matrix相同的问题。