From e0f3657032c77386582fbb24580116d7736562a4 Mon Sep 17 00:00:00 2001 From: HifiExperiments Date: Wed, 13 Dec 2023 21:58:48 -0800 Subject: [PATCH] promising --- .../gpu-gl-common/src/gpu/gl/GLBackend.cpp | 23 ++++++++++++++++--- .../gpu-gl-common/src/gpu/gl/GLBackend.h | 2 ++ .../src/gpu/gl/GLBackendTransform.cpp | 3 +-- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/libraries/gpu-gl-common/src/gpu/gl/GLBackend.cpp b/libraries/gpu-gl-common/src/gpu/gl/GLBackend.cpp index bb79c9073f..f4af949558 100644 --- a/libraries/gpu-gl-common/src/gpu/gl/GLBackend.cpp +++ b/libraries/gpu-gl-common/src/gpu/gl/GLBackend.cpp @@ -349,6 +349,7 @@ void GLBackend::renderPassTransfer(const Batch& batch) { case Batch::COMMAND_setViewTransform: case Batch::COMMAND_setProjectionTransform: case Batch::COMMAND_setProjectionJitter: + case Batch::COMMAND_setContextMirrorViewCorrection: { CommandCall call = _commandCalls[(*command)]; (this->*(call))(batch, *offset); @@ -386,6 +387,7 @@ void GLBackend::renderPassDraw(const Batch& batch) { case Batch::COMMAND_setModelTransform: case Batch::COMMAND_setViewTransform: case Batch::COMMAND_setProjectionTransform: + case Batch::COMMAND_setContextMirrorViewCorrection: break; case Batch::COMMAND_draw: @@ -411,6 +413,7 @@ void GLBackend::renderPassDraw(const Batch& batch) { //case Batch::COMMAND_setModelTransform: //case Batch::COMMAND_setViewTransform: //case Batch::COMMAND_setProjectionTransform: + //case Batch::COMMAND_setContextMirrorViewCorrection: case Batch::COMMAND_setProjectionJitter: case Batch::COMMAND_setViewportTransform: case Batch::COMMAND_setDepthRangeTransform: @@ -623,9 +626,23 @@ void GLBackend::do_restoreContextViewCorrection(const Batch& batch, size_t param void GLBackend::do_setContextMirrorViewCorrection(const Batch& batch, size_t paramOffset) { bool prevMirrorViewCorrection = _transform._mirrorViewCorrection; _transform._mirrorViewCorrection = batch._params[paramOffset + 1]._uint != 0; - if (prevMirrorViewCorrection != _transform._mirrorViewCorrection) { - static const mat4 flipXScale = glm::scale(glm::mat4(), glm::vec3(-1.0f, 1.0f, 1.0f)); - setCameraCorrection(_transform._correction.correction * flipXScale, _transform._correction.prevView); + + if (_transform._correction.correction != glm::mat4()) { + // If we were previously not flipped, take this opportunity to save our flipped and unflipped matrices. + if (!prevMirrorViewCorrection) { + _transform._unflippedCorrection = _transform._correction.correction; + quat flippedRotation = glm::quat_cast(_transform._unflippedCorrection); + flippedRotation.y *= -1.0f; + flippedRotation.z *= -1.0f; + vec3 flippedTranslation = _transform._unflippedCorrection[3]; + flippedTranslation.x *= -1.0f; + _transform._flippedCorrection = glm::translate(glm::mat4_cast(flippedRotation), flippedTranslation); + } + + if (prevMirrorViewCorrection != _transform._mirrorViewCorrection) { + setCameraCorrection(_transform._mirrorViewCorrection ? _transform._flippedCorrection : _transform._unflippedCorrection, _transform._correction.prevView); + _transform._invalidView = true; + } } } diff --git a/libraries/gpu-gl-common/src/gpu/gl/GLBackend.h b/libraries/gpu-gl-common/src/gpu/gl/GLBackend.h index 6e8af35037..fdc0bf983c 100644 --- a/libraries/gpu-gl-common/src/gpu/gl/GLBackend.h +++ b/libraries/gpu-gl-common/src/gpu/gl/GLBackend.h @@ -434,6 +434,8 @@ protected: Transform _view; CameraCorrection _correction; bool _viewCorrectionEnabled{ true }; + mat4 _unflippedCorrection; + mat4 _flippedCorrection; bool _mirrorViewCorrection{ false }; Mat4 _projection; diff --git a/libraries/gpu-gl-common/src/gpu/gl/GLBackendTransform.cpp b/libraries/gpu-gl-common/src/gpu/gl/GLBackendTransform.cpp index d6ccdb52c9..67ab502b6b 100644 --- a/libraries/gpu-gl-common/src/gpu/gl/GLBackendTransform.cpp +++ b/libraries/gpu-gl-common/src/gpu/gl/GLBackendTransform.cpp @@ -108,8 +108,7 @@ void GLBackend::TransformStageState::preUpdate(size_t commandIndex, const Stereo if (_invalidView) { // Apply the correction - static const mat4 flipXScale = glm::scale(glm::mat4(), glm::vec3(-1.0f, 1.0f, 1.0f)); - if (_viewIsCamera && (_viewCorrectionEnabled && _correction.correction != (_mirrorViewCorrection ? flipXScale : glm::mat4()))) { + if (_viewIsCamera && (_viewCorrectionEnabled && _correction.correction != glm::mat4())) { // FIXME should I switch to using the camera correction buffer in Transform.slf and leave this out? Transform result; _view.mult(result, _view, _correction.correctionInverse);