1 /* 2 * Copyright (C) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "frustum_util.h" 17 18 #include <base/math/matrix.h> 19 #include <base/math/vector.h> 20 #include <core/log.h> 21 #include <core/namespace.h> 22 #include <core/plugin/intf_plugin_register.h> 23 24 CORE_BEGIN_NAMESPACE() 25 using BASE_NS::string_view; 26 using BASE_NS::Uid; 27 using BASE_NS::Math::Mat4X4; 28 using BASE_NS::Math::Vec3; 29 CreateFrustum(const Mat4X4 & matrix) const30Frustum FrustumUtil::CreateFrustum(const Mat4X4& matrix) const 31 { 32 Frustum frustum; 33 34 auto& planes = frustum.planes; 35 planes[Frustum::PLANE_LEFT].x = matrix[0].w + matrix[0].x; 36 planes[Frustum::PLANE_LEFT].y = matrix[1].w + matrix[1].x; 37 planes[Frustum::PLANE_LEFT].z = matrix[2].w + matrix[2].x; 38 planes[Frustum::PLANE_LEFT].w = matrix[3].w + matrix[3].x; 39 40 planes[Frustum::PLANE_RIGHT].x = matrix[0].w - matrix[0].x; 41 planes[Frustum::PLANE_RIGHT].y = matrix[1].w - matrix[1].x; 42 planes[Frustum::PLANE_RIGHT].z = matrix[2].w - matrix[2].x; 43 planes[Frustum::PLANE_RIGHT].w = matrix[3].w - matrix[3].x; 44 45 planes[Frustum::PLANE_BOTTOM].x = matrix[0].w - matrix[0].y; 46 planes[Frustum::PLANE_BOTTOM].y = matrix[1].w - matrix[1].y; 47 planes[Frustum::PLANE_BOTTOM].z = matrix[2].w - matrix[2].y; 48 planes[Frustum::PLANE_BOTTOM].w = matrix[3].w - matrix[3].y; 49 50 planes[Frustum::PLANE_TOP].x = matrix[0].w + matrix[0].y; 51 planes[Frustum::PLANE_TOP].y = matrix[1].w + matrix[1].y; 52 planes[Frustum::PLANE_TOP].z = matrix[2].w + matrix[2].y; 53 planes[Frustum::PLANE_TOP].w = matrix[3].w + matrix[3].y; 54 55 planes[Frustum::PLANE_NEAR].x = matrix[0].w + matrix[0].z; 56 planes[Frustum::PLANE_NEAR].y = matrix[1].w + matrix[1].z; 57 planes[Frustum::PLANE_NEAR].z = matrix[2].w + matrix[2].z; 58 planes[Frustum::PLANE_NEAR].w = matrix[3].w + matrix[3].z; 59 60 planes[Frustum::PLANE_FAR].x = matrix[0].w - matrix[0].z; 61 planes[Frustum::PLANE_FAR].y = matrix[1].w - matrix[1].z; 62 planes[Frustum::PLANE_FAR].z = matrix[2].w - matrix[2].z; 63 planes[Frustum::PLANE_FAR].w = matrix[3].w - matrix[3].z; 64 65 for (uint32_t idx = 0; idx < Frustum::PLANE_COUNT; ++idx) { 66 const float rcpLength = 1.0f / Magnitude(Vec3(planes[idx])); 67 planes[idx] *= rcpLength; 68 } 69 return frustum; 70 } 71 SphereFrustumCollision(const Frustum & frustum,const Vec3 pos,const float radius) const72bool FrustumUtil::SphereFrustumCollision(const Frustum& frustum, const Vec3 pos, const float radius) const 73 { 74 for (auto const& plane : frustum.planes) { 75 const float d = (plane.x * pos.x) + (plane.y * pos.y) + (plane.z * pos.z) + plane.w; 76 if (d <= -radius) { 77 return false; 78 } 79 } 80 return true; 81 } 82 GetInterface(const Uid & uid) const83const IInterface* FrustumUtil::GetInterface(const Uid& uid) const 84 { 85 if (uid == IFrustumUtil::UID) { 86 return this; 87 } 88 return nullptr; 89 } 90 GetInterface(const Uid & uid)91IInterface* FrustumUtil::GetInterface(const Uid& uid) 92 { 93 if (uid == IFrustumUtil::UID) { 94 return this; 95 } 96 return nullptr; 97 } 98 Ref()99void FrustumUtil::Ref() {} 100 Unref()101void FrustumUtil::Unref() {} 102 CORE_END_NAMESPACE() 103