示例#1
0
        /// <summary>
        /// Draws a Texture.
        /// </summary>
        /// <param name="texture">The Texture.</param>
        /// <param name="rectangle">The Rectangle.</param>
        /// <param name="opacity">The Opacity.</param>
        /// <param name="color">The Color.</param>
        public void DrawTexture(ITexture texture, Rectangle rectangle, Color color, float opacity = 1)
        {
            var tex = texture as OpenGLTexture;

            if (tex == null)
            {
                throw new ArgumentException("Expected OpenGLTexture as resource.");
            }
            OpenGLColor col = OpenGLHelper.ConvertColor(color);

            var vertices = new[]
            {
                //  Position                                         Color             Texcoords
                rectangle.X, rectangle.Y, col.R, col.G, col.B, 0.0f, 0.0f,                   // Top-left
                rectangle.X + rectangle.Width, rectangle.Y, col.R, col.G, col.B, 1.0f, 0.0f, // Top-right
                rectangle.X + rectangle.Width, rectangle.Y + tex.Height, col.R, col.G, col.B, 1.0f, 1.0f,
                // Bottom-right
                rectangle.X, rectangle.Y + rectangle.Height, col.R, col.G, col.B, 0.0f, 1.0f // Bottom-left
            };

            _sourceVbo.SetData(vertices);

            tex.Bind();

            _colorShader.SetUniform("dim", _graphicsDevice.BackBuffer.Width, _graphicsDevice.BackBuffer.Height, opacity);
            _colorShader.SetUniformMatrix("transform", _matrix4);

            uint posAttrib = _colorShader.GetAttribLocation("position");

            VertexBuffer.EnableVertexAttribArray(posAttrib);
            VertexBuffer.VertexAttribPointer(posAttrib, 2, false, 7 * sizeof(float), 0);

            uint colAttrib = _colorShader.GetAttribLocation("color");

            VertexBuffer.EnableVertexAttribArray(colAttrib);
            VertexBuffer.VertexAttribPointer(colAttrib, 3, false, 7 * sizeof(float), 2 * sizeof(float));

            uint texAttrib = _colorShader.GetAttribLocation("texcoord");

            VertexBuffer.EnableVertexAttribArray(texAttrib);
            VertexBuffer.VertexAttribPointer(texAttrib, 2, false, 7 * sizeof(float), 5 * sizeof(float));

            OpenGLInterops.DrawElements(OpenGLInterops.GL_TRIANGLES, 6, OpenGLInterops.GL_UNSIGNED_SHORT, IntPtr.Zero);

            tex.Unbind();
        }
示例#2
0
        /// <summary>
        /// Initializes the renderer.
        /// </summary>
        public void Initialize()
        {
            _renderContext.Initialize();
            _colorShader = new ShaderProgram();
            var vshader = new VertexShader();

            vshader.Compile(SimpleVertexShader.SourceCode);
            var fshader = new FragmentShader();

            fshader.Compile(SimpleFragmentShader.SourceCode);
            _colorShader.Link(vshader, fshader);
            OpenGLInterops.Enable(OpenGLInterops.GL_BLEND);
            OpenGLInterops.AlphaBlend();
            SetTransform(Matrix2x3.Identity);
            OpenGLColor clearColor = OpenGLHelper.ConvertColor(_graphicsDevice.ClearColor);

            OpenGLInterops.ClearColor(clearColor);
            _windowSize = _window.Size;
            OpenGLInterops.Viewport(0, 0, (int)_windowSize.X, (int)_windowSize.Y);
            _sourceVao = new VertexArray();
            _sourceVao.Bind();
            _colorShader.Bind();
            _sourceEbo = new IndexBuffer();

            var elements = new ushort[]
            {
                0, 1, 2,
                2, 3, 0
            };

            _sourceEbo.Bind();
            _sourceEbo.SetData(elements);

            _sourceVbo = new VertexBuffer();
            _sourceVbo.Bind();

            _graphicsDevice.ClearColorChanged += GraphicsDeviceClearColorChanged;
            _window.ScreenSizeChanged         += WindowScreenSizeChanged;
        }
示例#3
0
        /// <summary>
        /// Triggered if the clear color changed.
        /// </summary>
        private void GraphicsDeviceClearColorChanged(object sender, EventArgs e)
        {
            OpenGLColor clearColor = OpenGLHelper.ConvertColor(_graphicsDevice.ClearColor);

            OpenGLInterops.ClearColor(clearColor);
        }
示例#4
0
 /// <summary>
 /// Sets the clear color.
 /// </summary>
 /// <param name="color">The Color.</param>
 public static void ClearColor(OpenGLColor color)
 {
     glClearColor(color.R, color.G, color.B, color.A);
 }