I am trying to implement camera pan like the one in Maya. I've got it almost working.
The problem is that the mouse cursor is moving faster than the 3d mesh (in fact I am moving the camera but I said mesh, because it is more intuitive), while in Maya the distance between the mouse position and the mesh stays always the same.
This is what my code looks like :
glm::vec3 dist = cameraPos-modelPos; float len=glm::length(dist); float clipX = startX*2.0 / width - 1.0; float clipY = 1.0 - startY*2.0 / height; float clipEndX = lastX*2.0 / width - 1.0; float clipEndY = 1.0 - lastY*2.0 / height; // convert begin and end mouse positions into world space glm::mat4 inverseMVP = glm::inverse(projection*view*modelMatrix); glm::vec4 outVector = inverseMVP*glm::vec4(clipX, clipY, 0.0, 1.0); glm::vec3 worldPos(outVector.x/outVector.w, outVector.y/outVector.w, outVector.z/outVector.w); glm::vec4 outEndVec = inverseMVP*glm::vec4(clipEndX, clipEndY, 0.0, 1.0); glm::vec3 worldPos2(outEndVec.x/outEndVec.w, outEndVec.y/outEndVec.w, outEndVec.z/outEndVec.w); glm::vec3 dir = worldPos2 - worldPos; glm::vec3 offset=glm::length(dir)*glm::normalize(dir)*angleFovRatio*len/2.f; cameraPos-=offset; cameraTarget-=offset;
angleFovRatio = std::tan(cameraFov*0.5f)
I am using
glm::lookAt(cameraPos, cameraTarget, cameraUp); for my
I will add picture of what my problem is:
As you can see, when I drag to the right, the red dots are my begin and end mouse positions. But the teapot is on the blue dot, which is not what I want. It must be centered on the second red dot (mouse end position, just like in maya).