Main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 "Components/BsCPlaneCollider.h"
  10. #include "Components/BsCBoxCollider.h"
  11. #include "Components/BsCCharacterController.h"
  12. #include "Components/BsCRigidbody.h"
  13. #include "Physics/BsPhysicsMaterial.h"
  14. #include "RenderAPI/BsRenderAPI.h"
  15. #include "RenderAPI/BsRenderWindow.h"
  16. #include "Scene/BsSceneObject.h"
  17. #include "Input/BsInput.h"
  18. #include "Components/BsCDecal.h"
  19. // Example includes
  20. #include "BsExampleFramework.h"
  21. #include "BsFPSWalker.h"
  22. #include "BsFPSCamera.h"
  23. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  24. // This example sets up a simple environment consisting of a floor and cube, and a decal projecting on both surfaces. The
  25. // example demonstrates how to set up decals, how decals are not shown on surfaces perpendicular to the decal direction,
  26. // and optionally how to use masking to only project a decal onto a certain set of surfaces.
  27. //
  28. // It also sets up necessary physical objects for collision, as well as the character collider and necessary components
  29. // for walking around the environment.
  30. //
  31. // The example first sets up the scene consisting of a floor, box and a skybox. Character controller is created next,
  32. // as well as the camera. Components for moving the character controller and the camera are attached to allow the user to
  33. // control the character. It then loads the required decal textures, sets up a decal material and initializes the actual
  34. // decal component. Finally the cursor is hidden and quit on Esc key press hooked up.
  35. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  36. namespace bs
  37. {
  38. constexpr float GROUND_PLANE_SCALE = 50.0f;
  39. UINT32 windowResWidth = 1280;
  40. UINT32 windowResHeight = 720;
  41. SPtr<Decal> decal;
  42. /** Set up the scene used by the example, and the camera to view the world through. */
  43. void setUpScene()
  44. {
  45. /************************************************************************/
  46. /* ASSETS */
  47. /************************************************************************/
  48. // Prepare all the resources we'll be using throughout this example
  49. // Grab a couple of test textures that we'll apply to the rendered objects
  50. HTexture gridPattern = ExampleFramework::loadTexture(ExampleTexture::GridPattern);
  51. HTexture gridPattern2 = ExampleFramework::loadTexture(ExampleTexture::GridPattern2);
  52. // Grab the default PBR shader
  53. HShader shader = gBuiltinResources().getBuiltinShader(BuiltinShader::Standard);
  54. // Create a set of materials to apply to renderables used
  55. HMaterial planeMaterial = Material::create(shader);
  56. planeMaterial->setTexture("gAlbedoTex", gridPattern2);
  57. // Tile the texture so every tile covers a 2x2m area
  58. planeMaterial->setVec2("gUVTile", Vector2::ONE * GROUND_PLANE_SCALE * 0.5f);
  59. // Load meshes we'll used for our rendered objects
  60. HMesh planeMesh = gBuiltinResources().getMesh(BuiltinMesh::Quad);
  61. /************************************************************************/
  62. /* FLOOR */
  63. /************************************************************************/
  64. // Set up renderable geometry for the floor plane
  65. HSceneObject floorSO = SceneObject::create("Floor");
  66. HRenderable floorRenderable = floorSO->addComponent<CRenderable>();
  67. floorRenderable->setMesh(planeMesh);
  68. floorRenderable->setMaterial(planeMaterial);
  69. floorSO->setScale(Vector3(GROUND_PLANE_SCALE, 1.0f, GROUND_PLANE_SCALE));
  70. // Add a plane collider that will prevent physical objects going through the floor
  71. HPlaneCollider planeCollider = floorSO->addComponent<CPlaneCollider>();
  72. HMaterial boxMaterial = Material::create(shader);
  73. boxMaterial->setTexture("gAlbedoTex", gridPattern);
  74. HMaterial sphereMaterial = Material::create(shader);
  75. // Load meshes we'll used for our rendered objects
  76. HMesh boxMesh = gBuiltinResources().getMesh(BuiltinMesh::Box);
  77. HSceneObject boxSO = SceneObject::create("Box");
  78. HRenderable boxRenderable = boxSO->addComponent<CRenderable>();
  79. boxRenderable->setMesh(boxMesh);
  80. boxRenderable->setMaterial(boxMaterial);
  81. // Set a non-default layer for the box, so we can use it for masking on which surfaces should the decal be
  82. // projected onto
  83. boxRenderable->setLayer(1 << 1);
  84. boxSO->setPosition(Vector3(0.0f, 0.5f, 0.5f));
  85. /************************************************************************/
  86. /* CHARACTER */
  87. /************************************************************************/
  88. // Add physics geometry and components for character movement and physics interaction
  89. HSceneObject characterSO = SceneObject::create("Character");
  90. characterSO->setPosition(Vector3(0.0f, 1.0f, 5.0f));
  91. // Add a character controller, representing the physical geometry of the character
  92. HCharacterController charController = characterSO->addComponent<CCharacterController>();
  93. // Make the character about 1.8m high, with 0.4m radius (controller represents a capsule)
  94. charController->setHeight(1.0f); // + 0.4 * 2 radius = 1.8m height
  95. charController->setRadius(0.4f);
  96. // FPS walker uses default input controls to move the character controller attached to the same object
  97. characterSO->addComponent<FPSWalker>();
  98. /************************************************************************/
  99. /* CAMERA */
  100. /************************************************************************/
  101. // In order something to render on screen we need at least one camera.
  102. // Like before, we create a new scene object at (0, 0, 0).
  103. HSceneObject sceneCameraSO = SceneObject::create("SceneCamera");
  104. // Get the primary render window we need for creating the camera.
  105. SPtr<RenderWindow> window = gApplication().getPrimaryWindow();
  106. // Add a Camera component that will output whatever it sees into that window
  107. // (You could also use a render texture or another window you created).
  108. HCamera sceneCamera = sceneCameraSO->addComponent<CCamera>();
  109. sceneCamera->getViewport()->setTarget(window);
  110. // Set up camera component properties
  111. // Set closest distance that is visible. Anything below that is clipped.
  112. sceneCamera->setNearClipDistance(0.005f);
  113. // Set farthest distance that is visible. Anything above that is clipped.
  114. sceneCamera->setFarClipDistance(1000);
  115. // Set aspect ratio depending on the current resolution
  116. sceneCamera->setAspectRatio(windowResWidth / (float)windowResHeight);
  117. // Add a component that allows the camera to be rotated using the mouse
  118. sceneCameraSO->setRotation(Quaternion(Degree(-10.0f), Degree(0.0f), Degree(0.0f)));
  119. HFPSCamera fpsCamera = sceneCameraSO->addComponent<FPSCamera>();
  120. // Set the character controller on the FPS camera, so the component can apply yaw rotation to it
  121. fpsCamera->setCharacter(characterSO);
  122. // Make the camera a child of the character scene object, and position it roughly at eye level
  123. sceneCameraSO->setParent(characterSO);
  124. sceneCameraSO->setPosition(Vector3(0.0f, 1.8f * 0.5f - 0.1f, -2.0f));
  125. /************************************************************************/
  126. /* SKYBOX */
  127. /************************************************************************/
  128. // Load a skybox texture
  129. HTexture skyCubemap = ExampleFramework::loadTexture(ExampleTexture::EnvironmentDaytime, false, true, true);
  130. // Add a skybox texture for sky reflections
  131. HSceneObject skyboxSO = SceneObject::create("Skybox");
  132. HSkybox skybox = skyboxSO->addComponent<CSkybox>();
  133. skybox->setTexture(skyCubemap);
  134. /************************************************************************/
  135. /* DECAL */
  136. /************************************************************************/
  137. // Load the decal textures
  138. HTexture decalAlbedoTex = ExampleFramework::loadTexture(ExampleTexture::DecalAlbedo);
  139. HTexture decalNormalTex = ExampleFramework::loadTexture(ExampleTexture::DecalNormal, false);
  140. // Create a material using the built-in decal shader and assign the textures
  141. HShader decalShader = gBuiltinResources().getBuiltinShader(BuiltinShader::Decal);
  142. HMaterial decalMaterial = Material::create(decalShader);
  143. decalMaterial->setTexture("gAlbedoTex", decalAlbedoTex);
  144. decalMaterial->setTexture("gNormalTex", decalNormalTex);
  145. decalMaterial->setVariation(ShaderVariation(
  146. {
  147. // Use the default, transparent blend mode that uses traditional PBR textures to project. Normally no need
  148. // to set the default explicitly but it's done here for example purposes. See the manual for all available
  149. // modes
  150. ShaderVariation::Param("BLEND_MODE", 0)
  151. })
  152. );
  153. // Create the decal scene object, position and orient it, facing down
  154. HSceneObject decalSO = SceneObject::create("Decal");
  155. decalSO->setPosition(Vector3(0.0f, 6.0f, 1.0f));
  156. decalSO->lookAt(Vector3(0.0f, 0.0f, 1.0f));
  157. // Set the material to project
  158. HDecal decal = decalSO->addComponent<CDecal>();
  159. decal->setMaterial(decalMaterial);
  160. // Optionally set a mask to only project onto elements with layer 1 set (in this case this is the floor since we
  161. // changed the default layer for the box)
  162. // decal->setLayerMask(1);
  163. /************************************************************************/
  164. /* INPUT */
  165. /************************************************************************/
  166. // Hook up input that launches a sphere when user clicks the mouse, and Esc key to quit
  167. gInput().onButtonUp.connect([=](const ButtonEvent& ev)
  168. {
  169. if(ev.buttonCode == BC_ESCAPE)
  170. {
  171. // Quit the application when Escape key is pressed
  172. gApplication().quitRequested();
  173. }
  174. });
  175. }
  176. }
  177. /** Main entry point into the application. */
  178. #if BS_PLATFORM == BS_PLATFORM_WIN32
  179. #include <windows.h>
  180. int CALLBACK WinMain(
  181. _In_ HINSTANCE hInstance,
  182. _In_ HINSTANCE hPrevInstance,
  183. _In_ LPSTR lpCmdLine,
  184. _In_ int nCmdShow
  185. )
  186. #else
  187. int main()
  188. #endif
  189. {
  190. using namespace bs;
  191. // Initializes the application and creates a window with the specified properties
  192. VideoMode videoMode(windowResWidth, windowResHeight);
  193. Application::startUp(videoMode, "Example", false);
  194. // Registers a default set of input controls
  195. ExampleFramework::setupInputConfig();
  196. // Set up the scene with an object to render and a camera
  197. setUpScene();
  198. // Runs the main loop that does most of the work. This method will exit when user closes the main
  199. // window or exits in some other way.
  200. Application::instance().runMainLoop();
  201. // When done, clean up
  202. Application::shutDown();
  203. return 0;
  204. }