Exemplo n.º 1
0
        public void Dispose()
        {
            if (m_view1 != null)
            {
                m_view1.Dispose();
            }
            if (m_view2 != null)
            {
                m_view2.Dispose();
            }
            Im3d.Im3d_DX11_Finalize();
            ImGui.DX11_Finalize();

            ImGui.ImGui_ImplDX11_Shutdown();
            ImGui.ImGui_ImplWin32_Shutdown();
            if (m_imgui != IntPtr.Zero)
            {
                ImGui.DestroyContext(m_imgui);
                m_imgui = IntPtr.Zero;
            }

            ClearRTV();
            if (m_swapChain != null)
            {
                m_swapChain.Dispose();
                m_swapChain = null;
            }
            if (m_device != null)
            {
                m_device.Dispose();
                m_device = null;
            }
            Console.WriteLine(SharpDX.Diagnostics.ObjectTracker.ReportActiveObjects());
        }
Exemplo n.º 2
0
 public void Dispose()
 {
     if (m_context != IntPtr.Zero)
     {
         Im3d.DestoryContext(m_context);
         m_context = IntPtr.Zero;
     }
 }
Exemplo n.º 3
0
        public void UpdateFrame(SharpDX.Direct3D11.Device device, TimeSpan deltaTime, ref MouseState mouse)
        {
            var nextSize = new Vector2(200, 200);

            ImGui.SetNextWindowSize(ref nextSize, ImGuiCond.FirstUseEver);
            var padding = Vector2.Zero;

            ImGui.PushStyleVar(ImGuiStyleVar.WindowPadding, ref padding);
            if (ImGui.Begin(m_name))
            {
                var size        = ImGui.GetContentRegionAvail();
                var pos         = ImGui.GetWindowPos();
                var frameHeight = ImGui.GetFrameHeight();
                var localMouse  = ImGui.GetMousePos() - pos - new Vector2(0, frameHeight);
                var width       = (int)size.X;
                var height      = (int)size.Y;
                if (localMouse.X >= 0 && localMouse.Y >= 0 && localMouse.X < size.X && localMouse.Y < size.Y)
                {
                    m_mouse = mouse;
                    // cursor on view
                    m_mouse.X = (int)localMouse.X;
                    m_mouse.Y = (int)localMouse.Y;
                }

                m_camera.MouseInput(m_mouse, width, height);

                // update
                m_im3d.Set();
                Im3d.Im3dGui_NewFrame(ref m_camera.state, ref m_mouse, (float)deltaTime.TotalSeconds, -1);
                Im3d.Gizmo("gizmo", ref m_model.M11);
                Im3d.EndFrame();


                if (m_renderTexture != null)
                {
                    var desc = m_renderTexture.Description;
                    if (desc.Width != width || desc.Height != height)
                    {
                        m_renderTexture.Dispose();
                        m_renderTexture = null;

                        m_srv.Dispose();
                        m_srv = null;
                    }
                }
                if (m_renderTexture == null)
                {
                    m_renderTexture = new Texture2D(device, new Texture2DDescription
                    {
                        Format            = SharpDX.DXGI.Format.B8G8R8X8_UNorm,
                        ArraySize         = 1,
                        MipLevels         = 1,
                        Width             = width,
                        Height            = height,
                        SampleDescription = new SharpDX.DXGI.SampleDescription
                        {
                            Count   = 1,
                            Quality = 0
                        },
                        BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource
                    });

                    m_srv = new ShaderResourceView(device, m_renderTexture);

                    m_rt.FromTexture(device, m_renderTexture);
                }
                m_rt.Setup(device, m_clearColor);

                // render
                ImGui.DX11_DrawTeapot(device.ImmediateContext.NativePointer, ref m_camera.state.viewProjection.M11, ref m_model.M11);

                // im3d
                Im3d.Im3d_DX11_Draw(device.ImmediateContext.NativePointer, ref m_camera.state.viewProjection.M11,
                                    (int)m_camera.state.viewportWidth,
                                    (int)m_camera.state.viewportHeight,
                                    Im3d.GetDrawLists(), (int)Im3d.GetDrawListCount().Value);

                //
                // show render target
                //
                var uv0  = Vector2.Zero;
                var uv1  = Vector2.One;
                var bg   = new Vector4(1, 1, 1, 1);
                var tint = new Vector4(1, 1, 1, 1);
                ImGui.ImageButton(m_srv.NativePointer, ref size, ref uv0, ref uv1, 0, ref bg, ref tint);
            }
            ImGui.End();
            ImGui.PopStyleVar();
        }
Exemplo n.º 4
0
 public Im3dContext()
 {
     m_context = Im3d.NewContext();
 }
Exemplo n.º 5
0
 public void Set()
 {
     Im3d.SetContext(m_context);
 }
Exemplo n.º 6
0
        public void SetHWnd(IntPtr hwnd)
        {
            RECT rect = default;

            User32.GetClientRect(new Win32API.HWND {
                Value = hwnd
            }, out rect);
            int width  = rect.right.Value - rect.left.Value;
            int height = rect.bottom.Value - rect.top.Value;

            var desc = new SwapChainDescription()
            {
                BufferCount     = 1,
                ModeDescription = new ModeDescription(width, height,
                                                      new Rational(60, 1),
                                                      Format.R8G8B8A8_UNorm),
                IsWindowed        = true,
                OutputHandle      = hwnd,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect        = SwapEffect.Discard,
                Usage             = Usage.RenderTargetOutput
            };

            // Create Device and SwapChain
            SharpDX.Direct3D11.Device.CreateWithSwapChain(
                SharpDX.Direct3D.DriverType.Hardware,
                SharpDX.Direct3D11.DeviceCreationFlags.Debug | SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport,
                desc,
                out m_device, out m_swapChain);

            m_imgui = ImGui.CreateContext(IntPtr.Zero);
            var io = (ImGuiIO)ImGui.GetIO();

            io.ConfigFlags |= ImGuiConfigFlags.NavEnableKeyboard;
            // Enable Keyboard Controls
            // io.ConfigFlags |= ImGuiConfigFlags.NavEnableGamepad;      // Enable Gamepad Controls
            io.ConfigFlags |= ImGuiConfigFlags.DockingEnable;           // Enable Docking
            // io.ConfigFlags |= ImGuiConfigFlags.ViewportsEnable;         // Enable Multi-Viewport / Platform Windows
            //io.ConfigViewportsNoAutoMerge = true;
            //io.ConfigViewportsNoTaskBarIcon = true;
            //io.ConfigViewportsNoDefaultParent = true;
            //io.ConfigDockingAlwaysTabBar = true;
            //io.ConfigDockingTransparentPayload = true;
            // #if 1
            //     io.ConfigFlags |= ImGuiConfigFlags_DpiEnableScaleFonts;     // FIXME-DPI: THIS CURRENTLY DOESN'T WORK AS EXPECTED. DON'T USE IN USER APP!
            //     io.ConfigFlags |= ImGuiConfigFlags_DpiEnableScaleViewports; // FIXME-DPI
            // #endif

            AddJapaneseFontFromFileTTF("c:\\Windows\\Fonts\\meiryo.ttc", 18.0f);

            ImGui.ImGui_ImplWin32_Init(hwnd);
            ImGui.ImGui_ImplDX11_Init(
                Device.NativePointer,
                Device.ImmediateContext.NativePointer);

            ImGui.DX11_Initialize();
            Im3d.Im3d_DX11_Initialize();

            m_view1 = new ImGuiSceneView("view1");
            m_view2 = new ImGuiSceneView("view2");
        }