AnimatingScene.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Camera.h"
  23. #include "CoreEvents.h"
  24. #include "Engine.h"
  25. #include "Font.h"
  26. #include "Graphics.h"
  27. #include "Input.h"
  28. #include "Material.h"
  29. #include "Model.h"
  30. #include "Octree.h"
  31. #include "Renderer.h"
  32. #include "ResourceCache.h"
  33. #include "Rotator.h"
  34. #include "StaticModel.h"
  35. #include "Text.h"
  36. #include "UI.h"
  37. #include "Zone.h"
  38. #include "AnimatingScene.h"
  39. #include "DebugNew.h"
  40. // Expands to this example's entry-point
  41. DEFINE_APPLICATION_MAIN(AnimatingScene)
  42. AnimatingScene::AnimatingScene(Context* context) :
  43. Sample(context),
  44. yaw_(0.0f),
  45. pitch_(0.0f)
  46. {
  47. // Register an object factory for our custom Rotator component so that we can create them to scene nodes
  48. context->RegisterFactory<Rotator>();
  49. }
  50. void AnimatingScene::Start()
  51. {
  52. // Execute base class startup
  53. Sample::Start();
  54. // Create the scene content
  55. CreateScene();
  56. // Create the UI content
  57. CreateInstructions();
  58. // Setup the viewport for displaying the scene
  59. SetupViewport();
  60. // Hook up to the frame update events
  61. SubscribeToEvents();
  62. }
  63. void AnimatingScene::CreateScene()
  64. {
  65. ResourceCache* cache = GetSubsystem<ResourceCache>();
  66. scene_ = new Scene(context_);
  67. // Create the Octree component to the scene so that drawable objects can be rendered. Use default volume
  68. // (-1000, -1000, -1000) to (1000, 1000, 1000)
  69. scene_->CreateComponent<Octree>();
  70. // Create a Zone component into a child scene node. The Zone controls ambient lighting and fog settings. Like the Octree,
  71. // 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
  72. // and Z axes.) Drawable objects "pick up" the zone they belong to and use it when rendering; several zones can exist
  73. Node* zoneNode = scene_->CreateChild("Zone");
  74. Zone* zone = zoneNode->CreateComponent<Zone>();
  75. // Set same volume as the Octree, set a close bluish fog and some ambient light
  76. zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f));
  77. zone->SetAmbientColor(Color(0.05f, 0.1f, 0.15f));
  78. zone->SetFogColor(Color(0.1f, 0.2f, 0.3f));
  79. zone->SetFogStart(10.0f);
  80. zone->SetFogEnd(100.0f);
  81. // Create randomly positioned and oriented box StaticModels in the scene
  82. const unsigned NUM_OBJECTS = 2000;
  83. for (unsigned i = 0; i < NUM_OBJECTS; ++i)
  84. {
  85. Node* boxNode = scene_->CreateChild("Box");
  86. boxNode->SetPosition(Vector3(Random(200.0f) - 100.0f, Random(200.0f) - 100.0f, Random(200.0f) - 100.0f));
  87. // Orient using random pitch, yaw and roll Euler angles
  88. boxNode->SetRotation(Quaternion(Random(360.0f), Random(360.0f), Random(360.0f)));
  89. StaticModel* boxObject = boxNode->CreateComponent<StaticModel>();
  90. boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
  91. boxObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml"));
  92. // Add our custom Rotator component which will rotate the scene node each frame, when the scene sends its update event.
  93. // Simply set same rotation speed for all objects
  94. Rotator* rotator = boxNode->CreateComponent<Rotator>();
  95. rotator->SetRotationSpeed(Vector3(10.0f, 20.0f, 30.0f));
  96. }
  97. // Create the camera. Let the starting position be at the world origin. As the fog limits maximum visible distance, we can
  98. // bring the far clip plane closer for more effective culling of distant objects
  99. cameraNode_ = scene_->CreateChild("Camera");
  100. Camera* camera = cameraNode_->CreateComponent<Camera>();
  101. camera->SetFarClip(100.0f);
  102. // Create a point light to the camera scene node
  103. Light* light = cameraNode_->CreateComponent<Light>();
  104. light->SetLightType(LIGHT_POINT);
  105. light->SetRange(30.0f);
  106. }
  107. void AnimatingScene::CreateInstructions()
  108. {
  109. ResourceCache* cache = GetSubsystem<ResourceCache>();
  110. UI* ui = GetSubsystem<UI>();
  111. // Construct new Text object, set string to display and font to use
  112. Text* instructionText = ui->GetRoot()->CreateChild<Text>();
  113. instructionText->SetText("Use WASD keys and mouse to move");
  114. instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  115. // Position the text relative to the screen center
  116. instructionText->SetHorizontalAlignment(HA_CENTER);
  117. instructionText->SetVerticalAlignment(VA_CENTER);
  118. instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
  119. }
  120. void AnimatingScene::SetupViewport()
  121. {
  122. Renderer* renderer = GetSubsystem<Renderer>();
  123. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  124. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  125. renderer->SetViewport(0, viewport);
  126. }
  127. void AnimatingScene::SubscribeToEvents()
  128. {
  129. // Subscribe HandleUpdate() function for processing update events
  130. SubscribeToEvent(E_UPDATE, HANDLER(AnimatingScene, HandleUpdate));
  131. }
  132. void AnimatingScene::MoveCamera(float timeStep)
  133. {
  134. // Do not move if the UI has a focused element (the console)
  135. if (GetSubsystem<UI>()->GetFocusElement())
  136. return;
  137. Input* input = GetSubsystem<Input>();
  138. // Movement speed as world units per second
  139. const float MOVE_SPEED = 20.0f;
  140. // Mouse sensitivity as degrees per pixel
  141. const float MOUSE_SENSITIVITY = 0.1f;
  142. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  143. IntVector2 mouseMove = input->GetMouseMove();
  144. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  145. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  146. pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  147. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  148. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
  149. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  150. if (input->GetKeyDown('W'))
  151. cameraNode_->TranslateRelative(Vector3::FORWARD * MOVE_SPEED * timeStep);
  152. if (input->GetKeyDown('S'))
  153. cameraNode_->TranslateRelative(Vector3::BACK * MOVE_SPEED * timeStep);
  154. if (input->GetKeyDown('A'))
  155. cameraNode_->TranslateRelative(Vector3::LEFT * MOVE_SPEED * timeStep);
  156. if (input->GetKeyDown('D'))
  157. cameraNode_->TranslateRelative(Vector3::RIGHT * MOVE_SPEED * timeStep);
  158. }
  159. void AnimatingScene::HandleUpdate(StringHash eventType, VariantMap& eventData)
  160. {
  161. using namespace Update;
  162. // Take the frame time step, which is stored as a float
  163. float timeStep = eventData[P_TIMESTEP].GetFloat();
  164. // Move the camera, scale movement with time step
  165. MoveCamera(timeStep);
  166. }