LightAnimation.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //
  2. // Copyright (c) 2008-2014 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 <Urho3D/Urho3D.h>
  23. #include <Urho3D/Scene/Animatable.h>
  24. #include <Urho3D/Graphics/Camera.h>
  25. #include <Urho3D/Core/CoreEvents.h>
  26. #include <Urho3D/Engine/Engine.h>
  27. #include <Urho3D/UI/Font.h>
  28. #include <Urho3D/Graphics/Graphics.h>
  29. #include <Urho3D/Input/Input.h>
  30. #include <Urho3D/Graphics/Material.h>
  31. #include <Urho3D/Graphics/Model.h>
  32. #include <Urho3D/Graphics/Octree.h>
  33. #include <Urho3D/Scene/ObjectAnimation.h>
  34. #include <Urho3D/Graphics/Renderer.h>
  35. #include <Urho3D/Resource/ResourceCache.h>
  36. #include <Urho3D/Scene/Scene.h>
  37. #include <Urho3D/Graphics/StaticModel.h>
  38. #include <Urho3D/UI/Text.h>
  39. #include <Urho3D/UI/UI.h>
  40. #include <Urho3D/Scene/ValueAnimation.h>
  41. #include "LightAnimation.h"
  42. #include <Urho3D/DebugNew.h>
  43. DEFINE_APPLICATION_MAIN(LightAnimation)
  44. LightAnimation::LightAnimation(Context* context) :
  45. Sample(context)
  46. {
  47. }
  48. void LightAnimation::Start()
  49. {
  50. // Execute base class startup
  51. Sample::Start();
  52. // Create the UI content
  53. CreateInstructions();
  54. // Create the scene content
  55. CreateScene();
  56. // Setup the viewport for displaying the scene
  57. SetupViewport();
  58. // Hook up to the frame update events
  59. SubscribeToEvents();
  60. }
  61. void LightAnimation::CreateScene()
  62. {
  63. ResourceCache* cache = GetSubsystem<ResourceCache>();
  64. scene_ = new Scene(context_);
  65. // Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  66. // show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates; it
  67. // is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  68. // optimizing manner
  69. scene_->CreateComponent<Octree>();
  70. // Create a child scene node (at world origin) and a StaticModel component into it. Set the StaticModel to show a simple
  71. // plane mesh with a "stone" material. Note that naming the scene nodes is optional. Scale the scene node larger
  72. // (100 x 100 world units)
  73. Node* planeNode = scene_->CreateChild("Plane");
  74. planeNode->SetScale(Vector3(100.0f, 1.0f, 100.0f));
  75. StaticModel* planeObject = planeNode->CreateComponent<StaticModel>();
  76. planeObject->SetModel(cache->GetResource<Model>("Models/Plane.mdl"));
  77. planeObject->SetMaterial(cache->GetResource<Material>("Materials/StoneTiled.xml"));
  78. // Create a point light to the world so that we can see something.
  79. Node* lightNode = scene_->CreateChild("PointLight");
  80. Light* light = lightNode->CreateComponent<Light>();
  81. light->SetLightType(LIGHT_POINT);
  82. light->SetRange(10.0f);
  83. // Create light animation
  84. SharedPtr<ObjectAnimation> lightAnimation(new ObjectAnimation(context_));
  85. // Create light position animation
  86. SharedPtr<ValueAnimation> positionAnimation(new ValueAnimation(context_));
  87. // Use spline interpolation method
  88. positionAnimation->SetInterpolationMethod(IM_SPLINE);
  89. // Set spline tension
  90. positionAnimation->SetSplineTension(0.7f);
  91. positionAnimation->SetKeyFrame(0.0f, Vector3(-30.0f, 5.0f, -30.0f));
  92. positionAnimation->SetKeyFrame(1.0f, Vector3( 30.0f, 5.0f, -30.0f));
  93. positionAnimation->SetKeyFrame(2.0f, Vector3( 30.0f, 5.0f, 30.0f));
  94. positionAnimation->SetKeyFrame(3.0f, Vector3(-30.0f, 5.0f, 30.0f));
  95. positionAnimation->SetKeyFrame(4.0f, Vector3(-30.0f, 5.0f, -30.0f));
  96. // Set position animation
  97. lightAnimation->AddAttributeAnimation("Position", positionAnimation);
  98. // Create text animation
  99. SharedPtr<ValueAnimation> textAnimation(new ValueAnimation(context_));
  100. textAnimation->SetKeyFrame(0.0f, "WHITE");
  101. textAnimation->SetKeyFrame(1.0f, "RED");
  102. textAnimation->SetKeyFrame(2.0f, "YELLOW");
  103. textAnimation->SetKeyFrame(3.0f, "GREEN");
  104. textAnimation->SetKeyFrame(4.0f, "WHITE");
  105. GetSubsystem<UI>()->GetRoot()->GetChild(String("animatingText"))->SetAttributeAnimation("Text", textAnimation);
  106. // Create light color animation
  107. SharedPtr<ValueAnimation> colorAnimation(new ValueAnimation(context_));
  108. colorAnimation->SetKeyFrame(0.0f, Color::WHITE);
  109. colorAnimation->SetKeyFrame(1.0f, Color::RED);
  110. colorAnimation->SetKeyFrame(2.0f, Color::YELLOW);
  111. colorAnimation->SetKeyFrame(3.0f, Color::GREEN);
  112. colorAnimation->SetKeyFrame(4.0f, Color::WHITE);
  113. // Set Light component's color animation
  114. lightAnimation->AddAttributeAnimation("@Light/Color", colorAnimation);
  115. // Apply light animation to light node
  116. lightNode->SetObjectAnimation(lightAnimation);
  117. // Create more StaticModel objects to the scene, randomly positioned, rotated and scaled. For rotation, we construct a
  118. // quaternion from Euler angles where the Y angle (rotation about the Y axis) is randomized. The mushroom model contains
  119. // LOD levels, so the StaticModel component will automatically select the LOD level according to the view distance (you'll
  120. // see the model get simpler as it moves further away). Finally, rendering a large number of the same object with the
  121. // same material allows instancing to be used, if the GPU supports it. This reduces the amount of CPU work in rendering the
  122. // scene.
  123. const unsigned NUM_OBJECTS = 200;
  124. for (unsigned i = 0; i < NUM_OBJECTS; ++i)
  125. {
  126. Node* mushroomNode = scene_->CreateChild("Mushroom");
  127. mushroomNode->SetPosition(Vector3(Random(90.0f) - 45.0f, 0.0f, Random(90.0f) - 45.0f));
  128. mushroomNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f));
  129. mushroomNode->SetScale(0.5f + Random(2.0f));
  130. StaticModel* mushroomObject = mushroomNode->CreateComponent<StaticModel>();
  131. mushroomObject->SetModel(cache->GetResource<Model>("Models/Mushroom.mdl"));
  132. mushroomObject->SetMaterial(cache->GetResource<Material>("Materials/Mushroom.xml"));
  133. }
  134. // Create a scene node for the camera, which we will move around
  135. // The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  136. cameraNode_ = scene_->CreateChild("Camera");
  137. cameraNode_->CreateComponent<Camera>();
  138. // Set an initial position for the camera scene node above the plane
  139. cameraNode_->SetPosition(Vector3(0.0f, 5.0f, 0.0f));
  140. }
  141. void LightAnimation::CreateInstructions()
  142. {
  143. ResourceCache* cache = GetSubsystem<ResourceCache>();
  144. UI* ui = GetSubsystem<UI>();
  145. // Construct new Text object, set string to display and font to use
  146. Text* instructionText = ui->GetRoot()->CreateChild<Text>();
  147. instructionText->SetText("Use WASD keys and mouse/touch to move");
  148. Font* font = cache->GetResource<Font>("Fonts/Anonymous Pro.ttf");
  149. instructionText->SetFont(font, 15);
  150. // Position the text relative to the screen center
  151. instructionText->SetHorizontalAlignment(HA_CENTER);
  152. instructionText->SetVerticalAlignment(VA_CENTER);
  153. instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
  154. // Animating text
  155. Text* text = ui->GetRoot()->CreateChild<Text>("animatingText");
  156. text->SetFont(font, 15);
  157. text->SetHorizontalAlignment(HA_CENTER);
  158. text->SetVerticalAlignment(VA_CENTER);
  159. text->SetPosition(0, ui->GetRoot()->GetHeight() / 4 + 20);
  160. }
  161. void LightAnimation::SetupViewport()
  162. {
  163. Renderer* renderer = GetSubsystem<Renderer>();
  164. // 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
  165. // at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  166. // use, but now we just use full screen and default render path configured in the engine command line options
  167. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  168. renderer->SetViewport(0, viewport);
  169. }
  170. void LightAnimation::MoveCamera(float timeStep)
  171. {
  172. // Do not move if the UI has a focused element (the console)
  173. if (GetSubsystem<UI>()->GetFocusElement())
  174. return;
  175. Input* input = GetSubsystem<Input>();
  176. // Movement speed as world units per second
  177. const float MOVE_SPEED = 20.0f;
  178. // Mouse sensitivity as degrees per pixel
  179. const float MOUSE_SENSITIVITY = 0.1f;
  180. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  181. IntVector2 mouseMove = input->GetMouseMove();
  182. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  183. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  184. pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  185. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  186. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
  187. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  188. // Use the Translate() function (default local space) to move relative to the node's orientation.
  189. if (input->GetKeyDown('W'))
  190. cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
  191. if (input->GetKeyDown('S'))
  192. cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep);
  193. if (input->GetKeyDown('A'))
  194. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  195. if (input->GetKeyDown('D'))
  196. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  197. }
  198. void LightAnimation::SubscribeToEvents()
  199. {
  200. // Subscribe HandleUpdate() function for processing update events
  201. SubscribeToEvent(E_UPDATE, HANDLER(LightAnimation, HandleUpdate));
  202. }
  203. void LightAnimation::HandleUpdate(StringHash eventType, VariantMap& eventData)
  204. {
  205. using namespace Update;
  206. // Take the frame time step, which is stored as a float
  207. float timeStep = eventData[P_TIMESTEP].GetFloat();
  208. // Move the camera, scale movement with time step
  209. MoveCamera(timeStep);
  210. }