Skip to content

Commit

Permalink
fixed cameracontrol required components and improved style
Browse files Browse the repository at this point in the history
  • Loading branch information
lshoek committed May 11, 2022
1 parent ddaab6e commit e9ffb57
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 78 deletions.
129 changes: 73 additions & 56 deletions modules/napcameracontrol/src/cameracontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,15 @@ namespace nap

CameraComponentInstance& CameraControllerInstance::getCameraComponent()
{
if (mMode == ECameraMode::FirstPerson)
switch (mMode)
{
case ECameraMode::FirstPerson:
return mFirstPersonComponent->getCameraComponent();
else if (mMode == ECameraMode::Orbit)
case ECameraMode::Orbit:
return mFirstPersonComponent->getCameraComponent();
else
default:
return mOrthoComponent->getCameraComponent();
}
}


Expand All @@ -83,81 +86,86 @@ namespace nap
*/
void CameraControllerInstance::switchMode(ECameraMode targetMode)
{
if (targetMode == ECameraMode::FirstPerson)
switch (targetMode)
{
// If we're switching back from orthographic camera, enable controller while resetting position to last know position
if ((mMode & ECameraMode::Orthographic) != ECameraMode::None)
mFirstPersonComponent->enable(mLastPerspPos, mLastPerspRotate);
else
mFirstPersonComponent->enable();

mOrbitComponent->disable();
mOrthoComponent->disable();
}
else if (targetMode == ECameraMode::Orbit)
{
nap::TransformComponentInstance& lookAtTransform = mLookAtTarget->getComponent<nap::TransformComponentInstance>();

// If we're switching back from orthographic camera, enable controller while resetting position to last know position
if ((mMode & ECameraMode::Orthographic) != ECameraMode::None)
mOrbitComponent->enable(mLastPerspPos, lookAtTransform.getTranslate());
else
mOrbitComponent->enable(lookAtTransform.getTranslate());
case ECameraMode::FirstPerson:
{
// If we're switching back from orthographic camera, enable controller while resetting position to last know position
if ((mMode & ECameraMode::Orthographic) != ECameraMode::None)
mFirstPersonComponent->enable(mLastPerspPos, mLastPerspRotate);
else
mFirstPersonComponent->enable();

mOrbitComponent->disable();
mOrthoComponent->disable();
break;
}
case ECameraMode::Orbit:
{
nap::TransformComponentInstance& lookAtTransform = mLookAtTarget->getComponent<nap::TransformComponentInstance>();

mFirstPersonComponent->disable();
mOrthoComponent->disable();
}
else
{
// Remember the current perspective transform before we alter the transform
if ((mMode & ECameraMode::Perspective) != ECameraMode::None)
storeLastPerspTransform();
// If we're switching back from orthographic camera, enable controller while resetting position to last know position
if ((mMode & ECameraMode::Orthographic) != ECameraMode::None)
mOrbitComponent->enable(mLastPerspPos, lookAtTransform.getTranslate());
else
mOrbitComponent->enable(lookAtTransform.getTranslate());

// Depending on orthographic mode, make a rotation.
glm::vec3 camera_translate_axis;
glm::quat rotation;
switch (targetMode)
mFirstPersonComponent->disable();
mOrthoComponent->disable();
break;
}
default:
{
// Remember the current perspective transform before we alter the transform
if ((mMode & ECameraMode::Perspective) != ECameraMode::None)
storeLastPerspTransform();

// Depending on orthographic mode, make a rotation.
glm::vec3 camera_translate_axis;
glm::quat rotation;
switch (targetMode)
{
case ECameraMode::OrthographicTop:
camera_translate_axis = glm::vec3(0.0f, -1.0f, 0.0f);
rotation = glm::angleAxis((float)-math::PI_2, glm::vec3(1.0f, 0.0f, 0.0f));
camera_translate_axis = glm::vec3{ 0.0f, -1.0f, 0.0f };
rotation = glm::angleAxis(static_cast<float>(-math::PI_2), glm::vec3{ 1.0f, 0.0f, 0.0f });
break;

case ECameraMode::OrthographicBottom:
camera_translate_axis = glm::vec3(0.0f, 1.0f, 0.0f);
rotation = glm::angleAxis((float)math::PI_2, glm::vec3(1.0f, 0.0f, 0.0f));
camera_translate_axis = glm::vec3{ 0.0f, 1.0f, 0.0f };
rotation = glm::angleAxis(static_cast<float>(math::PI_2), glm::vec3{ 1.0f, 0.0f, 0.0f });
break;

case ECameraMode::OrthographicLeft:
camera_translate_axis = glm::vec3(1.0f, 0.0f, 0.0f);
rotation = glm::angleAxis((float)-math::PI_2, glm::vec3(0.0f, 1.0f, 0.0f));
camera_translate_axis = glm::vec3{ 1.0f, 0.0f, 0.0f };
rotation = glm::angleAxis(static_cast<float>(-math::PI_2), glm::vec3{ 0.0f, 1.0f, 0.0f });
break;

case ECameraMode::OrthographicRight:
camera_translate_axis = glm::vec3(-1.0f, 0.0f, 0.0f);
rotation = glm::angleAxis((float)math::PI_2, glm::vec3(0.0f, 1.0f, 0.0f));
camera_translate_axis = glm::vec3{ -1.0f, 0.0f, 0.0f };
rotation = glm::angleAxis(static_cast<float>(math::PI_2), glm::vec3{ 0.0f, 1.0f, 0.0f });
break;

case ECameraMode::OrthographicFront:
camera_translate_axis = glm::vec3(0.0f, 0.0f, -1.0f);
rotation = glm::angleAxis((float)0.0f, glm::vec3(0.0f, 1.0f, 0.0f));
camera_translate_axis = glm::vec3{ 0.0f, 0.0f, -1.0f };
rotation = glm::angleAxis(0.0f, glm::vec3{ 0.0f, 1.0f, 0.0f });
break;

case ECameraMode::OrthographicBack:
camera_translate_axis = glm::vec3(0.0f, 0.0f, 1.0f);
rotation = glm::angleAxis((float)math::PI, glm::vec3(0.0f, 1.0f, 0.0f));
camera_translate_axis = glm::vec3{ 0.0f, 0.0f, 1.0f };
rotation = glm::angleAxis(static_cast<float>(math::PI), glm::vec3{ 0.0f, 1.0f, 0.0f });
break;
}
}

// The translation is placed some distance from the lookat target
nap::TransformComponentInstance& lookat_transform = mLookAtTarget->getComponent<nap::TransformComponentInstance>();
glm::vec3 target_pos(lookat_transform.getTranslate());
const float distance = 100.0f;
glm::vec3 camera_translate = target_pos - camera_translate_axis * distance;
// The translation is placed some distance from the lookat target
nap::TransformComponentInstance& lookat_transform = mLookAtTarget->getComponent<nap::TransformComponentInstance>();
const glm::vec3& target_pos = lookat_transform.getTranslate();
const float distance = 100.0f;
glm::vec3 camera_translate = target_pos - camera_translate_axis * distance;

mOrthoComponent->enable(camera_translate, rotation);
mFirstPersonComponent->disable();
mOrbitComponent->disable();
mOrthoComponent->enable(camera_translate, rotation);
mFirstPersonComponent->disable();
mOrbitComponent->disable();
}
}
mMode = targetMode;
}
Expand Down Expand Up @@ -213,4 +221,13 @@ namespace nap
break;
}
}
}


