Main.cpp 7.0 KB

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