基于鼠标选择Opengl拖动对象
Drag object based on mouse selection Opengl
在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
只是你希望它移动速度的倍数,与前一帧的时间增量相结合。基本上,你放大得越远,每秒滚动的速度就越快。无论缩放如何,窗口右边缘的任何像素都需要恒定的时间才能到达左边缘。
相关文章:
- 如何使用默认参数等选择模板专业化
- 如何在OpenGL中自动选择屏幕大小
- OpenGL 混合两种纹理总是会选择第一个纹理
- 如何为 QtQuick 应用程序选择 OpenGL 上下文
- QT 4.x/5.x和OpenGL用于桌面GUI应用程序:选择哪种模块
- 如何使用OpenGL中的框选择有效地选择对象
- 为跨平台OpenGL ES 2.0+引擎选择语言和设计 - iOS & Android
- 基于鼠标选择Opengl拖动对象
- 如何在现代 OpenGl 中选择网格的顶点或三角形(上面的 3.3)
- 持有唯一值集的向量,以便使用 C++ 和 OpenGL 进行颜色选择
- OpenGL选择模式
- 在做一次选择(GL_SELECT模式)后,一半Fps (OpenGL)
- 如何选择一个特定的视口转换,同时保持之前的转换在OpenGL
- OpenGL选择缓冲区-使用指向对象的指针而不是Unsigned Int
- 选择OpenGL范围元素GL_ELEMENT_ARRAY_BUFFER
- OpenGL拾取选择错误的位置
- 在 Windows + opengl 中选择一个图形设备
- OpenGL上下文创建从不选择像素格式
- 使用鼠标选择缩放OpenGL场景
- 画一个圆:在OpenGL中选择线条还是平坦的三角形