void CameraController::getDependentComponents(std::vector<rtti::TypeInfo>& components) const
{
components.emplace_back(RTTI_OF(OrbitController));
components.emplace_back(RTTI_OF(FirstPersonController));
components.emplace_back(RTTI_OF(OrthoController));
components.emplace_back(RTTI_OF(KeyInputComponent));
}
}
9 changes: 2 additions & 7 deletions modules/napcameracontrol/src/cameracontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ namespace nap
* Returns the controllers this component depends upon.
* @param components the various controllers this component depends upon.
*/
virtual void getDependentComponents(std::vector<rtti::TypeInfo>& components) const override
{
components.emplace_back(RTTI_OF(OrbitController));
components.emplace_back(RTTI_OF(FirstPersonController));
components.emplace_back(RTTI_OF(OrthoController));
}
virtual void getDependentComponents(std::vector<rtti::TypeInfo>& components) const override;

nap::EntityPtr mLookAtTarget; ///< Property: 'LookAtTarget' Object to look at, used by the orbit and ortho controller
};
Expand Down Expand Up @@ -109,4 +104,4 @@ namespace nap
glm::quat mLastPerspRotate; ///< Last perspective camera rotation
};

}
}
14 changes: 7 additions & 7 deletions modules/napcameracontrol/src/firstpersoncontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ namespace nap

float movement = mMovementSpeed * deltaTime;

glm::vec3 side(1.0, 0.0, 0.0);
glm::vec3 up(0.0, 1.0, 0.0);
glm::vec3 forward(0.0, 0.0, 1.0);
glm::vec3 side{ 1.0f, 0.0f, 0.0f };
glm::vec3 up{ 0.0f, 1.0f, 0.0f };
glm::vec3 forward{ 0.0f, 0.0f, 1.0f};

glm::vec3 dir_forward = glm::rotate(mTransformComponent->getRotate(), forward);
glm::vec3 movement_forward = dir_forward * movement;
Expand Down Expand Up @@ -156,10 +156,10 @@ namespace nap
float yaw = -(pointerMoveEvent.mRelX * mRotateSpeed);
float pitch = pointerMoveEvent.mRelY * mRotateSpeed;

glm::mat4 yaw_rotation = glm::rotate(yaw, glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 yaw_rotation = glm::rotate(yaw, glm::vec3{ 0.0f, 1.0f, 0.0f });

glm::vec4 right = mTransformComponent->getLocalTransform()[0];
glm::mat4 pitch_rotation = glm::rotate(pitch, glm::vec3(right.x, right.y, right.z));
const glm::vec3& right = mTransformComponent->getLocalTransform()[0];
glm::mat4 pitch_rotation = glm::rotate(pitch, right);

mTransformComponent->setRotate(yaw_rotation * pitch_rotation * glm::toMat4(mTransformComponent->getRotate()));
}
Expand Down Expand Up @@ -245,6 +245,6 @@ namespace nap
{
components.emplace_back(RTTI_OF(TransformComponent));
components.emplace_back(RTTI_OF(KeyInputComponent));
components.emplace_back(RTTI_OF(PointerInputComponent));
}

}
17 changes: 9 additions & 8 deletions modules/napcameracontrol/src/orbitcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace nap
// Construct a lookat matrix. Note that if this is currently called when facing up or downward, the
// camera may flip around the y axis. Currently this is only called when starting orbit so it isn't
// much of a problem, but if it is, we need to find another way of constructing a lookat camera.
glm::vec3 up(0.0f, 1.0f, 0.0f);
glm::vec3 up{ 0.0f, 1.0f, 0.0f };
glm::mat4 rotation = glm::lookAt(cameraPos, lookAtPos, up);
rotation = glm::inverse(rotation);
mTransformComponent->setRotate(rotation);
Expand All @@ -77,7 +77,7 @@ namespace nap

void OrbitControllerInstance::enable(const glm::vec3& lookAtPos)
{
glm::vec3 translate = mTransformComponent->getLocalTransform()[3];
const glm::vec3& translate = mTransformComponent->getLocalTransform()[3];
enable(translate, lookAtPos);
}

Expand Down Expand Up @@ -123,9 +123,9 @@ namespace nap

// We need to rotate around the target point. We always first rotate around the local X axis (pitch), and then
// we rotate around the y axis (yaw).
glm::mat4 yaw_rotation = glm::rotate(yaw, glm::vec3(0.0f, 1.0f, 0.0f));
glm::vec4 right = mTransformComponent->getLocalTransform()[0];
glm::mat4 pitch_rotation = glm::rotate(pitch, glm::vec3(right.x, right.y, right.z));
glm::mat4 yaw_rotation = glm::rotate(yaw, glm::vec3{ 0.0f, 1.0f, 0.0f });
const glm::vec3& right = mTransformComponent->getLocalTransform()[0];
glm::mat4 pitch_rotation = glm::rotate(pitch, right);

// To rotate around the target point, we take the current transform, then bring it into local target space (only translation), then first rotate pitch,
// then rotate yaw, and then bring it back to worldspace.
Expand Down Expand Up @@ -159,7 +159,8 @@ namespace nap

void OrbitController::getDependentComponents(std::vector<rtti::TypeInfo>& components) const
{
components.push_back(RTTI_OF(TransformComponent));
components.push_back(RTTI_OF(KeyInputComponent));
components.emplace_back(RTTI_OF(TransformComponent));
components.emplace_back(RTTI_OF(KeyInputComponent));
components.emplace_back(RTTI_OF(PointerInputComponent));
}
}
}

0 comments on commit e9ffb57

Please sign in to comment.