OpenGL旋转,旋错方向,有什么不对?

时间:2018-04-08 00:34:43

标签: opengl glm-math

我有一个非常简单的金字塔形状,由6个以这种方式定义的三角形组成:

unsigned int indices[] = {
    0, 1, 2, // front triange
    0, 3, 4, // back triangle
    0, 3, 1, // left side triangle
    0, 2, 4, // right side triangle
    1, 3, 4, // square base triangle 1
    4, 2, 1  // square base triangle 2
};

float pyramid[] = {
    0.0f,  1.0f,  0.0f, // top of the pyramid

// base vertices (a square)
   -1.0f, -1.0f,  1.0f, // left front
    1.0f, -1.0f,  1.0f, // right front
   -1.0f, -1.0f, -1.0f, // left back
    1.0f, -1.0f, -1.0f, // right back
};

我试图围绕Y轴旋转它,但是当我这样做时,它不是连续旋转,它最后向左旋转一点,然后向右旋转,左右旋转,左右旋转等等,但从未完全旋转360度。

我的轮换代码是:

glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(0.7f));
glm::mat4 rotate = glm::rotate(glm::mat4(1.0f), angle, glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 worldView = rotate * scale;

glUniformMatrix4fv(hWorld, 1, GL_TRUE, glm::value_ptr(worldView));

angle我在每个循环中递增它:

static float angle = 0.0f;
angle += 0.001f;

我放弃了,所以我希望有人可以解释我的错误。

我的顶点着色器:

#version 330

layout (location = 0) in vec3 pos;

uniform mat4 world;
out vec4 color;

void main()
{
    gl_Position = world * vec4(0.5 * pos, 1.0f);
    color = 8 * vec4(clamp(pos, 0.0f, 0.1f), 1.0f);
}

1 个答案:

答案 0 :(得分:0)

所以问题是我混淆了顶点数组:

unsigned int indices[] = {
    0, 1, 2, // front triange
    0, 3, 4, // back triangle
    0, 3, 1, // left side triangle
    0, 2, 4, // right side triangle
    1, 3, 4, // square base triangle 1
    4, 2, 1  // square base triangle 2
};

应该是:

   // index array
    unsigned int indices[] = {
        0, 1, 2,
        0, 3, 1,
        0, 4, 3,
        0, 2, 4,
        1, 3, 4,
        4, 2, 1 
    };

顶点的顺序应该<逆>逆时针。