Main.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. namespace BansheeEngine
  29. {
  30. Path exampleModelPath = "..\\..\\..\\..\\Data\\Examples\\Pyromancer.fbx";
  31. Path exampleTexturePath = "..\\..\\..\\..\\Data\\Examples\\Pyromancer.psd";
  32. Path exampleFragmentShaderPath = "..\\..\\..\\..\\Data\\Examples\\example_fs.gpuprog";
  33. Path exampleVertexShaderPath = "..\\..\\..\\..\\Data\\Examples\\example_vs.gpuprog";
  34. GUIButton* toggleFullscreenButton = nullptr;
  35. UINT32 resolutionWidth = 1280;
  36. UINT32 resolutionHeight = 720;
  37. bool fullscreen = false;
  38. const VideoMode* videoMode = nullptr;
  39. HMesh exampleModel;
  40. HTexture exampleTexture;
  41. HGpuProgram exampleFragmentGPUProg;
  42. HGpuProgram exampleVertexGPUProg;
  43. HCamera sceneCamera;
  44. void toggleFullscreen()
  45. {
  46. RenderWindowPtr window = gApplication().getPrimaryWindow();
  47. if (fullscreen)
  48. {
  49. gCoreAccessor().setWindowed(window, resolutionWidth, resolutionHeight);
  50. }
  51. else
  52. {
  53. //gCoreAccessor().setFullscreen(window, *videoMode);
  54. gCoreAccessor().setFullscreen(window, 1920, 1200);
  55. }
  56. fullscreen = !fullscreen;
  57. }
  58. void setUpExample()
  59. {
  60. // Import assets
  61. exampleModel = static_resource_cast<Mesh>(Importer::instance().import(exampleModelPath));
  62. exampleTexture = static_resource_cast<Texture>(Importer::instance().import(exampleTexturePath));
  63. ImportOptionsPtr gpuProgImportOptions = Importer::instance().createImportOptions(exampleFragmentShaderPath);
  64. if (rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
  65. {
  66. GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
  67. importOptions->setEntryPoint("ps_main");
  68. importOptions->setLanguage("hlsl");
  69. importOptions->setProfile(GPP_PS_4_0);
  70. importOptions->setType(GPT_FRAGMENT_PROGRAM);
  71. }
  72. exampleFragmentGPUProg = static_resource_cast<GpuProgram>(Importer::instance().import(exampleFragmentShaderPath, gpuProgImportOptions));
  73. gpuProgImportOptions = Importer::instance().createImportOptions(exampleVertexShaderPath);
  74. if (rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
  75. {
  76. GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
  77. importOptions->setEntryPoint("vs_main");
  78. importOptions->setLanguage("hlsl");
  79. importOptions->setProfile(GPP_VS_4_0);
  80. importOptions->setType(GPT_VERTEX_PROGRAM);
  81. }
  82. exampleVertexGPUProg = static_resource_cast<GpuProgram>(Importer::instance().import(exampleVertexShaderPath, gpuProgImportOptions));
  83. // 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.
  84. // Create material
  85. ShaderPtr exampleShader = Shader::create("ExampleShader");
  86. exampleShader->addParameter("matViewProjection", "matViewProjection", GPDT_MATRIX_4X4);
  87. exampleShader->addParameter("samp", "samp", GPOT_SAMPLER2D);
  88. exampleShader->addParameter("tex", "tex", GPOT_TEXTURE2D);
  89. TechniquePtr technique = exampleShader->addTechnique(RenderSystemDX11, RendererDefault); // TODO - This render system and forward renderer names should at least match the above names used for initialization
  90. PassPtr pass = technique->addPass();
  91. pass->setVertexProgram(exampleVertexGPUProg);
  92. pass->setFragmentProgram(exampleFragmentGPUProg);
  93. HMaterial exampleMaterial = Material::create(exampleShader);
  94. exampleMaterial->setTexture("tex", exampleTexture);
  95. // Set up the object to render
  96. HSceneObject pyromancerSO = SceneObject::create("Pyromancer");
  97. HRenderable renderable = pyromancerSO->addComponent<Renderable>();
  98. renderable->setMesh(exampleModel);
  99. renderable->setMaterial(exampleMaterial);
  100. // Set up scene camera
  101. HSceneObject sceneCameraSO = SceneObject::create("SceneCamera");
  102. RenderWindowPtr window = gApplication().getPrimaryWindow();
  103. sceneCamera = sceneCameraSO->addComponent<Camera>(window, 0.0f, 0.0f, 1.0f, 1.0f);
  104. sceneCamera->setPriority(1);
  105. sceneCameraSO->setPosition(Vector3(0, 50, 1240));
  106. sceneCameraSO->lookAt(Vector3(0, 50, -300));
  107. sceneCamera->setNearClipDistance(5);
  108. sceneCamera->setAspectRatio(resolutionWidth / (float)resolutionHeight); // TODO - This needs to get called whenever resolution changes
  109. // Register input configuration
  110. auto inputConfig = VirtualInput::instance().getConfiguration();
  111. inputConfig->registerButton("Forward", BC_W);
  112. inputConfig->registerButton("Left", BC_A);
  113. inputConfig->registerButton("Right", BC_D);
  114. inputConfig->registerButton("Back", BC_S);
  115. inputConfig->registerButton("SimThreadProfilerOverlay", BC_F1);
  116. inputConfig->registerButton("CoreThreadProfilerOverlay", BC_F2);
  117. inputConfig->registerButton("GPUProfilerOverlay", BC_F3);
  118. // TODO - Add vertical/horizontal axes here
  119. HSceneObject exampleSO = SceneObject::create("Example");
  120. HCamera guiCamera = exampleSO->addComponent<Camera>(window);
  121. guiCamera->setNearClipDistance(5);
  122. guiCamera->setAspectRatio(1.0f);
  123. guiCamera->setIgnoreSceneRenderables(true);
  124. HGUIWidget gui = exampleSO->addComponent<GUIWidget>(guiCamera->getViewport().get());
  125. gui->setDepth(128);
  126. gui->setSkin(BuiltinResources::instance().getGUISkin());
  127. // Profiler overlay GUI
  128. GUIArea* topArea = GUIArea::createStretchedXY(*gui, 0, 0, 0, 0);
  129. GUILayout& topLayout = topArea->getLayout().addLayoutY();
  130. topLayout.addElement(GUILabel::create(HString(L"Press F1 to toggle Sim thread profiler overlay")));
  131. topLayout.addElement(GUILabel::create(HString(L"Press F2 to toggle Core thread profiler overlay")));
  132. topLayout.addElement(GUILabel::create(HString(L"Press F3 to toggle GPU profiler overlay")));
  133. topLayout.addFlexibleSpace();
  134. // Resolution/Camera options GUI
  135. GUIArea* rightArea = GUIArea::createStretchedXY(*gui, 0, 0, 0, 0);
  136. rightArea->getLayout().addFlexibleSpace();
  137. GUILayout& rightLayout = rightArea->getLayout().addLayoutY();
  138. rightLayout.addSpace(50);
  139. toggleFullscreenButton = GUIButton::create(HString(L"Toggle fullscreen"));
  140. toggleFullscreenButton->onClick.connect(&toggleFullscreen);
  141. rightLayout.addElement(toggleFullscreenButton);
  142. // TODO - add ExampleGUI component
  143. }
  144. }
  145. using namespace BansheeEngine;
  146. int CALLBACK WinMain(
  147. _In_ HINSTANCE hInstance,
  148. _In_ HINSTANCE hPrevInstance,
  149. _In_ LPSTR lpCmdLine,
  150. _In_ int nCmdShow
  151. )
  152. {
  153. RENDER_WINDOW_DESC renderWindowDesc;
  154. renderWindowDesc.videoMode = VideoMode(resolutionWidth, resolutionHeight);
  155. renderWindowDesc.title = "Banshee Example App";
  156. renderWindowDesc.fullscreen = false;
  157. Application::startUp(renderWindowDesc, RenderSystemPlugin::DX11, RendererPlugin::Default);
  158. setUpExample();
  159. Application::instance().runMainLoop();
  160. Application::shutDown();
  161. return 0;
  162. }