我目前正在开发一款C++游戏引擎,并希望在应用程序中构建鼠标交互。我以前使用射线拾取来实现过这个功能,但当时我使用的是固定的鼠标位置,现在我想不用这种方法来实现。我看到可以使用glm::unProject函数来实现这个目的,但我的方案却不起作用。该函数给出的坐标不正确。我做错了什么?
rscore_projection_matrix = glm::perspective(45.0f, (float)(windowWidth)/(float)(windowHeight), 0.1f, 1000.0f);
rscore_view_matrix = glm::lookAt(glm::vec3(lengthdir_x(16, rscam_direction)+rscam_x, rscam_z, lengthdir_y(16, rscam_direction)+rscam_y), glm::vec3(rscam_x, 0, rscam_y), glm::vec3(0,1,0));
rscore_model_matrix = glm::mat4(1.0f);
glm::vec3 screenPos = glm::vec3(rscore_mouse_x, rscore_mouse_y, 0.1f);
glm::vec4 viewport = glm::vec4(0.0f, 0.0f, windowWidth, windowHeight);
glm::vec3 worldPos = glm::unProject(screenPos, rscore_model_matrix, rscore_projection_matrix, viewport);
我使用vec3 worldPos 位置来绘制对象。
screenPos
实际上是否使用了正确的OpenGL约定(视口左下角为原点)? - derhass