我目前遇到的问题是 GLM 提供不稳定的转换和旋转。我的意思是他们并不顺利。我正在使用 GLFW 3、GLM 和 OpenGL 3.2。我正在尝试使用操纵杆输入来转换一个简单的四边形。起初,我认为问题是由操纵杆引起的。为了反驳这一点,我更改了代码以独立于用户输入旋转四边形,但依赖于 deltaTime。下面是一些代码段。我假设它与我如何旋转矩阵有关?
CPP 文件:
float speed = 3.0f;
float deadZone = 0.10f;
glm::vec2 direction(0.0f);
while (!glfwWindowShouldClose(window))
{
currentTime = glfwGetTime();
deltaTime = (currentTime - lastUpdate) * 1000.0f;
//Update logic
inputHandler->tick(deltaTime);
float inputX, inputY;
inputX = inputHandler->getGamepad(0)->getAxes(AXIS::HORIZONTAL);
inputY = inputHandler->getGamepad(0)->getAxes(AXIS::VERTICAL);
float magnitude = sqrtf((inputX * inputX) + (inputY * inputY));
if (magnitude > deadZone) //Radial dead zone detection
{
//Could the stutter be caused by type casting?
direction.x += inputX * float((speed * deltaTime) / 1000.0f);
direction.y += inputY * float((speed * deltaTime) / 1000.0f);
}
... //Set vertex values and UV coord
//Set the matrix values according to the joystick input
glm::mat4 MVP(1.0f);
MVP = glm::translate(MVP, glm::vec3(direction, 0.0f));
glUniformMatrix4fv(mvpID, 1, GL_FALSE, glm::value_ptr(MVP));
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
...
}
顶点着色器:
#version 150
in vec2 vertexPosition_modelspace;
in vec2 UV;
uniform mat4 MVP;
out vec2 vUV;
void main() {
vUV = UV;
gl_Position = MVP * vec4(vertexPosition_modelspace, 0, 1);
}