OpenGL旋转在90度和270度之间表现怪异

我是OpenGL的新手,我试图通过编写自己的简单引擎来学习它。 到目前为止,我已经实现了纹理映射,世界,视图和透视矩阵。 但是我注意到了一个奇怪的旋转行为。 我有一个具有旋转属性的网格类(围绕x,y和z轴)。 当我改变这些属性时,对象的旋转矩阵被重新计算,然后在顶点着色器中应用它来转换顶点。 当我以这种方式在0 - 90度和270 - 360度之间旋转网格时,一切看起来都很好,但是如果旋转角度在90 - 270度之间,距相机更远的三角形开始变大,而不是比那些更接近的更小。 然而,更接近的三角形绘制在更远的三角形前面,因此z坐标应该很好。 当旋转继续时,一旦达到270度,所有东西都可以无缝地恢复到正常状态。 另外,当我在我的代码中使用旋转矩阵和函数(不在着色器中)时,一切正常,只有在着色器中应用旋转矩阵时才会出现问题。

我提供了一张图片和一个视频:

在这里输入图像描述 https://www.youtube.com/watch?v=jJLT4DTEYyQ&feature=youtu.be

我的顶点着色器是:

#version 330                                                 
layout (location = 0) in vec3 position;                      
layout (location = 1) in vec2 texture_coordination;          
layout (location = 2) in vec3 normal;                        
uniform mat4 perspective_matrix;                             
uniform mat4 world_matrix;                                   
uniform mat4 view_matrix;                                    
out vec2 uv_coordination;

void main()                                                  
{                                                            
  gl_Position = perspective_matrix * view_matrix * world_matrix * vec4(position, 1.0);
  uv_coordination = texture_coordination;                    
}  

我做这样的矩阵:

void make_rotation_matrix(float degrees_x, float degrees_y, float degrees_z, float matrix[4][4])

{
  degrees_x *= PI_DIVIDED_180;  // convert to radians
  degrees_y *= PI_DIVIDED_180;
  degrees_z *= PI_DIVIDED_180;

  float sin_x = sin(degrees_x);
  float cos_x = cos(degrees_x);
  float sin_y = sin(degrees_y);
  float cos_y = cos(degrees_y);
  float sin_z = sin(degrees_z);
  float cos_z = cos(degrees_z);

  matrix[0][0] = cos_z * cos_y;
  matrix[1][0] = cos_z * sin_y * sin_x - sin_z * cos_x;
  matrix[2][0] = cos_z * sin_y * cos_x + sin_z * sin_x;
  matrix[3][0] = 0;

  matrix[0][1] = sin_z * cos_y;
  matrix[1][1] = sin_z * sin_y * sin_x + cos_z * cos_x;
  matrix[2][1] = sin_z * sin_y * cos_x - cos_z * sin_x;
  matrix[3][1] = 0;

  matrix[0][2] = -1 * sin_y;
  matrix[1][2] = cos_y * sin_x;
  matrix[2][2] = cos_y * cos_y;
  matrix[3][2] = 0;

  matrix[0][3] = 0;
  matrix[1][3] = 0;
  matrix[2][3] = 0;
  matrix[3][3] = 1;
}

void make_perspective_matrix(float fov_degrees, float near_plane, float far_plane, float matrix[4][4])

{
  float aspect_ratio = global_window_width / ((float) global_window_height);
  float range = near_plane - far_plane;
  float tan_half_fov = tanf(fov_degrees / 2.0 * PI_DIVIDED_180); 

  matrix[0][0] = 1.0f / (tan_half_fov * aspect_ratio) ; matrix[1][0] = 0.0f; matrix[2][0] = 0.0f; matrix[3][0] = 0.0f;
  matrix[0][1] = 0.0f; matrix[1][1] = 1.0f / tan_half_fov; matrix[2][1] = 0.0f; matrix[3][1] = 0.0f;
  matrix[0][2] = 0.0f; matrix[1][2] = 0.0f; matrix[2][2] = (-1 * near_plane - far_plane) / (float) range; matrix[3][2] = 1.0;
  matrix[0][3] = 0.0f; matrix[1][3] = 0.0f; matrix[2][3] = 2.0f * far_plane * near_plane / (float) range; matrix[3][3] = 0.0f; 
}

