Skip to main content

CameraSceneNode Class

Represents an adapter for cameras, allowing spatial positioning and transformation within the scene graph. More...

Declaration

class helios::scene::CameraSceneNode { ... }

Base class

classSceneNode

Represents a SceneNode within a SceneGraph. More...

Public Constructors Index

CameraSceneNode (std::unique_ptr< helios::scene::Camera > camera)

Constructs a CameraSceneNode that takes ownership of the given camera. More...

Public Member Functions Index

SceneNode *addNode (std::unique_ptr< SceneNode > sceneNode) override

Prevents adding child nodes to a camera scene node. More...

const helios::scene::Camera &camera () const noexcept

Gets the camera associated with this scene node. More...

helios::scene::Camera &camera () noexcept

Gets the camera associated with this scene node. More...

voidlookAt (helios::math::vec3f target, helios::math::vec3f up)

Orients this camera node to look at a target position in world space. More...

voidlookAtLocal (helios::math::vec3f targetLocal, helios::math::vec3f upLocal)

Orients this camera node to look at a target position in local space. More...

voidonWorldTransformUpdate () noexcept override

Callback invoked when the world transform of this node changes. More...

Protected Member Attributes Index

std::unique_ptr< helios::scene::Camera >camera_

Unique pointer to the camera instance owned by this scene node. More...

Description

Represents an adapter for cameras, allowing spatial positioning and transformation within the scene graph.

A CameraSceneNode behaves just like a (non-renderable) SceneNode, except that it does not accept child nodes.

An instance of `CameraSceneNode` can be added as a direct descendant of the scene's root node for free transform, or it can be a child of a model hierarchy to inherit the corresponding positioning in the world.

Definition at line 31 of file CameraSceneNode.ixx.

Public Constructors

CameraSceneNode()

helios::scene::CameraSceneNode::CameraSceneNode (std::unique_ptr< helios::scene::Camera > camera)
inline explicit

Constructs a CameraSceneNode that takes ownership of the given camera.

The CameraSceneNode becomes the sole owner of the Camera instance. The camera's view matrix is automatically updated when `worldTransform()` is called.

Parameters
camera

A unique pointer to the camera to be owned by this scene node.

Exceptions
std::invalid_argument

if camera is a nullptr.

Definition at line 52 of file CameraSceneNode.ixx.

52 explicit CameraSceneNode(std::unique_ptr<helios::scene::Camera> camera) :
53 camera_(std::move(camera)) {
54 if (!camera_) {
55 throw std::invalid_argument("CameraSceneNode received a Camera nullptr.");
56 }
57 }

References camera and camera_.

Public Member Functions

addNode()

SceneNode * helios::scene::CameraSceneNode::addNode (std::unique_ptr< SceneNode > sceneNode)
inline nodiscard virtual

Prevents adding child nodes to a camera scene node.

This method does nothing and returns `nullptr`, indicating that a `CameraSceneNode` must not have child nodes.

Parameters
sceneNode

The attempted child node to add (ignored).

Returns

Always returns `nullptr`.

See Also

https://stackoverflow.com/questions/24609872/delete-virtual-function-from-a-derived-class

Definition at line 71 of file CameraSceneNode.ixx.

71 [[nodiscard]] SceneNode* addNode(std::unique_ptr<SceneNode> sceneNode) override {
72 assert(false && "CameraSceneNode does not accept child nodes.");
73 return nullptr;
74 }

Reference helios::scene::SceneNode::SceneNode.

camera()

const helios::scene::Camera & helios::scene::CameraSceneNode::camera ()
inline nodiscard noexcept

Gets the camera associated with this scene node.

Returns

A const ref to the associated camera instance.

Definition at line 81 of file CameraSceneNode.ixx.

81 [[nodiscard]] const helios::scene::Camera& camera() const noexcept {
82 return *camera_;
83 }

Reference camera_.

Referenced by CameraSceneNode.

camera()

helios::scene::Camera & helios::scene::CameraSceneNode::camera ()
inline nodiscard noexcept

Gets the camera associated with this scene node.

Returns

A ref to the associated camera instance.

Definition at line 90 of file CameraSceneNode.ixx.

90 [[nodiscard]] helios::scene::Camera& camera() noexcept {
91 return *camera_;
92 }

Reference camera_.

lookAt()

void helios::scene::CameraSceneNode::lookAt (helios::math::vec3f target, helios::math::vec3f up)
inline

Orients this camera node to look at a target position in world space.

Computes the rotation matrix required to align this scene node's forward direction with the specified target position. The rotation is computed in world space and then converted to local space by factoring out the parent's rotation.

Parameters
target

The world-space coordinates of the target to look at.

up

The up vector defining the camera's vertical orientation in world space.

Precondition

The camera node must have a valid parent (i.e., must be added to a scene graph).

info

The up vector should typically be `(0, 1, 0)` for standard upright orientation.

See Also

lookAtLocal()

See Also

helios::scene::SceneNode::setRotation()

Definition at line 112 of file CameraSceneNode.ixx.

