CameraManager.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 "common/VGearsApplication.h"
  23. #include "core/ConfigVar.h"
  24. #include "core/InputManager.h"
  25. #include "core/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. /*
  160. * TODO: Something is wrong here. The camera look 'mostly' okay, but it's still weird.
  161. * Also, there are variations and it doesn't look the same on successive calls.
  162. * I thinks it's something related to the lookAt functions.
  163. * Try to fix it. First, replace the deprecated calls, and see what happens.
  164. */
  165. if (module_ != Module::BATTLE){
  166. LOG_ERROR("Tried to start battle camera but the CameraManager is already in battle mode");
  167. return;
  168. }
  169. /*std::cout << "CAMERA BATTLE START: " << camera_->getPosition().x << ", "
  170. << camera_->getPosition().y << ", " << camera_->getPosition().z << ", "
  171. << camera_->getOrientation().w << ", "<< camera_->getOrientation().x << ", "
  172. << camera_->getOrientation().y << ", " << camera_->getOrientation().z << std::endl;*/
  173. position_backup_ = Ogre::Vector3(
  174. camera_->getPosition().x, camera_->getPosition().y, camera_->getPosition().z
  175. );
  176. orientation_backup_ = Ogre::Quaternion(
  177. camera_->getOrientation().w, camera_->getOrientation().x,
  178. camera_->getOrientation().y, camera_->getOrientation().z
  179. );
  180. // The field camera has its own field of view and a frustum moved by the 2D scroll. The battle
  181. // camera needs the default ones, and a known orientation: lookAt depends on the current
  182. // orientation, so the camera would look different depending on where the battle started.
  183. fov_backup_ = camera_->getFOVy();
  184. SetBattleCamera(position, orientation);
  185. /*Ogre::SceneNode* rootScene =
  186. Ogre::Root::getSingleton().getSceneManager("Scene") ->getRootSceneNode();
  187. rootScene->setPosition(position);//, Ogre::Node::TS_LOCAL);
  188. rootScene->lookAt(orientation, Ogre::Node::TS_WORLD);*/
  189. /*std::cout << "CAMERA BATTLE START FINISH: " << camera_->getPosition().x << ", "
  190. << camera_->getPosition().y << ", " << camera_->getPosition().z << ", "
  191. << camera_->getOrientation().w << ", "<< camera_->getOrientation().x << ", "
  192. << camera_->getOrientation().y << ", " << camera_->getOrientation().z << std::endl;*/
  193. /*std::cout << "CAMERA BATTLE START FINISH: " << rootScene->getPosition().x << ", "
  194. << rootScene->getPosition().y << ", " << rootScene->getPosition().z << ", "
  195. << rootScene->getOrientation().w << ", "<< rootScene->getOrientation().x << ", "
  196. << rootScene->getOrientation().y << ", " << rootScene->getOrientation().z << std::endl;*/
  197. }
  198. void CameraManager::SetBattleCamera(const Ogre::Vector3 position, const Ogre::Vector3 orientation){
  199. camera_->resetFrustumExtents();
  200. camera_->setFOVy(Ogre::Radian(Ogre::Math::PI / 4.0f));
  201. camera_->setOrientation(Ogre::Quaternion::IDENTITY);
  202. camera_->setPosition(position);
  203. //CameraManager::getSingleton().GetCurrentCamera()->setFixedYawAxis(true, Ogre::Vector3::UNIT_Y);
  204. camera_->lookAt(orientation);
  205. camera_->roll(Ogre::Angle(90));
  206. // Fix: compensate for the battle UI covered section of the screen.
  207. camera_->move(Ogre::Vector3(0, 0, -4));
  208. }
  209. void CameraManager::SetBattleCameraUpright(
  210. const Ogre::Vector3 position, const Ogre::Vector3 look_at
  211. ){
  212. Ogre::Vector3 direction = look_at - position;
  213. if (direction.squaredLength() < 1e-10f) return;
  214. direction.normalise();
  215. // Camera axes: X to the right, Y up and Z backwards, with the Z axis of the scene up.
  216. Ogre::Vector3 right = direction.crossProduct(Ogre::Vector3::UNIT_Z);
  217. // Looking straight up or down: any side is the right one.
  218. if (right.squaredLength() < 1e-8f) right = Ogre::Vector3::UNIT_X;
  219. right.normalise();
  220. const Ogre::Vector3 up = right.crossProduct(direction);
  221. camera_->resetFrustumExtents();
  222. camera_->setFOVy(Ogre::Radian(Ogre::Math::PI / 4.0f));
  223. camera_->setPosition(position);
  224. camera_->setOrientation(Ogre::Quaternion(right, up, -direction));
  225. // Compensation of the battle UI, like the other battle cameras. They move the camera 4 units
  226. // down at about 60 units from the scene, so it's proportional to the distance here, because
  227. // the cameras of the effects go from very far to very close.
  228. const float distance = (look_at - position).length();
  229. camera_->move(Ogre::Vector3(0, 0, -0.066f * distance));
  230. }
  231. void CameraManager::EndBattleCamera(){
  232. /*std::cout << "CAMERA BATTLE END: " << 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. CameraManager::getSingleton().GetCurrentCamera()->setPosition(
  237. Ogre::Vector3(position_backup_.x, position_backup_.y, position_backup_.z)
  238. );
  239. CameraManager::getSingleton().GetCurrentCamera()->setOrientation(Ogre::Quaternion(
  240. orientation_backup_.w, orientation_backup_.x, orientation_backup_.y, orientation_backup_.z
  241. ));
  242. // Back to the field of view and 2D scroll of the field.
  243. camera_->setFOVy(fov_backup_);
  244. Set2DScroll(d2_scroll_);
  245. /*std::cout << "CAMERA BATTLE END FINISH: " << camera_->getPosition().x << ", "
  246. << camera_->getPosition().y << ", " << camera_->getPosition().z << ", "
  247. << camera_->getOrientation().w << ", "<< camera_->getOrientation().x << ", "
  248. << camera_->getOrientation().y << ", " << camera_->getOrientation().z << std::endl;*/
  249. }
  250. void CameraManager::SetMovieCamera(
  251. const Ogre::Vector3& position, const Ogre::Quaternion& orientation, const Ogre::Radian& fov
  252. ){
  253. movie_camera_ = true;
  254. camera_->setPosition(position);
  255. camera_->setOrientation(orientation);
  256. camera_->setFOVy(fov);
  257. // The movie fills the screen: it doesn't scroll.
  258. camera_->resetFrustumExtents();
  259. }
  260. void CameraManager::EndMovieCamera(){
  261. if (!movie_camera_) return;
  262. movie_camera_ = false;
  263. camera_->setPosition(d2_position_);
  264. camera_->setOrientation(d2_orientation_);
  265. camera_->setFOVy(d2_fov_);
  266. Set2DScroll(d2_scroll_);
  267. }
  268. void CameraManager::Set2DScroll(const Ogre::Vector2& position){
  269. d2_scroll_ = position;
  270. if (movie_camera_) return;
  271. if(camera_free_ == true) return;
  272. float width = static_cast<float>(viewport_->getActualWidth());
  273. float height = static_cast<float>(viewport_->getActualHeight());
  274. float left, right, top, bottom;
  275. camera_->resetFrustumExtents();
  276. Ogre::RealRect frustrumRect = camera_->getFrustumExtents();
  277. //camera_->getFrustumExtents(left, right, top, bottom);
  278. left = frustrumRect.left;
  279. right = frustrumRect.right;
  280. top = frustrumRect.top;
  281. bottom = frustrumRect.bottom;
  282. float move_x = ((right - left) / width) * position.x;
  283. float move_y = ((bottom - top) / height) * -position.y;
  284. camera_->setFrustumExtents(left - move_x, right - move_x, top + move_y, bottom + move_y);
  285. }
  286. const Ogre::Vector2& CameraManager::Get2DScroll() const{return d2_scroll_;}
  287. const Ogre::Vector3 CameraManager::ProjectPointToScreen(const Ogre::Vector3& point){
  288. Ogre::Vector3 view = camera_->getViewMatrix() * point;
  289. float z = view.z;
  290. view = camera_->getProjectionMatrix() * view;
  291. view.z = z;
  292. float width = static_cast<float>(viewport_->getActualWidth() / 2);
  293. float height = static_cast<float>(viewport_->getActualHeight() / 2);
  294. view.x = width + view.x * width;
  295. view.y = height - view.y * height;
  296. return view;
  297. }
  298. std::tuple<float, float, float> CameraManager::ScriptProjectToUi(
  299. const float x, const float y, const float z
  300. ){
  301. if (camera_ == nullptr || viewport_ == nullptr) return std::make_tuple(0.0f, 0.0f, 0.0f);
  302. const Ogre::Vector3 point = ProjectPointToScreen(Ogre::Vector3(x, y, z));
  303. // Pixels to UI units.
  304. const float scale = 720.0f / static_cast<float>(viewport_->getActualHeight());
  305. return std::make_tuple(point.x * scale, point.y * scale, point.z);
  306. }
  307. Ogre::Camera* CameraManager::GetCurrentCamera(){return camera_;}
  308. Ogre::Viewport* CameraManager::getViewport(){return viewport_;}
  309. void CameraManager::EnableWireFrame(bool enable){
  310. if (camera_){
  311. //camera_->setPolygonMode(Ogre::PM_WIREFRAME);
  312. //camera_->rotate(
  313. // Ogre::Vector3::UNIT_Z, Ogre::Radian(Ogre::Degree(180))
  314. //);
  315. camera_->setFarClipDistance(900000000);
  316. }
  317. }
  318. void CameraManager::ScriptSetCamera(
  319. const int x, const int y, const int z, const int d_x, const int d_y, const int d_z
  320. ){
  321. Ogre::Vector3 position = Ogre::Vector3(x, y, z);
  322. camera_->setPosition(position);
  323. //Ogre::Quaternion orientation = position.getRotationTo(Ogre::Vector3(d_x, d_y, d_z));
  324. //camera_->setOrientation(Ogre::Quaternion(orientation_initial_.w, orientation_initial_.x, orientation_initial_.y, orientation_initial_.z));
  325. //CameraManager::getSingleton().GetCurrentCamera()->setFixedYawAxis(false);
  326. //camera_->setOrientation(orientation);
  327. //camera_->roll(orientation.getRoll());
  328. //camera_->yaw(orientation.getYaw());
  329. //camera_->pitch(orientation.getPitch());
  330. camera_->lookAt(Ogre::Vector3(d_x, d_y, camera_->getPosition().z));
  331. camera_->lookAt(Ogre::Vector3(d_x, d_y, d_z));
  332. }
  333. void CameraManager::UpdateField(){}
  334. void CameraManager::UpdateBattle(){}
  335. void CameraManager::UpdateWorld(){}