Esempio n. 1
0
        public void Copy(IntPtr source)
        {
            BindTexture();
            GL.TexImage2D(TextureTarget2d.Texture2D, 0, ComponentCount, Width, Height, 0, PixelFormat, PixelType.UnsignedByte, source);
            GLException.CheckError("Copy(TexImage2D)");

            GL.TexSubImage2D(TextureTarget2d.Texture2D, 0, 0, 0, Width, Height, PixelFormat, PixelType.UnsignedByte, source);
            GLException.CheckError("Copy(TexSubImage2D)");
            UnbindTexture();
        }
Esempio n. 2
0
 public Texture2D(int width, int height, PixelFormat pixelFormat, TextureComponentCount componentCount, TextureUnit slot)
 {
     Width          = width;
     Height         = height;
     PixelFormat    = pixelFormat;
     TextureSlot    = slot;
     ComponentCount = componentCount;
     GLTexture      = GL.GenTexture();
     GLException.CheckError("Texture2D");
 }
Esempio n. 3
0
        private int CompileShader(ShaderType type, string source)
        {
            GLException.CheckError("Before CreateShader(" + type + ")");
            int Shader = GL.CreateShader(type);

            GL.ShaderSource(Shader, 1, new string[] { source }, new int[] { source.Length });
            GL.CompileShader(Shader);
            GL.GetShader(Shader, ShaderParameter.CompileStatus, out int success);
            if (success != 1)
            {
                GL.GetShaderInfoLog(Shader, out string infoLog);
                throw new GLException(ErrorCode.InvalidOperation, $"GL.CompileShader for shader [{type}] had info log: {infoLog}");
            }

            return(Shader);
        }
Esempio n. 4
0
        public void CreateDeviceResources()
        {
            _vertexBufferSize = 10000;
            _indexBufferSize  = 2000;

            _shader = new Shader(Properties.Resources.imgui_vertex_glsl, Properties.Resources.imgui_fragment_glsl);

            // Create VertexBufferVertexArray object
            _vertexBuffer = GL.GenBuffer();
            GL.BindBuffer(BufferTarget.ArrayBuffer, _vertexBuffer);

            // Vector2
            int posLocation = _shader.GetAttributeLocation("in_position");

            GL.EnableVertexAttribArray(posLocation);
            GL.VertexAttribPointer(posLocation, 2, VertexAttribPointerType.Float, false, IM_DRAW_VERT_SIZE, 0);

            // Vector2
            int uvLocation = _shader.GetAttributeLocation("in_texCoord");

            GL.EnableVertexAttribArray(uvLocation);
            GL.VertexAttribPointer(uvLocation, 2, VertexAttribPointerType.Float, false, IM_DRAW_VERT_SIZE, 8);

            // uint
            int colorLocation = _shader.GetAttributeLocation("in_color");

            GL.EnableVertexAttribArray(colorLocation);
            GL.VertexAttribPointer(colorLocation, 4, VertexAttribPointerType.UnsignedByte, true, IM_DRAW_VERT_SIZE, 16);

            GL.BufferData(BufferTarget.ArrayBuffer, _vertexBufferSize, IntPtr.Zero, BufferUsageHint.DynamicDraw);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);

            _indexBuffer = GL.GenBuffer();
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, _indexBuffer);
            GL.BufferData(BufferTarget.ElementArrayBuffer, _indexBufferSize, IntPtr.Zero, BufferUsageHint.DynamicDraw);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);

            RecreateFontDeviceTexture();
            GLException.CheckError("ImGui Setup");
        }
Esempio n. 5
0
        public Shader(string vertexShader, string fragmentShader)
        {
            int vertShader = CompileShader(ShaderType.VertexShader, vertexShader);

            GLException.CheckError("Compile vertex shader");
            int fragShader = CompileShader(ShaderType.FragmentShader, fragmentShader);

            GLException.CheckError("Compile fragment shader");
            Program = GL.CreateProgram();
            GLException.CheckError("Create shader program");

            GL.AttachShader(Program, vertShader);
            GL.AttachShader(Program, fragShader);
            GL.LinkProgram(Program);
            GL.GetProgram(Program, GetProgramParameterName.LinkStatus, out int Success);
            if (Success == 0)
            {
                throw new GLException(ErrorCode.InvalidOperation, "GL.LinkProgram had info log: " + GL.GetProgramInfoLog(Program));
            }

            GL.DeleteShader(vertShader);
            GL.DeleteShader(fragShader);
            GLException.CheckError("Shader");
        }
