DynamicGeometry.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "Sample.h"
  5. namespace Urho3D
  6. {
  7. class Node;
  8. class Scene;
  9. }
  10. /// Dynamic geometry example.
  11. /// This sample demonstrates:
  12. /// - Cloning a Model resource
  13. /// - Modifying the vertex buffer data of the cloned models at runtime to efficiently animate them
  14. /// - Creating a Model resource and its buffer data from scratch
  15. class DynamicGeometry : public Sample
  16. {
  17. URHO3D_OBJECT(DynamicGeometry, Sample);
  18. public:
  19. /// Construct.
  20. explicit DynamicGeometry(Context* context);
  21. /// Setup after engine initialization and before running the main loop.
  22. void Start() override;
  23. protected:
  24. /// Return XML patch instructions for screen joystick layout for a specific sample app, if any.
  25. String GetScreenJoystickPatchString() const override { return
  26. "<patch>"
  27. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />"
  28. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Animation</replace>"
  29. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">"
  30. " <element type=\"Text\">"
  31. " <attribute name=\"Name\" value=\"KeyBinding\" />"
  32. " <attribute name=\"Text\" value=\"SPACE\" />"
  33. " </element>"
  34. " </add>"
  35. "</patch>";
  36. }
  37. private:
  38. /// Construct the scene content.
  39. void CreateScene();
  40. /// Construct an instruction text to the UI.
  41. void CreateInstructions();
  42. /// Set up a viewport for displaying the scene.
  43. void SetupViewport();
  44. /// Subscribe to application-wide logic update events.
  45. void SubscribeToEvents();
  46. /// Read input and move the camera.
  47. void MoveCamera(float timeStep);
  48. /// Animate the vertex data of the objects.
  49. void AnimateObjects(float timeStep);
  50. /// Handle the logic update event.
  51. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  52. /// Cloned models' vertex buffers that we will animate.
  53. Vector<SharedPtr<VertexBuffer>> animatingBuffers_;
  54. /// Original vertex positions for the sphere model.
  55. Vector<Vector3> originalVertices_;
  56. /// If the vertices are duplicates, indices to the original vertices (to allow seamless animation.)
  57. Vector<i32> vertexDuplicates_;
  58. /// Animation flag.
  59. bool animate_;
  60. /// Animation's elapsed time.
  61. float time_;
  62. };