基于鼠标选择Opengl拖动对象

Drag object based on mouse selection Opengl

本文关键字:选择 Opengl 拖动 对象 鼠标 于鼠标      更新时间:2023-10-16

在OpenGL中用鼠标选择对象需要做什么?我发现了一些类似选择缓冲区的东西,但我也读到了一些不推荐使用它的地方。所以我被卡住了,不知道该找什么。我也在用C++做这个。

对于2D,这是我正在处理的代码——你必须对它进行一些修改,但希望它能给你一些想法。这段代码为您提供了"0高度"的世界坐标——如果某个东西没有0高度,则可能无法根据透视正确选择它。

// for the current mouse position on the screen, where does that correspond to in the world?
glm::vec2 World::world_position_for_mouse(const glm::vec2 mouse_position,
                                          const glm::mat4 projection_matrix,
                                          const glm::mat4 view_matrix) 
{
    int window_width;
    int window_height;
    this->graphics.get_window_dimensions(window_width, window_height);
    const int mouse_x = mouse_position[0];
    const int mouse_y = mouse_position[1];
    // normalize mouse position from window pixel space to between -1, 1
    GLfloat normalized_mouse_x =  (2.0f * mouse_x) / window_width - 1.0f;
    float normalized_mouse_y = 1.0f - (2.0f * mouse_y) / window_height;

    glm::vec3 normalized_mouse_vector = glm::vec3(normalized_mouse_x, normalized_mouse_y, 1.0f);
    glm::vec4 ray_clip = glm::vec4(normalized_mouse_vector.xy(), -1.0, 1.0);
    glm::vec4 ray_eye = glm::inverse(projection_matrix) * ray_clip;
    ray_eye = glm::vec4(ray_eye.xy(), -1.0, 0.0);
    glm::vec3 ray_world = (glm::inverse(view_matrix) * ray_eye).xyz();
    ray_world = glm::normalize(ray_world);
    float l = -(camera.z / ray_world.z);

    return {camera.x + l * ray_world.x, camera.y + l * ray_world.y};
}

为了在不缩放的情况下通过相同的"屏幕单位"平移世界,我根据上面代码的结果使用了这个代码:

    float camera_motion = time.get_wall_clock_delta() * camera_motion_per_second;
    auto x1 = this->world_position_for_mouse(glm::vec2(1,0), this->cached_projection_matrix, this->cached_view_matrix).x;
    auto x2 = this->world_position_for_mouse(glm::vec2(0,0), this->cached_projection_matrix, this->cached_view_matrix).x;
    auto camera_change = (x1 - x2) * camera_motion;

其中camera_motion只是你希望它移动速度的倍数,与前一帧的时间增量相结合。基本上,你放大得越远,每秒滚动的速度就越快。无论缩放如何,窗口右边缘的任何像素都需要恒定的时间才能到达左边缘。