1 /* 2 * Copyright (c) 2024 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 #ifndef SCENEPLUGINAPI_CAMERA_H 17 #define SCENEPLUGINAPI_CAMERA_H 18 19 #include <scene_plugin/api/camera_uid.h> 20 #include <scene_plugin/api/postprocess.h> 21 #include <scene_plugin/api/postprocess_uid.h> 22 #include <scene_plugin/interface/intf_camera.h> 23 24 #include <meta/api/internal/object_api.h> 25 SCENE_BEGIN_NAMESPACE() 26 27 /** 28 * @brief Camera class wraps ICamera interface. It keeps the referenced object alive using strong ref. 29 * The construction of the object is asynchronous, the properties of the engine may not be available 30 * right after the object instantiation, but OnLoaded() event can be used to observe the state changes. 31 */ 32 class Camera final : public META_NS::Internal::ObjectInterfaceAPI<Camera, ClassId::Camera> { 33 META_API(Camera) 34 META_API_OBJECT_CONVERTIBLE(ICamera) 35 META_API_OBJECT_CONVERTIBLE(INode) 36 META_API_CACHE_INTERFACE(ICamera, Camera) 37 META_API_CACHE_INTERFACE(INode, Node) 38 39 public: 40 // From Node 41 META_API_INTERFACE_PROPERTY_CACHED(Node, Name, BASE_NS::string) 42 META_API_INTERFACE_PROPERTY_CACHED(Node, Position, BASE_NS::Math::Vec3) 43 META_API_INTERFACE_PROPERTY_CACHED(Node, Scale, BASE_NS::Math::Vec3) 44 META_API_INTERFACE_PROPERTY_CACHED(Node, Rotation, BASE_NS::Math::Quat) 45 META_API_INTERFACE_PROPERTY_CACHED(Node, Visible, bool) 46 META_API_INTERFACE_PROPERTY_CACHED(Node, LayerMask, uint64_t) 47 META_API_INTERFACE_PROPERTY_CACHED(Node, LocalMatrix, BASE_NS::Math::Mat4X4) 48 META_API_INTERFACE_PROPERTY_CACHED(Camera, FoV, float) 49 META_API_INTERFACE_PROPERTY_CACHED(Camera, AspectRatio, float) 50 META_API_INTERFACE_PROPERTY_CACHED(Camera, NearPlane, float) 51 META_API_INTERFACE_PROPERTY_CACHED(Camera, FarPlane, float) 52 META_API_INTERFACE_PROPERTY_CACHED(Camera, XOffset, float) 53 META_API_INTERFACE_PROPERTY_CACHED(Camera, YOffset, float) 54 META_API_INTERFACE_PROPERTY_CACHED(Camera, Projection, uint8_t) 55 META_API_INTERFACE_PROPERTY_CACHED(Camera, Culling, uint8_t) 56 META_API_INTERFACE_PROPERTY_CACHED(Camera, RenderingPipeline, uint8_t) 57 META_API_INTERFACE_PROPERTY_CACHED(Camera, SceneFlags, uint32_t) 58 META_API_INTERFACE_PROPERTY_CACHED(Camera, PipelineFlags, uint32_t) 59 META_API_INTERFACE_PROPERTY_CACHED(Camera, Viewport, BASE_NS::Math::Vec4) 60 META_API_INTERFACE_PROPERTY_CACHED(Camera, Scissor, BASE_NS::Math::Vec4) 61 META_API_INTERFACE_PROPERTY_CACHED(Camera, RenderTargetSize, BASE_NS::Math::UVec2) 62 META_API_INTERFACE_PROPERTY_CACHED(Camera, ClearColor, SCENE_NS::Color) 63 META_API_INTERFACE_PROPERTY_CACHED(Camera, ClearDepth, float) 64 MAP_PROPERTY_TO_USER(Camera, PostProcess) 65 66 /** 67 * @brief Set camera to use orthographic projection with default property values 68 */ SetOrthographicProjection()69 void SetOrthographicProjection() 70 { 71 if (Projection()->GetValue() != SCENE_NS::ICamera::SCENE_CAM_PROJECTION_ORTHOGRAPHIC) { 72 auto renderSize = RenderTargetSize()->GetValue(); 73 XOffset()->SetValue(renderSize.x / 2); 74 YOffset()->SetValue(renderSize.y / 2); 75 Projection()->SetValue(SCENE_NS::ICamera::SCENE_CAM_PROJECTION_ORTHOGRAPHIC); 76 } 77 } 78 79 /** 80 * @brief Set camera to use perspective projection. This is on by default. 81 */ SetPerspectiveProjection()82 void SetPerspectiveProjection() 83 { 84 META_API_CACHED_INTERFACE(Camera)->Projection()->SetValue(SCENE_NS::ICamera::SCENE_CAM_PROJECTION_PERSPECTIVE); 85 } 86 87 /** 88 * @brief Construct Camera instance from ICamera strong pointer. 89 * @param node the object pointed by interface is kept alive 90 */ Camera(const INode::Ptr& node)91 explicit Camera(const INode::Ptr& node) 92 { 93 Initialize(interface_pointer_cast<META_NS::IObject>(node)); 94 } 95 96 /** 97 * @brief Construct Camera instance from ICamera strong pointer. 98 * @param node the object pointed by interface is kept alive 99 */ Camera(const ICamera::Ptr& node)100 explicit Camera(const ICamera::Ptr& node) 101 { 102 Initialize(interface_pointer_cast<META_NS::IObject>(node)); 103 } 104 105 /** 106 * @brief Gets OnLoaded event reference from INode-interface 107 * @return INode::OnLoaded 108 */ OnLoaded()109 auto OnLoaded() 110 { 111 return META_API_CACHED_INTERFACE(Node)->OnLoaded(); 112 } 113 114 /** 115 * @brief Runs a callback once the camera is loaded. If camera is already initialized, callback will not run. 116 * @param callback Code to run, if strong reference is passed, it will keep the instance alive 117 * causing engine to report memory leak on application exit. 118 * @return reference to this instance of Camera. 119 */ 120 template<class Callback> OnLoaded(Callback&& callback)121 auto OnLoaded(Callback&& callback) 122 { 123 OnLoaded()->AddHandler(META_NS::MakeCallback<META_NS::IOnChanged>(callback)); 124 return *this; 125 } 126 AddMultiviewCamera(Camera camera)127 void AddMultiviewCamera(Camera camera) 128 { 129 META_API_CACHED_INTERFACE(Camera)->AddMultiviewCamera(camera); 130 } 131 RemoveMultiviewCamera(Camera camera)132 void RemoveMultiviewCamera(Camera camera) 133 { 134 META_API_CACHED_INTERFACE(Camera)->RemoveMultiviewCamera(camera); 135 } 136 }; 137 138 SCENE_END_NAMESPACE() 139 140 #endif // SCENEPLUGINAPI_CAMERA_H 141