Main.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Framework includes
  2. #include "BsApplication.h"
  3. #include "Resources/BsResources.h"
  4. #include "Resources/BsBuiltinResources.h"
  5. #include "Material/BsMaterial.h"
  6. #include "Components/BsCCamera.h"
  7. #include "Components/BsCRenderable.h"
  8. #include "Components/BsCSkybox.h"
  9. #include "RenderAPI/BsRenderAPI.h"
  10. #include "RenderAPI/BsRenderWindow.h"
  11. #include "Scene/BsSceneObject.h"
  12. // Example includes
  13. #include "BsObjectRotator.h"
  14. #include "BsExampleFramework.h"
  15. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. // This example renders an object using the standard built-in physically based material.
  17. //
  18. // The example first loads necessary resources, including a mesh and textures to use for rendering. Then it creates a
  19. // material using the standard PBR shader. It then proceeds to register the relevant keys used for controling the camera
  20. // and the rendered object. Finally it sets up the 3D scene using the mesh, textures, material and sets up a camera, along
  21. // with CameraFlyer and ObjectRotator components that allow the user to fly around the scene and rotate the 3D model.
  22. //
  23. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  24. namespace bs
  25. {
  26. UINT32 windowResWidth = 1280;
  27. UINT32 windowResHeight = 720;
  28. /** Container for all resources used by the example. */
  29. struct Assets
  30. {
  31. HMesh exampleModel;
  32. HTexture exampleAlbedoTex;
  33. HTexture exampleNormalsTex;
  34. HTexture exampleRoughnessTex;
  35. HTexture exampleMetalnessTex;
  36. HTexture exampleSkyCubemap;
  37. HMaterial exampleMaterial;
  38. };
  39. /** Load the resources we'll be using throughout the example. */
  40. Assets loadAssets()
  41. {
  42. Assets assets;
  43. // Load a 3D model
  44. assets.exampleModel = ExampleFramework::loadMesh(ExampleMesh::Cerberus);
  45. // Load PBR textures for the 3D model
  46. assets.exampleAlbedoTex = ExampleFramework::loadTexture(ExampleTexture::CerberusAlbedo);
  47. assets.exampleNormalsTex = ExampleFramework::loadTexture(ExampleTexture::CerberusNormal, false);
  48. assets.exampleRoughnessTex = ExampleFramework::loadTexture(ExampleTexture::CerberusRoughness, false);
  49. assets.exampleMetalnessTex = ExampleFramework::loadTexture(ExampleTexture::CerberusMetalness, false);
  50. // Create a material using the default physically based shader, and apply the PBR textures we just loaded
  51. HShader shader = gBuiltinResources().getBuiltinShader(BuiltinShader::Standard);
  52. assets.exampleMaterial = Material::create(shader);
  53. assets.exampleMaterial->setTexture("gAlbedoTex", assets.exampleAlbedoTex);
  54. assets.exampleMaterial->setTexture("gNormalTex", assets.exampleNormalsTex);
  55. assets.exampleMaterial->setTexture("gRoughnessTex", assets.exampleRoughnessTex);
  56. assets.exampleMaterial->setTexture("gMetalnessTex", assets.exampleMetalnessTex);
  57. // Load an environment map
  58. assets.exampleSkyCubemap = ExampleFramework::loadTexture(ExampleTexture::EnvironmentPaperMill, false, true, true);
  59. return assets;
  60. }
  61. /** Set up the 3D object used by the example, and the camera to view the world through. */
  62. void setUp3DScene(const Assets& assets)
  63. {
  64. /************************************************************************/
  65. /* RENDERABLE */
  66. /************************************************************************/
  67. // Now we create a scene object that has a position, orientation, scale and optionally components to govern its
  68. // logic. In this particular case we are creating a SceneObject with a Renderable component which will render a
  69. // mesh at the position of the scene object with the provided material.
  70. // Create new scene object at (0, 0, 0)
  71. HSceneObject pistolSO = SceneObject::create("Pistol");
  72. // Attach the Renderable component and hook up the mesh we loaded, and the material we created.
  73. HRenderable renderable = pistolSO->addComponent<CRenderable>();
  74. renderable->setMesh(assets.exampleModel);
  75. renderable->setMaterial(assets.exampleMaterial);
  76. pistolSO->setRotation(Quaternion(Degree(0.0f), Degree(-160.0f), Degree(0.0f)));
  77. // Add a rotator component so we can rotate the object during runtime
  78. pistolSO->addComponent<ObjectRotator>();
  79. /************************************************************************/
  80. /* SKYBOX */
  81. /************************************************************************/
  82. // Add a skybox texture for sky reflections
  83. HSceneObject skyboxSO = SceneObject::create("Skybox");
  84. HSkybox skybox = skyboxSO->addComponent<CSkybox>();
  85. skybox->setTexture(assets.exampleSkyCubemap);
  86. /************************************************************************/
  87. /* CAMERA */
  88. /************************************************************************/
  89. // In order something to render on screen we need at least one camera.
  90. // Like before, we create a new scene object at (0, 0, 0).
  91. HSceneObject sceneCameraSO = SceneObject::create("SceneCamera");
  92. // Get the primary render window we need for creating the camera.
  93. SPtr<RenderWindow> window = gApplication().getPrimaryWindow();
  94. // Add a Camera component that will output whatever it sees into that window
  95. // (You could also use a render texture or another window you created).
  96. HCamera sceneCamera = sceneCameraSO->addComponent<CCamera>();
  97. sceneCamera->getViewport()->setTarget(window);
  98. // Set up camera component properties
  99. // Set closest distance that is visible. Anything below that is clipped.
  100. sceneCamera->setNearClipDistance(0.005f);
  101. // Set farthest distance that is visible. Anything above that is clipped.
  102. sceneCamera->setFarClipDistance(1000);
  103. // Set aspect ratio depending on the current resolution
  104. sceneCamera->setAspectRatio(windowResWidth / (float)windowResHeight);
  105. // Position and orient the camera scene object
  106. sceneCameraSO->setPosition(Vector3(0.2f, 0.05f, 1.4f));
  107. sceneCameraSO->lookAt(Vector3(0.2f, 0.05f, 0.0f));
  108. }
  109. }
  110. /** Main entry point into the application. */
  111. #if BS_PLATFORM == BS_PLATFORM_WIN32
  112. #include <windows.h>
  113. int CALLBACK WinMain(
  114. _In_ HINSTANCE hInstance,
  115. _In_ HINSTANCE hPrevInstance,
  116. _In_ LPSTR lpCmdLine,
  117. _In_ int nCmdShow
  118. )
  119. #else
  120. int main()
  121. #endif
  122. {
  123. using namespace bs;
  124. // Initializes the application and creates a window with the specified properties
  125. VideoMode videoMode(windowResWidth, windowResHeight);
  126. Application::startUp(videoMode, "Example", false);
  127. // Registers a default set of input controls
  128. ExampleFramework::setupInputConfig();
  129. // Load a model and textures, create materials
  130. Assets assets = loadAssets();
  131. // Set up the scene with an object to render and a camera
  132. setUp3DScene(assets);
  133. // Runs the main loop that does most of the work. This method will exit when user closes the main
  134. // window or exits in some other way.
  135. Application::instance().runMainLoop();
  136. // When done, clean up
  137. Application::shutDown();
  138. return 0;
  139. }