Beispiel #1
0
        private static void IGRender2(ImDrawDataPtr dd)
        {
            if (dd.CmdListsCount == 0)
            {
                return;
            }

            for (int i = 0; i < dd.CmdListsCount; i++)
            {
                ImDrawListPtr cmd_list = dd.CmdListsRange[i];

                int vertexSize = cmd_list.VtxBuffer.Size * Unsafe.SizeOf <ImDrawVert>();
                if (vertexSize > VtxBufSize)
                {
                    int newSize = (int)Math.Max(VtxBufSize * 1.5f, vertexSize);
                    GL.NamedBufferData(VertexBuffer, newSize, IntPtr.Zero, BufferUsageHint.DynamicDraw);
                    VtxBufSize = newSize;
                }

                int indexSize = cmd_list.IdxBuffer.Size * sizeof(ushort);
                if (indexSize > IdxBufSize)
                {
                    int newSize = (int)Math.Max(IdxBufSize * 1.5f, indexSize);
                    GL.NamedBufferData(IndexBuffer, newSize, IntPtr.Zero, BufferUsageHint.DynamicDraw);
                    IdxBufSize = newSize;
                }
            }

            var io  = ImGui.GetIO();
            var MVP = Matrix4.CreateOrthographicOffCenter(0.0f, Engine.Width, Engine.Height, 0.0f, -1.0f, 1.0f);

            ImGuiProgram.Use();

            ImGuiProgram.UniformMatrix4("Projection", ref MVP);
            GL.Uniform1(ImGuiProgram.UniformLocation("Texture"), 0);

            IG_VAO.Bind();

            dd.ScaleClipRects(io.DisplayFramebufferScale);

            GL.Enable(EnableCap.Blend);
            GL.Enable(EnableCap.ScissorTest);
            GL.BlendEquation(BlendEquationMode.FuncAdd);
            GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
            GL.Disable(EnableCap.CullFace);
            GL.Disable(EnableCap.DepthTest);

            for (int n = 0; n < dd.CmdListsCount; n++)
            {
                ImDrawListPtr cmdList = dd.CmdListsRange[n];

                GL.NamedBufferSubData(VertexBuffer, IntPtr.Zero, cmdList.VtxBuffer.Size * Unsafe.SizeOf <ImDrawVert>(), cmdList.VtxBuffer.Data);
                GL.NamedBufferSubData(IndexBuffer, IntPtr.Zero, cmdList.IdxBuffer.Size * sizeof(ushort), cmdList.IdxBuffer.Data);

                int vOffset = 0;
                int iOffset = 0;

                for (int i = 0; i < cmdList.CmdBuffer.Size; i++)
                {
                    ImDrawCmdPtr pcmd = cmdList.CmdBuffer[i];
                    if (pcmd.UserCallback != IntPtr.Zero)
                    {
                        throw new NotImplementedException();
                    }
                    else
                    {
                        GLTexture.Active(TextureUnit.Texture0);
                        GL.BindTexture(TextureTarget.Texture2D, (int)pcmd.TextureId);

                        var clip = pcmd.ClipRect;
                        GL.Scissor((int)clip.X, Engine.Height - (int)clip.W, (int)(clip.Z - clip.X), (int)(clip.W - clip.Y));

                        if ((io.BackendFlags & ImGuiBackendFlags.RendererHasVtxOffset) != 0)
                        {
                            GL.DrawElementsBaseVertex(PrimitiveType.Triangles, (int)pcmd.ElemCount, DrawElementsType.UnsignedShort, (IntPtr)(iOffset * sizeof(ushort)), vOffset);
                        }
                        else
                        {
                            GL.DrawElements(BeginMode.Triangles, (int)pcmd.ElemCount, DrawElementsType.UnsignedShort, (int)pcmd.IdxOffset * sizeof(ushort));
                        }
                    }
                    iOffset += (int)pcmd.ElemCount;
                }

                vOffset += cmdList.VtxBuffer.Size;
            }

            GL.Disable(EnableCap.Blend);
            GL.Disable(EnableCap.ScissorTest);
        }
