Showing the changes to interface/src/SecondaryCamera.cpp (1 of 1 file). Show all
Unified · Split
| @@ -54,42 +54,41 @@ public: · 53 unmodified lines | |||
| 54 | 54 | ||
| 55 | void setMirrorProjection(ViewFrustum& srcViewFrustum) { | 55 | void setMirrorProjection(ViewFrustum& srcViewFrustum) { |
| 56 | if (_attachedEntityId.isNull()) { | 56 | if (_attachedEntityId.isNull()) { |
| 57 | qDebug() << "ERROR: Cannot set mirror projection for SecondaryCamera without an attachedEntityId set."; | 57 | qWarning() << "ERROR: Cannot set mirror projection for SecondaryCamera without an attachedEntityId set."; |
| 58 | return; | 58 | return; |
| 59 | } | 59 | } |
| 60 | 60 | ||
| 61 | EntityItemProperties entityProperties = _entityScriptingInterface->getEntityProperties(_attachedEntityId, _attachedEntityPropertyFlags); | 61 | EntityItemProperties entityProperties = _entityScriptingInterface->getEntityProperties(_attachedEntityId, |
| 62 | glm::vec3 mirrorPropsPos = entityProperties.getPosition(); | 62 | _attachedEntityPropertyFlags); |
| 63 | glm::quat mirrorPropsRot = entityProperties.getRotation(); | 63 | glm::vec3 mirrorPropertiesPosition = entityProperties.getPosition(); |
| 64 | glm::vec3 mirrorPropsDim = entityProperties.getDimensions(); | 64 | glm::quat mirrorPropertiesRotation = entityProperties.getRotation(); |
| 65 | glm::vec3 mirrorPropertiesDimensions = entityProperties.getDimensions(); | ||
| 66 | glm::vec3 halfMirrorPropertiesDimensions = 0.5f * mirrorPropertiesDimensions; | ||
| 65 | 67 | ||
| 66 | // setup mirror from world as inverse of world from mirror transformation using inverted x and z for mirrored image | 68 | // setup mirror from world as inverse of world from mirror transformation using inverted x and z for mirrored image |
| 67 | glm::mat4 worldFromMirrorRot = glm::mat4_cast(mirrorPropsRot) * glm::scale(vec3(-1.f, 1.f, -1.f)); | 69 | glm::mat4 worldFromMirrorRotation = glm::mat4_cast(mirrorPropertiesRotation) * glm::scale(vec3(-1.0f, 1.0f, -1.0f)); |
| 68 | glm::mat4 worldFromMirrorTrans = glm::translate(mirrorPropsPos); | 70 | glm::mat4 worldFromMirrorTranslation = glm::translate(mirrorPropertiesPosition); |
| 69 | glm::mat4 worldFromMirror = worldFromMirrorTrans * worldFromMirrorRot; | 71 | glm::mat4 worldFromMirror = worldFromMirrorTranslation * worldFromMirrorRotation; |
| 70 | glm::mat4 mirrorFromWorld = glm::inverse(worldFromMirror); | 72 | glm::mat4 mirrorFromWorld = glm::inverse(worldFromMirror); |
| 71 | 73 | ||
| 72 | // get mirror camera position by reflecting main camera position's z coordinate in mirror space | 74 | // get mirror camera position by reflecting main camera position's z coordinate in mirror space |
| 73 | glm::vec3 mainCamPosWorld = qApp->getCamera().getPosition(); | 75 | glm::vec3 mainCameraPositionWorld = qApp->getCamera().getPosition(); |
| 74 | glm::vec3 mainCamPosMirror = vec3(mirrorFromWorld * vec4(mainCamPosWorld, 1.f)); | 76 | glm::vec3 mainCameraPositionMirror = vec3(mirrorFromWorld * vec4(mainCameraPositionWorld, 1.0f)); |
| 75 | glm::vec3 mirrorCamPosMirror = vec3(mainCamPosMirror.x, mainCamPosMirror.y, -mainCamPosMirror.z); | 77 | glm::vec3 mirrorCameraPositionMirror = vec3(mainCameraPositionMirror.x, mainCameraPositionMirror.y, |
| 76 | glm::vec3 mirrorCamPosWorld = vec3(worldFromMirror * vec4(mirrorCamPosMirror, 1.f)); | 78 | -mainCameraPositionMirror.z); |
| 79 | glm::vec3 mirrorCameraPositionWorld = vec3(worldFromMirror * vec4(mirrorCameraPositionMirror, 1.0f)); | ||
| 77 | 80 | ||
| 78 | // set frustum position to be mirrored camera and set orientation to mirror's adjusted rotation | 81 | // set frustum position to be mirrored camera and set orientation to mirror's adjusted rotation |
| 79 | glm::vec3 eyePos = mirrorCamPosWorld; | 82 | glm::quat mirrorCamOrientation = glm::quat_cast(worldFromMirrorRotation); |
| 80 | glm::quat eyeOri = glm::quat_cast(worldFromMirrorRot); | 83 | srcViewFrustum.setPosition(mirrorCameraPositionWorld); |
| 81 | srcViewFrustum.setPosition(eyePos); | 84 | srcViewFrustum.setOrientation(mirrorCamOrientation); |
| 82 | srcViewFrustum.setOrientation(eyeOri); | ||
| 83 | 85 | ||
| 84 | // build frustum using mirror space translation of mirrored camera | 86 | // build frustum using mirror space translation of mirrored camera |
| 85 | float n = mirrorCamPosMirror.z + mirrorPropsDim.z * 2.f; | 87 | float nearClip = mirrorCameraPositionMirror.z + mirrorPropertiesDimensions.z * 2.0f; |
| 86 | float f = _farClipPlaneDistance; | 88 | glm::vec3 upperRight = halfMirrorPropertiesDimensions - mirrorCameraPositionMirror; |
| 87 | float r = -mirrorCamPosMirror.x + mirrorPropsDim.x / 2.f; | 89 | glm::vec3 bottomLeft = -halfMirrorPropertiesDimensions - mirrorCameraPositionMirror; |
| 88 | float l = -mirrorCamPosMirror.x - mirrorPropsDim.x / 2.f; | 90 | glm::mat4 frustum = glm::frustum(bottomLeft.x, upperRight.x, bottomLeft.y, upperRight.y, nearClip, _farClipPlaneDistance); |
| 89 | float t = -mirrorCamPosMirror.y + mirrorPropsDim.y / 2.f; | 91 | |
| 90 | float b = -mirrorCamPosMirror.y - mirrorPropsDim.y / 2.f; | ||
| 91 | |||
| 92 | glm::mat4 frustum = glm::frustum(l, r, b, t, n, f); | ||
| 93 | srcViewFrustum.setProjection(frustum); | 92 | srcViewFrustum.setProjection(frustum); |
| 94 | } | 93 | } |
| 95 | 94 | ||
| @@ -118,15 +117,17 @@ public: · 22 unmodified lines | |||
| 118 | setMirrorProjection(srcViewFrustum); | 117 | setMirrorProjection(srcViewFrustum); |
| 119 | } else { | 118 | } else { |
| 120 | if (!_attachedEntityId.isNull()) { | 119 | if (!_attachedEntityId.isNull()) { |
| 121 | EntityItemProperties entityProperties = _entityScriptingInterface->getEntityProperties(_attachedEntityId, _attachedEntityPropertyFlags); | 120 | EntityItemProperties entityProperties = _entityScriptingInterface->getEntityProperties(_attachedEntityId, |
| 121 | _attachedEntityPropertyFlags); | ||
| 122 | srcViewFrustum.setPosition(entityProperties.getPosition()); | 122 | srcViewFrustum.setPosition(entityProperties.getPosition()); |
| 123 | srcViewFrustum.setOrientation(entityProperties.getRotation()); | 123 | srcViewFrustum.setOrientation(entityProperties.getRotation()); |
| 124 | } | 124 | } else { |
| 125 | else { | ||
| 126 | srcViewFrustum.setPosition(_position); | 125 | srcViewFrustum.setPosition(_position); |
| 127 | srcViewFrustum.setOrientation(_orientation); | 126 | srcViewFrustum.setOrientation(_orientation); |
| 128 | } | 127 | } |
| 129 | srcViewFrustum.setProjection(glm::perspective(glm::radians(_vFoV), ((float)args->_viewport.z / (float)args->_viewport.w), _nearClipPlaneDistance, _farClipPlaneDistance)); | 128 | srcViewFrustum.setProjection(glm::perspective(glm::radians(_vFoV), |
| 129 | ((float)args->_viewport.z / (float)args->_viewport.w), | ||
| 130 | _nearClipPlaneDistance, _farClipPlaneDistance)); | ||
| 130 | } | 131 | } |
| 131 | // Without calculating the bound planes, the secondary camera will use the same culling frustum as the main camera, | 132 | // Without calculating the bound planes, the secondary camera will use the same culling frustum as the main camera, |
| 132 | // which is not what we want here. | 133 | // which is not what we want here. |