如何从屏幕坐标获取对象坐标?

dev_nut

我正在尝试从鼠标位置获取对象空间坐标。我有一些标准的渲染代码,效果很好。

问题出在鼠标拾取代码上。我尝试了很多事情,并经历了类似的问题,但我似乎无法理解为什么它不起作用。

我希望结果基于鼠标在对象上方的位置返回[-1,1]内的ax,y坐标。我的确获得了[-1,1]内的点,但是它们极度偏斜,例如(2.63813e-012,-1,300)。

取消项目代码:

int z;
glReadPixels(mouse_pos_[0], int( navWidget->height() - mouse_pos_[1]), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);
glm::vec3 win(mouse_pos_[0], navWidget->height() - mouse_pos_[1], z);
glm::vec4 viewport(0, 0, navWidget->width(), navWidget->height());
auto result_vec3 = glm::unProject(win, view * model1, proj, viewport);

auto result = glm::normalize(glm::vec2(result_vec3.x, result_vec3.y)); // < -- I normalize here since that gave good results without the translate


bool left_image = true;

    if (!(result.x <= length_per_side && result.x >= -length_per_side &&
        result.y <= length_per_side && result.y >= -length_per_side)) {
// do stuff
        }
    }

渲染代码:

float fov = 2*(atan((camProjModule->camResY()/2*camProjModule->camPixSizeY()) /
    camProjModule->camFocalLength()) / M_PI * 180.0);
float znear = 1.0f;
float zfar = 6000.0f;
//float aspect = 1024.f / 683.f;
float aspect = navWidget->width() / navWidget->height();

glm::mat4 proj = glm::perspective(fov, aspect, znear, zfar);
float required_height =(float)( znear * tan((fov / 2.f) * M_PI / 180.f)); 
float eye_distance = znear / required_height * ((float)(navWidget->height()) / 2.f);
eye_distance = 300.f;
glm::mat4 view = glm::lookAt(glm::vec3(0.f, 0.f, 1.f * eye_distance), glm::vec3(0.f, 0.f, 0.f), glm::vec3(0.f, 1.f, 0.f));


glUseProgram(correspond_shader_);
glBindVertexArray(quad_vao_);
glUniform3f(colorLoc, 1.0f, 1.0f, 1.0f);

// draw left
if (left_correspond_texture_) {
    glEnable(GL_TEXTURE_2D);
    glActiveTexture(GL_TEXTURE0 + 0);
    glBindTexture(GL_TEXTURE_2D, left_correspond_texture_);
    glUniform1i(drawTexLoc, left_correspond_texture_);
}

GLint proj_loc = glGetUniformLocation(correspond_shader_, "proj");
GLint view_loc = glGetUniformLocation(correspond_shader_, "view");
GLint draw_tex_loc = glGetUniformLocation(correspond_shader_, "drawTex");

glUniformMatrix4fv(proj_loc, 1, GL_FALSE, glm::value_ptr(proj));
glUniformMatrix4fv(view_loc, 1, GL_FALSE, glm::value_ptr(view));


float ratio = 1024.f / 683.f;
float height = navWidget->height() / 2.f;
float ratio_to_multiply = height / 2.f;

glm::vec3 translation_vector = glm::vec3(0.f, height / 2.f, 0.f); // < --- If I remove this translation I get results that seem to be correct, and can be used after normalizing the x and y

glm::mat4 left_model = glm::scale(glm::translate(glm::mat4(1.f), translation_vector), glm::vec3(ratio * ratio_to_multiply, ratio_to_multiply, 1.f));
glm::mat4 right_model = glm::scale(glm::translate(glm::mat4(1.f), -1.f * translation_vector), glm::vec3(ratio * ratio_to_multiply, ratio_to_multiply, 1.f));

glUniformMatrix4fv(glGetUniformLocation(correspond_shader_, "model"), 1, GL_FALSE, glm::value_ptr(left_model));

glDrawArrays(GL_TRIANGLES, 0, 6); //, GL_UNSIGNED_INT, NULL);

编辑:我认为我的问题需要改进。我正在绘制两个四边形并为其渲染单独的纹理。我想做的就是根据它的四边形将鼠标坐标作为归一化的纹理坐标。

铬硒

我看到您正在使用glm库。您可以使用非投影方法获得鼠标坐标/射线方向。

glm::vec2 screenPos(mousePos.x, mousePos.y);
screenPos.y = height - screenPos.y;

float aspect = width / height;
glm::vec4 viewport = glm::vec4(0.0f, 0.0f, width , height);
glm::mat4 proj     = glm::perspective(75.0f, aspect, 0.1f, 10000.0f);

glm::vec3 a (screenPos.x, screenPos.y, 0);
glm::vec3 b (screenPos.x, screenPos.y, 1);

glm::vec3 result  = glm::unProject(a, viewMatrix, proj, viewport);
glm::vec3 result2 = glm::unProject(b, viewMatrix, proj, viewport);

glm::vec3 pickingPos = result;
glm::vec3 pickingDir = result2 - result;

之后,您可以使用方向和位置检查碰撞

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何从对象获取坐标?

来自分类Dev

如何从Vispy中的屏幕坐标获取世界坐标

来自分类Dev

如何从xyz世界坐标中获取xy屏幕坐标?

来自分类Dev

如何从R中的范围对象获取坐标?

来自分类Dev

获取GridLayout项的坐标(屏幕)

来自分类Dev

如何获取键盘附件视图内部的屏幕视图坐标?

来自分类Dev

locationManager如何获取坐标?

来自分类Dev

如何从CAShapeLayer获取坐标

来自分类Dev

如何从MediaWiki获取坐标

来自分类Dev

如何获取vtkboxwidget坐标

来自分类Dev

在通过 JavaScript 的 Canvas 中,如何获取对象的纵坐标,然后将该对象移动到不同的坐标?

来自分类Dev

如何计算从屏幕坐标到等距屏幕坐标的转换?

来自分类Dev

如果我们在android中有小屏幕的坐标,如何在大屏幕上获取相对坐标

来自分类Dev

如何获取<g>组内对象的绝对坐标?

来自分类Dev

如何使用API在SolidWorks中获取对象的坐标数据

来自分类Dev

如何在Java中获取Shape对象的x坐标

来自分类Dev

如何在AS3中获取对象碰撞的坐标?

来自分类Dev

QML地图:点击屏幕时获取坐标

来自分类Dev

获取手机屏幕的中心坐标

来自分类Dev

C#在屏幕上获取鼠标坐标

来自分类Dev

从给定角度获取屏幕边缘的坐标

来自分类Dev

如何将工作空间坐标转换为屏幕坐标?

来自分类Dev

如何将屏幕坐标转换为控制坐标

来自分类Dev

如何将工作空间坐标转换为屏幕坐标?

来自分类Dev

Robocode如何获取敌人的坐标

来自分类Dev

如何获取svg元素的坐标?

来自分类Dev

如何从坐标中获取城市?

来自分类Dev

如何获取图像/ JLabel的坐标?

来自分类Dev

如何从KML文件获取坐标?

Related 相关文章

热门标签

归档