CameraManager.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <iostream>
  16. #include <OgreRenderWindow.h>
  17. #include <OgreRoot.h>
  18. #include <OgreViewport.h>
  19. #include <OIS/OIS.h>
  20. #include "core/CameraManager.h"
  21. #include "core/CameraManagerCommands.h"
  22. #include "app/VGearsApplication.h"
  23. #include "engine/config/ConfigVar.h"
  24. #include "core/InputManager.h"
  25. #include "common/Logger.h"
  26. ConfigVar cv_cam_speed("camera_speed", "Camera speed", "0.02");
  27. /**
  28. * Camera manager singleton.
  29. */
  30. template<>CameraManager* Ogre::Singleton<CameraManager>::msSingleton = nullptr;
  31. CameraManager::CameraManager():
  32. camera_free_(false),
  33. camera_free_rotate_(false),
  34. d2_position_(Ogre::Vector3::ZERO),
  35. d2_orientation_(Ogre::Quaternion::ZERO),
  36. d2_fov_(0),
  37. d2_scroll_(Ogre::Vector2::ZERO)
  38. {
  39. LOG_TRIVIAL("CameraManager started.");
  40. InitCommands();
  41. camera_ = Ogre::Root::getSingleton().getSceneManager("Scene")->createCamera("Camera");
  42. position_initial_
  43. = Ogre::Vector3(camera_->getPosition().x, camera_->getPosition().y, camera_->getPosition().z);
  44. orientation_initial_ = Ogre::Quaternion(
  45. camera_->getOrientation().w, camera_->getOrientation().x,
  46. camera_->getOrientation().z, camera_->getOrientation().z
  47. );
  48. camera_->setNearClipDistance(0.001f);
  49. camera_->setFarClipDistance(1000.0f);
  50. //camera_->setFixedYawAxis(true, Ogre::Vector3::UNIT_Y);
  51. //camera_->setPosition(Ogre::Vector3(0, 0, 0));
  52. Ogre::Root::getSingleton().getSceneManager("Scene")->getRootSceneNode()
  53. ->setPosition(Ogre::Vector3(0, 0, 0));
  54. //camera_->lookAt(Ogre::Vector3(0, 0, 0));
  55. Ogre::Root::getSingleton().getSceneManager("Scene")->getRootSceneNode()
  56. ->lookAt(Ogre::Vector3(0, 0, 0), Ogre::Node::TS_LOCAL);
  57. viewport_ = VGears::Application::getSingleton().getRenderWindow()->addViewport(camera_, 0);
  58. viewport_->setBackgroundColour(Ogre::ColourValue(0, 0, 0));
  59. camera_->setAspectRatio(
  60. Ogre::Real(viewport_->getActualWidth()) / Ogre::Real(viewport_->getActualHeight())
  61. );
  62. }
  63. CameraManager::~CameraManager(){LOG_TRIVIAL("CameraManager finished.");}
  64. void CameraManager::Input(const VGears::Event& event){}
  65. void CameraManager::Input(const VGears::Event& event, Ogre::Real time_since_last_frame){
  66. if (camera_free_ == true){
  67. float speed = cv_cam_speed.GetF() * time_since_last_frame;
  68. if(
  69. InputManager::getSingleton().IsButtonPressed(OIS::KC_RSHIFT)
  70. || InputManager::getSingleton().IsButtonPressed(OIS::KC_LSHIFT)
  71. ) speed *= 4;
  72. Ogre::SceneNode* rootScene =
  73. Ogre::Root::getSingleton().getSceneManager("Scene") ->getRootSceneNode();
  74. if (event.type == VGears::ET_KEY_IMPULSE && event.param1 == OIS::KC_W){
  75. //camera_->moveRelative(Ogre::Vector3(0, 0, -speed));
  76. rootScene->translate(Ogre::Vector3(0, 0, -speed), Ogre::Node::TS_LOCAL);
  77. }
  78. else if (event.type == VGears::ET_KEY_IMPULSE && event.param1 == OIS::KC_A){
  79. //camera_->moveRelative(Ogre::Vector3(-speed, 0, 0));
  80. rootScene->translate(Ogre::Vector3(-speed, 0, 0), Ogre::Node::TS_LOCAL);
  81. }
  82. else if (event.type == VGears::ET_KEY_IMPULSE && event.param1 == OIS::KC_S){
  83. //camera_->moveRelative(Ogre::Vector3(0, 0, speed));
  84. rootScene->translate(Ogre::Vector3(0, 0, speed), Ogre::Node::TS_LOCAL);
  85. }
  86. else if (event.type == VGears::ET_KEY_IMPULSE && event.param1 == OIS::KC_D){
  87. //camera_->moveRelative(Ogre::Vector3(speed, 0, 0));
  88. rootScene->translate(Ogre::Vector3(speed, 0, 0), Ogre::Node::TS_LOCAL);
  89. }
  90. //else if (event.type == 1)
  91. else if (event.type == VGears::ET_MOUSE_PRESS && event.param1 == OIS::MB_Right)
  92. camera_free_rotate_ = true;
  93. //else if (event.type == 5)
  94. else if (event.type == VGears::ET_MOUSE_RELEASE && event.param1 == OIS::MB_Right)
  95. camera_free_rotate_ = false;
  96. //else if (event.type == 8 && camera_free_rotate_ == true){
  97. else if (event.type == VGears::ET_MOUSE_MOVE && camera_free_rotate_ == true){
  98. rootScene->rotate(
  99. Ogre::Vector3::UNIT_Z, Ogre::Radian(Ogre::Degree(-event.param1 * 0.13f))
  100. );
  101. rootScene->pitch(Ogre::Degree(-event.param2 * 0.13f));
  102. }
  103. }
  104. }
  105. void CameraManager::OnResize(){
  106. camera_->setAspectRatio(
  107. Ogre::Real(viewport_->getActualWidth()) / Ogre::Real(viewport_->getActualHeight())
  108. );
  109. }
  110. void CameraManager::UpdateDebug(){}
  111. void CameraManager::ClearField(){}
  112. void CameraManager::ClearBattle(){}
  113. void CameraManager::ClearWorld(){}
  114. void CameraManager::SetCameraFree(const bool enable){
  115. camera_free_ = enable;
  116. if (camera_free_ == true){
  117. camera_->resetFrustumExtents();
  118. CameraManager::getSingleton().GetCurrentCamera()->setFOVy(
  119. Ogre::Radian(Ogre::Math::PI / 4.0f)
  120. );
  121. }
  122. else{
  123. // Set2DScroll sets position and orientation
  124. Set2DScroll(Get2DScroll());
  125. // TODO
  126. //CameraManager::getSingleton().GetCurrentCamera()->setPosition(
  127. // d2_position_
  128. //);
  129. //Ogre::Root::getSingleton().getSceneManager("Scene")
  130. // ->getRootSceneNode()->setPosition(d2_position_);
  131. //CameraManager::getSingleton().GetCurrentCamera()->setOrientation(
  132. // d2_orientation_
  133. //);
  134. //Ogre::Root::getSingleton().getSceneManager("Scene")
  135. // ->getRootSceneNode()->setOrientation(d2_orientation_);
  136. //CameraManager::getSingleton().GetCurrentCamera()->setFOVy(d2_fov_);
  137. //Set2DScroll(Get2DScroll());
  138. }
  139. }
  140. void CameraManager::Set2DCamera(
  141. const Ogre::Vector3 position, const Ogre::Quaternion orientation, const Ogre::Radian fov
  142. ){
  143. d2_position_ = position;
  144. d2_orientation_ = orientation;
  145. d2_fov_ = fov;
  146. // TODO
  147. CameraManager::getSingleton().GetCurrentCamera()->setPosition(d2_position_);
  148. //Ogre::Root::getSingleton().getSceneManager("Scene")
  149. // ->getRootSceneNode()->setPosition(d2_position_);
  150. CameraManager::getSingleton().GetCurrentCamera()->setOrientation(d2_orientation_);
  151. //Ogre::Root::getSingleton().getSceneManager("Scene")
  152. // ->getRootSceneNode()->setOrientation(d2_orientation_);
  153. CameraManager::getSingleton().GetCurrentCamera()->setFOVy(d2_fov_);
  154. Set2DScroll(Ogre::Vector2::ZERO);
  155. }
  156. void CameraManager::StartBattleCamera(
  157. const Ogre::Vector3 position, const Ogre::Vector3 orientation
  158. ){
  159. if (module_ != Module::BATTLE){
  160. LOG_ERROR("Tried to start battle camera but the CameraManager is already in battle mode");
  161. return;
  162. }
  163. /*std::cout << "CAMERA BATTLE START: " << camera_->getPosition().x << ", "
  164. << camera_->getPosition().y << ", " << camera_->getPosition().z << ", "
  165. << camera_->getOrientation().w << ", "<< camera_->getOrientation().x << ", "
  166. << camera_->getOrientation().y << ", " << camera_->getOrientation().z << std::endl;*/
  167. position_backup_ = Ogre::Vector3(
  168. camera_->getPosition().x, camera_->getPosition().y, camera_->getPosition().z
  169. );
  170. orientation_backup_ = Ogre::Quaternion(
  171. camera_->getOrientation().w, camera_->getOrientation().x,
  172. camera_->getOrientation().y, camera_->getOrientation().z
  173. );
  174. // The field camera has its own field of view and a frustum moved by the 2D scroll. The battle
  175. // camera needs the default ones, and a known orientation: lookAt depends on the current
  176. // orientation, so the camera would look different depending on where the battle started.
  177. fov_backup_ = camera_->getFOVy();
  178. SetBattleCamera(position, orientation);
  179. /*Ogre::SceneNode* rootScene =
  180. Ogre::Root::getSingleton().getSceneManager("Scene") ->getRootSceneNode();
  181. rootScene->setPosition(position);//, Ogre::Node::TS_LOCAL);
  182. rootScene->lookAt(orientation, Ogre::Node::TS_WORLD);*/
  183. /*std::cout << "CAMERA BATTLE START FINISH: " << camera_->getPosition().x << ", "
  184. << camera_->getPosition().y << ", " << camera_->getPosition().z << ", "
  185. << camera_->getOrientation().w << ", "<< camera_->getOrientation().x << ", "
  186. << camera_->getOrientation().y << ", " << camera_->getOrientation().z << std::endl;*/
  187. /*std::cout << "CAMERA BATTLE START FINISH: " << rootScene->getPosition().x << ", "
  188. << rootScene->getPosition().y << ", " << rootScene->getPosition().z << ", "
  189. << rootScene->getOrientation().w << ", "<< rootScene->getOrientation().x << ", "
  190. << rootScene->getOrientation().y << ", " << rootScene->getOrientation().z << std::endl;*/
  191. }
  192. void CameraManager::SetBattleCamera(const Ogre::Vector3 position, const Ogre::Vector3 orientation){
  193. // The second vector is the point the camera looks at.
  194. SetBattleCameraUpright(position, orientation);
  195. }
  196. void CameraManager::SetBattleCameraUpright(
  197. const Ogre::Vector3 position, const Ogre::Vector3 look_at
  198. ){
  199. Ogre::Vector3 direction = look_at - position;
  200. if (direction.squaredLength() < 1e-10f) return;
  201. direction.normalise();
  202. // Camera axes: X to the right, Y up and Z backwards, with the Z axis of the scene up.
  203. Ogre::Vector3 right = direction.crossProduct(Ogre::Vector3::UNIT_Z);
  204. // Looking straight up or down: any side is the right one.
  205. if (right.squaredLength() < 1e-8f) right = Ogre::Vector3::UNIT_X;
  206. right.normalise();
  207. const Ogre::Vector3 up = right.crossProduct(direction);
  208. camera_->resetFrustumExtents();
  209. camera_->setFOVy(Ogre::Radian(Ogre::Math::PI / 4.0f));
  210. camera_->setPosition(position);
  211. camera_->setOrientation(Ogre::Quaternion(right, up, -direction));
  212. // Compensation of the section of the screen that the battle UI covers: the camera moves down. It's
  213. // proportional to the distance (4 units at about 60 from the scene), because the cameras of the
  214. // effects go from very far to very close.
  215. const float distance = (look_at - position).length();
  216. camera_->move(Ogre::Vector3(0, 0, -0.066f * distance));
  217. }
  218. void CameraManager::EndBattleCamera(){
  219. /*std::cout << "CAMERA BATTLE END: " << camera_->getPosition().x << ", "
  220. << camera_->getPosition().y << ", " << camera_->getPosition().z << ", "
  221. << camera_->getOrientation().w << ", "<< camera_->getOrientation().x << ", "
  222. << camera_->getOrientation().y << ", " << camera_->getOrientation().z << std::endl;*/
  223. CameraManager::getSingleton().GetCurrentCamera()->setPosition(
  224. Ogre::Vector3(position_backup_.x, position_backup_.y, position_backup_.z)
  225. );
  226. CameraManager::getSingleton().GetCurrentCamera()->setOrientation(Ogre::Quaternion(
  227. orientation_backup_.w, orientation_backup_.x, orientation_backup_.y, orientation_backup_.z
  228. ));
  229. // Back to the field of view and 2D scroll of the field.
  230. camera_->setFOVy(fov_backup_);
  231. Set2DScroll(d2_scroll_);
  232. /*std::cout << "CAMERA BATTLE END FINISH: " << camera_->getPosition().x << ", "
  233. << camera_->getPosition().y << ", " << camera_->getPosition().z << ", "
  234. << camera_->getOrientation().w << ", "<< camera_->getOrientation().x << ", "
  235. << camera_->getOrientation().y << ", " << camera_->getOrientation().z << std::endl;*/
  236. }
  237. void CameraManager::SetMovieCamera(
  238. const Ogre::Vector3& position, const Ogre::Quaternion& orientation, const Ogre::Radian& fov
  239. ){
  240. movie_camera_ = true;
  241. camera_->setPosition(position);
  242. camera_->setOrientation(orientation);
  243. camera_->setFOVy(fov);
  244. // The movie fills the screen: it doesn't scroll.
  245. camera_->resetFrustumExtents();
  246. }
  247. void CameraManager::EndMovieCamera(){
  248. if (!movie_camera_) return;
  249. movie_camera_ = false;
  250. camera_->setPosition(d2_position_);
  251. camera_->setOrientation(d2_orientation_);
  252. camera_->setFOVy(d2_fov_);
  253. Set2DScroll(d2_scroll_);
  254. }
  255. void CameraManager::Set2DScroll(const Ogre::Vector2& position){
  256. d2_scroll_ = position;
  257. if (movie_camera_) return;
  258. if(camera_free_ == true) return;
  259. float width = static_cast<float>(viewport_->getActualWidth());
  260. float height = static_cast<float>(viewport_->getActualHeight());
  261. float left, right, top, bottom;
  262. camera_->resetFrustumExtents();
  263. Ogre::RealRect frustrumRect = camera_->getFrustumExtents();
  264. //camera_->getFrustumExtents(left, right, top, bottom);
  265. left = frustrumRect.left;
  266. right = frustrumRect.right;
  267. top = frustrumRect.top;
  268. bottom = frustrumRect.bottom;
  269. float move_x = ((right - left) / width) * position.x;
  270. float move_y = ((bottom - top) / height) * -position.y;
  271. camera_->setFrustumExtents(left - move_x, right - move_x, top + move_y, bottom + move_y);
  272. }
  273. const Ogre::Vector2& CameraManager::Get2DScroll() const{return d2_scroll_;}
  274. const Ogre::Vector3 CameraManager::ProjectPointToScreen(const Ogre::Vector3& point){
  275. Ogre::Vector3 view = camera_->getViewMatrix() * point;
  276. float z = view.z;
  277. view = camera_->getProjectionMatrix() * view;
  278. view.z = z;
  279. float width = static_cast<float>(viewport_->getActualWidth() / 2);
  280. float height = static_cast<float>(viewport_->getActualHeight() / 2);
  281. view.x = width + view.x * width;
  282. view.y = height - view.y * height;
  283. return view;
  284. }
  285. std::tuple<float, float, float> CameraManager::ScriptProjectToUi(
  286. const float x, const float y, const float z
  287. ){
  288. if (camera_ == nullptr || viewport_ == nullptr) return std::make_tuple(0.0f, 0.0f, 0.0f);
  289. const Ogre::Vector3 point = ProjectPointToScreen(Ogre::Vector3(x, y, z));
  290. // Pixels to UI units.
  291. const float scale = 720.0f / static_cast<float>(viewport_->getActualHeight());
  292. return std::make_tuple(point.x * scale, point.y * scale, point.z);
  293. }
  294. Ogre::Camera* CameraManager::GetCurrentCamera(){return camera_;}
  295. Ogre::Viewport* CameraManager::getViewport(){return viewport_;}
  296. void CameraManager::ScriptSetCamera(
  297. const int x, const int y, const int z, const int d_x, const int d_y, const int d_z
  298. ){
  299. Ogre::Vector3 position = Ogre::Vector3(x, y, z);
  300. camera_->setPosition(position);
  301. //Ogre::Quaternion orientation = position.getRotationTo(Ogre::Vector3(d_x, d_y, d_z));
  302. //camera_->setOrientation(Ogre::Quaternion(orientation_initial_.w, orientation_initial_.x, orientation_initial_.y, orientation_initial_.z));
  303. //CameraManager::getSingleton().GetCurrentCamera()->setFixedYawAxis(false);
  304. //camera_->setOrientation(orientation);
  305. //camera_->roll(orientation.getRoll());
  306. //camera_->yaw(orientation.getYaw());
  307. //camera_->pitch(orientation.getPitch());
  308. camera_->lookAt(Ogre::Vector3(d_x, d_y, camera_->getPosition().z));
  309. camera_->lookAt(Ogre::Vector3(d_x, d_y, d_z));
  310. }
  311. void CameraManager::UpdateField(){}
  312. void CameraManager::UpdateBattle(){}
  313. void CameraManager::UpdateWorld(){}