From 26411a42b1c5ad132c24fd98a47d5647d1e3f794 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Fri, 6 Sep 2013 15:07:15 -0700 Subject: [PATCH] More focal length bits. --- interface/src/Application.cpp | 32 ++++++++++++++++++++++++---- libraries/voxels/src/ViewFrustum.cpp | 30 +++++++++++++++++--------- libraries/voxels/src/ViewFrustum.h | 15 +++++++------ 3 files changed, 57 insertions(+), 20 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 296a3e2df2..741cb6ad52 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -638,8 +638,8 @@ void Application::keyPressEvent(QKeyEvent* event) { case Qt::Key_J: if (isShifted) { - _myCamera.setEyeOffsetOrientation(glm::normalize( - glm::quat(glm::vec3(0, 0.002f, 0)) * _myCamera.getEyeOffsetOrientation())); + _viewFrustum.setFocalLength(_viewFrustum.getFocalLength() - 0.1f); + } else { _myCamera.setEyeOffsetPosition(_myCamera.getEyeOffsetPosition() + glm::vec3(-0.001, 0, 0)); } @@ -648,8 +648,8 @@ void Application::keyPressEvent(QKeyEvent* event) { case Qt::Key_M: if (isShifted) { - _myCamera.setEyeOffsetOrientation(glm::normalize( - glm::quat(glm::vec3(0, -0.002f, 0)) * _myCamera.getEyeOffsetOrientation())); + _viewFrustum.setFocalLength(_viewFrustum.getFocalLength() + 0.1f); + } else { _myCamera.setEyeOffsetPosition(_myCamera.getEyeOffsetPosition() + glm::vec3(0.001, 0, 0)); } @@ -2934,6 +2934,30 @@ void Application::renderViewFrustum(ViewFrustum& viewFrustum) { // left plane - top edge - viewFrustum.getNear to distant glVertex3f(viewFrustum.getNearTopLeft().x, viewFrustum.getNearTopLeft().y, viewFrustum.getNearTopLeft().z); glVertex3f(viewFrustum.getFarTopLeft().x, viewFrustum.getFarTopLeft().y, viewFrustum.getFarTopLeft().z); + + // focal plane - bottom edge + glColor3f(1.0f, 0.0f, 1.0f); + float focalProportion = (viewFrustum.getFocalLength() - viewFrustum.getNearClip()) / + (viewFrustum.getFarClip() - viewFrustum.getNearClip()); + glm::vec3 focalBottomLeft = glm::mix(viewFrustum.getNearBottomLeft(), viewFrustum.getFarBottomLeft(), focalProportion); + glm::vec3 focalBottomRight = glm::mix(viewFrustum.getNearBottomRight(), + viewFrustum.getFarBottomRight(), focalProportion); + glVertex3f(focalBottomLeft.x, focalBottomLeft.y, focalBottomLeft.z); + glVertex3f(focalBottomRight.x, focalBottomRight.y, focalBottomRight.z); + + // focal plane - top edge + glm::vec3 focalTopLeft = glm::mix(viewFrustum.getNearTopLeft(), viewFrustum.getFarTopLeft(), focalProportion); + glm::vec3 focalTopRight = glm::mix(viewFrustum.getNearTopRight(), viewFrustum.getFarTopRight(), focalProportion); + glVertex3f(focalTopLeft.x, focalTopLeft.y, focalTopLeft.z); + glVertex3f(focalTopRight.x, focalTopRight.y, focalTopRight.z); + + // focal plane - left edge + glVertex3f(focalBottomLeft.x, focalBottomLeft.y, focalBottomLeft.z); + glVertex3f(focalTopLeft.x, focalTopLeft.y, focalTopLeft.z); + + // focal plane - right edge + glVertex3f(focalBottomRight.x, focalBottomRight.y, focalBottomRight.z); + glVertex3f(focalTopRight.x, focalTopRight.y, focalTopRight.z); } glEnd(); glEnable(GL_LIGHTING); diff --git a/libraries/voxels/src/ViewFrustum.cpp b/libraries/voxels/src/ViewFrustum.cpp index a35dc49ec0..7914ad0165 100644 --- a/libraries/voxels/src/ViewFrustum.cpp +++ b/libraries/voxels/src/ViewFrustum.cpp @@ -32,6 +32,7 @@ ViewFrustum::ViewFrustum() : _aspectRatio(1.0), _nearClip(0.1), _farClip(500.0), + _focalLength(0.25f), _keyholeRadius(DEFAULT_KEYHOLE_RADIUS), _farTopLeft(0,0,0), _farTopRight(0,0,0), @@ -310,15 +311,16 @@ bool testMatches(float lhs, float rhs) { bool ViewFrustum::matches(const ViewFrustum& compareTo, bool debug) const { bool result = - testMatches(compareTo._position, _position ) && - testMatches(compareTo._direction, _direction ) && - testMatches(compareTo._up, _up ) && - testMatches(compareTo._right, _right ) && - testMatches(compareTo._fieldOfView, _fieldOfView ) && - testMatches(compareTo._aspectRatio, _aspectRatio ) && - testMatches(compareTo._nearClip, _nearClip ) && - testMatches(compareTo._farClip, _farClip ) && - testMatches(compareTo._eyeOffsetPosition, _eyeOffsetPosition ) && + testMatches(compareTo._position, _position) && + testMatches(compareTo._direction, _direction) && + testMatches(compareTo._up, _up) && + testMatches(compareTo._right, _right) && + testMatches(compareTo._fieldOfView, _fieldOfView) && + testMatches(compareTo._aspectRatio, _aspectRatio) && + testMatches(compareTo._nearClip, _nearClip) && + testMatches(compareTo._farClip, _farClip) && + testMatches(compareTo._focalLength, _focalLength) && + testMatches(compareTo._eyeOffsetPosition, _eyeOffsetPosition) && testMatches(compareTo._eyeOffsetOrientation, _eyeOffsetOrientation); if (!result && debug) { @@ -351,6 +353,9 @@ bool ViewFrustum::matches(const ViewFrustum& compareTo, bool debug) const { qDebug("%s -- compareTo._farClip=%f _farClip=%f\n", (testMatches(compareTo._farClip, _farClip) ? "MATCHES " : "NO MATCH"), compareTo._farClip, _farClip); + qDebug("%s -- compareTo._focalLength=%f _focalLength=%f\n", + (testMatches(compareTo._focalLength, _focalLength) ? "MATCHES " : "NO MATCH"), + compareTo._focalLength, _focalLength); qDebug("%s -- compareTo._eyeOffsetPosition=%f,%f,%f _eyeOffsetPosition=%f,%f,%f\n", (testMatches(compareTo._eyeOffsetPosition, _eyeOffsetPosition) ? "MATCHES " : "NO MATCH"), compareTo._eyeOffsetPosition.x, compareTo._eyeOffsetPosition.y, compareTo._eyeOffsetPosition.z, @@ -401,13 +406,17 @@ void ViewFrustum::computeOffAxisFrustum(float& left, float& right, float& bottom nearClipPlane = glm::vec4(-normal.x, -normal.y, -normal.z, glm::dot(normal, corners[0])); farClipPlane = glm::vec4(normal.x, normal.y, normal.z, -glm::dot(normal, corners[4])); + // compute the focal proportion (zero is near clip, one is far clip) + float focalProportion = (_focalLength - _nearClip) / (_farClip - _nearClip); + // get the extents at Z = -near left = FLT_MAX; right = -FLT_MAX; bottom = FLT_MAX; top = -FLT_MAX; for (int i = 0; i < 4; i++) { - glm::vec4 intersection = corners[i] * (-near / corners[i].z); + glm::vec4 corner = glm::mix(corners[i], corners[i + 4], focalProportion); + glm::vec4 intersection = corner * (-near / corner.z); left = min(left, intersection.x); right = max(right, intersection.x); bottom = min(bottom, intersection.y); @@ -426,6 +435,7 @@ void ViewFrustum::printDebugDetails() const { qDebug("_keyHoleRadius=%f\n", _keyholeRadius); qDebug("_nearClip=%f\n", _nearClip); qDebug("_farClip=%f\n", _farClip); + qDebug("_focalLength=%f\n", _focalLength); qDebug("_eyeOffsetPosition=%f,%f,%f\n", _eyeOffsetPosition.x, _eyeOffsetPosition.y, _eyeOffsetPosition.z ); qDebug("_eyeOffsetOrientation=%f,%f,%f,%f\n", _eyeOffsetOrientation.x, _eyeOffsetOrientation.y, _eyeOffsetOrientation.z, _eyeOffsetOrientation.w ); diff --git a/libraries/voxels/src/ViewFrustum.h b/libraries/voxels/src/ViewFrustum.h index 22830cb48d..803e52908e 100644 --- a/libraries/voxels/src/ViewFrustum.h +++ b/libraries/voxels/src/ViewFrustum.h @@ -39,6 +39,7 @@ public: void setAspectRatio(float a) { _aspectRatio = a; } void setNearClip(float n) { _nearClip = n; } void setFarClip(float f) { _farClip = f; } + void setFocalLength(float length) { _focalLength = length; } void setEyeOffsetPosition(const glm::vec3& p) { _eyeOffsetPosition = p; } void setEyeOffsetOrientation(const glm::quat& o) { _eyeOffsetOrientation = o; } @@ -47,6 +48,7 @@ public: float getAspectRatio() const { return _aspectRatio; } float getNearClip() const { return _nearClip; } float getFarClip() const { return _farClip; } + float getFocalLength() const { return _focalLength; } const glm::vec3& getEyeOffsetPosition() const { return _eyeOffsetPosition; } const glm::quat& getEyeOffsetOrientation() const { return _eyeOffsetOrientation; } @@ -111,12 +113,13 @@ private: glm::vec3 _right; // Lens attributes - float _fieldOfView; - float _aspectRatio; - float _nearClip; - float _farClip; - glm::vec3 _eyeOffsetPosition; - glm::quat _eyeOffsetOrientation; + float _fieldOfView; + float _aspectRatio; + float _nearClip; + float _farClip; + float _focalLength; + glm::vec3 _eyeOffsetPosition; + glm::quat _eyeOffsetOrientation; // keyhole attributes float _keyholeRadius;