OgreBase.cpp 5.1 KB

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