Esempio n. 1
0
        public static unsafe void render(BasicEffect effect)
        {
            IO io = ImGui.GetIO();

            ImGui.Render();

            DrawData *draw_data      = ImGui.GetDrawData();
            var       graphicsDevice = _game.GraphicsDevice;

            graphicsDevice.DepthStencilState = DepthStencilState.None;
            graphicsDevice.RasterizerState   = new RasterizerState()
            {
                ScissorTestEnable = true, CullMode = CullMode.None
            };
            graphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
            graphicsDevice.BlendState       = BlendState.AlphaBlend;

            // Handle cases of screen coordinates != from framebuffer coordinates (e.g. retina displays)
            ImGui.ScaleClipRects(draw_data, io.DisplayFramebufferScale);

            //var effect = Assets.defaultEffect;
            //effect.Texture = Assets.dummyTexture;
            effect.World      = Matrix.Identity;
            effect.View       = Matrix.Identity;// camera.viewMatrix3D;
            effect.Projection =
                Matrix.CreateOrthographicOffCenter(
                    0.0f,
                    io.DisplaySize.X / io.DisplayFramebufferScale.X,
                    io.DisplaySize.Y / io.DisplayFramebufferScale.Y,
                    0.0f,
                    -1.0f,
                    1.0f
                    );

            // Render command lists

            for (int n = 0; n < draw_data->CmdListsCount; n++)
            {
                NativeDrawList *cmd_list       = draw_data->CmdLists[n];
                DrawVert *      vtx_buffer     = (DrawVert *)cmd_list->VtxBuffer.Data;
                ushort *        idx_buffer     = (ushort *)cmd_list->IdxBuffer.Data;
                var             vertexElements = cmd_list->VtxBuffer.Size;
                var             indexElements  = cmd_list->IdxBuffer.Size;
                var             idxPos         = 0;

                if (vertices.Length < vertexElements)
                {
                    vertices = new ImGuiVertex[vertexElements / 2 * 3];
                    _vertexBuffer.Dispose();
                    _vertexBuffer = new VertexBuffer(graphicsDevice, ImGuiVertex.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly);
                }

                for (int i = 0; i < vertexElements; i++)
                {
                    DrawVert vert = *vtx_buffer++;
                    vertices[i].x     = vert.pos.X;
                    vertices[i].y     = vert.pos.Y;
                    vertices[i].tx    = vert.uv.X;
                    vertices[i].ty    = vert.uv.Y;
                    vertices[i].color = vert.col;
                }

                _vertexBuffer.SetData(vertices, 0, vertexElements);

                if (indices.Length < indexElements)
                {
                    indices = new short[indexElements / 2 * 3];
                    _indexBuffer.Dispose();
                    _indexBuffer = new IndexBuffer(graphicsDevice, IndexElementSize.SixteenBits, indices.Length, BufferUsage.WriteOnly);
                }

                for (int i = 0; i < indexElements; i++)
                {
                    indices[i] = (short)idx_buffer[i];
                }
                _indexBuffer.SetData(indices, 0, indexElements);

                graphicsDevice.Indices = _indexBuffer;
                graphicsDevice.SetVertexBuffer(_vertexBuffer);

                for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
                {
                    DrawCmd *pcmd = &(((DrawCmd *)cmd_list->CmdBuffer.Data)[cmd_i]);
                    if (pcmd->UserCallback != IntPtr.Zero)
                    {
                        throw new NotImplementedException();
                    }
                    else
                    {
                        graphicsDevice.ScissorRectangle = new Rectangle((int)pcmd->ClipRect.X, (int)pcmd->ClipRect.Y, (int)(pcmd->ClipRect.Z - pcmd->ClipRect.X), (int)(pcmd->ClipRect.W - pcmd->ClipRect.Y));
                        if (pcmd->TextureId.ToInt32() == fontTexture.GetHashCode())
                        {
                            effect.Texture = fontTexture;
                        }
                        else
                        {
                            throw new NotImplementedException();
                        }

                        var primivites = (int)pcmd->ElemCount / 3;
                        foreach (var pass in effect.CurrentTechnique.Passes)
                        {
                            pass.Apply();
                            graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, idxPos, primivites);
                            //graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertices, 0, vertexElements, indices, 0, indexElements / 3);
                        }
                    }
                    idxPos += (int)pcmd->ElemCount;
                }
            }
        }
        private unsafe void RenderImDrawData(DrawData *draw_data)
        {
            // Rendering
            int display_w, display_h;

            display_w = Application.GetWindow().GetWidth();
            display_h = Application.GetWindow().GetHeight();

            var clear_color = new System.Numerics.Vector4(114f / 255f, 144f / 255f, 154f / 255f, 1.0f);

            GL.Viewport(0, 0, display_w, display_h);
            GL.ClearColor(clear_color.X, clear_color.Y, clear_color.Z, clear_color.W);
            GL.Clear(ClearBufferMask.ColorBufferBit);

            // We are using the OpenGL fixed pipeline to make the example code simpler to read!
            // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, vertex/texcoord/color pointers.
            int last_texture;

            GL.GetInteger(GetPName.TextureBinding2D, out last_texture);
            GL.PushAttrib(AttribMask.EnableBit | AttribMask.ColorBufferBit | AttribMask.TransformBit);
            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
            GL.Disable(EnableCap.CullFace);
            GL.Disable(EnableCap.DepthTest);
            GL.Enable(EnableCap.ScissorTest);
            GL.EnableClientState(ArrayCap.VertexArray);
            GL.EnableClientState(ArrayCap.TextureCoordArray);
            GL.EnableClientState(ArrayCap.ColorArray);
            GL.Enable(EnableCap.Texture2D);

            GL.UseProgram(0);

            // Handle cases of screen coordinates != from framebuffer coordinates (e.g. retina displays)
            IO io = ImGui.GetIO();

            ImGui.ScaleClipRects(draw_data, io.DisplayFramebufferScale);

            // Setup orthographic projection matrix
            GL.MatrixMode(MatrixMode.Projection);
            GL.PushMatrix();
            GL.LoadIdentity();
            GL.Ortho(
                0.0f,
                io.DisplaySize.X / io.DisplayFramebufferScale.X,
                io.DisplaySize.Y / io.DisplayFramebufferScale.Y,
                0.0f,
                -1.0f,
                1.0f);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.PushMatrix();
            GL.LoadIdentity();

            // Render command lists

            for (int n = 0; n < draw_data->CmdListsCount; n++)
            {
                DrawList *cmd_list   = draw_data->CmdLists[n];
                byte *    vtx_buffer = (byte *)cmd_list->VtxBuffer.Data;
                ushort *  idx_buffer = (ushort *)cmd_list->IdxBuffer.Data;

                DrawVert vert0 = *((DrawVert *)vtx_buffer);
                DrawVert vert1 = *(((DrawVert *)vtx_buffer) + 1);
                DrawVert vert2 = *(((DrawVert *)vtx_buffer) + 2);

                GL.VertexPointer(2, VertexPointerType.Float, sizeof(DrawVert), new IntPtr(vtx_buffer + DrawVert.PosOffset));
                GL.TexCoordPointer(2, TexCoordPointerType.Float, sizeof(DrawVert), new IntPtr(vtx_buffer + DrawVert.UVOffset));
                GL.ColorPointer(4, ColorPointerType.UnsignedByte, sizeof(DrawVert), new IntPtr(vtx_buffer + DrawVert.ColOffset));

                for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
                {
                    DrawCmd *pcmd = &(((DrawCmd *)cmd_list->CmdBuffer.Data)[cmd_i]);
                    if (pcmd->UserCallback != IntPtr.Zero)
                    {
                        throw new NotImplementedException();
                    }
                    else
                    {
                        GL.BindTexture(TextureTarget.Texture2D, pcmd->TextureId.ToInt32());
                        GL.Scissor(
                            (int)pcmd->ClipRect.X,
                            (int)(io.DisplaySize.Y - pcmd->ClipRect.W),
                            (int)(pcmd->ClipRect.Z - pcmd->ClipRect.X),
                            (int)(pcmd->ClipRect.W - pcmd->ClipRect.Y));
                        ushort[] indices = new ushort[pcmd->ElemCount];
                        for (int i = 0; i < indices.Length; i++)
                        {
                            indices[i] = idx_buffer[i];
                        }
                        GL.DrawElements(PrimitiveType.Triangles, (int)pcmd->ElemCount, DrawElementsType.UnsignedShort, new IntPtr(idx_buffer));
                    }
                    idx_buffer += pcmd->ElemCount;
                }
            }

            // Restore modified state
            GL.DisableClientState(ArrayCap.ColorArray);
            GL.DisableClientState(ArrayCap.TextureCoordArray);
            GL.DisableClientState(ArrayCap.VertexArray);
            GL.BindTexture(TextureTarget.Texture2D, last_texture);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.PopMatrix();
            GL.MatrixMode(MatrixMode.Projection);
            GL.PopMatrix();
            GL.PopAttrib();
        }
