RocketApplication.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "RocketApplication.h"
  28. #include <Rocket/Core.h>
  29. #include <Rocket/Controls.h>
  30. #include <Rocket/Debugger.h>
  31. #include "RenderInterfaceOgre3D.h"
  32. #include "RocketFrameListener.h"
  33. #include "SystemInterfaceOgre3D.h"
  34. #include <direct.h>
  35. RocketApplication::RocketApplication()
  36. {
  37. context = NULL;
  38. ogre_system = NULL;
  39. ogre_renderer = NULL;
  40. // Switch the working directory to Ogre's bin directory.
  41. #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
  42. char path[MAX_PATH];
  43. rocket_path = getcwd(path, MAX_PATH);
  44. rocket_path += "/../../";
  45. // Normalise the path. This path is used to specify the resource location (see line 56 below).
  46. _fullpath(path, rocket_path.CString(), MAX_PATH);
  47. rocket_path = Rocket::Core::String(path).Replace("\\", "/");
  48. // The sample path is the path to the Ogre3D sample directory. All resources are loaded
  49. // relative to this path.
  50. sample_path = getcwd(path, MAX_PATH);
  51. sample_path += "/../Samples/basic/ogre3d/";
  52. #if OGRE_DEBUG_MODE
  53. chdir((Ogre::String(getenv("OGRE_HOME")) + "\\bin\\debug\\").c_str());
  54. #else
  55. chdir((Ogre::String(getenv("OGRE_HOME")) + "\\bin\\release\\").c_str());
  56. #endif
  57. #endif
  58. }
  59. RocketApplication::~RocketApplication()
  60. {
  61. }
  62. void RocketApplication::createScene()
  63. {
  64. Ogre::ResourceGroupManager::getSingleton().createResourceGroup("Rocket");
  65. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(rocket_path.Replace("\\", "/").CString(), "FileSystem", "Rocket");
  66. // Rocket initialisation.
  67. ogre_renderer = new RenderInterfaceOgre3D(mWindow->getWidth(), mWindow->getHeight());
  68. Rocket::Core::SetRenderInterface(ogre_renderer);
  69. ogre_system = new SystemInterfaceOgre3D();
  70. Rocket::Core::SetSystemInterface(ogre_system);
  71. Rocket::Core::Initialise();
  72. Rocket::Controls::Initialise();
  73. // Load the fonts from the path to the sample directory.
  74. Rocket::Core::FontDatabase::LoadFontFace(sample_path + "../../assets/Delicious-Roman.otf");
  75. Rocket::Core::FontDatabase::LoadFontFace(sample_path + "../../assets/Delicious-Bold.otf");
  76. Rocket::Core::FontDatabase::LoadFontFace(sample_path + "../../assets/Delicious-Italic.otf");
  77. Rocket::Core::FontDatabase::LoadFontFace(sample_path + "../../assets/Delicious-BoldItalic.otf");
  78. context = Rocket::Core::CreateContext("main", Rocket::Core::Vector2i(mWindow->getWidth(), mWindow->getHeight()));
  79. Rocket::Debugger::Initialise(context);
  80. // Load the mouse cursor and release the caller's reference.
  81. Rocket::Core::ElementDocument* cursor = context->LoadMouseCursor(sample_path + "../../assets/cursor.rml");
  82. if (cursor)
  83. cursor->RemoveReference();
  84. Rocket::Core::ElementDocument* document = context->LoadDocument(sample_path + "../../assets/demo.rml");
  85. if (document)
  86. {
  87. document->Show();
  88. document->RemoveReference();
  89. }
  90. // Add the application as a listener to Ogre's render queue so we can render during the overlay.
  91. mSceneMgr->addRenderQueueListener(this);
  92. }
  93. void RocketApplication::destroyScene()
  94. {
  95. // Shutdown Rocket.
  96. context->RemoveReference();
  97. Rocket::Core::Shutdown();
  98. delete ogre_system;
  99. ogre_system = NULL;
  100. delete ogre_renderer;
  101. ogre_renderer = NULL;
  102. }
  103. void RocketApplication::createFrameListener()
  104. {
  105. // Create the RocketFrameListener.
  106. mFrameListener = new RocketFrameListener(mWindow, mCamera, context);
  107. mRoot->addFrameListener(mFrameListener);
  108. // Show the frame stats overlay.
  109. mFrameListener->showDebugOverlay(true);
  110. }
  111. // Called from Ogre before a queue group is rendered.
  112. void RocketApplication::renderQueueStarted(uint8 queueGroupId, const Ogre::String& invocation, bool& ROCKET_UNUSED_PARAMETER(skipThisInvocation))
  113. {
  114. ROCKET_UNUSED(skipThisInvocation);
  115. if (queueGroupId == Ogre::RENDER_QUEUE_OVERLAY && Ogre::Root::getSingleton().getRenderSystem()->_getViewport()->getOverlaysEnabled())
  116. {
  117. context->Update();
  118. ConfigureRenderSystem();
  119. context->Render();
  120. }
  121. }
  122. // Called from Ogre after a queue group is rendered.
  123. void RocketApplication::renderQueueEnded(uint8 ROCKET_UNUSED_PARAMETER(queueGroupId), const Ogre::String& ROCKET_UNUSED_PARAMETER(invocation), bool& ROCKET_UNUSED_PARAMETER(repeatThisInvocation))
  124. {
  125. ROCKET_UNUSED(queueGroupId);
  126. ROCKET_UNUSED(invocation);
  127. ROCKET_UNUSED(repeatThisInvocation);
  128. }
  129. // Configures Ogre's rendering system for rendering Rocket.
  130. void RocketApplication::ConfigureRenderSystem()
  131. {
  132. Ogre::RenderSystem* render_system = Ogre::Root::getSingleton().getRenderSystem();
  133. // Set up the projection and view matrices.
  134. Ogre::Matrix4 projection_matrix;
  135. BuildProjectionMatrix(projection_matrix);
  136. render_system->_setProjectionMatrix(projection_matrix);
  137. render_system->_setViewMatrix(Ogre::Matrix4::IDENTITY);
  138. // Disable lighting, as all of Rocket's geometry is unlit.
  139. render_system->setLightingEnabled(false);
  140. // Disable depth-buffering; all of the geometry is already depth-sorted.
  141. render_system->_setDepthBufferParams(false, false);
  142. // Rocket generates anti-clockwise geometry, so enable clockwise-culling.
  143. render_system->_setCullingMode(Ogre::CULL_CLOCKWISE);
  144. // Disable fogging.
  145. render_system->_setFog(Ogre::FOG_NONE);
  146. // Enable writing to all four channels.
  147. render_system->_setColourBufferWriteEnabled(true, true, true, true);
  148. // Unbind any vertex or fragment programs bound previously by the application.
  149. render_system->unbindGpuProgram(Ogre::GPT_FRAGMENT_PROGRAM);
  150. render_system->unbindGpuProgram(Ogre::GPT_VERTEX_PROGRAM);
  151. // Set texture settings to clamp along both axes.
  152. Ogre::TextureUnitState::UVWAddressingMode addressing_mode;
  153. addressing_mode.u = Ogre::TextureUnitState::TAM_CLAMP;
  154. addressing_mode.v = Ogre::TextureUnitState::TAM_CLAMP;
  155. addressing_mode.w = Ogre::TextureUnitState::TAM_CLAMP;
  156. render_system->_setTextureAddressingMode(0, addressing_mode);
  157. // Set the texture coordinates for unit 0 to be read from unit 0.
  158. render_system->_setTextureCoordSet(0, 0);
  159. // Disable texture coordinate calculation.
  160. render_system->_setTextureCoordCalculation(0, Ogre::TEXCALC_NONE);
  161. // Enable linear filtering; images should be rendering 1 texel == 1 pixel, so point filtering could be used
  162. // except in the case of scaling tiled decorators.
  163. render_system->_setTextureUnitFiltering(0, Ogre::FO_LINEAR, Ogre::FO_LINEAR, Ogre::FO_POINT);
  164. // Disable texture coordinate transforms.
  165. render_system->_setTextureMatrix(0, Ogre::Matrix4::IDENTITY);
  166. // Reject pixels with an alpha of 0.
  167. render_system->_setAlphaRejectSettings(Ogre::CMPF_GREATER, 0, false);
  168. // Disable all texture units but the first.
  169. render_system->_disableTextureUnitsFrom(1);
  170. // Enable simple alpha blending.
  171. render_system->_setSceneBlending(Ogre::SBF_SOURCE_ALPHA, Ogre::SBF_ONE_MINUS_SOURCE_ALPHA);
  172. // Disable depth bias.
  173. render_system->_setDepthBias(0, 0);
  174. }
  175. // Builds an OpenGL-style orthographic projection matrix.
  176. void RocketApplication::BuildProjectionMatrix(Ogre::Matrix4& projection_matrix)
  177. {
  178. float z_near = -1;
  179. float z_far = 1;
  180. projection_matrix = Ogre::Matrix4::ZERO;
  181. // Set up matrices.
  182. projection_matrix[0][0] = 2.0f / mWindow->getWidth();
  183. projection_matrix[0][3]= -1.0000000f;
  184. projection_matrix[1][1]= -2.0f / mWindow->getHeight();
  185. projection_matrix[1][3]= 1.0000000f;
  186. projection_matrix[2][2]= -2.0f / (z_far - z_near);
  187. projection_matrix[3][3]= 1.0000000f;
  188. }