Particles3D.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // Copyright (c) 2008-2016 the Urho3D project.
  3. // Copyright (c) 2014-2016, THUNDERBEAST GAMES LLC All rights reserved
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include <Atomic/Core/CoreEvents.h>
  24. #include <Atomic/Engine/Engine.h>
  25. #include <Atomic/Graphics/Camera.h>
  26. #include <Atomic/Graphics/Graphics.h>
  27. #include <Atomic/Graphics/Material.h>
  28. #include <Atomic/Graphics/Model.h>
  29. #include <Atomic/Graphics/Octree.h>
  30. #include <Atomic/Graphics/Renderer.h>
  31. #include <Atomic/Graphics/StaticModel.h>
  32. #include <Atomic/Input/Input.h>
  33. #include <Atomic/Resource/ResourceCache.h>
  34. #include <Atomic/Resource/XMLFile.h>
  35. #include <Atomic/Scene/Scene.h>
  36. #include <Atomic/UI/UI.h>
  37. #include "Particles3D.h"
  38. #include <Atomic/DebugNew.h>
  39. Particles3D::Particles3D(Context* context) :
  40. Sample(context)
  41. {
  42. }
  43. void Particles3D::Start()
  44. {
  45. // Execute base class startup
  46. Sample::Start();
  47. // Create the scene content
  48. CreateScene();
  49. // Create the UI content
  50. CreateInstructions();
  51. // Setup the viewport for displaying the scene
  52. SetupViewport();
  53. // Hook up to the frame update events
  54. SubscribeToEvents();
  55. // Set the mouse mode to use in the sample
  56. Sample::InitMouseMode(MM_RELATIVE);
  57. }
  58. void Particles3D::CreateScene()
  59. {
  60. ResourceCache* cache = GetSubsystem<ResourceCache>();
  61. scene_ = new Scene(context_);
  62. scene_->LoadXML(*(cache->GetFile("Scenes/ParticleEmitter3D.scene")));
  63. // Create a scene node for the camera, which we will move around
  64. // The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  65. cameraNode_ = scene_->CreateChild("Camera");
  66. cameraNode_->CreateComponent<Camera>();
  67. // Set an initial position for the camera scene node above the plane
  68. cameraNode_->SetPosition(Vector3(0.0f, 0.0f, -4.0f));
  69. }
  70. void Particles3D::CreateInstructions()
  71. {
  72. SimpleCreateInstructions("Use WASD keys and mouse/touch to move");
  73. }
  74. void Particles3D::SetupViewport()
  75. {
  76. Renderer* renderer = GetSubsystem<Renderer>();
  77. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen. We need to define the scene and the camera
  78. // at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  79. // use, but now we just use full screen and default render path configured in the engine command line options
  80. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  81. renderer->SetViewport(0, viewport);
  82. }
  83. void Particles3D::MoveCamera(float timeStep)
  84. {
  85. Input* input = GetSubsystem<Input>();
  86. // Movement speed as world units per second
  87. const float MOVE_SPEED = 20.0f;
  88. // Mouse sensitivity as degrees per pixel
  89. const float MOUSE_SENSITIVITY = 0.1f;
  90. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  91. IntVector2 mouseMove = input->GetMouseMove();
  92. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  93. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  94. pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  95. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  96. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
  97. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  98. // Use the Translate() function (default local space) to move relative to the node's orientation.
  99. if (input->GetKeyDown(KEY_W))
  100. cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
  101. if (input->GetKeyDown(KEY_S))
  102. cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep);
  103. if (input->GetKeyDown(KEY_A))
  104. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  105. if (input->GetKeyDown(KEY_D))
  106. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  107. }
  108. void Particles3D::SubscribeToEvents()
  109. {
  110. // Subscribe HandleUpdate() function for processing update events
  111. SubscribeToEvent(E_UPDATE, ATOMIC_HANDLER(Particles3D, HandleUpdate));
  112. }
  113. void Particles3D::HandleUpdate(StringHash eventType, VariantMap& eventData)
  114. {
  115. using namespace Update;
  116. // Take the frame time step, which is stored as a float
  117. float timeStep = eventData[P_TIMESTEP].GetFloat();
  118. // Move the camera, scale movement with time step
  119. MoveCamera(timeStep);
  120. }