/* * Copyright (C) 2022 The V-Gears Team * * This file is part of V-Gears * * V-Gears is free software: you can redistribute it and/or modify it under * terms of the GNU General Public License as published by the Free Software * Foundation, version 3.0 (GPLv3) of the License. * * V-Gears is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ #include #include #include #include #include #include "core/CameraManager.h" #include "core/CameraManagerCommands.h" #include "common/VGearsApplication.h" #include "core/ConfigVar.h" #include "core/InputManager.h" #include "core/Logger.h" ConfigVar cv_cam_speed("camera_speed", "Camera speed", "0.02"); /** * Camera manager singleton. */ template<>CameraManager* Ogre::Singleton::msSingleton = nullptr; CameraManager::CameraManager(): camera_free_(false), camera_free_rotate_(false), d2_position_(Ogre::Vector3::ZERO), d2_orientation_(Ogre::Quaternion::ZERO), d2_fov_(0), d2_scroll_(Ogre::Vector2::ZERO) { LOG_TRIVIAL("CameraManager started."); InitCommands(); camera_ = Ogre::Root::getSingleton().getSceneManager("Scene")->createCamera("Camera"); position_initial_ = Ogre::Vector3(camera_->getPosition().x, camera_->getPosition().y, camera_->getPosition().z); orientation_initial_ = Ogre::Quaternion( camera_->getOrientation().w, camera_->getOrientation().x, camera_->getOrientation().z, camera_->getOrientation().z ); camera_->setNearClipDistance(0.001f); camera_->setFarClipDistance(1000.0f); //camera_->setFixedYawAxis(true, Ogre::Vector3::UNIT_Y); //camera_->setPosition(Ogre::Vector3(0, 0, 0)); Ogre::Root::getSingleton().getSceneManager("Scene")->getRootSceneNode() ->setPosition(Ogre::Vector3(0, 0, 0)); //camera_->lookAt(Ogre::Vector3(0, 0, 0)); Ogre::Root::getSingleton().getSceneManager("Scene")->getRootSceneNode() ->lookAt(Ogre::Vector3(0, 0, 0), Ogre::Node::TS_LOCAL); viewport_ = VGears::Application::getSingleton().getRenderWindow()->addViewport(camera_, 0); viewport_->setBackgroundColour(Ogre::ColourValue(0, 0, 0)); camera_->setAspectRatio( Ogre::Real(viewport_->getActualWidth()) / Ogre::Real(viewport_->getActualHeight()) ); } CameraManager::~CameraManager(){LOG_TRIVIAL("CameraManager finished.");} void CameraManager::Input(const VGears::Event& event){} void CameraManager::Input(const VGears::Event& event, Ogre::Real time_since_last_frame){ if (camera_free_ == true){ float speed = cv_cam_speed.GetF() * time_since_last_frame; if( InputManager::getSingleton().IsButtonPressed(OIS::KC_RSHIFT) || InputManager::getSingleton().IsButtonPressed(OIS::KC_LSHIFT) ) speed *= 4; Ogre::SceneNode* rootScene = Ogre::Root::getSingleton().getSceneManager("Scene") ->getRootSceneNode(); if (event.type == VGears::ET_KEY_IMPULSE && event.param1 == OIS::KC_W){ //camera_->moveRelative(Ogre::Vector3(0, 0, -speed)); rootScene->translate(Ogre::Vector3(0, 0, -speed), Ogre::Node::TS_LOCAL); } else if (event.type == VGears::ET_KEY_IMPULSE && event.param1 == OIS::KC_A){ //camera_->moveRelative(Ogre::Vector3(-speed, 0, 0)); rootScene->translate(Ogre::Vector3(-speed, 0, 0), Ogre::Node::TS_LOCAL); } else if (event.type == VGears::ET_KEY_IMPULSE && event.param1 == OIS::KC_S){ //camera_->moveRelative(Ogre::Vector3(0, 0, speed)); rootScene->translate(Ogre::Vector3(0, 0, speed), Ogre::Node::TS_LOCAL); } else if (event.type == VGears::ET_KEY_IMPULSE && event.param1 == OIS::KC_D){ //camera_->moveRelative(Ogre::Vector3(speed, 0, 0)); rootScene->translate(Ogre::Vector3(speed, 0, 0), Ogre::Node::TS_LOCAL); } //else if (event.type == 1) else if (event.type == VGears::ET_MOUSE_PRESS && event.param1 == OIS::MB_Right) camera_free_rotate_ = true; //else if (event.type == 5) else if (event.type == VGears::ET_MOUSE_RELEASE && event.param1 == OIS::MB_Right) camera_free_rotate_ = false; //else if (event.type == 8 && camera_free_rotate_ == true){ else if (event.type == VGears::ET_MOUSE_MOVE && camera_free_rotate_ == true){ rootScene->rotate( Ogre::Vector3::UNIT_Z, Ogre::Radian(Ogre::Degree(-event.param1 * 0.13f)) ); rootScene->pitch(Ogre::Degree(-event.param2 * 0.13f)); } } } void CameraManager::OnResize(){ camera_->setAspectRatio( Ogre::Real(viewport_->getActualWidth()) / Ogre::Real(viewport_->getActualHeight()) ); } void CameraManager::UpdateDebug(){} void CameraManager::ClearField(){} void CameraManager::ClearBattle(){} void CameraManager::ClearWorld(){} void CameraManager::SetCameraFree(const bool enable){ camera_free_ = enable; if (camera_free_ == true){ camera_->resetFrustumExtents(); CameraManager::getSingleton().GetCurrentCamera()->setFOVy( Ogre::Radian(Ogre::Math::PI / 4.0f) ); } else{ // Set2DScroll sets position and orientation Set2DScroll(Get2DScroll()); // TODO //CameraManager::getSingleton().GetCurrentCamera()->setPosition( // d2_position_ //); //Ogre::Root::getSingleton().getSceneManager("Scene") // ->getRootSceneNode()->setPosition(d2_position_); //CameraManager::getSingleton().GetCurrentCamera()->setOrientation( // d2_orientation_ //); //Ogre::Root::getSingleton().getSceneManager("Scene") // ->getRootSceneNode()->setOrientation(d2_orientation_); //CameraManager::getSingleton().GetCurrentCamera()->setFOVy(d2_fov_); //Set2DScroll(Get2DScroll()); } } void CameraManager::Set2DCamera( const Ogre::Vector3 position, const Ogre::Quaternion orientation, const Ogre::Radian fov ){ d2_position_ = position; d2_orientation_ = orientation; d2_fov_ = fov; // TODO CameraManager::getSingleton().GetCurrentCamera()->setPosition(d2_position_); //Ogre::Root::getSingleton().getSceneManager("Scene") // ->getRootSceneNode()->setPosition(d2_position_); CameraManager::getSingleton().GetCurrentCamera()->setOrientation(d2_orientation_); //Ogre::Root::getSingleton().getSceneManager("Scene") // ->getRootSceneNode()->setOrientation(d2_orientation_); CameraManager::getSingleton().GetCurrentCamera()->setFOVy(d2_fov_); Set2DScroll(Ogre::Vector2::ZERO); } void CameraManager::StartBattleCamera( const Ogre::Vector3 position, const Ogre::Vector3 orientation ){ /* * TODO: Something is wrong here. The camera look 'mostly' okay, but it's still weird. * Also, there are variations and it doesn't look the same on successive calls. * I thinks it's something related to the lookAt functions. * Try to fix it. First, replace the deprecated calls, and see what happens. */ if (module_ != Module::BATTLE){ LOG_ERROR("Tried to start battle camera but the CameraManager is already in battle mode"); return; } /*std::cout << "CAMERA BATTLE START: " << camera_->getPosition().x << ", " << camera_->getPosition().y << ", " << camera_->getPosition().z << ", " << camera_->getOrientation().w << ", "<< camera_->getOrientation().x << ", " << camera_->getOrientation().y << ", " << camera_->getOrientation().z << std::endl;*/ position_backup_ = Ogre::Vector3( camera_->getPosition().x, camera_->getPosition().y, camera_->getPosition().z ); orientation_backup_ = Ogre::Quaternion( camera_->getOrientation().w, camera_->getOrientation().x, camera_->getOrientation().y, camera_->getOrientation().z ); // The field camera has its own field of view and a frustum moved by the 2D scroll. The battle // camera needs the default ones, and a known orientation: lookAt depends on the current // orientation, so the camera would look different depending on where the battle started. fov_backup_ = camera_->getFOVy(); SetBattleCamera(position, orientation); /*Ogre::SceneNode* rootScene = Ogre::Root::getSingleton().getSceneManager("Scene") ->getRootSceneNode(); rootScene->setPosition(position);//, Ogre::Node::TS_LOCAL); rootScene->lookAt(orientation, Ogre::Node::TS_WORLD);*/ /*std::cout << "CAMERA BATTLE START FINISH: " << camera_->getPosition().x << ", " << camera_->getPosition().y << ", " << camera_->getPosition().z << ", " << camera_->getOrientation().w << ", "<< camera_->getOrientation().x << ", " << camera_->getOrientation().y << ", " << camera_->getOrientation().z << std::endl;*/ /*std::cout << "CAMERA BATTLE START FINISH: " << rootScene->getPosition().x << ", " << rootScene->getPosition().y << ", " << rootScene->getPosition().z << ", " << rootScene->getOrientation().w << ", "<< rootScene->getOrientation().x << ", " << rootScene->getOrientation().y << ", " << rootScene->getOrientation().z << std::endl;*/ } void CameraManager::SetBattleCamera(const Ogre::Vector3 position, const Ogre::Vector3 orientation){ camera_->resetFrustumExtents(); camera_->setFOVy(Ogre::Radian(Ogre::Math::PI / 4.0f)); camera_->setOrientation(Ogre::Quaternion::IDENTITY); camera_->setPosition(position); //CameraManager::getSingleton().GetCurrentCamera()->setFixedYawAxis(true, Ogre::Vector3::UNIT_Y); camera_->lookAt(orientation); camera_->roll(Ogre::Angle(90)); // Fix: compensate for the battle UI covered section of the screen. camera_->move(Ogre::Vector3(0, 0, -4)); } void CameraManager::SetBattleCameraUpright( const Ogre::Vector3 position, const Ogre::Vector3 look_at ){ Ogre::Vector3 direction = look_at - position; if (direction.squaredLength() < 1e-10f) return; direction.normalise(); // Camera axes: X to the right, Y up and Z backwards, with the Z axis of the scene up. Ogre::Vector3 right = direction.crossProduct(Ogre::Vector3::UNIT_Z); // Looking straight up or down: any side is the right one. if (right.squaredLength() < 1e-8f) right = Ogre::Vector3::UNIT_X; right.normalise(); const Ogre::Vector3 up = right.crossProduct(direction); camera_->resetFrustumExtents(); camera_->setFOVy(Ogre::Radian(Ogre::Math::PI / 4.0f)); camera_->setPosition(position); camera_->setOrientation(Ogre::Quaternion(right, up, -direction)); // Compensation of the battle UI, like the other battle cameras. They move the camera 4 units // down at about 60 units from the scene, so it's proportional to the distance here, because // the cameras of the effects go from very far to very close. const float distance = (look_at - position).length(); camera_->move(Ogre::Vector3(0, 0, -0.066f * distance)); } void CameraManager::EndBattleCamera(){ /*std::cout << "CAMERA BATTLE END: " << camera_->getPosition().x << ", " << camera_->getPosition().y << ", " << camera_->getPosition().z << ", " << camera_->getOrientation().w << ", "<< camera_->getOrientation().x << ", " << camera_->getOrientation().y << ", " << camera_->getOrientation().z << std::endl;*/ CameraManager::getSingleton().GetCurrentCamera()->setPosition( Ogre::Vector3(position_backup_.x, position_backup_.y, position_backup_.z) ); CameraManager::getSingleton().GetCurrentCamera()->setOrientation(Ogre::Quaternion( orientation_backup_.w, orientation_backup_.x, orientation_backup_.y, orientation_backup_.z )); // Back to the field of view and 2D scroll of the field. camera_->setFOVy(fov_backup_); Set2DScroll(d2_scroll_); /*std::cout << "CAMERA BATTLE END FINISH: " << camera_->getPosition().x << ", " << camera_->getPosition().y << ", " << camera_->getPosition().z << ", " << camera_->getOrientation().w << ", "<< camera_->getOrientation().x << ", " << camera_->getOrientation().y << ", " << camera_->getOrientation().z << std::endl;*/ } void CameraManager::SetMovieCamera( const Ogre::Vector3& position, const Ogre::Quaternion& orientation, const Ogre::Radian& fov ){ movie_camera_ = true; camera_->setPosition(position); camera_->setOrientation(orientation); camera_->setFOVy(fov); // The movie fills the screen: it doesn't scroll. camera_->resetFrustumExtents(); } void CameraManager::EndMovieCamera(){ if (!movie_camera_) return; movie_camera_ = false; camera_->setPosition(d2_position_); camera_->setOrientation(d2_orientation_); camera_->setFOVy(d2_fov_); Set2DScroll(d2_scroll_); } void CameraManager::Set2DScroll(const Ogre::Vector2& position){ d2_scroll_ = position; if (movie_camera_) return; if(camera_free_ == true) return; float width = static_cast(viewport_->getActualWidth()); float height = static_cast(viewport_->getActualHeight()); float left, right, top, bottom; camera_->resetFrustumExtents(); Ogre::RealRect frustrumRect = camera_->getFrustumExtents(); //camera_->getFrustumExtents(left, right, top, bottom); left = frustrumRect.left; right = frustrumRect.right; top = frustrumRect.top; bottom = frustrumRect.bottom; float move_x = ((right - left) / width) * position.x; float move_y = ((bottom - top) / height) * -position.y; camera_->setFrustumExtents(left - move_x, right - move_x, top + move_y, bottom + move_y); } const Ogre::Vector2& CameraManager::Get2DScroll() const{return d2_scroll_;} const Ogre::Vector3 CameraManager::ProjectPointToScreen(const Ogre::Vector3& point){ Ogre::Vector3 view = camera_->getViewMatrix() * point; float z = view.z; view = camera_->getProjectionMatrix() * view; view.z = z; float width = static_cast(viewport_->getActualWidth() / 2); float height = static_cast(viewport_->getActualHeight() / 2); view.x = width + view.x * width; view.y = height - view.y * height; return view; } std::tuple CameraManager::ScriptProjectToUi( const float x, const float y, const float z ){ if (camera_ == nullptr || viewport_ == nullptr) return std::make_tuple(0.0f, 0.0f, 0.0f); const Ogre::Vector3 point = ProjectPointToScreen(Ogre::Vector3(x, y, z)); // Pixels to UI units. const float scale = 720.0f / static_cast(viewport_->getActualHeight()); return std::make_tuple(point.x * scale, point.y * scale, point.z); } Ogre::Camera* CameraManager::GetCurrentCamera(){return camera_;} Ogre::Viewport* CameraManager::getViewport(){return viewport_;} void CameraManager::EnableWireFrame(bool enable){ if (camera_){ //camera_->setPolygonMode(Ogre::PM_WIREFRAME); //camera_->rotate( // Ogre::Vector3::UNIT_Z, Ogre::Radian(Ogre::Degree(180)) //); camera_->setFarClipDistance(900000000); } } void CameraManager::ScriptSetCamera( const int x, const int y, const int z, const int d_x, const int d_y, const int d_z ){ Ogre::Vector3 position = Ogre::Vector3(x, y, z); camera_->setPosition(position); //Ogre::Quaternion orientation = position.getRotationTo(Ogre::Vector3(d_x, d_y, d_z)); //camera_->setOrientation(Ogre::Quaternion(orientation_initial_.w, orientation_initial_.x, orientation_initial_.y, orientation_initial_.z)); //CameraManager::getSingleton().GetCurrentCamera()->setFixedYawAxis(false); //camera_->setOrientation(orientation); //camera_->roll(orientation.getRoll()); //camera_->yaw(orientation.getYaw()); //camera_->pitch(orientation.getPitch()); camera_->lookAt(Ogre::Vector3(d_x, d_y, camera_->getPosition().z)); camera_->lookAt(Ogre::Vector3(d_x, d_y, d_z)); } void CameraManager::UpdateField(){} void CameraManager::UpdateBattle(){} void CameraManager::UpdateWorld(){}