OgreBase.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "common/OgreBase.h"
  16. #include <iostream>
  17. #include <OgreFontManager.h>
  18. #include <OgreOverlay.h>
  19. #include <Overlay/OgreOverlayManager.h>
  20. Ogre::Root* root;
  21. Ogre::RenderWindow* window;
  22. std::vector<Ogre::Entity*> entities;
  23. Ogre::Camera *camera;
  24. DisplayFrameListener *frame_listener;
  25. void InitializeOgreBase(const Ogre::String& name){
  26. std::cout << "[OGRE] Initializing..." << std::endl;
  27. Ogre::LogManager* log_manager = new Ogre::LogManager();
  28. log_manager->createLog("v-gears.log", true, true);
  29. log_manager->getDefaultLog()->setLogDetail((Ogre::LoggingLevel)3);
  30. Ogre::String ressource_cfg("");
  31. Ogre::String plugins_cfg("");
  32. Ogre::String dyn_lib_ext("");
  33. Ogre::String render_system("");
  34. #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
  35. dyn_lib_ext = ".dll";
  36. std::cout << "[OGRE] Initializing LIBW32" << std::endl;
  37. #else // Assume Linux for now
  38. dyn_lib_ext = ".so";
  39. std::cout << "[OGRE] Initializing LIBLINUX" << std::endl;
  40. #endif
  41. #ifdef NDEBUG
  42. ressource_cfg = "resources.cfg";
  43. plugins_cfg = "plugins.cfg";
  44. render_system = "./RenderSystem_GL" + dyn_lib_ext;
  45. #else
  46. std::cout << "[OGRE] Initializing in debug mode." << std::endl;
  47. ressource_cfg = "resources_d.cfg";
  48. plugins_cfg = "plugins_d.cfg";
  49. render_system = "./RenderSystem_GL_d" + dyn_lib_ext;
  50. #endif
  51. // Init root early.
  52. root = new Ogre::Root(plugins_cfg);
  53. std::cout << "[InitializeOgreBase] 2" << std::endl;
  54. // Set up resources.
  55. // Load resource paths from config file.
  56. Ogre::ConfigFile cf;
  57. cf.load(ressource_cfg);
  58. // Go through all sections and settings in the file.
  59. Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
  60. Ogre::String secName, typeName, archName;
  61. Ogre::ResourceGroupManager &res_gm(Ogre::ResourceGroupManager::getSingleton());
  62. while (seci.hasMoreElements()){
  63. secName = seci.peekNextKey();
  64. Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
  65. Ogre::ConfigFile::SettingsMultiMap::iterator i;
  66. for (i = settings->begin(); i != settings->end(); ++i){
  67. typeName = i->first;
  68. archName = i->second;
  69. res_gm.addResourceLocation(archName, typeName, secName, true);
  70. }
  71. }
  72. // Configure.
  73. // Show the configuration dialog and initialize the system
  74. // You can skip this and use root.restoreConfig() to load configuration
  75. // settings if you were sure there are valid ones saved in ogre.cfg
  76. // TODO: This gives a compilation error, don't show the dialog until fixed.
  77. //if (!root->restoreConfig() && !root->showConfigDialog()){
  78. // root->setRenderSystem(root->getAvailableRenderers()[ 0 ]);
  79. //}*/
  80. root->setRenderSystem(root->getAvailableRenderers()[ 0 ]);
  81. root->initialise(false);
  82. Ogre::NameValuePairList misc;
  83. misc["title"] = name;
  84. window = root->createRenderWindow("VGearsWindow", 800, 600, false, &misc);
  85. // Initialize resources in the current launch directory.
  86. Ogre::ResourceGroupManager::getSingleton().addResourceLocation("./", "FileSystem", "General");
  87. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  88. "./exported", "FileSystem", "General"
  89. );
  90. Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
  91. Ogre::SceneManager* scene_manager;
  92. Ogre::Viewport* viewport;
  93. frame_listener = new DisplayFrameListener(window);
  94. root->addFrameListener(frame_listener);
  95. scene_manager = root->createSceneManager(Ogre::ST_GENERIC, "Scene");
  96. scene_manager->clearScene();
  97. scene_manager->setAmbientLight(Ogre::ColourValue(0.5, 0.5, 0.5));
  98. Ogre::Light* directional_light = scene_manager->createLight("directionalLight");
  99. directional_light->setType(Ogre::Light::LT_DIRECTIONAL);
  100. directional_light->setDiffuseColour(Ogre::ColourValue(0.5, 0.5, 0.5));
  101. directional_light->setSpecularColour(Ogre::ColourValue(0.5, 0.5, 0.5));
  102. directional_light->getParentSceneNode()->setDirection(Ogre::Vector3(0, 0, -1));
  103. camera = scene_manager->createCamera("Camera");
  104. camera->setNearClipDistance(0.01f);
  105. camera->getParentSceneNode()->setPosition(0, 5, 10);
  106. camera->getParentSceneNode()->lookAt(Ogre::Vector3(0, 0, 0), Ogre::Node::TS_LOCAL);
  107. viewport = window->addViewport(camera);
  108. viewport->setBackgroundColour(Ogre::ColourValue(0.0f, 0.4f, 0.0f));
  109. camera->setAspectRatio(
  110. Ogre::Real(viewport->getActualWidth()) / Ogre::Real(viewport->getActualHeight())
  111. );
  112. LOGGER = new Logger("game.log");
  113. }
  114. void DeinitializeOgreBase(){
  115. delete LOGGER;
  116. delete root;
  117. delete frame_listener;
  118. }