App3D.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "App3D.h"
  11. #include "Ogre.h"
  12. #include "RakAssert.h"
  13. #include "FormatString.h"
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdarg.h>
  17. #include "OgreRenderSystemCapabilities.h"
  18. #if defined(__GNUC__)
  19. #define _vsnprintf vsnprintf
  20. #endif
  21. static const char *defaultCameraName = "DefaultCamera";
  22. static const char *defaultSceneManagerName = "DefaultSceneManager";
  23. App3D::App3D()
  24. {
  25. root=0;
  26. camera=0;
  27. sceneManager=0;
  28. window=0;
  29. viewport=0;
  30. math=0;
  31. isVisible=true;
  32. //statsOverlay=0;
  33. }
  34. App3D::~App3D()
  35. {
  36. }
  37. void App3D::Update(AppTime curTimeMS,AppTime elapsedTimeMS)
  38. {
  39. AppInterface::Update(curTimeMS, elapsedTimeMS);
  40. UpdateDefault(curTimeMS, elapsedTimeMS);
  41. }
  42. void App3D::Render(AppTime curTimeMS)
  43. {
  44. if (isVisible)
  45. {
  46. ///
  47. /// SINCE WE ARE CALLING OGRE EXCEPTIONS ARE POSSIBLE HERE
  48. ///
  49. #ifdef _DEBUG
  50. try
  51. {
  52. #endif
  53. if (window->isActive())
  54. {
  55. root->renderOneFrame();
  56. }
  57. else if (window->isVisible()) // Need this isVisible check or it crashes like hell when alt-tabbing
  58. {
  59. window->update();
  60. }
  61. #ifdef _DEBUG
  62. }
  63. catch (Ogre::Exception &e)
  64. {
  65. RakAssert(0 && "Something has happened to the Ogre rendering state. Recovery is possible. The cause needs to be fixed.");
  66. }
  67. #endif
  68. }
  69. }
  70. void App3D::SetVisible(bool _isVisible)
  71. {
  72. isVisible=_isVisible;
  73. }
  74. bool App3D::IsVisible(void) const
  75. {
  76. return isVisible;
  77. }
  78. void App3D::UpdateDefault(AppTime curTimeMS,AppTime elapsedTimeMS)
  79. {
  80. }
  81. void App3D::PreConfigure(void)
  82. {
  83. #ifdef WIN32
  84. ::GetCurrentDirectory(sizeof(workingDirectory)-1, workingDirectory);
  85. #else
  86. // TODO - Get the current directory in linux
  87. RakAssert(0);
  88. #endif
  89. try
  90. {
  91. // If it throws an exception, change project properties / configuration properties / debugging / working directory to $(ProjectDir)
  92. #ifdef WIN32
  93. #ifdef _DEBUG
  94. root = new Ogre::Root("PluginsDebug.cfg", "Graphics.cfg", "Graphics.log");
  95. #else
  96. root = new Ogre::Root("Plugins.cfg", "Graphics.cfg", "Graphics.log");
  97. #endif
  98. #else
  99. #ifdef _DEBUG
  100. root = new Ogre::Root("PluginsDebugL.cfg", "Graphics.cfg", "Graphics.log");
  101. #else
  102. root = new Ogre::Root("PluginsL.cfg", "Graphics.cfg", "Graphics.log");
  103. #endif
  104. #endif
  105. }
  106. catch (Ogre::Exception* e)
  107. {
  108. e->getFullDescription();
  109. }
  110. }
  111. bool App3D::Configure(void)
  112. {
  113. try
  114. {
  115. if(root->restoreConfig() || root->showConfigDialog())
  116. {
  117. // If returned true, user clicked OK so initialise
  118. // Here we choose to let the system create a default rendering window by passing 'true'
  119. window = root->initialise(true, GetWindowTitle());
  120. return true;
  121. }
  122. else
  123. {
  124. return false;
  125. }
  126. }
  127. catch (Ogre::InvalidParametersException &e)
  128. {
  129. return false;
  130. }
  131. }
  132. void App3D::PostConfigure(const char *defaultResourceConfigurationPath, bool recursive)
  133. {
  134. // Instantiate the math class to build the trig tables.
  135. math = new Ogre::Math;
  136. // Load resource paths from config file
  137. Ogre::ConfigFile cf;
  138. cf.load(defaultResourceConfigurationPath);
  139. // Go through all sections & settings in the file
  140. Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
  141. Ogre::String secName, typeName, archName;
  142. while (seci.hasMoreElements())
  143. {
  144. secName = seci.peekNextKey();
  145. // Seems like General is created automatically going by ogre.log
  146. if (secName!="General")
  147. {
  148. try
  149. {
  150. Ogre::ResourceGroupManager::getSingleton().createResourceGroup(secName);
  151. }
  152. catch (Ogre::Exception& e)
  153. {
  154. e;
  155. }
  156. }
  157. Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
  158. Ogre::ConfigFile::SettingsMultiMap::iterator i;
  159. for (i = settings->begin(); i != settings->end(); ++i)
  160. {
  161. typeName = i->first;
  162. archName = i->second;
  163. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  164. archName, typeName, secName, recursive);
  165. }
  166. }
  167. // Window should have been created in Configure, or created here in a derived class
  168. Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup("Bootstrap");
  169. Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup("General");
  170. const Ogre::RenderSystemCapabilities* cap = root->getRenderSystem()->getCapabilities();
  171. // + (*cap).mMaxFragmentProgramVersion "ps_2_b" std::basic_string<char,std::char_traits<char>,std::allocator<char> >
  172. // if (cap->getMaxFragmentProgramVersion().c_str()[3]>='2')
  173. hasPixelShader2=true;
  174. // else
  175. // hasPixelShader2=false;
  176. }
  177. void App3D::InitSceneManager(Ogre::SceneManager *sm)
  178. {
  179. if (sm)
  180. sceneManager=sm;
  181. else
  182. sceneManager = root->createSceneManager(Ogre::ST_GENERIC, defaultSceneManagerName);
  183. // Normal object visibility is mask 1.
  184. Ogre::MovableObject::setDefaultVisibilityFlags(1);
  185. sceneManager->setVisibilityMask(1);
  186. }
  187. // Must be called after InitSceneManager
  188. void App3D::InitGUIManager(void)
  189. {
  190. }
  191. void App3D::InitCamera(Ogre::Camera *cam)
  192. {
  193. RakAssert(sceneManager);
  194. if (cam)
  195. camera=cam;
  196. else
  197. camera = sceneManager->createCamera(defaultCameraName);
  198. camera->setFOVy(Ogre::Radian(3.1415927/4.0f));
  199. }
  200. void App3D::InitViewport(Ogre::Viewport *vp)
  201. {
  202. // Create one viewport, entire window
  203. if (vp==0)
  204. {
  205. if (viewport==0)
  206. {
  207. viewport = window->addViewport(camera);
  208. viewport->setBackgroundColour(Ogre::ColourValue(0,0,0));
  209. }
  210. }
  211. else
  212. viewport=vp;
  213. // Alter the camera aspect ratio to match the viewport
  214. camera->setAspectRatio(
  215. (float)viewport->getActualWidth() / (float)viewport->getActualHeight());
  216. }
  217. void App3D::OnAppShutdown(void)
  218. {
  219. if (window)
  220. window->removeAllViewports();
  221. delete root;
  222. delete math;
  223. camera=0;
  224. sceneManager=0;
  225. }
  226. bool App3D::ShouldQuit(void) const
  227. {
  228. return window->isClosed();
  229. }
  230. void App3D::SetState(int stateType, RunnableState* state)
  231. {
  232. AppInterface::SetState(stateType, state);
  233. }
  234. Ogre::SceneManager* App3D::GetSceneManager(void) const
  235. {
  236. return sceneManager;
  237. }
  238. const char* App3D::GetWorkingDirectory(void) const
  239. {
  240. return workingDirectory;
  241. }
  242. bool App3D::HasPixelShader2(void) const
  243. {
  244. return hasPixelShader2;
  245. }
  246. const char * App3D::TakeScreenshot(const char *prefix, const char *suffix)
  247. {
  248. time_t aclock;
  249. time( &aclock ); // Get time in seconds
  250. tm *newtime = localtime( &aclock ); // Convert time to struct tm form
  251. char text[1024];
  252. strcpy(text, asctime( newtime ));
  253. text[strlen(text)-1]=0;
  254. // ':' character is not allowed for file names
  255. size_t len=strlen(text);
  256. for (size_t i=0;i<len;i++)
  257. {
  258. if (text[i]==':') text[i]='_';
  259. }
  260. char *str = FormatString("%s%s%s", prefix, text, suffix);
  261. window->writeContentsToFile(str);
  262. return str;
  263. }
粤ICP备19079148号