Beispiel #2
0
        public static unsafe void Initialise()
        {
            GL.LoadBindings(new SDLBindingsContext());

            Context = ImGui.CreateContext();

            ImGui.SetCurrentContext(Context);

            var io = ImGui.GetIO();

            io.DisplaySize.X = Engine.Width;
            io.DisplaySize.Y = Engine.Height;

            io.KeyMap[(int)ImGuiKey.Tab]         = (int)SC.SDL_SCANCODE_TAB;
            io.KeyMap[(int)ImGuiKey.LeftArrow]   = (int)SC.SDL_SCANCODE_LEFT;
            io.KeyMap[(int)ImGuiKey.RightArrow]  = (int)SC.SDL_SCANCODE_RIGHT;
            io.KeyMap[(int)ImGuiKey.UpArrow]     = (int)SC.SDL_SCANCODE_UP;
            io.KeyMap[(int)ImGuiKey.DownArrow]   = (int)SC.SDL_SCANCODE_DOWN;
            io.KeyMap[(int)ImGuiKey.PageUp]      = (int)SC.SDL_SCANCODE_PAGEUP;
            io.KeyMap[(int)ImGuiKey.PageDown]    = (int)SC.SDL_SCANCODE_PAGEDOWN;
            io.KeyMap[(int)ImGuiKey.Home]        = (int)SC.SDL_SCANCODE_HOME;
            io.KeyMap[(int)ImGuiKey.End]         = (int)SC.SDL_SCANCODE_END;
            io.KeyMap[(int)ImGuiKey.Insert]      = (int)SC.SDL_SCANCODE_INSERT;
            io.KeyMap[(int)ImGuiKey.Delete]      = (int)SC.SDL_SCANCODE_DELETE;
            io.KeyMap[(int)ImGuiKey.Backspace]   = (int)SC.SDL_SCANCODE_BACKSPACE;
            io.KeyMap[(int)ImGuiKey.Space]       = (int)SC.SDL_SCANCODE_SPACE;
            io.KeyMap[(int)ImGuiKey.Enter]       = (int)SC.SDL_SCANCODE_RETURN;
            io.KeyMap[(int)ImGuiKey.Escape]      = (int)SC.SDL_SCANCODE_ESCAPE;
            io.KeyMap[(int)ImGuiKey.KeyPadEnter] = (int)SC.SDL_SCANCODE_KP_ENTER;
            io.KeyMap[(int)ImGuiKey.A]           = (int)SC.SDL_SCANCODE_A;
            io.KeyMap[(int)ImGuiKey.C]           = (int)SC.SDL_SCANCODE_C;
            io.KeyMap[(int)ImGuiKey.V]           = (int)SC.SDL_SCANCODE_V;
            io.KeyMap[(int)ImGuiKey.X]           = (int)SC.SDL_SCANCODE_X;
            io.KeyMap[(int)ImGuiKey.Y]           = (int)SC.SDL_SCANCODE_Y;
            io.KeyMap[(int)ImGuiKey.Z]           = (int)SC.SDL_SCANCODE_Z;

            var maj = GL.GetInteger(GetPName.MajorVersion);
            var min = GL.GetInteger(GetPName.MinorVersion);

            Log.Info($"OpenGLManager: This appears to be OpenGL {maj}.{min}.");

            VtxBufSize = 10000;
            IdxBufSize = 2000;

            IG_VAO = new("ImGui");

            IG_VAO.Bind();

            VertexSource = File.ReadAllText("EngineResources/2D.vert");
            FragSource   = File.ReadAllText("EngineResources/standard.frag");

            VS = new GLShader(VertexSource, GLShaderType.VERTEX);
            FS = new GLShader(FragSource, GLShaderType.FRAGMENT);

            VS.Compile();
            FS.Compile();

            ImGuiProgram = new GLShaderProgram().Attach(VS).Attach(FS).Link();

            GLHelper.VertexBuffer("ImGui VBO", out VertexBuffer);
            GLHelper.ElementBuffer("ImGui EBO", out IndexBuffer);

            GL.NamedBufferData(VertexBuffer, VtxBufSize, IntPtr.Zero, BufferUsageHint.DynamicDraw);
            GL.NamedBufferData(IndexBuffer, IdxBufSize, IntPtr.Zero, BufferUsageHint.DynamicDraw);

            var va = IG_VAO.GLObject;

            GL.VertexArrayVertexBuffer(va, 0, VertexBuffer, IntPtr.Zero, Unsafe.SizeOf <ImDrawVert>());
            GL.VertexArrayElementBuffer(va, IndexBuffer);

            GL.EnableVertexArrayAttrib(va, 0);
            GL.VertexArrayAttribBinding(va, 0, 0);
            GL.VertexArrayAttribFormat(va, 0, 2, VertexAttribType.Float, false, 0);

            GL.EnableVertexArrayAttrib(va, 1);
            GL.VertexArrayAttribBinding(va, 1, 0);
            GL.VertexArrayAttribFormat(va, 1, 2, VertexAttribType.Float, false, 8);

            GL.EnableVertexArrayAttrib(va, 2);
            GL.VertexArrayAttribBinding(va, 2, 0);
            GL.VertexArrayAttribFormat(va, 2, 4, VertexAttribType.UnsignedByte, true, 16);

            Initialised = true;

            OnInitGL?.Invoke();
        }
