-bool ScreenToLocalInternal(
- const Matrix& viewMatrix,
- const Matrix& projectionMatrix,
- const Matrix& worldMatrix,
- const Viewport& viewport,
- const Vector3& currentSize,
- float& localX,
- float& localY,
- float screenX,
- float screenY)
-{
- // Get the ModelView matrix
- Matrix modelView;
- Matrix::Multiply(modelView, worldMatrix, viewMatrix);
-
- // Calculate the inverted ModelViewProjection matrix; this will be used for 2 unprojects
- Matrix invertedMvp(false /*don't init*/);
- Matrix::Multiply(invertedMvp, modelView, projectionMatrix);
- bool success = invertedMvp.Invert();
-
- // Convert to GL coordinates
- Vector4 screenPos(screenX - static_cast<float>(viewport.x), static_cast<float>(viewport.height) - screenY - static_cast<float>(viewport.y), 0.f, 1.f);
-
- Vector4 nearPos;
- if(success)
- {
- success = Unproject(screenPos, invertedMvp, static_cast<float>(viewport.width), static_cast<float>(viewport.height), nearPos);
- }
-
- Vector4 farPos;
- if(success)
- {
- screenPos.z = 1.0f;
- success = Unproject(screenPos, invertedMvp, static_cast<float>(viewport.width), static_cast<float>(viewport.height), farPos);
- }
-
- if(success)
- {
- Vector4 local;
- if(XyPlaneIntersect(nearPos, farPos, local))
- {
- Vector3 size = currentSize;
- localX = local.x + size.x * 0.5f;
- localY = local.y + size.y * 0.5f;
- }
- else
- {
- success = false;
- }
- }
-
- return success;
-}
-