CamelotClient.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #include "stdafx.h"
  2. #include <windows.h>
  3. #include "BsApplication.h"
  4. #include "CmApplication.h"
  5. #include "CmDynLibManager.h"
  6. #include "CmSceneObject.h"
  7. #include "BsCamera.h"
  8. #include "CmHighLevelGpuProgramManager.h"
  9. #include "CmRenderSystem.h"
  10. #include "CmRenderWindow.h"
  11. #include "CmResources.h"
  12. #include "BsRenderable.h"
  13. #include "CmMaterial.h"
  14. #include "CmShader.h"
  15. #include "CmTechnique.h"
  16. #include "CmPass.h"
  17. #include "CmImporter.h"
  18. #include "CmMesh.h"
  19. #include "CmGpuProgInclude.h" // DEBUG ONLY
  20. #include "CmGpuProgramImportOptions.h"
  21. #include "CmFontImportOptions.h"
  22. #include "CmCommandQueue.h"
  23. #include "CmBlendState.h"
  24. #include "BsEditorWindowManager.h"
  25. #include "CmDebugCamera.h"
  26. #include "CmTestTextSprite.h"
  27. #include "DbgEditorWidget1.h"
  28. #include "DbgEditorWidget2.h"
  29. #include "CmRTTIType.h"
  30. #include "CmCursor.h"
  31. #define DX11
  32. //#define DX9
  33. //#define GL
  34. using namespace CamelotFramework;
  35. using namespace BansheeEditor;
  36. using namespace BansheeEngine;
  37. int CALLBACK WinMain(
  38. _In_ HINSTANCE hInstance,
  39. _In_ HINSTANCE hPrevInstance,
  40. _In_ LPSTR lpCmdLine,
  41. _In_ int nCmdShow
  42. )
  43. {
  44. RENDER_WINDOW_DESC renderWindowDesc;
  45. renderWindowDesc.width = 1280;
  46. renderWindowDesc.height = 720;
  47. renderWindowDesc.title = "Banshee";
  48. renderWindowDesc.fullscreen = false;
  49. #ifdef DX11
  50. gBansheeApp().startUp(renderWindowDesc, "CamelotD3D11RenderSystem", "BansheeForwardRenderer", "D:\\CamelotResourceMetas");
  51. #elif defined DX9
  52. gBansheeApp().startUp(renderWindowDesc, "CamelotD3D9RenderSystem", "BansheeForwardRenderer", "D:\\CamelotResourceMetas");
  53. #else
  54. gBansheeApp().startUp(renderWindowDesc, "CamelotGLRenderSystem", "BansheeForwardRenderer", "D:\\CamelotResourceMetas");
  55. #endif
  56. //CommandQueue::addBreakpoint(0, 19);
  57. //CommandQueue::addBreakpoint(0, 22);
  58. //CommandQueue::addBreakpoint(0, 12);
  59. RenderSystem* renderSystem = RenderSystem::instancePtr();
  60. RenderWindowPtr renderWindow = gApplication().getPrimaryWindow();
  61. HSceneObject cameraGO = SceneObject::create("MainCamera");
  62. HCamera camera = cameraGO->addComponent<Camera>();
  63. camera->initialize(renderWindow, 0.0f, 0.0f, 1.0f, 1.0f, 0);
  64. cameraGO->setPosition(Vector3(0,50,1240));
  65. cameraGO->lookAt(Vector3(0,50,-300));
  66. camera->setNearClipDistance(5);
  67. camera->setAspectRatio(800.0f / 600.0f);
  68. GameObjectHandle<DebugCamera> debugCamera = cameraGO->addComponent<DebugCamera>();
  69. HSceneObject testModelGO = SceneObject::create("TestMesh");
  70. HRenderable testRenderable = testModelGO->addComponent<Renderable>();
  71. HSceneObject testTextGO = SceneObject::create("TestText");
  72. GameObjectHandle<TestTextSprite> textSprite = testTextGO->addComponent<TestTextSprite>();
  73. textSprite->initialize(camera->getViewport().get(), renderWindow.get());
  74. textSprite->init(camera, "Testing in a new row, does this work?");
  75. #if defined DX9
  76. ///////////////// HLSL 9 SHADERS //////////////////////////
  77. String dx9psLoc = "C:\\Projects\\BansheeEngine\\Data\\hlsl9_ps.gpuprog";
  78. String dx9vsLoc = "C:\\Projects\\BansheeEngine\\Data\\hlsl9_vs.gpuprog";
  79. ImportOptionsPtr gpuProgImportOptions = Importer::instance().createImportOptions(dx9psLoc);
  80. if(rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
  81. {
  82. GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
  83. importOptions->setEntryPoint("ps_main");
  84. importOptions->setLanguage("hlsl");
  85. importOptions->setProfile(GPP_PS_2_0);
  86. importOptions->setType(GPT_FRAGMENT_PROGRAM);
  87. }
  88. HHighLevelGpuProgram fragProgRef = Importer::instance().import(dx9psLoc, gpuProgImportOptions);
  89. gpuProgImportOptions = Importer::instance().createImportOptions(dx9vsLoc);
  90. if(rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
  91. {
  92. GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
  93. importOptions->setEntryPoint("vs_main");
  94. importOptions->setLanguage("hlsl");
  95. importOptions->setProfile(GPP_VS_2_0);
  96. importOptions->setType(GPT_VERTEX_PROGRAM);
  97. }
  98. HHighLevelGpuProgram vertProgRef = Importer::instance().import(dx9vsLoc, gpuProgImportOptions);
  99. #elif defined DX11
  100. HGpuProgInclude gpuProgInclude = Importer::instance().import("C:\\testInclude.gpuproginc");
  101. const String& debugString = gpuProgInclude->getString();
  102. /////////////////// HLSL 11 SHADERS //////////////////////////
  103. String dx11psLoc = "C:\\Projects\\BansheeEngine\\Data\\hlsl11_ps.gpuprog";
  104. String dx11vsLoc = "C:\\Projects\\BansheeEngine\\Data\\hlsl11_vs.gpuprog";
  105. ImportOptionsPtr gpuProgImportOptions = Importer::instance().createImportOptions(dx11psLoc);
  106. if(rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
  107. {
  108. GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
  109. importOptions->setEntryPoint("ps_main");
  110. importOptions->setLanguage("hlsl");
  111. importOptions->setProfile(GPP_PS_4_0);
  112. importOptions->setType(GPT_FRAGMENT_PROGRAM);
  113. }
  114. HHighLevelGpuProgram fragProgRef = Importer::instance().import(dx11psLoc, gpuProgImportOptions);
  115. gpuProgImportOptions = Importer::instance().createImportOptions(dx11vsLoc);
  116. if(rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
  117. {
  118. GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
  119. importOptions->setEntryPoint("vs_main");
  120. importOptions->setLanguage("hlsl");
  121. importOptions->setProfile(GPP_VS_4_0);
  122. importOptions->setType(GPT_VERTEX_PROGRAM);
  123. }
  124. HHighLevelGpuProgram vertProgRef = Importer::instance().import(dx11vsLoc, gpuProgImportOptions);
  125. #else
  126. ///////////////// GLSL SHADERS ////////////////////////////
  127. String glslpsLoc = "C:\\Projects\\BansheeEngine\\Data\\glsl_ps.gpuprog";
  128. String glslvsLoc = "C:\\Projects\\BansheeEngine\\Data\\glsl_vs.gpuprog";
  129. ImportOptionsPtr gpuProgImportOptions = Importer::instance().createImportOptions(glslpsLoc);
  130. if(rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
  131. {
  132. GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
  133. importOptions->setEntryPoint("main");
  134. importOptions->setLanguage("glsl");
  135. importOptions->setProfile(GPP_PS_2_0);
  136. importOptions->setType(GPT_FRAGMENT_PROGRAM);
  137. }
  138. HHighLevelGpuProgram fragProgRef = Importer::instance().import(glslpsLoc, gpuProgImportOptions);
  139. gpuProgImportOptions = Importer::instance().createImportOptions(glslvsLoc);
  140. if(rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
  141. {
  142. GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
  143. importOptions->setEntryPoint("main");
  144. importOptions->setLanguage("glsl");
  145. importOptions->setProfile(GPP_VS_2_0);
  146. importOptions->setType(GPT_VERTEX_PROGRAM);
  147. }
  148. HHighLevelGpuProgram vertProgRef = Importer::instance().import(glslvsLoc, gpuProgImportOptions);
  149. #endif
  150. gResources().create(vertProgRef, "C:\\vertProgCg.vprog", true);
  151. gResources().unload(vertProgRef);
  152. vertProgRef = gResources().load("C:\\vertProgCg.vprog");
  153. gResources().create(fragProgRef, "C:\\fragProgCg.vprog", true);
  154. gResources().unload(fragProgRef);
  155. fragProgRef = gResources().load("C:\\fragProgCg.vprog");
  156. ShaderPtr testShader = Shader::create("TestShader");
  157. testShader->addParameter("matViewProjection", "matViewProjection", GPDT_MATRIX_4X4);
  158. #if defined DX11
  159. testShader->addParameter("input", "input", GPDT_STRUCT, 2, 8);
  160. #endif
  161. testShader->addParameter("samp", "samp", GPOT_SAMPLER2D);
  162. testShader->addParameter("tex", "tex", GPOT_TEXTURE2D);
  163. TechniquePtr newTechniqueGL = testShader->addTechnique("GLRenderSystem", "ForwardRenderer");
  164. PassPtr newPassGL = newTechniqueGL->addPass();
  165. newPassGL->setVertexProgram(vertProgRef);
  166. newPassGL->setFragmentProgram(fragProgRef);
  167. // TODO - I need to create different techniques for different render systems (and renderers, if there were any),
  168. // which is redundant as some techniques can be reused. I should add a functionality that supports multiple
  169. // render systems/renderers per technique
  170. TechniquePtr newTechniqueDX = testShader->addTechnique("D3D9RenderSystem", "ForwardRenderer");
  171. PassPtr newPassDX = newTechniqueDX->addPass();
  172. newPassDX->setVertexProgram(vertProgRef);
  173. newPassDX->setFragmentProgram(fragProgRef);
  174. TechniquePtr newTechniqueDX11 = testShader->addTechnique("D3D11RenderSystem", "ForwardRenderer");
  175. PassPtr newPassDX11 = newTechniqueDX11->addPass();
  176. newPassDX11->setVertexProgram(vertProgRef);
  177. newPassDX11->setFragmentProgram(fragProgRef);
  178. HMaterial testMaterial = Material::create();
  179. testMaterial->setShader(testShader);
  180. testMaterial->setMat4("matViewProjection", Matrix4::IDENTITY);
  181. #if defined DX11
  182. float dbgMultipliers1[2];
  183. dbgMultipliers1[0] = 0.0f;
  184. dbgMultipliers1[1] = 0.0f;
  185. float dbgMultipliers2[2];
  186. dbgMultipliers2[0] = 1.0f;
  187. dbgMultipliers2[1] = 1.0f;
  188. testMaterial->setStructData("input", dbgMultipliers1, sizeof(dbgMultipliers1), 0);
  189. testMaterial->setStructData("input", dbgMultipliers2, sizeof(dbgMultipliers2), 1);
  190. #endif
  191. //testMaterialRef = gResources().load("C:\\testMaterial.mat");
  192. //testMaterialRef.waitUntilLoaded();
  193. /*TextureRef testTex = static_resource_cast<Texture>(Importer::instance().import("C:\\ImportTest.tga"));*/
  194. HTexture testTexRef = static_resource_cast<Texture>(Importer::instance().import("C:\\ArenaTowerDFS.psd"));
  195. HMesh dbgMeshRef = static_resource_cast<Mesh>(Importer::instance().import("C:\\X_Arena_Tower.FBX"));
  196. //int tmpFlag = _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_CHECK_CRT_DF | _CRTDBG_DELAY_FREE_MEM_DF);
  197. gResources().create(testTexRef, "C:\\ExportTest.tex", true);
  198. gResources().create(dbgMeshRef, "C:\\ExportMesh.mesh", true);
  199. gResources().unload(testTexRef);
  200. gResources().unload(dbgMeshRef);
  201. testTexRef = static_resource_cast<Texture>(gResources().loadAsync("C:\\ExportTest.tex"));
  202. dbgMeshRef = static_resource_cast<Mesh>(gResources().loadAsync("C:\\ExportMesh.mesh"));
  203. dbgMeshRef.waitUntilLoaded();
  204. testTexRef.waitUntilLoaded();
  205. testMaterial->setTexture("tex", testTexRef);
  206. gResources().create(testMaterial, "C:\\ExportMaterial.mat", true);
  207. gResources().unload(testMaterial);
  208. testMaterial = gResources().load("C:\\ExportMaterial.mat");
  209. //_ASSERT(_CrtCheckMemory());
  210. testRenderable->setMesh(dbgMeshRef);
  211. testRenderable->setMaterial(testMaterial);
  212. //// Set the new state for the flag
  213. //_CrtSetDbgFlag( tmpFlag );
  214. HTexture dbgCursor = static_resource_cast<Texture>(Importer::instance().import("C:\\CursorDbg.psd"));
  215. PixelDataPtr cursorPixelData = dbgCursor->allocateSubresourceBuffer(0);
  216. gMainSyncedCA().readSubresource(dbgCursor.getInternalPtr(), 0, *cursorPixelData);
  217. gMainSyncedCA().submitToCoreThread(true);
  218. //Cursor::setCustomCursor(*cursorPixelData, Int2(0, 0));
  219. dbgCursor.reset();
  220. /************************************************************************/
  221. /* EDITOR INIT */
  222. /************************************************************************/
  223. EditorWindowManager::startUp(cm_new<EditorWindowManager>());
  224. /************************************************************************/
  225. /* EDITOR INIT END */
  226. /************************************************************************/
  227. DbgEditorWidget1::open();
  228. DbgEditorWidget2::open();
  229. gBansheeApp().runMainLoop();
  230. /************************************************************************/
  231. /* EDITOR SHUTDOWN */
  232. /************************************************************************/
  233. EditorWindowManager::shutDown();
  234. /************************************************************************/
  235. /* EDITOR SHUTDOWN END */
  236. /************************************************************************/
  237. //testMaterial->destroy();
  238. #ifdef DX11
  239. gpuProgInclude.reset();
  240. #endif
  241. gResources().unload(testTexRef);
  242. gResources().unload(dbgMeshRef);
  243. gResources().unload(fragProgRef);
  244. gResources().unload(vertProgRef);
  245. gResources().unload(testMaterial);
  246. testMaterial.reset();
  247. testTexRef.reset();
  248. dbgMeshRef.reset();
  249. fragProgRef.reset();
  250. vertProgRef.reset();
  251. testModelGO->destroy();
  252. cameraGO->destroy();
  253. newPassGL = nullptr;
  254. newTechniqueGL = nullptr;
  255. newPassDX = nullptr;
  256. newTechniqueDX = nullptr;
  257. newPassDX11 = nullptr;
  258. newTechniqueDX11 = nullptr;
  259. testShader = nullptr;
  260. renderWindow = nullptr;
  261. gBansheeApp().shutDown();
  262. return 0;
  263. }