简体   繁体   English

OpenGL 奇怪的鼠标旋转结果

[英]OpenGL weird rotation result with mouse

So I'm working on a 3d painting application.所以我正在研究 3d 绘画应用程序。 I managed to make a basic renderer and model loader.我设法制作了一个基本的渲染器和 model 加载器。

I created a camera system where I can use it to navigate around the scene(mouse/keyboard) but that's not what I want, so I made that camera static and now I'm trying to rotate/pan/zoom the model itself .我创建了一个相机系统,可以使用它在场景中导航(鼠标/键盘),但这不是我想要的,所以我制作了相机 static,现在我正在尝试旋转/平移/缩放 model 本身 I managed to implement panning and zooming .设法实现了平移和缩放 for panning i change the x/y position according to the mouse and for zooming I add or substract from the z-axis according to the mouse scroll.对于平移,我根据鼠标更改 x/y position,对于缩放,我根据鼠标滚动添加或减去 z 轴。

But now I want to be able to rotate the 3d model with the mouse .但现在我希望能够用鼠标旋转 3d model Example: When i hold right mouse button and move the mouse up the model should rotate on it's x-axis(pitch) and if i move the mouse to the left/right it should rotate on y-axis(yaw) .示例:当我按住鼠标右键并将鼠标向上移动时,model 应该在它的x 轴(俯仰)上旋转,如果我将鼠标向左/向右移动,它应该在y 轴(偏航)上旋转。 And I just couldn't do it.而我就是做不到。

The code bellow I get xpos/ypos of the cursor on the screen, calculate the offset and " trying to rotate the cube ".下面的代码我在屏幕上得到 cursor 的 xpos/ypos,计算偏移量并“尝试旋转立方体”。 The only problem is that i can't rotate the cuber normally if i move the mouse up the model rotate on the x-axis and y-axis with a little tilt and vice-versa.唯一的问题是,如果我将鼠标向上移动,model 在 x 轴和 y 轴上旋转一点倾斜,反之亦然,我无法正常旋转立方体。

This is the code in my rendering loop:这是我的渲染循环中的代码:

    shader.use();
    glm::mat4 projection = glm::perspective(glm::radians(45.0f), 
    (float)SCR_WIDTH/(float)SCR_HEIGHT, 0.01f, 100.0f);
    shader.setMat4f("projection", projection);

    glm::mat4 view = camera.getViewMatrix();
    shader.setMat4f("view", view);

    glm::mat4 modelm = glm::mat4(1.0f);
    modelm = glm::translate(modelm, object_translation);
    // Should rotate cube according to mouse movement
    //modelm = glm::rotate(modelm, glm::radians(angle), glm::vec3(0.0f));
    shader.setMat4f("model", modelm);

    renderer.draw(model, shader);

This is the call where i handle the mouse movement callback:这是我处理鼠标移动回调的调用:

void mouseCallback(GLFWwindow* window, double xpos, double ypos)
{
if (is_rotating)
{
    if (is_first_mouse)
    {
        lastX = xpos;
        lastY = ypos;
        is_first_mouse = false;
    }

    // xpos and ypos are the cursor coords i get those with the 
    mouse_callback
    double xoffset = xpos - lastX;
    double yoffset = lastY - ypos;

    lastX = xpos;
    lastY = ypos;

    object_rotation.y += xoffset; // If i use yoffset the rotation flips
    object_rotation.x += yoffset;

    rotation_angle += (xoffset + yoffset) * 0.25f;
}
}

Mouse panning works fine too can't say the same for the rotation.鼠标平移工作正常也不能说旋转相同。

I fixed it.我修好了它。 After some research and asking i was told that u can only do one rotation at a time and i was trying to do both x/y-axis in the same time .经过一些研究并询问我被告知你一次只能做一个旋转,试图同时做两个 x/y 轴 Once i seperate the two rotations.一旦我分开两个旋转。 object will now rotate on x-axis first then y-axis the problem was solved. object 现在将先在 x 轴上旋转,然后在 y 轴上旋转问题已解决。

The code should be like this:代码应该是这样的:

    shader.use();
    glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH/(float)SCR_HEIGHT, 0.01f, 100.0f);
    shader.setMat4f("projection", projection);

    glm::mat4 view = camera.getViewMatrix();
    shader.setMat4f("view", view);

    glm::mat4 modelm = glm::mat4(1.0f);
    modelm = glm::scale(modelm, glm::vec3(1.0f));
    modelm = glm::translate(modelm, glm::vec3(0.0f, 0.0f, -5.0f));
    // Handle x-axis rotation
    modelm = glm::rotate(modelm, glm::radians(object_orientation_angle_x), glm::vec3(object_rotation_x, 0.0f, 0.0f));
    // Handle y-axis rotation
    modelm = glm::rotate(modelm, glm::radians(object_orientation_angle_y), glm::vec3(0.0f, object_rotation_y, 0.0f));
    shader.setMat4f("model", modelm);

    renderer.draw(model, shader);

You are storing your rotation as euler angle inside object_rotation.您将旋转作为欧拉角存储在 object_rotation 中。

I advised you to use:我建议你使用:

glm::mat4 rotation = glm::eulerAngleYX(object_rotation.y, object_rotation.x); // pitch then yaw

or或者

glm::mat4 rotation = glm::eulerAngleXY(object_rotation.x, object_rotation.y); // yaw then roll

In your case both should do the job, I advised you in the future to store thoose informations inside your camera (eye, up, center) instead of your object, everything become more simpler.在您的情况下,两者都应该完成这项工作,我建议您将来将这些信息存储在您的相机(眼睛,向上,中心)而不是您的 object 中,一切都变得更加简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM