Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions engine/includes/components/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ENGINE_EXPORT Camera : public Component {

public:
Camera();
~Camera();

Matrix4 viewMatrix() const;
Matrix4 projectionMatrix() const;
Expand Down Expand Up @@ -66,6 +67,8 @@ class ENGINE_EXPORT Camera : public Component {
static Camera *current();
static void setCurrent(Camera *current);

static Camera *findActiveCamera(World *world);

Vector3 project(const Vector3 &worldSpace) const;
Vector3 unproject(const Vector3 &screenSpace) const;

Expand Down Expand Up @@ -101,6 +104,8 @@ class ENGINE_EXPORT Camera : public Component {

bool m_screen;

bool m_main;

mutable bool m_dirty;

};
Expand Down
44 changes: 31 additions & 13 deletions engine/src/components/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,15 @@ Camera::Camera() :
m_screen(false),
m_dirty(true) {

static uint32_t hash = Mathf::hashString("cameras");
addTagByHash(hash);
}

Camera::~Camera() {
if(s_currentCamera == this) {
s_currentCamera = nullptr;
}
}
/*!
Returns view matrix for the camera.
*/
Expand Down Expand Up @@ -259,21 +266,15 @@ void Camera::setScreenSpace(bool mode) {
*/
Camera *Camera::current() {
if(s_currentCamera == nullptr || !s_currentCamera->isEnabled() || !s_currentCamera->actor()->isEnabled()) {
s_currentCamera = nullptr;
for(auto it : Engine::world()->findChildren<Camera *>()) {
if(it->isEnabled() && it->actor()->isEnabled()) { // Get first active Camera
s_currentCamera = it;
break;
}
}
s_currentCamera = findActiveCamera(Engine::world());

if(s_currentCamera == nullptr) {
static Camera *reserveCamera = nullptr;
if(reserveCamera == nullptr) {
Actor *cameraActor = Engine::composeActor<Camera>("ReserveCamera", Engine::world());
reserveCamera = cameraActor->getComponent<Camera>();
static Camera *fallbackCamera = nullptr;
if(fallbackCamera == nullptr) {
Actor *cameraActor = Engine::composeActor<Camera>("FallbackCamera", nullptr);
fallbackCamera = cameraActor->getComponent<Camera>();
}
s_currentCamera = reserveCamera;
s_currentCamera = fallbackCamera;
s_currentCamera->actor()->setEnabled(true);
s_currentCamera->setEnabled(true);
}
Expand All @@ -287,7 +288,24 @@ Camera *Camera::current() {
void Camera::setCurrent(Camera *current) {
s_currentCamera = current;
}

/*!
Returns a first active camera in the \a world
*/
Camera *Camera::findActiveCamera(World *world) {
static uint32_t hash = Mathf::hashString("cameras");
for(auto scene : world->scenes()) {
for(auto it : scene->getObjectsInGroupByHash(hash)) {
Camera *camera = static_cast<Camera *>(it);
if(camera->isEnabled() && camera->actor()->isEnabled()) { // Get first active Camera
return camera;
}
}
}
return nullptr;
}
/*!
Returns a set of frustum planes.
*/
Frustum Camera::frustum() const {
Transform *t = transform();

Expand Down
12 changes: 4 additions & 8 deletions engine/src/editor/viewport/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,7 @@ void Viewport::onDraw() {
Engine::renderSystem()->setPipelineContext(m_pipelineContext);

if(m_gameView) {
for(auto it : m_world->findChildren<Camera *>()) {
if(it->isEnabled() && it->actor()->isEnabled()) { // Get first active Camera
Camera::setCurrent(it);
break;
}
}

Camera::setCurrent(Camera::findActiveCamera(Engine::world()));
Engine::update(m_world);

if(!m_gamePaused && isFocused()) {
Expand Down Expand Up @@ -217,7 +211,9 @@ void Viewport::onDraw() {
m_controller->update();
}
onCursorSet(instance.mouseCursor());
instance.update();
if(!m_gameView) {
instance.update();
}
}

if(m_screenInProgress && m_color) {
Expand Down
Loading