SceneView3D.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include "AtomicEditor.h"
  5. #include <Atomic/IO/Log.h>
  6. #include <Atomic/Core/CoreEvents.h>
  7. #include <Atomic/Scene/Scene.h>
  8. #include <Atomic/Graphics/Camera.h>
  9. #include <Atomic/Graphics/Graphics.h>
  10. #include <Atomic/Graphics/DebugRenderer.h>
  11. #include <Atomic/Graphics/Viewport.h>
  12. #include <Atomic/Graphics/Octree.h>
  13. #include <Atomic/Input/Input.h>
  14. #include <Atomic/IO/FileSystem.h>
  15. #include <Atomic/Resource/ResourceCache.h>
  16. #include <Atomic/Physics/PhysicsWorld.h>
  17. #include "AEEditor.h"
  18. #include "AEEvents.h"
  19. #include "SceneView3D.h"
  20. #include "SceneEditor3D.h"
  21. #include <Atomic/UI/UI.h>
  22. #include <Atomic/UI/TBUI.h>
  23. namespace AtomicEditor
  24. {
  25. SceneView3D ::SceneView3D(Context* context, SceneEditor3D *sceneEditor) :
  26. View3D(context),
  27. yaw_(0.0f),
  28. pitch_(0.0f)
  29. {
  30. sceneEditor_ = sceneEditor;
  31. ResourceCache* cache = GetSubsystem<ResourceCache>();
  32. scene_ = sceneEditor->GetScene();
  33. debugRenderer_ = scene_->GetComponent<DebugRenderer>();
  34. if (debugRenderer_.Null())
  35. {
  36. debugRenderer_ = scene_->CreateComponent<DebugRenderer>();
  37. }
  38. octree_ = scene_->GetComponent<Octree>();
  39. if (octree_.Null())
  40. {
  41. LOGWARNING("Scene without an octree loaded");
  42. octree_ = scene_->CreateComponent<Octree>();
  43. }
  44. cameraNode_ = scene_->CreateChild("Camera");
  45. camera_ = cameraNode_->CreateComponent<Camera>();
  46. debugRenderer_ = scene_->GetComponent<DebugRenderer>();
  47. assert(debugRenderer_.NotNull());
  48. octree_ = scene_->GetComponent<Octree>();
  49. assert(octree_.NotNull());
  50. cameraNode_->SetPosition(Vector3(0, 0, -10));
  51. SetView(scene_, camera_);
  52. SetAutoUpdate(false);
  53. SubscribeToEvent(E_UPDATE, HANDLER(SceneView3D, HandleUpdate));
  54. SubscribeToEvent(E_EDITORACTIVENODECHANGE, HANDLER(SceneView3D, HandleEditorActiveNodeChange));
  55. SubscribeToEvent(E_POSTRENDERUPDATE, HANDLER(SceneView3D, HandlePostRenderUpdate));
  56. // TODO: generate this event properly
  57. VariantMap eventData;
  58. eventData[EditorActiveSceneChange::P_SCENE] = scene_;
  59. SendEvent(E_EDITORACTIVESCENECHANGE, eventData);
  60. }
  61. SceneView3D::~SceneView3D()
  62. {
  63. }
  64. void SceneView3D::MoveCamera(float timeStep)
  65. {
  66. // Do not move if the UI has a focused element (the console)
  67. if (GetSubsystem<UI>()->GetFocusElement())
  68. return;
  69. Input* input = GetSubsystem<Input>();
  70. // Movement speed as world units per second
  71. const float MOVE_SPEED = 20.0f;
  72. // Mouse sensitivity as degrees per pixel
  73. const float MOUSE_SENSITIVITY = 0.2f;
  74. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  75. if (input->GetKeyDown('F'))
  76. {
  77. IntVector2 mouseMove = input->GetMouseMove();
  78. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  79. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  80. pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  81. }
  82. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  83. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
  84. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  85. // Use the Translate() function (default local space) to move relative to the node's orientation.
  86. if (input->GetKeyDown('W'))
  87. cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
  88. if (input->GetKeyDown('S'))
  89. cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep);
  90. if (input->GetKeyDown('A'))
  91. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  92. if (input->GetKeyDown('D'))
  93. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  94. }
  95. Ray SceneView3D::GetCameraRay()
  96. {
  97. Ray camRay;
  98. UI* ui = GetSubsystem<UI>();
  99. IntVector2 cpos = ui->GetCursorPosition();
  100. TBRect rect = GetWidgetDelegate()->GetRect();
  101. if (!rect.w || !rect.h)
  102. return camRay;
  103. GetWidgetDelegate()->ConvertToRoot(rect.x, rect.y);
  104. return camera_->GetScreenRay(float(cpos.x_ - rect.x) / rect.w,
  105. float(cpos.y_ - rect.y) /rect.h);
  106. }
  107. void SceneView3D::HandlePostRenderUpdate(StringHash eventType, VariantMap& eventData)
  108. {
  109. Ray camRay = GetCameraRay();
  110. PODVector<RayQueryResult> result;
  111. RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_ANY, DEFAULT_VIEWMASK);
  112. octree_->RaycastSingle(query);
  113. if (query.result_.Size())
  114. {
  115. const RayQueryResult& r = result[0];
  116. if (r.drawable_)
  117. {
  118. debugRenderer_->AddNode(r.drawable_->GetNode(), 1.0, false);
  119. r.drawable_->DrawDebugGeometry(debugRenderer_, false);
  120. }
  121. }
  122. }
  123. void SceneView3D::SelectNode(Node* node)
  124. {
  125. }
  126. void SceneView3D::HandleUpdate(StringHash eventType, VariantMap& eventData)
  127. {
  128. // Timestep parameter is same no matter what event is being listened to
  129. float timeStep = eventData[Update::P_TIMESTEP].GetFloat();
  130. MoveCamera(timeStep);
  131. QueueUpdate();
  132. }
  133. void SceneView3D::HandleEditorActiveNodeChange(StringHash eventType, VariantMap& eventData)
  134. {
  135. Node* node = (Node*) (eventData[EditorActiveNodeChange::P_NODE].GetPtr());
  136. SelectNode(node);
  137. }
  138. }