|
| 1 | +/* |
| 2 | + * Copyright (C) 2021 Open Source Robotics Foundation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | +*/ |
| 17 | + |
| 18 | +#include "Ogre2IgnHlmsCustomizations.hh" |
| 19 | + |
| 20 | +#ifdef _MSC_VER |
| 21 | + #pragma warning(push, 0) |
| 22 | +#endif |
| 23 | +#include <OgreCamera.h> |
| 24 | +#include <OgreHlms.h> |
| 25 | +#include <OgreRenderTarget.h> |
| 26 | +#include <OgreSceneManager.h> |
| 27 | +#include <OgreViewport.h> |
| 28 | +#ifdef _MSC_VER |
| 29 | + #pragma warning(pop) |
| 30 | +#endif |
| 31 | + |
| 32 | +using namespace ignition; |
| 33 | +using namespace rendering; |
| 34 | + |
| 35 | +////////////////////////////////////////////////// |
| 36 | +void Ogre2IgnHlmsCustomizations::preparePassHash( |
| 37 | + const Ogre::CompositorShadowNode */*_shadowNode*/, |
| 38 | + bool _casterPass, bool /*_dualParaboloid*/, |
| 39 | + Ogre::SceneManager */*_sceneManager*/, |
| 40 | + Ogre::Hlms *_hlms) |
| 41 | +{ |
| 42 | + this->needsWorldPos = false; |
| 43 | + if (!_casterPass && this->MinDistanceClipEnabled()) |
| 44 | + { |
| 45 | + const Ogre::int32 numClipPlanes = |
| 46 | + _hlms->_getProperty("hlms_pso_clip_distances"); |
| 47 | + _hlms->_setProperty( "ign_spherical_clip_min_distance", 1 ); |
| 48 | + _hlms->_setProperty( "ign_spherical_clip_idx", numClipPlanes ); |
| 49 | + _hlms->_setProperty( "hlms_pso_clip_distances", numClipPlanes + 1 ); |
| 50 | + |
| 51 | + if (_hlms->getType() == Ogre::HLMS_UNLIT) |
| 52 | + { |
| 53 | + if (_hlms->_getProperty("hlms_global_clip_planes") == 0) |
| 54 | + { |
| 55 | + this->needsWorldPos = true; |
| 56 | + _hlms->_setProperty( "ign_spherical_clip_needs_worldPos", 1 ); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +////////////////////////////////////////////////// |
| 63 | +Ogre::uint32 Ogre2IgnHlmsCustomizations::getPassBufferSize( |
| 64 | + const Ogre::CompositorShadowNode */*_shadowNode*/, |
| 65 | + bool _casterPass, bool /*_dualParaboloid*/, |
| 66 | + Ogre::SceneManager */*_sceneManager*/) const |
| 67 | +{ |
| 68 | + if (_casterPass || !this->MinDistanceClipEnabled()) |
| 69 | + return 0u; |
| 70 | + |
| 71 | + Ogre::uint32 bufferSize = sizeof(float) * 4u; |
| 72 | + if (this->needsWorldPos) |
| 73 | + bufferSize += sizeof(float) * 16u; |
| 74 | + |
| 75 | + return bufferSize; |
| 76 | +} |
| 77 | + |
| 78 | +////////////////////////////////////////////////// |
| 79 | +float* Ogre2IgnHlmsCustomizations::preparePassBuffer( |
| 80 | + const Ogre::CompositorShadowNode */*_shadowNode*/, |
| 81 | + bool _casterPass, bool /*_dualParaboloid*/, |
| 82 | + Ogre::SceneManager *_sceneManager, |
| 83 | + float *_passBufferPtr) |
| 84 | +{ |
| 85 | + if (!_casterPass && this->MinDistanceClipEnabled()) |
| 86 | + { |
| 87 | + Ogre::Camera *camera = _sceneManager->getCameraInProgress(); |
| 88 | + const Ogre::Vector3 &camPos = camera->getDerivedPosition(); |
| 89 | + |
| 90 | + // float4 ignMinClipDistance_ignCameraPos |
| 91 | + *_passBufferPtr++ = this->minDistanceClip; |
| 92 | + *_passBufferPtr++ = camPos.x; |
| 93 | + *_passBufferPtr++ = camPos.y; |
| 94 | + *_passBufferPtr++ = camPos.z; |
| 95 | + |
| 96 | + if (this->needsWorldPos) |
| 97 | + { |
| 98 | + // TODO(dark_sylinc): Modify Ogre to access HlmsUnlit::mPreparedPass |
| 99 | + // so we get the view matrix that's going to be used instead of |
| 100 | + // recalculating everything again (which can get complex if VR is |
| 101 | + // being used) |
| 102 | + const Ogre::RenderTarget *renderTarget = |
| 103 | + _sceneManager->getCurrentViewport()->getTarget(); |
| 104 | + Ogre::Matrix4 projectionMatrix = |
| 105 | + camera->getProjectionMatrixWithRSDepth(); |
| 106 | + if( renderTarget->requiresTextureFlipping() ) |
| 107 | + { |
| 108 | + projectionMatrix[1][0] = -projectionMatrix[1][0]; |
| 109 | + projectionMatrix[1][1] = -projectionMatrix[1][1]; |
| 110 | + projectionMatrix[1][2] = -projectionMatrix[1][2]; |
| 111 | + projectionMatrix[1][3] = -projectionMatrix[1][3]; |
| 112 | + } |
| 113 | + |
| 114 | + Ogre::Matrix4 invViewProj = (projectionMatrix * |
| 115 | + camera->getViewMatrix(true)).inverse(); |
| 116 | + for (size_t i = 0; i < 16u; ++i) |
| 117 | + *_passBufferPtr++ = static_cast<float>(invViewProj[0][i]); |
| 118 | + } |
| 119 | + } |
| 120 | + return _passBufferPtr; |
| 121 | +} |
0 commit comments