void multiply_matrices(float matrix_a[4][4], float matrix_b[4][4], float matrix_result[4][4])

{
  unsigned int i,j;

  for (j = 0; j < 4; j++)
    for (i = 0; i < 4; i++)
      matrix_result[i][j] =
        matrix_b[0][j] * matrix_a[i][0] +
        matrix_b[1][j] * matrix_a[i][1] +
        matrix_b[2][j] * matrix_a[i][2] +
        matrix_b[3][j] * matrix_a[i][3];
}

void mesh_3d::update_transformation_matrix()

{
  float helper_matrix[4][4];

  multiply_matrices(this->translation_matrix,this->rotation_matrix,helper_matrix);
  multiply_matrices(helper_matrix,this->scale_matrix,this->transformation_matrix);
}

我画这样的网格:

void mesh_3d::draw()

{
  glUniformMatrix4fv(world_matrix_location,1,GL_TRUE,(const GLfloat *)this->transformation_matrix); // load this model's transformation matrix

  glEnableVertexAttribArray(0);
  glEnableVertexAttribArray(1);
  glEnableVertexAttribArray(2);

  if (this->texture != NULL)
    {
      glActiveTexture(GL_TEXTURE0);      // set the active texture unit to 0
      glBindTexture(GL_TEXTURE_2D,this->texture->get_texture_object());
    }

  glBindBuffer(GL_ARRAY_BUFFER,this->vbo);
  glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,sizeof(vertex_3d),0);                   // position
  glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,sizeof(vertex_3d),(const GLvoid*) 12);  // texture coordination
  glVertexAttribPointer(2,3,GL_FLOAT,GL_FALSE,sizeof(vertex_3d),(const GLvoid*) 20);  // normal

  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,this->ibo);

  glDrawElements(GL_TRIANGLES,this->triangles.size() * 3,GL_UNSIGNED_INT,0);
  glDisableVertexAttribArray(0);
  glDisableVertexAttribArray(1);
  glDisableVertexAttribArray(2);
}

当然,网格默认情况下会将其日历和翻译矩阵设置为标识。

我一直坚持这几天,我不知道如何继续前进,如果有人能够至少指出什么是错的,我会很高兴。

我已经打印出0和122度的矩阵值:

0度:

模型(世界):

1 0 0 0
0 1 0 0
0 0 1 0
0 0 3 1

视图:

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

投影(透视):

0.75 0 0 0
0 1 0 0
0 0 1.06186 1
0 0 -0.618557 0

122度:

模型(世界):

-0.529919 0 -0.848048 0
0 1 0 0
0.848048 0 0.280814 0
0 0 3 1

观点:和以前一样

投影(透视):与之前一样


当我尝试复制它们时,一些矩阵计算与我得到的结果不相符。 用你的符号,我得到了旋转矩阵的以下内容:

matrix[0][0] = cos_z * cos_y;
matrix[1][0] = cos_z * sin_y * sin_x + sin_z * cos_x;
matrix[2][0] = -cos_z * sin_y * cos_x + sin_z * sin_x;
matrix[3][0] = 0.0f;

matrix[0][1] = -sin_z * cos_y;
matrix[1][1] = -sin_z * sin_y * sin_x + cos_z * cos_x;
matrix[2][1] = sin_z * sin_y * cos_x + cos_z * sin_x;
matrix[3][1] = 0.0f;

matrix[0][2] = sin_y;
matrix[1][2] = -cos_y * sin_x;
matrix[2][2] = cos_y * cos_x;
matrix[3][2] = 0.0f;

如果您将结果与您的结果进行比较,则主要只是标志差异。 看起来你用左手旋转,而我用右手,这改变了所有sin_[xyz]值的符号。 matrix[2][2]虽然非常不同,但看起来你在那里有一个错字。

您的投影矩阵也看起来不标准。 在通常使用的OpenGL投影矩阵(例如匹配红皮书)中,这些矩阵元素是不同的:

matrix[2][2] = (near_plane + far_plane) / range;
matrix[3][2] = -1.0f;
链接地址: http://www.djcxy.com/p/81749.html

上一篇: OpenGL rotation behaves weird between 90 and 270 degrees

下一篇: Object Transformations and VBOs