Main.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include <windows.h>
  2. #include "CmApplication.h"
  3. #include "CmImporter.h"
  4. #include "CmGpuProgramImportOptions.h"
  5. #include "CmMaterial.h"
  6. #include "CmShader.h"
  7. #include "CmTechnique.h"
  8. #include "CmPass.h"
  9. #include "CmCoreThreadAccessor.h"
  10. #include "BsApplication.h"
  11. #include "BsVirtualInput.h"
  12. #include "BsCamera.h"
  13. #include "BsRenderable.h"
  14. #include "BsGUIWidget.h"
  15. #include "BsGUIArea.h"
  16. #include "BsGUILayoutX.h"
  17. #include "BsGUILayoutY.h"
  18. #include "BsGUILabel.h"
  19. #include "BsGUIButton.h"
  20. #include "BsGUIListBox.h"
  21. #include "BsBuiltinResources.h"
  22. #include "CmRTTIType.h"
  23. #include "CmHString.h"
  24. #include "CmRenderWindow.h"
  25. #include "CmSceneObject.h"
  26. #include "CmCoreThread.h"
  27. using namespace BansheeEngine;
  28. Path exampleModelPath = "..\\..\\..\\..\\Data\\Examples\\Pyromancer.fbx";
  29. Path exampleTexturePath = "..\\..\\..\\..\\Data\\Examples\\Pyromancer.psd";
  30. Path exampleFragmentShaderPath = "..\\..\\..\\..\\Data\\Examples\\example_fs.gpuprog";
  31. Path exampleVertexShaderPath = "..\\..\\..\\..\\Data\\Examples\\example_vs.gpuprog";
  32. GUIButton* toggleFullscreenButton = nullptr;
  33. UINT32 resolutionWidth = 1280;
  34. UINT32 resolutionHeight = 720;
  35. bool fullscreen = false;
  36. const VideoMode* videoMode = nullptr;
  37. HMesh exampleModel;
  38. HTexture exampleTexture;
  39. HGpuProgram exampleFragmentGPUProg;
  40. HGpuProgram exampleVertexGPUProg;
  41. HCamera sceneCamera;
  42. void setUpExample();
  43. int CALLBACK WinMain(
  44. _In_ HINSTANCE hInstance,
  45. _In_ HINSTANCE hPrevInstance,
  46. _In_ LPSTR lpCmdLine,
  47. _In_ int nCmdShow
  48. )
  49. {
  50. RENDER_WINDOW_DESC renderWindowDesc;
  51. renderWindowDesc.width = resolutionWidth;
  52. renderWindowDesc.height = resolutionHeight;
  53. renderWindowDesc.title = "Banshee Example App";
  54. renderWindowDesc.fullscreen = false;
  55. gBansheeApp().startUp(renderWindowDesc, "CamelotD3D11RenderSystem", "BansheeForwardRenderer"); // TODO - Use enums instead of names. BansheeApp is a high level system that doesn't need to be as customizable.
  56. setUpExample();
  57. gBansheeApp().runMainLoop();
  58. gBansheeApp().shutDown();
  59. return 0;
  60. }
  61. void toggleFullscreen()
  62. {
  63. RenderWindowPtr window = gApplication().getPrimaryWindow();
  64. if (fullscreen)
  65. {
  66. gCoreAccessor().setWindowed(window);
  67. }
  68. else
  69. {
  70. //gCoreAccessor().setFullscreen(window, *videoMode);
  71. gCoreAccessor().setFullscreen(window, 1920, 1200);
  72. }
  73. fullscreen = !fullscreen;
  74. }
  75. void setUpExample()
  76. {
  77. // Import assets
  78. exampleModel = static_resource_cast<Mesh>(Importer::instance().import(exampleModelPath));
  79. exampleTexture = static_resource_cast<Texture>(Importer::instance().import(exampleTexturePath));
  80. ImportOptionsPtr gpuProgImportOptions = Importer::instance().createImportOptions(exampleFragmentShaderPath);
  81. if(rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
  82. {
  83. GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
  84. importOptions->setEntryPoint("ps_main");
  85. importOptions->setLanguage("hlsl");
  86. importOptions->setProfile(GPP_PS_4_0);
  87. importOptions->setType(GPT_FRAGMENT_PROGRAM);
  88. }
  89. exampleFragmentGPUProg = static_resource_cast<GpuProgram>(Importer::instance().import(exampleFragmentShaderPath, gpuProgImportOptions));
  90. gpuProgImportOptions = Importer::instance().createImportOptions(exampleVertexShaderPath);
  91. if(rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
  92. {
  93. GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
  94. importOptions->setEntryPoint("vs_main");
  95. importOptions->setLanguage("hlsl");
  96. importOptions->setProfile(GPP_VS_4_0);
  97. importOptions->setType(GPT_VERTEX_PROGRAM);
  98. }
  99. exampleVertexGPUProg = static_resource_cast<GpuProgram>(Importer::instance().import(exampleVertexShaderPath, gpuProgImportOptions));
  100. // TODO - Optionally make the entire import step one-time. After import save resources and the created scene. Then on next load just load them directly from disk.
  101. // Create material
  102. ShaderPtr exampleShader = Shader::create("ExampleShader");
  103. exampleShader->addParameter("matViewProjection", "matViewProjection", GPDT_MATRIX_4X4);
  104. exampleShader->addParameter("samp", "samp", GPOT_SAMPLER2D);
  105. exampleShader->addParameter("tex", "tex", GPOT_TEXTURE2D);
  106. TechniquePtr technique = exampleShader->addTechnique("D3D11RenderSystem", "ForwardRenderer"); // TODO - This render system and forward renderer names should at least match the above names used for initialization
  107. PassPtr pass = technique->addPass();
  108. pass->setVertexProgram(exampleVertexGPUProg);
  109. pass->setFragmentProgram(exampleFragmentGPUProg);
  110. HMaterial exampleMaterial = Material::create(exampleShader);
  111. exampleMaterial->setTexture("tex", exampleTexture);
  112. // Set up the object to render
  113. HSceneObject pyromancerSO = SceneObject::create("Pyromancer");
  114. HRenderable renderable = pyromancerSO->addComponent<Renderable>();
  115. renderable->setMesh(exampleModel);
  116. renderable->setMaterial(exampleMaterial);
  117. // Set up scene camera
  118. HSceneObject sceneCameraGO = SceneObject::create("SceneCamera");
  119. RenderWindowPtr window = gApplication().getPrimaryWindow(); // TODO - Up until now I'm using gBansheeApp and now I'm using gApplication. It's confusing. BansheeApp should derive from application
  120. sceneCamera = sceneCameraGO->addComponent<Camera>(window, 0.0f, 0.0f, 1.0f, 1.0f);
  121. sceneCamera->setPriority(1);
  122. sceneCameraGO->setPosition(Vector3(0,50,1240));
  123. sceneCameraGO->lookAt(Vector3(0,50,-300));
  124. sceneCamera->setNearClipDistance(5);
  125. sceneCamera->setAspectRatio(resolutionWidth / (float)resolutionHeight); // TODO - This needs to get called whenever resolution changes
  126. // Register input configuration
  127. auto inputConfig = VirtualInput::instance().getConfiguration();
  128. inputConfig->registerButton("Forward", BC_W);
  129. inputConfig->registerButton("Left", BC_A);
  130. inputConfig->registerButton("Right", BC_D);
  131. inputConfig->registerButton("Back", BC_S);
  132. inputConfig->registerButton("SimThreadProfilerOverlay", BC_F1);
  133. inputConfig->registerButton("CoreThreadProfilerOverlay", BC_F2);
  134. inputConfig->registerButton("GPUProfilerOverlay", BC_F3);
  135. // TODO - Add vertical/horizontal axes here
  136. HSceneObject exampleSO = SceneObject::create("Example");
  137. HCamera guiCamera = exampleSO->addComponent<Camera>(window);
  138. guiCamera->setNearClipDistance(5);
  139. guiCamera->setAspectRatio(1.0f);
  140. guiCamera->setIgnoreSceneRenderables(true);
  141. HGUIWidget gui = exampleSO->addComponent<GUIWidget>();
  142. gui->setDepth(128);
  143. gui->setSkin(BuiltinResources::instance().getGUISkin());
  144. // Profiler overlay GUI
  145. GUIArea* topArea = GUIArea::createStretchedXY(*gui, 0, 0, 0, 0);
  146. GUILayout& topLayout = topArea->getLayout().addLayoutY();
  147. topLayout.addElement(GUILabel::create(HString(L"Press F1 to toggle Sim thread profiler overlay")));
  148. topLayout.addElement(GUILabel::create(HString(L"Press F2 to toggle Core thread profiler overlay")));
  149. topLayout.addElement(GUILabel::create(HString(L"Press F3 to toggle GPU profiler overlay")));
  150. topLayout.addFlexibleSpace();
  151. // Resolution/Camera options GUI
  152. GUIArea* rightArea = GUIArea::createStretchedXY(*gui, 0, 0, 0, 0);
  153. rightArea->getLayout().addFlexibleSpace();
  154. GUILayout& rightLayout = rightArea->getLayout().addLayoutY();
  155. rightLayout.addSpace(50);
  156. toggleFullscreenButton = GUIButton::create(HString(L"Toggle fullscreen"));
  157. toggleFullscreenButton->onClick.connect(&toggleFullscreen);
  158. rightLayout.addElement(toggleFullscreenButton);
  159. // TODO - add ExampleGUI component
  160. }