Beispiel #1
0
        public UiRenderer(StatedWindow window, int maxQuadCount = DefaultMaxQuads)
        {
            Window       = window;
            RenderingApi = (GfxRenderer)Window.Renderer;
            UiShader     = ShaderProgram.Link(new ShaderSingle[]
            {
                ShaderSingle.Compile(@"
                                
                    #version 330 core
                
                    layout (location = 0) in vec2 a_Position;
                    layout (location = 1) in vec4 a_Color;
                    layout (location = 2) in vec2 a_TextureCoords;
                    layout (location = 3) in float a_SamplerIndex;
                
                    uniform mat4 u_Projection;
                
                    out vec4 f_Color;
                    out vec2 f_TextureCoords;
                    flat out int f_SamplerIndex;
                
                    void main()
                    {
                        f_Color = a_Color;
                        f_TextureCoords = a_TextureCoords;
                        f_SamplerIndex = int(a_SamplerIndex);
                        gl_Position = u_Projection * vec4(a_Position, -1.0f, 1.0f);
                        
                        // Shift the vertex coordinates, so that (0,0) points to bottom left corner
                        gl_Position.x -= 1;
                        gl_Position.y -= 1;
                    }
                
                ", ShaderType.VertexShader),

                ShaderSingle.Compile(@"
                
                    #version 330 core
                
                    in vec4 f_Color;
                    in vec2 f_TextureCoords;
                    flat in int f_SamplerIndex;
                
                    uniform sampler2D u_Textures[${MaxTextureUnits}];
                
                    out vec4 o_Color;
                
                    void main()
                    {
                        if (f_SamplerIndex == 0)
                            o_Color = f_Color;
                        else
                            o_Color = texture(u_Textures[f_SamplerIndex - 1], f_TextureCoords) * f_Color;
                    }
                
                ".Replace("${MaxTextureUnits}", GL.GetInteger(GetPName.MaxTextureImageUnits).ToString()), ShaderType.FragmentShader),
            });

            MaxQuads    = maxQuadCount;
            MaxVertices = MaxQuads * VerticesPerQuad;
            MaxIndices  = MaxQuads * IndicesPerQuad;
            MaxTextures = GL.GetInteger(GetPName.MaxTextureImageUnits);

            PendingVertices = new List <UiVertex>();
            PendingIndices  = new List <uint>();
            PendingTextures = new List <Texture>();

            Vao = new VertexArray <UiVertex>(
                BufferUsageHint.DynamicDraw,
                new UiVertex[MaxVertices],
                new uint[MaxIndices]);
            Vao.EnableAttributeArray(0, "Position");
            Vao.EnableAttributeArray(1, "Color");
            Vao.EnableAttributeArray(2, "TextureCoords");
            Vao.EnableAttributeArray(3, "SamplerIndex");
        }
Beispiel #2
0
 public GfxRenderer(StatedWindow parent)
 {
     Parent = parent;
 }