113 // We treat up as a vector in world-space for now.
114 const auto wt = worldTransform();
115 const auto worldPos = helios::math::vec3f(
116 wt(0, 3), wt(1, 3), wt(2, 3)
117 );
118
119 auto z = (target - worldPos).normalize();
120 auto y = up.normalize();
121 auto x = helios::math::cross(y, z).normalize();
122 y = helios::math::cross(z, x).normalize();
123
124 const helios::math::mat4f worldRotation = helios::math::mat4f{
125 x[0], x[1], x[2], 0.0f,
126 y[0], y[1], y[2], 0.0f,
127 z[0], z[1], z[2], 0.0f,
128 0.0f, 0.0f, 0.0f, 1.0f
129 };
130
133 /**
134 * @todo add support for different rotations
135 */
136 setRotation(worldRotation);
137 return;
138 }
139
140 // ParentTransform
141 assert(parent() && "parent() of CameraSceneNode returned null, are you sure the node was added properly to the scenegraph?");
143
144 const helios::math::mat4f parentRotInv = pT.decompose(helios::math::TransformType::Rotation).transpose();
145
146 // Apply rotation in local space by undoing the parent rotation
147 setRotation(parentRotInv * worldRotation);
148 }

References helios::scene::SceneNode::cachedWorldTransform, helios::math::cross, helios::math::mat4< T >::decompose, helios::scene::SceneNode::inheritance_, helios::math::vec3< T >::normalize, helios::scene::SceneNode::parent, helios::math::Rotation, helios::scene::SceneNode::setRotation, helios::math::transformTypeMatch and helios::scene::SceneNode::worldTransform.

lookAtLocal()

void helios::scene::CameraSceneNode::lookAtLocal (helios::math::vec3f targetLocal, helios::math::vec3f upLocal)
inline

Orients this camera node to look at a target position in local space.

Similar to `lookAt()`, but the target and up vector are interpreted relative to this node's local coordinate system rather than world space. Useful when the camera should track objects within the same local hierarchy.

Parameters
targetLocal

The local-space coordinates of the target to look at.

upLocal

The up vector defining the camera's vertical orientation in local space.

See Also

lookAt()

Definition at line 162 of file CameraSceneNode.ixx.

163 // We treat up as a vector in world-space for now.
164 const auto lt = localTransform().transform();
165 const auto localPos = helios::math::vec3f(
166 lt(0, 3), lt(1, 3), lt(2, 3)
167 );
168
169 auto z = (targetLocal - localPos).normalize();
170 auto y = upLocal.normalize();
171 auto x = helios::math::cross(y, z).normalize();
172 y = helios::math::cross(z, x).normalize();
173
174 const helios::math::mat4f localRotation = helios::math::mat4f{
175 x[0], x[1], x[2], 0.0f,
176 y[0], y[1], y[2], 0.0f,
177 z[0], z[1], z[2], 0.0f,
178 0.0f, 0.0f, 0.0f, 1.0f
179 };
180
181 setRotation(localRotation);
182 }

References helios::math::cross, helios::scene::SceneNode::localTransform, helios::math::vec3< T >::normalize, helios::scene::SceneNode::setRotation and helios::core::spatial::Transform::transform.

onWorldTransformUpdate()

void helios::scene::CameraSceneNode::onWorldTransformUpdate ()
inline noexcept virtual

Callback invoked when the world transform of this node changes.

Overrides the base class implementation to update the camera's view matrix whenever the node's position or orientation in the scene graph changes. This ensures the camera always reflects the current world-space transform.

See Also

SceneNode::onWorldTransformUpdate()

Definition at line 193 of file CameraSceneNode.ixx.

193 void onWorldTransformUpdate() noexcept override {
194
196
197 // Updates this SceneNode's worldTransform_
199
200 const auto x = helios::math::vec3f{wt(0, 0), wt(1, 0), wt(2, 0)};
201 const auto y = helios::math::vec3f{wt(0, 1), wt(1, 1), wt(2, 1)};
202 const auto z = helios::math::vec3f{wt(0, 2), wt(1, 2), wt(2, 2)};
203
204 const auto eye = helios::math::vec3f{wt(0, 3), wt(1, 3), wt(2, 3)};
205
206 // Compute view matrix: inverse of world transform with Z-negation for OpenGL RHS
207 auto inv = helios::math::mat4f{
208 x[0], y[0], -z[0], 0.0f,
209 x[1], y[1], -z[1], 0.0f,
210 x[2], y[2], -z[2], 0.0f,
211 -dot(x, eye), -dot(y, eye), dot(z, eye), 1.0f
212 };
213
214 camera_->setViewMatrix(inv);
215 }

References helios::scene::SceneNode::cachedWorldTransform, camera_ and helios::scene::SceneNode::onWorldTransformUpdate.

Protected Member Attributes

camera_

std::unique_ptr<helios::scene::Camera> helios::scene::CameraSceneNode::camera_
protected

Unique pointer to the camera instance owned by this scene node.

Definition at line 39 of file CameraSceneNode.ixx.

39 std::unique_ptr<helios::scene::Camera> camera_;

Referenced by camera, camera, CameraSceneNode and onWorldTransformUpdate.


The documentation for this class was generated from the following file:


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.