Browse Source

Merge pull request #852 from AtomicGameEngine/JME-ATOMIC-GRID

Grid functionality rebased onto mouse speed change branch
JoshEngebretson 9 years ago
parent
commit
fcd8908305

+ 56 - 1
Source/Atomic/Graphics/DebugRenderer.cpp

@@ -52,7 +52,15 @@ static const unsigned MAX_LINES = 1000000;
 static const unsigned MAX_TRIANGLES = 100000;
 static const unsigned MAX_TRIANGLES = 100000;
 
 
 DebugRenderer::DebugRenderer(Context* context) :
 DebugRenderer::DebugRenderer(Context* context) :
-    Component(context)
+    Component(context),
+    position1_(0, 0, 0),
+    position2_(0, 0, 0),
+    position3_(0, 0, 0),
+    numGridLines_(100),
+    scale_(0),
+    lineLength_(0),
+    offset_(0),
+    scaleIncrement_(10)
 {
 {
     vertexBuffer_ = new VertexBuffer(context_);
     vertexBuffer_ = new VertexBuffer(context_);
 
 
@@ -348,6 +356,53 @@ void DebugRenderer::AddTriangleMesh(const void* vertexData, unsigned vertexSize,
     }
     }
 }
 }
 
 
+void DebugRenderer::CreateXAxisLines(unsigned gridColor, bool depthTest, int x, int y, int z)
+{
+    for (int i = 0; i < numGridLines_; i++)
+    {
+        position1_ = Vector3(x, y, z - offset_);
+        position2_ = Vector3(position1_.x_ + lineLength_, y, z - offset_);
+        position3_ = Vector3(position1_.x_ + -lineLength_, y, z - offset_);
+
+        AddLine(position1_, position2_, gridColor, depthTest);
+        AddLine(position3_, position1_, gridColor, depthTest);
+
+        z += scale_;
+    }
+
+}
+
+void DebugRenderer::CreateZAxisLines(unsigned gridColor, bool depthTest, int x, int y, int z)
+{
+    for (int j = 0; j < numGridLines_; j++)
+    {
+        position1_ = Vector3(x - offset_, y, z);
+        position2_ = Vector3(x - offset_, y, position1_.z_ + lineLength_);
+        position3_ = Vector3(x - offset_, y, position1_.z_ + -lineLength_);
+
+        AddLine(position1_, position2_, gridColor, depthTest);
+        AddLine(position3_, position1_, gridColor, depthTest);
+
+        x += scale_;
+    }
+}
+
+void DebugRenderer::CreateGrid(const Color& grid, bool depthTest, Vector3 position)
+{
+    unsigned gridColor = grid.ToUInt();
+
+    scale_ = position.y_ / scaleIncrement_;
+
+    if (position.y_ < scaleIncrement_)
+        scale_ = 1;
+
+    lineLength_ = (numGridLines_ / 2) * scale_;
+    offset_ = (numGridLines_ / 2) * scale_;
+
+    CreateXAxisLines(gridColor, depthTest, 0, 0, 0);
+    CreateZAxisLines(gridColor, depthTest, 0, 0, 0);
+}
+
 void DebugRenderer::Render()
 void DebugRenderer::Render()
 {
 {
     if (!HasContent())
     if (!HasContent())

+ 26 - 0
Source/Atomic/Graphics/DebugRenderer.h

@@ -134,6 +134,14 @@ public:
     void AddTriangleMesh
     void AddTriangleMesh
         (const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize, unsigned indexStart,
         (const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize, unsigned indexStart,
             unsigned indexCount, const Matrix3x4& transform, const Color& color, bool depthTest = true);
             unsigned indexCount, const Matrix3x4& transform, const Color& color, bool depthTest = true);
+
+    /// Create positive and negative X axis lines
+    void CreateXAxisLines(unsigned gridColor, bool depthTest, int x, int y, int z);
+    /// Create positive and negative Z axis lines
+    void CreateZAxisLines(unsigned gridColor, bool depthTest, int x, int y, int z);
+    /// Creates a grid on all axis
+    void CreateGrid(const Color& grid, bool depthTest, Vector3 position);
+
     /// Update vertex buffer and render all debug lines. The viewport and rendertarget should be set before.
     /// Update vertex buffer and render all debug lines. The viewport and rendertarget should be set before.
     void Render();
     void Render();
 
 
@@ -171,6 +179,24 @@ private:
     Frustum frustum_;
     Frustum frustum_;
     /// Vertex buffer.
     /// Vertex buffer.
     SharedPtr<VertexBuffer> vertexBuffer_;
     SharedPtr<VertexBuffer> vertexBuffer_;
+
+    /// Positioning of grid lines point 1
+    Vector3 position1_;
+    /// Positioning of grid lines point 2
+    Vector3 position2_;
+    /// Positioning of grid lines point 3
+    Vector3 position3_;
+    
+    /// Number of total grid lines
+    int numGridLines_;
+    /// Length of a grid line
+    int lineLength_;
+    /// Offset centres the grid
+    int offset_;
+    /// Scales the grid according to y-position of camera
+    int scale_;
+    /// The amount the scale gets incremented
+    int scaleIncrement_;
 };
 };
 
 
 }
 }

