CmApplication.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #include "CmApplication.h"
  2. #include "CmRenderSystem.h"
  3. #include "CmRenderSystemManager.h"
  4. #include "CmWindowEventUtilities.h"
  5. #include "CmHardwareBufferManager.h"
  6. #include "CmRenderWindow.h"
  7. #include "CmCamera.h"
  8. #include "CmViewport.h"
  9. #include "CmVector2.h"
  10. #include "CmHighLevelGpuProgram.h"
  11. #include "CmHighLevelGpuProgramManager.h"
  12. #include "CmDynLib.h"
  13. #include "CmDynLibManager.h"
  14. #include "CmSceneManager.h"
  15. #include "CmImporter.h"
  16. #include "CmResources.h"
  17. #include "CmMesh.h"
  18. #include "CmGameObject.h"
  19. #include "CmTime.h"
  20. #include "CmInput.h"
  21. #include "CmMaterial.h"
  22. #include "CmShader.h"
  23. #include "CmTechnique.h"
  24. #include "CmPass.h"
  25. #include "CmRendererManager.h"
  26. namespace CamelotEngine
  27. {
  28. Application::Application()
  29. :mRenderWindow(nullptr), mCamera(nullptr)
  30. { }
  31. void Application::startUp(const String& renderSystemDll, const String& rendererDll)
  32. {
  33. Time::startUp(new Time());
  34. Input::startUp(new Input());
  35. DynLibManager::startUp(new DynLibManager());
  36. HighLevelGpuProgramManager::startUp(new HighLevelGpuProgramManager());
  37. RenderSystemManager::startUp(renderSystemDll);
  38. loadPlugin(rendererDll);
  39. RendererManager::setActive("ForwardRenderer");
  40. RenderSystem* renderSystem = RenderSystemManager::getActive();
  41. renderSystem->_initialise(false, "Camelot Renderer");
  42. SceneManager::startUp(new SceneManager());
  43. Resources::startUp(new Resources("D:\\CamelotResourceMetas"));
  44. mRenderWindow = renderSystem->_createRenderWindow("Camelot Renderer", 800, 600, false);
  45. //renderSystem->setAmbientLight(1.0f, 1.0f, 1.0f);
  46. renderSystem->setLightingEnabled(false);
  47. mCameraGO = GameObject::create("MainCamera");
  48. mCamera = mCameraGO->addComponent<Camera>();
  49. mCamera->init(mRenderWindow, 0.0f, 0.0f, 1.0f, 1.0f, 0);
  50. mCameraGO->setPosition(Vector3(0,50,1240));
  51. mCameraGO->lookAt(Vector3(0,50,-300));
  52. mCamera->setNearClipDistance(5);
  53. mCamera->setAspectRatio(800.0f / 600.0f);
  54. /////////////////// HLSL SHADERS //////////////////////////
  55. //String fragShaderCode = "sampler2D tex; \
  56. // float4 ps_main(float2 uv : TEXCOORD0) : COLOR0 \
  57. // { \
  58. // float4 color = tex2D(tex, uv); \
  59. // return color; \
  60. // }";
  61. //mFragProg = HighLevelGpuProgramManager::instance().createProgram(fragShaderCode, "ps_main", "hlsl", GPT_FRAGMENT_PROGRAM, GPP_PS_2_0);
  62. //mFragProg->load();
  63. //String vertShaderCode = "float4x4 matViewProjection; \
  64. // void vs_main( \
  65. // float4 inPos : POSITION, \
  66. // float2 uv : TEXCOORD0, \
  67. // out float4 oPosition : POSITION, \
  68. // out float2 oUv : TEXCOORD0) \
  69. // { \
  70. // oPosition = mul(matViewProjection, inPos); \
  71. // oUv = uv; \
  72. // }";
  73. //mVertProg = HighLevelGpuProgramManager::instance().createProgram(vertShaderCode, "vs_main", "hlsl", GPT_VERTEX_PROGRAM, GPP_VS_2_0);
  74. //mVertProg->load();
  75. /////////////////// CG SHADERS //////////////////////////
  76. String fragShaderCode = "sampler2D tex; \
  77. float4 ps_main(float2 uv : TEXCOORD0) : COLOR0 \
  78. { \
  79. float4 color = tex2D(tex, uv); \
  80. return color; \
  81. }";
  82. mFragProg = HighLevelGpuProgramManager::instance().createProgram(fragShaderCode, "ps_main", "cg", GPT_FRAGMENT_PROGRAM, GPP_PS_2_0);
  83. mFragProg->init();
  84. String vertShaderCode = "float4x4 matViewProjection; \
  85. void vs_main( \
  86. float4 inPos : POSITION, \
  87. float2 uv : TEXCOORD0, \
  88. out float4 oPosition : POSITION, \
  89. out float2 oUv : TEXCOORD0) \
  90. { \
  91. oPosition = mul(matViewProjection, inPos); \
  92. oUv = uv; \
  93. }";
  94. mVertProg = HighLevelGpuProgramManager::instance().createProgram(vertShaderCode, "vs_main", "cg", GPT_VERTEX_PROGRAM, GPP_VS_2_0);
  95. mVertProg->init();
  96. HighLevelGpuProgramRef vertProgRef(mVertProg);
  97. gResources().create(vertProgRef, "C:\\vertProgCg.vprog", true);
  98. vertProgRef = static_resource_cast<HighLevelGpuProgram>(gResources().load("C:\\vertProgCg.vprog"));
  99. HighLevelGpuProgramRef fragProgRef(mFragProg);
  100. gResources().create(fragProgRef, "C:\\fragProgCg.vprog", true);
  101. fragProgRef = static_resource_cast<HighLevelGpuProgram>(gResources().load("C:\\fragProgCg.vprog"));
  102. ///////////////// GLSL SHADERS ////////////////////////////
  103. //String fragShaderCode = "uniform sampler2D tex; \
  104. // void main() \
  105. // {\
  106. // vec4 texColor = texture2D(tex,gl_TexCoord[0].st);\
  107. // gl_FragColor = texColor; \
  108. // }";
  109. //mFragProg = HighLevelGpuProgramManager::instance().createProgram(fragShaderCode, "main", "glsl", GPT_FRAGMENT_PROGRAM, GPP_PS_2_0);
  110. //mFragProg->load();
  111. //// TODO - Ogres GLSL parsing requires some strict parameter naming, can that be avoided?
  112. //String vertShaderCode = "uniform mat4 matViewProjection; \
  113. // attribute vec4 vertex; \
  114. // void main() \
  115. // { \
  116. // gl_TexCoord[0] = gl_MultiTexCoord0; \
  117. // gl_Position = matViewProjection * vertex; \
  118. // }";
  119. //mVertProg = HighLevelGpuProgramManager::instance().createProgram(vertShaderCode, "main", "glsl", GPT_VERTEX_PROGRAM, GPP_VS_2_0);
  120. //mVertProg->load();
  121. mTestShader = ShaderPtr(new Shader("TestShader"));
  122. TechniquePtr newTechnique = mTestShader->addTechnique("GLRenderSystem", "ForwardRenderer");
  123. PassPtr newPass = newTechnique->addPass();
  124. newPass->setVertexProgram(vertProgRef);
  125. newPass->setFragmentProgram(fragProgRef);
  126. mTestMaterial = MaterialPtr(new Material());
  127. mTestMaterial->setShader(mTestShader);
  128. // IMPORTER TEST
  129. Importer::startUp(new Importer());
  130. loadPlugin("CamelotFreeImgImporter"); // TODO - Load this automatically somehow
  131. loadPlugin("CamelotFBXImporter"); // TODO - Load this automatically somehow
  132. //mDbgTexture = std::static_pointer_cast<Texture>(Importer::instance().import("C:\\ImportTest.tga"));
  133. TextureRef testTex = static_resource_cast<Texture>(Importer::instance().import("C:\\ImportTest.tga"));
  134. mDbgMesh = static_resource_cast<Mesh>(Importer::instance().import("C:\\X_Arena_Tower.FBX"));
  135. //mDbgMesh = std::static_pointer_cast<Mesh>(Importer::instance().import("C:\\BarrelMesh.fbx"));
  136. gResources().create(testTex, "C:\\ExportTest.tex", true);
  137. gResources().create(mDbgMesh, "C:\\ExportMesh.mesh", true);
  138. mDbgTexture = static_resource_cast<Texture>(gResources().loadAsync("C:\\ExportTest.tex"));
  139. mDbgMesh = static_resource_cast<Mesh>(gResources().loadAsync("C:\\ExportMesh.mesh"));
  140. loadPlugin("CamelotOISInput"); // TODO - Load this automatically somehow
  141. }
  142. void Application::runMainLoop()
  143. {
  144. while(true)
  145. {
  146. WindowEventUtilities::messagePump();
  147. DBG_renderSimpleFrame();
  148. gTime().update();
  149. gInput().update();
  150. gResources().update();
  151. }
  152. }
  153. void Application::shutDown()
  154. {
  155. SceneManager::shutDown();
  156. if(RenderSystemManager::getActive() != nullptr)
  157. RenderSystemManager::getActive()->shutdown();
  158. HighLevelGpuProgramManager::shutDown();
  159. DynLibManager::shutDown();
  160. Resources::shutDown();
  161. Input::shutDown();
  162. Time::shutDown();
  163. }
  164. void Application::loadPlugin(const String& pluginName)
  165. {
  166. String name = pluginName;
  167. #if CM_PLATFORM == CM_PLATFORM_LINUX
  168. // dlopen() does not add .so to the filename, like windows does for .dll
  169. if (name.substr(name.length() - 3, 3) != ".so")
  170. name += ".so";
  171. #elif CM_PLATFORM == CM_PLATFORM_APPLE
  172. // dlopen() does not add .dylib to the filename, like windows does for .dll
  173. if (name.substr(name.length() - 6, 6) != ".dylib")
  174. name += ".dylib";
  175. #elif CM_PLATFORM == CM_PLATFORM_WIN32
  176. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  177. // if you include a relative path then it does not. So, add it to be sure.
  178. if (name.substr(name.length() - 4, 4) != ".dll")
  179. name += ".dll";
  180. #endif
  181. DynLib* library = gDynLibManager().load(name);
  182. if(library != nullptr)
  183. {
  184. typedef const void (*LoadPluginFunc)();
  185. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)library->getSymbol("loadPlugin");
  186. loadPluginFunc();
  187. }
  188. }
  189. UINT32 Application::getAppWindowId()
  190. {
  191. if(!mRenderWindow)
  192. {
  193. CM_EXCEPT(InternalErrorException, "Unable to get window handle. No active window is set!");
  194. }
  195. UINT32 windowId;
  196. mRenderWindow->getCustomAttribute("WINDOW", &windowId);
  197. return windowId;
  198. }
  199. void Application::DBG_renderSimpleFrame()
  200. {
  201. if(!mDbgTexture.isResolved() || !mDbgMesh.isResolved())
  202. {
  203. return;
  204. }
  205. RenderSystem* renderSystem = RenderSystemManager::getActive();
  206. renderSystem->_setViewport(mCamera->getViewport());
  207. //Matrix4 projMatrix = mCamera->getProjectionMatrixRS();
  208. //renderSystem->_setProjectionMatrix(projMatrix);
  209. //Matrix4 viewMatrix = mCamera->getViewMatrix(true);
  210. //renderSystem->_setViewMatrix(viewMatrix);
  211. Matrix4 projMatrixCstm = mCamera->getProjectionMatrix();
  212. Matrix4 viewMatrixCstm = mCamera->getViewMatrix();
  213. Matrix4 viewProjMatrix = projMatrixCstm * viewMatrixCstm;
  214. renderSystem->setInvertVertexWinding(false);
  215. renderSystem->_setDepthBufferParams();
  216. renderSystem->clearFrameBuffer(FBT_COLOUR | FBT_DEPTH, Color::Blue);
  217. renderSystem->_beginFrame();
  218. mTestMaterial->setMat4("matViewProjection", viewProjMatrix);
  219. mTestMaterial->setTexture("tex", mDbgTexture);
  220. mTestMaterial->applyPass(0);
  221. renderSystem->_render(mDbgMesh->getRenderOperation());
  222. renderSystem->_endFrame();
  223. renderSystem->_swapAllRenderTargetBuffers(false);
  224. }
  225. Application& gApplication()
  226. {
  227. static Application application;
  228. return application;
  229. }
  230. }