c++ - 使用键旋转对象

标签 c++ opengl rotation glfw glm-math

我想使用键旋转立方体。这是代码的一部分。当我按下 LEFT 键时,立方体向左旋转,等等。我的目标是旋转立方体,所以我必须通过 xy 轴旋转它,这会导致问题。

我已经定义了 mat4 rotation; 并在我按住一个键时用它来指定一个旋转。当我按住键时,它会旋转,例如向左旋转。然后我松开按键,物体回到初始位置(相机回到初始位置,因为物体没有移动)。我认为这个问题导致 auto rotateMat = rotation; 行定义在关键函数下方。 我做错了什么?

    mat4 rotation; //global 

    if(keysPressed[GLFW_KEY_LEFT]){
                timer -= delta;
                rotation = rotate(mat4{}, timer * 0.5f, {0, 1, 0});
    }

    if(keysPressed[GLFW_KEY_RIGHT]){
                timer += delta;
                rotation = rotate(mat4{}, timer * 0.5f, {0, 1, 0});
    }

   if(keysPressed[GLFW_KEY_UP]){
                timer += delta;
                rotation = rotate(mat4{}, timer * 0.5f, {1, 0, 0});
   }

   if(keysPressed[GLFW_KEY_DOWN]){
                timer -= delta;
                rotation = rotate(mat4{}, timer * 0.5f, {1, 0, 0});
   }

   ...
   program.setUniform("ModelMatrix", rotation* cubeMat); 
   cube.render();

更新:

所以当我使用矩阵变量作为全局变量而不是局部变量时,这个问题就解决了。

最佳答案

有多种方式可以实现这种交互。一种更简单的方法是在每一帧而不是全局帧中创建一个相对平移,并将其添加到当前旋转:

为此,必须将所有旋转的总和存储在全局变量中

//Global variable
mat4 total_rotate;

并计算每一帧的相对平移:

//In the function
mat4 rotation;

if(keysPressed[GLFW_KEY_LEFT]){
            rotation = rotate(mat4{}, delta, {0, 1, 0});
}

if(keysPressed[GLFW_KEY_RIGHT]){
            rotation = rotate(mat4{}, -delta, {0, 1, 0});
}

if(keysPressed[GLFW_KEY_UP]){
             rotation = rotate(mat4{}, delta, {1, 0, 0});
}

if(keysPressed[GLFW_KEY_DOWN]){
             rotation = rotate(mat4{}, -delta, {1, 0, 0});
}

total_rotate = total_rotate * rotation;
...
program.setUniform("ModelMatrix", total_rotate * cubeMat); 
cube.render();

作为替代方案,您可以存储两个旋转角度并计算每一帧中的矩阵:

//Global variables
float rot_x = 0.0f, rot_y = 0.0f;

//In every frame    
if(keysPressed[GLFW_KEY_LEFT]){
            rot_x += delta;
}

if(keysPressed[GLFW_KEY_RIGHT]){
            rot_x -= delta;
}

//Same for y

auto rotation = rotate(rotate(mat4{}, rot_y, {0, 1, 0}), rot_x, {1, 0, 0}
...
program.setUniform("ModelMatrix", rotation * cubeMat); 
cube.render();

关于c++ - 使用键旋转对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47115481/

相关文章:

C++ 从 void* 转换为 SomeClass*

c++ - OpenGL 纹理透明度

c++ - SFML 中的 OpenGL 累积缓冲区?

javascript - 如何按一天中的时间更改 div 内容?

ios - 当 UIDeviceOrientationDidChangeNotification 在 UIView 中捕获时,UIInterfaceOrientation 尚未更新

c++ - 获取 constexpr 全局变量(不是静态成员)的链接器符号

c++ - 为什么录制的音频数据仅使用缓冲区的一半(WaveIn)?

c++ - OpenMP shared vs. firstprivate performancewise

c++ - OpenGL 和 STL?

ios - 禁止在 iOS 中旋转启动 View