+ 12 - 1
Source/AtomicEditor/Editors/SceneEditor3D/SceneView3D.cpp

@@ -74,7 +74,8 @@ SceneView3D ::SceneView3D(Context* context, SceneEditor3D *sceneEditor) :
     mouseMoved_(false),
     mouseMoved_(false),
     enabled_(true),
     enabled_(true),
     cameraMove_(false),
     cameraMove_(false),
-    cameraMoveSpeed_(20.0f)
+    cameraMoveSpeed_(20.0f),
+    gridEnabled_(false)
 {
 {
 
 
     sceneEditor_ = sceneEditor;
     sceneEditor_ = sceneEditor;
@@ -504,7 +505,13 @@ bool SceneView3D::OnEvent(const TBWidgetEvent &ev)
             sceneEditor_->GetSelection()->Clear();
             sceneEditor_->GetSelection()->Clear();
         }
         }
     }
     }
+    if (ev.type == EVENT_TYPE_KEY_DOWN)
+    {
+        Input* input = GetSubsystem<Input>();
 
 
+        if (input->GetKeyPress(KEY_G))
+            gridEnabled_ = !gridEnabled_;
+    }
     return sceneEditor_->OnEvent(ev);
     return sceneEditor_->OnEvent(ev);
 }
 }
 
 
@@ -527,6 +534,10 @@ void SceneView3D::HandleUpdate(StringHash eventType, VariantMap& eventData)
 
 
     QueueUpdate();
     QueueUpdate();
 
 
+    if (gridEnabled_)
+        debugRenderer_->CreateGrid(Color::GRAY, true, cameraNode_->GetPosition());
+
+
     if (preloadResourceScene_.NotNull())
     if (preloadResourceScene_.NotNull())
     {
     {
         if (preloadResourceScene_->GetAsyncProgress() == 1.0f)
         if (preloadResourceScene_->GetAsyncProgress() == 1.0f)

+ 2 - 0
Source/AtomicEditor/Editors/SceneEditor3D/SceneView3D.h

@@ -76,6 +76,7 @@ private:
     bool GetZooming();
     bool GetZooming();
 
 
     bool GetChangingCameraSpeed();
     bool GetChangingCameraSpeed();
+    void ToggleGrid();
 
 
     void HandleMouseMove(StringHash eventType, VariantMap& eventData);
     void HandleMouseMove(StringHash eventType, VariantMap& eventData);
 
 
@@ -103,6 +104,7 @@ private:
     bool mouseMoved_;
     bool mouseMoved_;
 
 
     bool enabled_;
     bool enabled_;
+    bool gridEnabled_;
 
 
     bool cameraMove_;
     bool cameraMove_;
     float cameraMoveTime_;
     float cameraMoveTime_;