-------------------------------------------------------------------------------- Description: Go through all i count of frustum splits. Calculate the corresponding bounding box based on zNear and zFar values. --------------------------------------------------------------------------------
| 142 | // bounding box based on zNear and zFar values. |
| 143 | // -------------------------------------------------------------------------------- |
| 144 | ShadowMap::SplitSetup Tr2ShadowMap::SetupShadowSplit( int splitIndex, Matrix invViewTransform, const Vector3 lightDirection, float zNear, float leftDivNear, float rightDivNear, float topDivNear, float bottomDivNear ) |
| 145 | { |
| 146 | CCP_STATS_ZONE( __FUNCTION__ ); |
| 147 | |
| 148 | ShadowMap::SplitSetup splitSetup; |
| 149 | |
| 150 | if( splitIndex == 0 ) |
| 151 | { |
| 152 | // reset this value |
| 153 | m_oldZFar = zNear; |
| 154 | } |
| 155 | |
| 156 | float zFar = m_splitValues[splitIndex]; |
| 157 | |
| 158 | // to save some shader data mem we combine the zFar values into a float4 array of 4 so (x,y,z,w) are zFar values |
| 159 | m_perSplitData.ShadowMapValues[splitIndex / 4][splitIndex % 4] = zFar; |
| 160 | |
| 161 | float left = leftDivNear * m_oldZFar; |
| 162 | float right = rightDivNear * m_oldZFar; |
| 163 | float top = topDivNear * m_oldZFar; |
| 164 | float bottom = bottomDivNear * m_oldZFar; |
| 165 | auto projection = PerspectiveOffCenterMatrix( left, right, bottom, top, m_oldZFar, zFar ); |
| 166 | |
| 167 | m_oldZFar = zFar; |
| 168 | |
| 169 | // we can apply the inverse of the view and projection matrices on the corner points of the unit cube to get the frustum corners in world space |
| 170 | splitSetup.invViewProj = Inverse( projection ) * invViewTransform; |
| 171 | |
| 172 | // Find light view |
| 173 | Matrix lightView = Inverse( OrthoNormalBasisZ( -lightDirection ) ); |
| 174 | |
| 175 | Vector3 corners[8]; |
| 176 | |
| 177 | AxisAlignedBoundingBox aabb = CalculateAABB( projection, invViewTransform, lightView, corners ); |
| 178 | |
| 179 | // Snap the projection in texel-sized increments to avoid crawling shadows on still objects |
| 180 | if( m_disableShimmer ) |
| 181 | { |
| 182 | // find max dist |
| 183 | float maxDist = 0.0; |
| 184 | |
| 185 | // First take the farthest corners of our camera view projection |
| 186 | // subtract them, get the length of the results and divide by 2 to get the radius |
| 187 | for( unsigned int i = 0; i < 8; ++i ) |
| 188 | { |
| 189 | // length between i and j is the same as between j and i so let's skip that comparison |
| 190 | for( unsigned int j = i + 1; j < 8; ++j ) |
| 191 | { |
| 192 | // longest dist between 2 corners |
| 193 | maxDist = std::max( maxDist, Length( corners[i] - corners[j] ) ); |
| 194 | } |
| 195 | } |
| 196 | float radius = std::ceil( maxDist / 2.0f ); |
| 197 | Vector3 shipPos = Vector3( 0.0, 0.0, 0.0 ); |
| 198 | Vector3 center = aabb.Center(); |
| 199 | // rounding up |
| 200 | float texelSize = ( radius * 2.0f ) / m_size; |
| 201 | center.x = std::floor( center.x / texelSize + 0.5f ) * texelSize; |
no test coverage detected