Esempio n. 3
0
        private static unsafe void RenderDrawData(DrawData *drawData, int displayW, int displayH)
        {
            if (!Engine.Get.RenderImGui)
            {
                return;
            }

            GL.Viewport(0, 0, displayW, displayH);

            GL.GetInteger(GetPName.TextureBinding2D, out int last_texture);
            GL.PushAttrib(AttribMask.EnableBit | AttribMask.ColorBufferBit | AttribMask.TransformBit);
            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
            GL.Disable(EnableCap.CullFace);
            GL.Disable(EnableCap.DepthTest);
            GL.Enable(EnableCap.ScissorTest);
            GL.EnableClientState(ArrayCap.VertexArray);
            GL.EnableClientState(ArrayCap.TextureCoordArray);
            GL.EnableClientState(ArrayCap.ColorArray);
            GL.Enable(EnableCap.Texture2D);

            GL.UseProgram(0);

            // Handle cases of screen coordinates != from framebuffer coordinates (e.g. retina displays)
            IO io = ImGui.GetIO();

            ImGui.ScaleClipRects(drawData, io.DisplayFramebufferScale);

            // Setup orthographic projection matrix
            GL.MatrixMode(MatrixMode.Projection);
            GL.PushMatrix();
            GL.LoadIdentity();
            GL.Ortho(
                0.0f,
                io.DisplaySize.X / io.DisplayFramebufferScale.X,
                io.DisplaySize.Y / io.DisplayFramebufferScale.Y,
                0.0f,
                -1.0f,
                1.0f);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.PushMatrix();
            GL.LoadIdentity();

            // Render command lists

            for (int n = 0; n < drawData->CmdListsCount; n++)
            {
                NativeDrawList *cmd_list   = drawData->CmdLists[n];
                byte *          vtx_buffer = (byte *)cmd_list->VtxBuffer.Data;
                ushort *        idx_buffer = (ushort *)cmd_list->IdxBuffer.Data;

                DrawVert vert0 = *((DrawVert *)vtx_buffer);
                DrawVert vert1 = *(((DrawVert *)vtx_buffer) + 1);
                DrawVert vert2 = *(((DrawVert *)vtx_buffer) + 2);

                GL.VertexPointer(2, VertexPointerType.Float, sizeof(DrawVert), new IntPtr(vtx_buffer + DrawVert.PosOffset));
                GL.TexCoordPointer(2, TexCoordPointerType.Float, sizeof(DrawVert), new IntPtr(vtx_buffer + DrawVert.UVOffset));
                GL.ColorPointer(4, ColorPointerType.UnsignedByte, sizeof(DrawVert), new IntPtr(vtx_buffer + DrawVert.ColOffset));

                for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
                {
                    DrawCmd *pcmd = &(((DrawCmd *)cmd_list->CmdBuffer.Data)[cmd_i]);
                    if (pcmd->UserCallback != IntPtr.Zero)
                    {
                        throw new NotImplementedException();
                    }
                    else
                    {
                        GL.BindTexture(TextureTarget.Texture2D, pcmd->TextureId.ToInt32());
                        GL.Scissor(
                            (int)pcmd->ClipRect.X,
                            (int)(io.DisplaySize.Y - pcmd->ClipRect.W),
                            (int)(pcmd->ClipRect.Z - pcmd->ClipRect.X),
                            (int)(pcmd->ClipRect.W - pcmd->ClipRect.Y));
                        ushort[] indices = new ushort[pcmd->ElemCount];
                        for (int i = 0; i < indices.Length; i++)
                        {
                            indices[i] = idx_buffer[i];
                        }
                        GL.DrawElements(PrimitiveType.Triangles, (int)pcmd->ElemCount, DrawElementsType.UnsignedShort, new IntPtr(idx_buffer));
                    }
                    idx_buffer += pcmd->ElemCount;
                }
            }

            // Restore modified state
            GL.DisableClientState(ArrayCap.ColorArray);
            GL.DisableClientState(ArrayCap.TextureCoordArray);
            GL.DisableClientState(ArrayCap.VertexArray);
            GL.BindTexture(TextureTarget.Texture2D, last_texture);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.PopMatrix();
            GL.MatrixMode(MatrixMode.Projection);
            GL.PopMatrix();
            GL.PopAttrib();
        }