Merge branch 'lua-world-to-viewport' into 'master'

[Lua] Add world to viewport function to camera

See merge request OpenMW/openmw!3139
revert-6246b479
psi29a 2 years ago
commit 44c3c40058

@ -106,6 +106,23 @@ namespace MWLua
return invertedViewMatrix.preMult(osg::Vec3f(x, y, -1)) - camera->getPosition();
};
api["worldToViewportVector"] = [camera](osg::Vec3f pos) {
double width = Settings::Manager::getInt("resolution x", "Video");
double height = Settings::Manager::getInt("resolution y", "Video");
osg::Matrix windowMatrix
= osg::Matrix::translate(1.0, 1.0, 1.0) * osg::Matrix::scale(0.5 * width, 0.5 * height, 0.5);
osg::Vec3f vpCoords = pos * (camera->getViewMatrix() * camera->getProjectionMatrix() * windowMatrix);
// Move 0,0 to top left to match viewportToWorldVector
vpCoords.y() = height - vpCoords.y();
// Set the z component to be distance from camera, in world space units
vpCoords.z() = (pos - camera->getPosition()).length();
return vpCoords;
};
return LuaUtil::makeReadOnly(api);
}

@ -127,6 +127,7 @@ namespace MWRender
}
cam->setViewMatrixAsLookAt(pos, pos + forward, up);
mViewMatrix = cam->getViewMatrix();
mProjectionMatrix = cam->getProjectionMatrix();
}
void Camera::update(float duration, bool paused)

@ -109,6 +109,7 @@ namespace MWRender
void setCollisionType(int collisionType) { mCollisionType = collisionType; }
const osg::Matrixf& getViewMatrix() const { return mViewMatrix; }
const osg::Matrixf& getProjectionMatrix() const { return mProjectionMatrix; }
private:
MWWorld::Ptr mTrackingPtr;
@ -138,6 +139,7 @@ namespace MWRender
bool mLockPitch = false, mLockYaw = false;
osg::Vec3d mPosition;
osg::Matrixf mViewMatrix;
osg::Matrixf mProjectionMatrix;
float mCameraDistance, mPreferredCameraDistance;

@ -224,5 +224,11 @@
-- @param openmw.util#Vector2 normalizedScreenPos
-- @return openmw.util#Vector3
--- Get vector from the world to the viewport for the given point in world space.
-- (0, 0) is the top left corner of the screen.
-- The z component of the return value holds the distance from the camera to the position, in world space
-- @function [parent=#camera] worldToViewportVector
-- @param openmw.util#Vector3 worldPos
-- @return openmw.util#Vector3
return nil

Loading…
Cancel
Save