AnimatingScene.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/Graphics/Zone.h>
  33. #include <Atomic/Input/Input.h>
  34. #include <Atomic/Resource/ResourceCache.h>
  35. #include <Atomic/Scene/Scene.h>
  36. #include <Atomic/UI/UI.h>
  37. #include "AnimatingScene.h"
  38. #include "Rotator.h"
  39. #include <Atomic/DebugNew.h>
  40. AnimatingScene::AnimatingScene(Context* context) :
  41. Sample(context)
  42. {
  43. // Register an object factory for our custom Rotator component so that we can create them to scene nodes
  44. context->RegisterFactory<Rotator>();
  45. }
  46. void AnimatingScene::Start()
  47. {
  48. // Execute base class startup
  49. Sample::Start();
  50. // Create the scene content
  51. CreateScene();
  52. // Create the UI content
  53. CreateInstructions();
  54. // Setup the viewport for displaying the scene
  55. SetupViewport();
  56. // Hook up to the frame update events
  57. SubscribeToEvents();
  58. // Set the mouse mode to use in the sample
  59. Sample::InitMouseMode(MM_RELATIVE);
  60. }
  61. void AnimatingScene::CreateScene()
  62. {
  63. ResourceCache* cache = GetSubsystem<ResourceCache>();
  64. scene_ = new Scene(context_);
  65. // Create the Octree component to the scene so that drawable objects can be rendered. Use default volume
  66. // (-1000, -1000, -1000) to (1000, 1000, 1000)
  67. scene_->CreateComponent<Octree>();
  68. // Create a Zone component into a child scene node. The Zone controls ambient lighting and fog settings. Like the Octree,
  69. // it also defines its volume with a bounding box, but can be rotated (so it does not need to be aligned to the world X, Y
  70. // and Z axes.) Drawable objects "pick up" the zone they belong to and use it when rendering; several zones can exist
  71. Node* zoneNode = scene_->CreateChild("Zone");
  72. Zone* zone = zoneNode->CreateComponent<Zone>();
  73. // Set same volume as the Octree, set a close bluish fog and some ambient light
  74. zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f));
  75. zone->SetAmbientColor(Color(0.05f, 0.1f, 0.15f));
  76. zone->SetFogColor(Color(0.1f, 0.2f, 0.3f));
  77. zone->SetFogStart(10.0f);
  78. zone->SetFogEnd(100.0f);
  79. // Create randomly positioned and oriented box StaticModels in the scene
  80. const unsigned NUM_OBJECTS = 2000;
  81. for (unsigned i = 0; i < NUM_OBJECTS; ++i)
  82. {
  83. Node* boxNode = scene_->CreateChild("Box");
  84. boxNode->SetPosition(Vector3(Random(200.0f) - 100.0f, Random(200.0f) - 100.0f, Random(200.0f) - 100.0f));
  85. // Orient using random pitch, yaw and roll Euler angles
  86. boxNode->SetRotation(Quaternion(Random(360.0f), Random(360.0f), Random(360.0f)));
  87. StaticModel* boxObject = boxNode->CreateComponent<StaticModel>();
  88. boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
  89. boxObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml"));
  90. // Add our custom Rotator component which will rotate the scene node each frame, when the scene sends its update event.
  91. // The Rotator component derives from the base class LogicComponent, which has convenience functionality to subscribe
  92. // to the various update events, and forward them to virtual functions that can be implemented by subclasses. This way
  93. // writing logic/update components in C++ becomes similar to scripting.
  94. // Now we simply set same rotation speed for all objects
  95. Rotator* rotator = boxNode->CreateComponent<Rotator>();
  96. rotator->SetRotationSpeed(Vector3(10.0f, 20.0f, 30.0f));
  97. }
  98. // Create the camera. Let the starting position be at the world origin. As the fog limits maximum visible distance, we can
  99. // bring the far clip plane closer for more effective culling of distant objects
  100. cameraNode_ = scene_->CreateChild("Camera");
  101. Camera* camera = cameraNode_->CreateComponent<Camera>();
  102. camera->SetFarClip(100.0f);
  103. // Create a point light to the camera scene node
  104. Light* light = cameraNode_->CreateComponent<Light>();
  105. light->SetLightType(LIGHT_POINT);
  106. light->SetRange(30.0f);
  107. }
  108. void AnimatingScene::CreateInstructions()
  109. {
  110. SimpleCreateInstructions("Use WASD keys and mouse/touch to move");
  111. }
  112. void AnimatingScene::SetupViewport()
  113. {
  114. Renderer* renderer = GetSubsystem<Renderer>();
  115. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  116. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  117. renderer->SetViewport(0, viewport);
  118. }
  119. void AnimatingScene::SubscribeToEvents()
  120. {
  121. // Subscribe HandleUpdate() function for processing update events
  122. SubscribeToEvent(E_UPDATE, ATOMIC_HANDLER(AnimatingScene, HandleUpdate));
  123. }
  124. void AnimatingScene::MoveCamera(float timeStep)
  125. {
  126. Input* input = GetSubsystem<Input>();
  127. // Movement speed as world units per second
  128. const float MOVE_SPEED = 20.0f;
  129. // Mouse sensitivity as degrees per pixel
  130. const float MOUSE_SENSITIVITY = 0.1f;
  131. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  132. IntVector2 mouseMove = input->GetMouseMove();
  133. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  134. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  135. pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  136. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  137. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
  138. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  139. if (input->GetKeyDown(KEY_W))
  140. cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
  141. if (input->GetKeyDown(KEY_S))
  142. cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep);
  143. if (input->GetKeyDown(KEY_A))
  144. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  145. if (input->GetKeyDown(KEY_D))
  146. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  147. }
  148. void AnimatingScene::HandleUpdate(StringHash eventType, VariantMap& eventData)
  149. {
  150. using namespace Update;
  151. // Take the frame time step, which is stored as a float
  152. float timeStep = eventData[P_TIMESTEP].GetFloat();
  153. // Move the camera, scale movement with time step
  154. MoveCamera(timeStep);
  155. }