Esempio n. 6
0
 public Texture2D(int width, int height, PixelFormat pixelFormat, TextureComponentCount componentCount, TextureUnit slot, IntPtr data) : this(width, height, pixelFormat, componentCount, slot)
 {
     Copy(data);
     GLException.CheckError("Texture2D");
 }
Esempio n. 7
0
        /*
         * internal void PressChar(char keyChar)
         * {
         *  PressedChars.Add(keyChar);
         * }
         *
         * private static void SetKeyMappings()
         * {
         *  ImGuiIOPtr io = ImGui.GetIO();
         *  io.KeyMap[(int)ImGuiKey.Tab] = (int)Key.Tab;
         *  io.KeyMap[(int)ImGuiKey.LeftArrow] = (int)Key.Left;
         *  io.KeyMap[(int)ImGuiKey.RightArrow] = (int)Key.Right;
         *  io.KeyMap[(int)ImGuiKey.UpArrow] = (int)Key.Up;
         *  io.KeyMap[(int)ImGuiKey.DownArrow] = (int)Key.Down;
         *  io.KeyMap[(int)ImGuiKey.PageUp] = (int)Key.PageUp;
         *  io.KeyMap[(int)ImGuiKey.PageDown] = (int)Key.PageDown;
         *  io.KeyMap[(int)ImGuiKey.Home] = (int)Key.Home;
         *  io.KeyMap[(int)ImGuiKey.End] = (int)Key.End;
         *  io.KeyMap[(int)ImGuiKey.Delete] = (int)Key.Delete;
         *  io.KeyMap[(int)ImGuiKey.Backspace] = (int)Key.BackSpace;
         *  io.KeyMap[(int)ImGuiKey.Enter] = (int)Key.Enter;
         *  io.KeyMap[(int)ImGuiKey.Escape] = (int)Key.Escape;
         *  io.KeyMap[(int)ImGuiKey.A] = (int)Key.A;
         *  io.KeyMap[(int)ImGuiKey.C] = (int)Key.C;
         *  io.KeyMap[(int)ImGuiKey.V] = (int)Key.V;
         *  io.KeyMap[(int)ImGuiKey.X] = (int)Key.X;
         *  io.KeyMap[(int)ImGuiKey.Y] = (int)Key.Y;
         *  io.KeyMap[(int)ImGuiKey.Z] = (int)Key.Z;
         * }
         */

        private void RenderImDrawData(ImDrawDataPtr draw_data)
        {
            uint vertexOffsetInVertices = 0;
            uint indexOffsetInElements  = 0;

            if (draw_data.CmdListsCount == 0)
            {
                return;
            }

            uint totalVBSize = (uint)(draw_data.TotalVtxCount * IM_DRAW_VERT_SIZE);

            if (totalVBSize > _vertexBufferSize)
            {
                int newSize = (int)Math.Max(_vertexBufferSize * 1.5f, totalVBSize);
                GL.BindBuffer(BufferTarget.ArrayBuffer, _vertexBuffer);
                GL.BufferData(BufferTarget.ArrayBuffer, newSize, IntPtr.Zero, BufferUsageHint.DynamicDraw);
                _vertexBufferSize = newSize;
                GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
                Console.WriteLine($"Resized vertex buffer to new size {_vertexBufferSize}");
            }

            uint totalIBSize = (uint)(draw_data.TotalIdxCount * sizeof(ushort));

            if (totalIBSize > _indexBufferSize)
            {
                int newSize = (int)Math.Max(_indexBufferSize * 1.5f, totalIBSize);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, _indexBuffer);
                GL.BufferData(BufferTarget.ElementArrayBuffer, newSize, IntPtr.Zero, BufferUsageHint.DynamicDraw);
                _indexBufferSize = newSize;
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
                Console.WriteLine($"Resized index buffer to new size {_indexBufferSize}");
            }


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

                GL.BindBuffer(BufferTarget.ArrayBuffer, _vertexBuffer);
                GL.BufferSubData(BufferTarget.ArrayBuffer, (IntPtr)(vertexOffsetInVertices * IM_DRAW_VERT_SIZE), cmd_list.VtxBuffer.Size * IM_DRAW_VERT_SIZE, cmd_list.VtxBuffer.Data);
                GLException.CheckError($"Data Vert {i}");
                GL.BindBuffer(BufferTarget.ArrayBuffer, 0);

                GL.BindBuffer(BufferTarget.ElementArrayBuffer, _indexBuffer);
                GL.BufferSubData(BufferTarget.ElementArrayBuffer, (IntPtr)(indexOffsetInElements * sizeof(ushort)), cmd_list.IdxBuffer.Size * sizeof(ushort), cmd_list.IdxBuffer.Data);
                GLException.CheckError($"Data Idx {i}");
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);

                vertexOffsetInVertices += (uint)cmd_list.VtxBuffer.Size;
                indexOffsetInElements  += (uint)cmd_list.IdxBuffer.Size;
            }

            // Setup orthographic projection matrix into our constant buffer
            ImGuiIOPtr io  = ImGui.GetIO();
            Matrix4    mvp = Matrix4.CreateOrthographicOffCenter(0, io.DisplaySize.X, io.DisplaySize.Y, 0.0f, -1.0f, 1.0f);

            _shader.UseShader();
            GL.UniformMatrix4(_shader.GetUniformLocation("projection_matrix"), false, ref mvp);
            GL.Uniform1(_shader.GetUniformLocation("in_fontTexture"), 0);
            GLException.CheckError("Projection");

            GL.BindBuffer(BufferTarget.ArrayBuffer, _vertexBuffer);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, _indexBuffer);
            GLException.CheckError("VAO");

            draw_data.ScaleClipRects(io.DisplayFramebufferScale);

            GL.Enable(EnableCap.Blend);
            GL.Enable(EnableCap.ScissorTest);
            GL.BlendEquation(BlendEquationMode.FuncAdd);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
            GL.Disable(EnableCap.CullFace);
            GL.Disable(EnableCap.DepthTest);
            GLException.CheckError("ImGUI EnableCap");

            // Render command lists
            int vtx_offset = 0;
            int idx_offset = 0;

            GLException.CheckError("Bind Font Texture");
            for (int n = 0; n < draw_data.CmdListsCount; n++)
            {
                ImDrawListPtr cmd_list = draw_data.CmdListsRange[n];
                for (int cmd_i = 0; cmd_i < cmd_list.CmdBuffer.Size; cmd_i++)
                {
                    ImDrawCmdPtr pcmd = cmd_list.CmdBuffer[cmd_i];
                    if (pcmd.UserCallback != IntPtr.Zero)
                    {
                        throw new NotImplementedException();
                    }

                    GL.ActiveTexture(TextureUnit.Texture0);
                    GL.BindTexture(TextureTarget.Texture2D, (int)pcmd.TextureId);

                    // We do _windowHeight - (int)clip.W instead of (int)clip.Y because gl has flipped Y when it comes to these coordinates
                    var clip = pcmd.ClipRect;
                    GL.Scissor((int)clip.X, _windowHeight - (int)clip.W, (int)(clip.Z - clip.X), (int)(clip.W - clip.Y));
                    GLException.CheckError("Scissor");

                    GL.DrawElements(PrimitiveType.Triangles, (int)pcmd.ElemCount, DrawElementsType.UnsignedShort, (IntPtr)((idx_offset * sizeof(ushort)) + vtx_offset));
                    GLException.CheckError("DrawElements");

                    idx_offset += (int)pcmd.ElemCount;
                }
                vtx_offset += cmd_list.VtxBuffer.Size;
            }

            //_fontTexture.UnbindTexture();

            GL.Disable(EnableCap.Blend);
            GL.Disable(EnableCap.ScissorTest);

            // unbind VAO
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
        }