Main.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include <windows.h>
  2. #include "BsApplication.h"
  3. #include "BsImporter.h"
  4. #include "BsGpuProgramImportOptions.h"
  5. #include "BsMaterial.h"
  6. #include "BsShader.h"
  7. #include "BsTechnique.h"
  8. #include "BsPass.h"
  9. #include "BsCoreThreadAccessor.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 "BsGUISpace.h"
  19. #include "BsGUILabel.h"
  20. #include "BsGUIButton.h"
  21. #include "BsGUIListBox.h"
  22. #include "BsBuiltinResources.h"
  23. #include "BsRTTIType.h"
  24. #include "BsHString.h"
  25. #include "BsRenderWindow.h"
  26. #include "BsSceneObject.h"
  27. #include "BsCoreThread.h"
  28. #include "BsProfilerOverlay.h"
  29. namespace BansheeEngine
  30. {
  31. Path exampleModelPath = "..\\..\\..\\..\\Data\\Examples\\Pyromancer.fbx";
  32. Path exampleTexturePath = "..\\..\\..\\..\\Data\\Examples\\Pyromancer.psd";
  33. Path exampleFragmentShaderPath = "..\\..\\..\\..\\Data\\Examples\\example_fs.gpuprog";
  34. Path exampleVertexShaderPath = "..\\..\\..\\..\\Data\\Examples\\example_vs.gpuprog";
  35. GUIButton* toggleFullscreenButton = nullptr;
  36. UINT32 resolutionWidth = 1280;
  37. UINT32 resolutionHeight = 720;
  38. bool fullscreen = false;
  39. const VideoMode* videoMode = nullptr;
  40. HMesh exampleModel;
  41. HTexture exampleTexture;
  42. HGpuProgram exampleFragmentGPUProg;
  43. HGpuProgram exampleVertexGPUProg;
  44. HCamera sceneCamera;
  45. HProfilerOverlay profilerOverlay;
  46. VirtualButton toggleCPUProfilerBtn;
  47. VirtualButton toggleGPUProfilerBtn;
  48. bool cpuProfilerActive = false;
  49. bool gpuProfilerActive = false;
  50. void toggleFullscreen()
  51. {
  52. RenderWindowPtr window = gApplication().getPrimaryWindow();
  53. if (fullscreen)
  54. {
  55. gCoreAccessor().setWindowed(window, resolutionWidth, resolutionHeight);
  56. }
  57. else
  58. {
  59. //gCoreAccessor().setFullscreen(window, *videoMode);
  60. gCoreAccessor().setFullscreen(window, 1920, 1200);
  61. }
  62. fullscreen = !fullscreen;
  63. }
  64. void buttonUp(const VirtualButton& button, UINT32 deviceIdx)
  65. {
  66. if (button == toggleCPUProfilerBtn)
  67. {
  68. if (cpuProfilerActive)
  69. {
  70. profilerOverlay->hide();
  71. cpuProfilerActive = false;
  72. }
  73. else
  74. {
  75. profilerOverlay->show(ProfilerOverlayType::CPUSamples);
  76. cpuProfilerActive = true;
  77. gpuProfilerActive = false;
  78. }
  79. }
  80. else if (button == toggleGPUProfilerBtn)
  81. {
  82. if (gpuProfilerActive)
  83. {
  84. profilerOverlay->hide();
  85. gpuProfilerActive = false;
  86. }
  87. else
  88. {
  89. profilerOverlay->show(ProfilerOverlayType::GPUSamples);
  90. gpuProfilerActive = true;
  91. cpuProfilerActive = false;
  92. }
  93. }
  94. }
  95. void setUpExample()
  96. {
  97. // Import assets
  98. exampleModel = static_resource_cast<Mesh>(Importer::instance().import(exampleModelPath));
  99. exampleTexture = static_resource_cast<Texture>(Importer::instance().import(exampleTexturePath));
  100. ImportOptionsPtr gpuProgImportOptions = Importer::instance().createImportOptions(exampleFragmentShaderPath);
  101. if (rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
  102. {
  103. GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
  104. importOptions->setEntryPoint("ps_main");
  105. importOptions->setLanguage("hlsl");
  106. importOptions->setProfile(GPP_PS_4_0);
  107. importOptions->setType(GPT_FRAGMENT_PROGRAM);
  108. }
  109. exampleFragmentGPUProg = static_resource_cast<GpuProgram>(Importer::instance().import(exampleFragmentShaderPath, gpuProgImportOptions));
  110. gpuProgImportOptions = Importer::instance().createImportOptions(exampleVertexShaderPath);
  111. if (rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
  112. {
  113. GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
  114. importOptions->setEntryPoint("vs_main");
  115. importOptions->setLanguage("hlsl");
  116. importOptions->setProfile(GPP_VS_4_0);
  117. importOptions->setType(GPT_VERTEX_PROGRAM);
  118. }
  119. exampleVertexGPUProg = static_resource_cast<GpuProgram>(Importer::instance().import(exampleVertexShaderPath, gpuProgImportOptions));
  120. // 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.
  121. // Create material
  122. ShaderPtr exampleShader = Shader::create("ExampleShader");
  123. exampleShader->addParameter("matViewProjection", "matViewProjection", GPDT_MATRIX_4X4);
  124. exampleShader->addParameter("samp", "samp", GPOT_SAMPLER2D);
  125. exampleShader->addParameter("tex", "tex", GPOT_TEXTURE2D);
  126. TechniquePtr technique = exampleShader->addTechnique(RenderSystemDX11, RendererDefault); // TODO - This render system and forward renderer names should at least match the above names used for initialization
  127. PassPtr pass = technique->addPass();
  128. pass->setVertexProgram(exampleVertexGPUProg);
  129. pass->setFragmentProgram(exampleFragmentGPUProg);
  130. HMaterial exampleMaterial = Material::create(exampleShader);
  131. exampleMaterial->setTexture("tex", exampleTexture);
  132. // Set up the object to render
  133. HSceneObject pyromancerSO = SceneObject::create("Pyromancer");
  134. HRenderable renderable = pyromancerSO->addComponent<Renderable>();
  135. renderable->setMesh(exampleModel);
  136. renderable->setMaterial(exampleMaterial);
  137. // Set up scene camera
  138. HSceneObject sceneCameraSO = SceneObject::create("SceneCamera");
  139. RenderWindowPtr window = gApplication().getPrimaryWindow();
  140. sceneCamera = sceneCameraSO->addComponent<Camera>(window, 0.0f, 0.0f, 1.0f, 1.0f);
  141. sceneCamera->setPriority(1);
  142. sceneCameraSO->setPosition(Vector3(0, 50, 1240));
  143. sceneCameraSO->lookAt(Vector3(0, 50, -300));
  144. sceneCamera->setNearClipDistance(5);
  145. sceneCamera->setAspectRatio(resolutionWidth / (float)resolutionHeight); // TODO - This needs to get called whenever resolution changes
  146. // Register input configuration
  147. auto inputConfig = VirtualInput::instance().getConfiguration();
  148. inputConfig->registerButton("Forward", BC_W);
  149. inputConfig->registerButton("Left", BC_A);
  150. inputConfig->registerButton("Right", BC_D);
  151. inputConfig->registerButton("Back", BC_S);
  152. inputConfig->registerButton("CPUProfilerOverlay", BC_F1);
  153. inputConfig->registerButton("GPUProfilerOverlay", BC_F2);
  154. toggleCPUProfilerBtn = VirtualButton("CPUProfilerOverlay");
  155. toggleGPUProfilerBtn = VirtualButton("GPUProfilerOverlay");
  156. VirtualInput::instance().onButtonUp.connect(&buttonUp);
  157. // TODO - Add vertical/horizontal axes here
  158. HSceneObject exampleSO = SceneObject::create("Example");
  159. HCamera guiCamera = exampleSO->addComponent<Camera>(window);
  160. guiCamera->setNearClipDistance(5);
  161. guiCamera->setAspectRatio(1.0f);
  162. guiCamera->setIgnoreSceneRenderables(true);
  163. HGUIWidget gui = exampleSO->addComponent<GUIWidget>(guiCamera->getViewport().get());
  164. gui->setDepth(128);
  165. gui->setSkin(BuiltinResources::instance().getGUISkin());
  166. // Profiler overlay GUI
  167. GUIArea* topArea = GUIArea::createStretchedXY(*gui, 0, 0, 0, 0);
  168. GUILayout& topLayout = topArea->getLayout().addLayoutY();
  169. topLayout.addElement(GUILabel::create(HString(L"Press F1 to toggle CPU profiler overlay")));
  170. topLayout.addElement(GUILabel::create(HString(L"Press F2 to toggle GPU profiler overlay")));
  171. topLayout.addFlexibleSpace();
  172. // Resolution/Camera options GUI
  173. GUIArea* rightArea = GUIArea::createStretchedXY(*gui, 0, 0, 0, 0);
  174. rightArea->getLayout().addFlexibleSpace();
  175. GUILayout& rightLayout = rightArea->getLayout().addLayoutY();
  176. rightLayout.addSpace(50);
  177. toggleFullscreenButton = GUIButton::create(HString(L"Toggle fullscreen"));
  178. toggleFullscreenButton->onClick.connect(&toggleFullscreen);
  179. rightLayout.addElement(toggleFullscreenButton);
  180. // Initialize profiler overlay
  181. profilerOverlay = exampleSO->addComponent<ProfilerOverlay>(guiCamera->getViewport());
  182. // TODO - add ExampleGUI component
  183. }
  184. }
  185. using namespace BansheeEngine;
  186. int CALLBACK WinMain(
  187. _In_ HINSTANCE hInstance,
  188. _In_ HINSTANCE hPrevInstance,
  189. _In_ LPSTR lpCmdLine,
  190. _In_ int nCmdShow
  191. )
  192. {
  193. RENDER_WINDOW_DESC renderWindowDesc;
  194. renderWindowDesc.videoMode = VideoMode(resolutionWidth, resolutionHeight);
  195. renderWindowDesc.title = "Banshee Example App";
  196. renderWindowDesc.fullscreen = false;
  197. Application::startUp(renderWindowDesc, RenderSystemPlugin::DX11, RendererPlugin::Default);
  198. setUpExample();
  199. Application::instance().runMainLoop();
  200. Application::shutDown();
  201. return 0;
  202. }