Beispiel #3
0
        public static unsafe void Initialise()
        {
            GL.LoadBindings(new SDLBindingsContext());

            Context = ImGui.CreateContext();

            ImGui.SetCurrentContext(Context);

            ImGuizmo.SetImGuiContext(Context);

            var io = ImGui.GetIO();

            GL.Enable(EnableCap.DebugOutput);
            GL.Enable(EnableCap.DebugOutputSynchronous);
            GL.DebugMessageCallback(_DebugCb, IntPtr.Zero);

            io.DisplaySize.X = Engine.Width;
            io.DisplaySize.Y = Engine.Height;

            io.KeyMap[(int)ImGuiKey.Tab]         = (int)SC.SDL_SCANCODE_TAB;
            io.KeyMap[(int)ImGuiKey.LeftArrow]   = (int)SC.SDL_SCANCODE_LEFT;
            io.KeyMap[(int)ImGuiKey.RightArrow]  = (int)SC.SDL_SCANCODE_RIGHT;
            io.KeyMap[(int)ImGuiKey.UpArrow]     = (int)SC.SDL_SCANCODE_UP;
            io.KeyMap[(int)ImGuiKey.DownArrow]   = (int)SC.SDL_SCANCODE_DOWN;
            io.KeyMap[(int)ImGuiKey.PageUp]      = (int)SC.SDL_SCANCODE_PAGEUP;
            io.KeyMap[(int)ImGuiKey.PageDown]    = (int)SC.SDL_SCANCODE_PAGEDOWN;
            io.KeyMap[(int)ImGuiKey.Home]        = (int)SC.SDL_SCANCODE_HOME;
            io.KeyMap[(int)ImGuiKey.End]         = (int)SC.SDL_SCANCODE_END;
            io.KeyMap[(int)ImGuiKey.Insert]      = (int)SC.SDL_SCANCODE_INSERT;
            io.KeyMap[(int)ImGuiKey.Delete]      = (int)SC.SDL_SCANCODE_DELETE;
            io.KeyMap[(int)ImGuiKey.Backspace]   = (int)SC.SDL_SCANCODE_BACKSPACE;
            io.KeyMap[(int)ImGuiKey.Space]       = (int)SC.SDL_SCANCODE_SPACE;
            io.KeyMap[(int)ImGuiKey.Enter]       = (int)SC.SDL_SCANCODE_RETURN;
            io.KeyMap[(int)ImGuiKey.Escape]      = (int)SC.SDL_SCANCODE_ESCAPE;
            io.KeyMap[(int)ImGuiKey.KeyPadEnter] = (int)SC.SDL_SCANCODE_KP_ENTER;
            io.KeyMap[(int)ImGuiKey.A]           = (int)SC.SDL_SCANCODE_A;
            io.KeyMap[(int)ImGuiKey.C]           = (int)SC.SDL_SCANCODE_C;
            io.KeyMap[(int)ImGuiKey.V]           = (int)SC.SDL_SCANCODE_V;
            io.KeyMap[(int)ImGuiKey.X]           = (int)SC.SDL_SCANCODE_X;
            io.KeyMap[(int)ImGuiKey.Y]           = (int)SC.SDL_SCANCODE_Y;
            io.KeyMap[(int)ImGuiKey.Z]           = (int)SC.SDL_SCANCODE_Z;

            var maj = GL.GetInteger(GetPName.MajorVersion);
            var min = GL.GetInteger(GetPName.MinorVersion);

            Log.Info($"OpenGLManager: This appears to be OpenGL {maj}.{min}.");

            var t = new GLVersion()
            {
                Major         = maj,
                Minor         = min,
                VersionString = $"{maj}.{min}"
            };

            Version = t;

            if (Version.Major < 4 || (Version.Major == 4 && Version.Minor < 5))
            {
                // This is not OpenGL 4.5 or higher.
                Log.Fatal("This version of OpenGL is too old! Luminal applications require at least GL 4.5.");
                Log.Fatal("Showing message box and exiting.");
                MessageBox.Error("Sorry, applications built with Luminal cannot run on your graphics card.\n" +
                                 $"Your graphics card only supports OpenGL {Version.VersionString}, and at least OpenGL 4.5 is required.",
                                 "Engine incompatible: OpenGL version too old");

                Engine.Quit(1);
            }

            VtxBufSize = 10000;
            IdxBufSize = 2000;

            IG_VAO = new("ImGui");

            IG_VAO.Bind();

            VertexSource = File.ReadAllText("EngineResources/Shaders/2D/2D.vert");
            FragSource   = File.ReadAllText("EngineResources/Shaders/2D/2D.frag");

            VS = new GLShader(VertexSource, GLShaderType.Vertex);
            FS = new GLShader(FragSource, GLShaderType.Fragment);

            VS.Compile();
            FS.Compile();

            ImGuiProgram = new GLShaderProgram().Attach(VS).Attach(FS).Link();

            GLHelper.VertexBuffer("ImGui VBO", out VertexBuffer);
            GLHelper.ElementBuffer("ImGui EBO", out IndexBuffer);

            GL.NamedBufferData(VertexBuffer, VtxBufSize, IntPtr.Zero, BufferUsageHint.DynamicDraw);
            GL.NamedBufferData(IndexBuffer, IdxBufSize, IntPtr.Zero, BufferUsageHint.DynamicDraw);

            var va = IG_VAO.GLObject;

            GL.VertexArrayVertexBuffer(va, 0, VertexBuffer, IntPtr.Zero, Unsafe.SizeOf <ImDrawVert>());
            GL.VertexArrayElementBuffer(va, IndexBuffer);

            GL.EnableVertexArrayAttrib(va, 0);
            GL.VertexArrayAttribBinding(va, 0, 0);
            GL.VertexArrayAttribFormat(va, 0, 2, VertexAttribType.Float, false, 0);

            GL.EnableVertexArrayAttrib(va, 1);
            GL.VertexArrayAttribBinding(va, 1, 0);
            GL.VertexArrayAttribFormat(va, 1, 2, VertexAttribType.Float, false, 8);

            GL.EnableVertexArrayAttrib(va, 2);
            GL.VertexArrayAttribBinding(va, 2, 0);
            GL.VertexArrayAttribFormat(va, 2, 4, VertexAttribType.UnsignedByte, true, 16);

            _SetClipboard         = SetClipboard;
            io.SetClipboardTextFn = Marshal.GetFunctionPointerForDelegate(_SetClipboard);

            _GetClipboard         = GetClipboard;
            io.GetClipboardTextFn = Marshal.GetFunctionPointerForDelegate(_GetClipboard);

            Initialised = true;

            OnInitGL?.Invoke();
        }