Browse Source

3D rendering working in TB

Josh Engebretson 10 years ago
parent
commit
3f4b7e43eb

+ 2 - 1
Source/Atomic/Engine/Engine.cpp

@@ -647,11 +647,12 @@ void Engine::Render()
     if (!graphics->BeginFrame())
         return;
 
+    GetSubsystem<Renderer>()->Render();
+
     #ifdef ATOMIC_TBUI
     GetSubsystem<TBUI>()->Render();
     #endif
 
-    GetSubsystem<Renderer>()->Render();
     GetSubsystem<UI>()->Render();
     graphics->EndFrame();
 }

+ 21 - 0
Source/Atomic/UI/TBUI.cpp

@@ -359,6 +359,24 @@ void TBUI::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData,
     }
 }
 
+void TBUI::SubmitBatchVertexData(Texture* texture, const PODVector<float>& vertexData)
+{
+    UIBatch b(this, BLEND_ALPHA , TBUIRenderer::renderer_->currentScissor_, texture, &vertexData_);
+
+    unsigned begin = b.vertexData_->Size();
+    b.vertexData_->Resize(begin + vertexData.Size());
+    float* dest = &(b.vertexData_->At(begin));
+    b.vertexEnd_ = b.vertexData_->Size();
+
+    for (unsigned i = 0; i < vertexData.Size(); i++, dest++)
+    {
+        *dest = vertexData[i];
+    }
+
+    UIBatch::AddOrMerge(b, batches_);
+
+}
+
 void TBUI::HandleScreenMode(StringHash eventType, VariantMap& eventData)
 {
     if (!initialized_)
@@ -781,6 +799,9 @@ void TBUI::Render(VertexBuffer* buffer, const PODVector<UIBatch>& batches, unsig
     graphics_->SetDrawAntialiased(false);
     graphics_->SetFillMode(FILL_SOLID);
     graphics_->SetStencilTest(false);
+
+    graphics_->ResetRenderTargets();
+
     graphics_->SetVertexBuffer(buffer);
 
     ShaderVariation* noTextureVS = graphics_->GetShader(VS, "Basic", "VERTEXCOLOR");

+ 3 - 0
Source/Atomic/UI/TBUI.h

@@ -47,8 +47,11 @@ public:
     bool LoadResourceFile(tb::TBWidget* widget, const String& filename);
 
     void Render();
+
     void GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor);
 
+    void SubmitBatchVertexData(Texture* texture, const PODVector<float>& vertexData);
+
     void SetInputDisabled(bool disabled) { inputDisabled_ = disabled; }
 
     void FadeOut(float time);

+ 1 - 1
Source/Atomic/UI/View3D.cpp

@@ -70,7 +70,7 @@ void View3D::OnResize()
     int width = GetWidth();
     int height = GetHeight();
     
-    if (width > 0 && height >> 0)
+    if (width > 0 && height > 0)
     {
         renderTexture_->SetSize(width, height, rttFormat_, TEXTURE_RENDERTARGET);
         depthTexture_->SetSize(width, height, Graphics::GetDepthStencilFormat(), TEXTURE_DEPTHSTENCIL);

+ 9 - 1
Source/AtomicEditor/Source/Editors/SceneEditor3D/SceneEditor3D.cpp

@@ -21,12 +21,15 @@
 #include "AEEvents.h"
 
 #include <Atomic/Input/Input.h>
-#include "SceneEditor3D.h"
+
 
 #include <Atomic/UI/TBUI.h>
 #include <Atomic/UI/UI.h>
 #include <Atomic/UI/View3D.h>
 
+#include "SceneEditor3D.h"
+#include "View3DWidget.h"
+
 namespace AtomicEditor
 {
 
@@ -72,6 +75,11 @@ SceneEditor3D ::SceneEditor3D(Context* context, const String &fullpath, TBTabCon
     // once octree/debugrenderer exist
     sceneView_ = new SceneView3D(context_, this);
 
+    View3DWidget* widget = sceneView_->GetWidget();
+    widget->SetGravity(WIDGET_GRAVITY_ALL);
+
+    view3DContainer_->AddChild(widget);
+
     layout_->AddChild(view3DContainer_);
 
     container_->GetContentRoot()->AddChild(layout_);

+ 7 - 3
Source/AtomicEditor/Source/Editors/SceneEditor3D/SceneView3D.cpp

@@ -8,6 +8,7 @@
 #include <Atomic/Scene/Scene.h>
 #include <Atomic/Graphics/Camera.h>
 
+#include <Atomic/Graphics/Graphics.h>
 #include <Atomic/Graphics/DebugRenderer.h>
 #include <Atomic/Graphics/Viewport.h>
 #include <Atomic/Graphics/Octree.h>
@@ -29,6 +30,8 @@
 #include <Atomic/UI/UI.h>
 #include <Atomic/UI/View3D.h>
 
+#include "View3DWidget.h"
+
 namespace AtomicEditor
 {
 
@@ -57,9 +60,10 @@ SceneView3D ::SceneView3D(Context* context, SceneEditor3D *sceneEditor) :
     view3D_ = new View3D(context_);
     view3D_->SetView(scene_, camera_);
     view3D_->SetAutoUpdate(false);
-    view3D_->SetSize(800, 800);
 
-    GetSubsystem<UI>()->GetRoot()->AddChild(view3D_);
+    TBUI* tbui = GetSubsystem<TBUI>();
+    widget_ = new View3DWidget();
+    widget_->SetView3D(tbui, view3D_);
 
     SubscribeToEvent(E_UPDATE, HANDLER(SceneView3D, HandleUpdate));
     SubscribeToEvent(E_EDITORACTIVENODECHANGE, HANDLER(SceneView3D, HandleEditorActiveNodeChange));
@@ -74,7 +78,7 @@ SceneView3D ::SceneView3D(Context* context, SceneEditor3D *sceneEditor) :
 
 SceneView3D::~SceneView3D()
 {
-    GetSubsystem<UI>()->GetRoot()->RemoveChild(view3D_);
+
 }
 
 void SceneView3D::MoveCamera(float timeStep)

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

@@ -22,6 +22,7 @@ namespace AtomicEditor
 {
 
 class SceneEditor3D;
+class View3DWidget;
 
 class SceneView3D: public Object
 {
@@ -35,6 +36,8 @@ public:
 
     void SelectNode(Node* node);
 
+    View3DWidget* GetWidget() { return widget_; }
+
 private:
 
     void HandleUpdate(StringHash eventType, VariantMap& eventData);
@@ -55,6 +58,8 @@ private:
 
     SharedPtr<View3D> view3D_;
 
+    View3DWidget* widget_;
+
     WeakPtr<DebugRenderer> debugRenderer_;
     WeakPtr<Octree> octree_;
     WeakPtr<Node> selectedNode_;

+ 69 - 0
Source/AtomicEditor/Source/Editors/SceneEditor3D/View3DWidget.cpp

@@ -0,0 +1,69 @@
+
+#include "AtomicEditor.h"
+
+#include <Atomic/UI/TBUI.h>
+#include "View3DWidget.h"
+
+namespace AtomicEditor
+{
+
+View3DWidget::View3DWidget()
+{
+    vertexData_.Resize(6 * UI_VERTEX_SIZE);
+    float color;
+    ((unsigned&)color) = 0xFFFFFFFF;
+
+    float* data = &vertexData_[0];
+
+    data[2] = 0; data[3] = color; data[4] = 0; data[5] = 0;
+    data[8] = 0; data[9] = color; data[10] = 1; data[11] = 0;
+    data[14] = 0; data[15] = color; data[16] = 1; data[17] = 1;
+    data[20] = 0; data[21] = color; data[22] = 0; data[23] = 0;
+    data[26] = 0; data[27] = color; data[28] = 1; data[29] = 1;
+    data[32] = 0; data[33] = color; data[34] = 0; data[35] = 1;
+}
+
+void View3DWidget::OnPaint(const PaintProps &paint_props)
+{
+    if (view3D_.Null())
+        return;
+
+    TBRect rect = GetRect();
+
+    ConvertToRoot(rect.x, rect.y);
+
+    IntVector2 size = view3D_->GetSize();
+
+    if (size.x_ != rect.w || size.y_ != rect.h)
+    {
+        size.x_ = rect.w;
+        size.y_ = rect.h;
+
+        view3D_->SetSize(size);
+    }
+
+    float* data = &vertexData_[0];
+
+    data[0] = rect.x;
+    data[1] = rect.y;
+
+    data[6] = rect.x + rect.w;
+    data[7] =  rect.y;
+
+    data[12] = rect.x + rect.w;
+    data[13] = rect.y + rect.h;
+
+    data[18] = rect.x;
+    data[19] = rect.y;
+
+    data[24] = rect.x + rect.w;
+    data[25] = rect.y + rect.h;
+
+    data[30] = rect.x;
+    data[31] = rect.y + rect.h;
+
+    tbui_->SubmitBatchVertexData(view3D_->GetRenderTexture(), vertexData_);
+
+}
+
+}

+ 37 - 0
Source/AtomicEditor/Source/Editors/SceneEditor3D/View3DWidget.h

@@ -0,0 +1,37 @@
+
+#pragma once
+
+#include <TurboBadger/tb_widgets.h>
+#include <Atomic/Graphics/Texture2D.h>
+#include <Atomic/UI/View3D.h>
+#include <Atomic/UI/TBUI.h>
+
+using namespace tb;
+using namespace Atomic;
+
+namespace  AtomicEditor
+{
+
+class View3DWidget : public TBWidget
+{
+public:
+    // For safe typecasting
+    TBOBJECT_SUBCLASS(View3DWidget, TBWidget);
+
+    View3DWidget();
+
+    void SetView3D(TBUI* tbui, View3D* view3D) { tbui_ = tbui, view3D_ = view3D; }
+
+    virtual void OnPaint(const PaintProps &paint_props);
+
+private:
+
+    WeakPtr<TBUI> tbui_;
+    SharedPtr<View3D> view3D_;
+
+    PODVector<float> vertexData_;
+
+};
+
+
+}