Пример #1
0
        /// <summary>Renders a static frustrum based background</summary>
        /// <param name="data">The background to render</param>
        /// <param name="alpha">The alpha level</param>
        /// <param name="scale">The scale</param>
        private void RenderStaticBackgroundRetained(StaticBackground data, float alpha, float scale)
        {
            Texture t = data.Texture;

            if (data.Texture != null && renderer.currentHost.LoadTexture(ref t, OpenGlTextureWrapMode.RepeatClamp))
            {
                renderer.LastBoundTexture = t.OpenGlTextures[(int)OpenGlTextureWrapMode.RepeatClamp];
                if (alpha == 1.0f)
                {
                    GL.Disable(EnableCap.Blend);
                }
                else
                {
                    GL.Enable(EnableCap.Blend);
                }

                if (data.VAO == null)
                {
                    data.CreateVAO(renderer.DefaultShader.VertexLayout, renderer);
                }

                renderer.DefaultShader.Activate();
                renderer.ResetShader(renderer.DefaultShader);

                // matrix
                renderer.DefaultShader.SetCurrentProjectionMatrix(renderer.CurrentProjectionMatrix);
                renderer.DefaultShader.SetCurrentModelViewMatrix(Matrix4D.Scale(scale) * renderer.CurrentViewMatrix);

                // fog
                if (renderer.OptionFog)
                {
                    renderer.DefaultShader.SetIsFog(true);
                    renderer.DefaultShader.SetFog(renderer.Fog);
                }

                // texture
                GL.BindTexture(TextureTarget.Texture2D, t.OpenGlTextures[(int)OpenGlTextureWrapMode.RepeatClamp].Name);
                renderer.LastBoundTexture = null;

                // alpha test
                renderer.SetAlphaFunc(AlphaFunction.Greater, 0.0f);

                // blend mode
                renderer.DefaultShader.SetOpacity(alpha);

                // render polygon
                VertexArrayObject VAO = (VertexArrayObject)data.VAO;
                VAO.Bind();
                renderer.lastVAO = VAO.handle;
                for (int i = 0; i + 11 < 32 * 12; i += 12)
                {
                    VAO.Draw(PrimitiveType.Triangles, i, 12);
                }
                renderer.RestoreBlendFunc();
            }
        }
Пример #2
0
        /// <summary>Renders a static frustrum based background</summary>
        /// <param name="data">The background to render</param>
        /// <param name="alpha">The alpha level</param>
        /// <param name="scale">The scale</param>
        private void RenderStaticBackground(StaticBackground data, float alpha, float scale)
        {
            if (data.Texture != null && renderer.currentHost.LoadTexture(data.Texture, OpenGlTextureWrapMode.RepeatClamp))
            {
                GL.Enable(EnableCap.Texture2D);

                if (alpha == 1.0f)
                {
                    GL.Disable(EnableCap.Blend);
                }
                else
                {
                    GL.Enable(EnableCap.Blend);
                }

                if (data.VAO == null)
                {
                    data.CreateVAO();
                }

                renderer.DefaultShader.Activate();
                renderer.ResetShader(renderer.DefaultShader);

                // matrix
                renderer.DefaultShader.SetCurrentProjectionMatrix(renderer.CurrentProjectionMatrix);
                renderer.DefaultShader.SetCurrentModelViewMatrix(Matrix4D.Scale(scale) * renderer.CurrentViewMatrix);

                // fog
                if (renderer.OptionFog)
                {
                    renderer.DefaultShader.SetIsFog(true);
                    renderer.DefaultShader.SetFogStart(renderer.Fog.Start);
                    renderer.DefaultShader.SetFogEnd(renderer.Fog.End);
                    renderer.DefaultShader.SetFogColor(new Color4(renderer.Fog.Color.R, renderer.Fog.Color.G, renderer.Fog.Color.B, 255));
                }

                // texture
                renderer.DefaultShader.SetIsTexture(true);
                renderer.DefaultShader.SetTexture(0);
                GL.BindTexture(TextureTarget.Texture2D, data.Texture.OpenGlTextures[(int)OpenGlTextureWrapMode.RepeatClamp].Name);

                // alpha test
                renderer.SetAlphaFunc(AlphaFunction.Greater, 0.0f);

                // blend mode
                renderer.DefaultShader.SetOpacity(alpha);

                // render polygon
                VertexArrayObject VAO = (VertexArrayObject)data.VAO;
                VAO.Bind();

                for (int i = 0; i + 9 < 32 * 10; i += 10)
                {
                    VAO.Draw(renderer.DefaultShader.VertexLayout, PrimitiveType.Quads, i, 4);
                    VAO.Draw(renderer.DefaultShader.VertexLayout, PrimitiveType.Triangles, i + 4, 3);
                    VAO.Draw(renderer.DefaultShader.VertexLayout, PrimitiveType.Triangles, i + 7, 3);
                }

                VAO.UnBind();

                GL.BindTexture(TextureTarget.Texture2D, 0);
                renderer.DefaultShader.Deactivate();

                GL.Disable(EnableCap.Texture2D);
                renderer.RestoreBlendFunc();
            }
        }
Пример #3
0
        private void DrawImmediate(string text, OpenGlFont font, double left, double top, Color128 color)
        {
            /*
             * Render the string.
             * */
            GL.Enable(EnableCap.Texture2D);

            GL.MatrixMode(MatrixMode.Projection);
            GL.PushMatrix();
            unsafe
            {
                fixed(double *matrixPointer = &renderer.CurrentProjectionMatrix.Row0.X)
                {
                    GL.LoadMatrix(matrixPointer);
                }

                GL.MatrixMode(MatrixMode.Modelview);
                GL.PushMatrix();
                fixed(double *matrixPointer = &renderer.CurrentViewMatrix.Row0.X)
                {
                    GL.LoadMatrix(matrixPointer);
                }
            }


            for (int i = 0; i < text.Length; i++)
            {
                Texture        texture;
                OpenGlFontChar data;
                i += font.GetCharacterData(text, i, out texture, out data) - 1;

                if (renderer.currentHost.LoadTexture(texture, OpenGlTextureWrapMode.ClampClamp))
                {
                    GL.BindTexture(TextureTarget.Texture2D, texture.OpenGlTextures[(int)OpenGlTextureWrapMode.ClampClamp].Name);

                    double x = left - (data.PhysicalSize.X - data.TypographicSize.X) / 2;
                    double y = top - (data.PhysicalSize.Y - data.TypographicSize.Y) / 2;

                    /*
                     * In the first pass, mask off the background with pure black.
                     * */
                    GL.BlendFunc(BlendingFactor.Zero, BlendingFactor.OneMinusSrcColor);
                    GL.Begin(PrimitiveType.Quads);
                    GL.Color4(color.A, color.A, color.A, 1.0f);
                    GL.TexCoord2(data.TextureCoordinates.X, data.TextureCoordinates.Y);
                    GL.Vertex2(x, y);
                    GL.TexCoord2(data.TextureCoordinates.X + data.TextureCoordinates.Z, data.TextureCoordinates.Y);
                    GL.Vertex2(x + data.PhysicalSize.X, y);
                    GL.TexCoord2(data.TextureCoordinates.X + data.TextureCoordinates.Z, data.TextureCoordinates.Y + data.TextureCoordinates.W);
                    GL.Vertex2(x + data.PhysicalSize.X, y + data.PhysicalSize.Y);
                    GL.TexCoord2(data.TextureCoordinates.X, data.TextureCoordinates.Y + data.TextureCoordinates.W);
                    GL.Vertex2(x, y + data.PhysicalSize.Y);
                    GL.End();

                    /*
                     * In the second pass, add the character onto the background.
                     * */
                    GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.One);
                    GL.Begin(PrimitiveType.Quads);
                    GL.Color4(color.R, color.G, color.B, color.A);
                    GL.TexCoord2(data.TextureCoordinates.X, data.TextureCoordinates.Y);
                    GL.Vertex2(x, y);
                    GL.TexCoord2(data.TextureCoordinates.X + data.TextureCoordinates.Z, data.TextureCoordinates.Y);
                    GL.Vertex2(x + data.PhysicalSize.X, y);
                    GL.TexCoord2(data.TextureCoordinates.X + data.TextureCoordinates.Z, data.TextureCoordinates.Y + data.TextureCoordinates.W);
                    GL.Vertex2(x + data.PhysicalSize.X, y + data.PhysicalSize.Y);
                    GL.TexCoord2(data.TextureCoordinates.X, data.TextureCoordinates.Y + data.TextureCoordinates.W);
                    GL.Vertex2(x, y + data.PhysicalSize.Y);
                    GL.End();
                }

                left += data.TypographicSize.X;
            }

            renderer.RestoreBlendFunc();
            GL.Disable(EnableCap.Texture2D);

            GL.PopMatrix();

            GL.MatrixMode(MatrixMode.Projection);
            GL.PopMatrix();
        }
Пример #4
0
        /// <summary>Renders a string to the screen.</summary>
        /// <param name="font">The font to use.</param>
        /// <param name="text">The string to render.</param>
        /// <param name="location">The location.</param>
        /// <param name="alignment">The alignment.</param>
        /// <param name="color">The color.</param>
        /// <remarks>This function sets the OpenGL blend function to glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE_MINUS_SRC_ALPHA).</remarks>
        public void Draw(OpenGlFont font, string text, Point location, TextAlignment alignment, Color128 color)
        {
            if (text == null || font == null)
            {
                return;
            }
            renderer.LastBoundTexture = null;

            /*
             * Prepare the top-left coordinates for rendering, incorporating the
             * orientation of the string in relation to the specified location.
             * */
            int left;

            if ((alignment & TextAlignment.Left) == 0)
            {
                int width = 0;

                for (int i = 0; i < text.Length; i++)
                {
                    Texture        texture;
                    OpenGlFontChar data;
                    i     += font.GetCharacterData(text, i, out texture, out data) - 1;
                    width += data.TypographicSize.Width;
                }

                if ((alignment & TextAlignment.Right) != 0)
                {
                    left = location.X - width;
                }
                else
                {
                    left = location.X - width / 2;
                }
            }
            else
            {
                left = location.X;
            }

            int top;

            if ((alignment & TextAlignment.Top) == 0)
            {
                int height = 0;

                for (int i = 0; i < text.Length; i++)
                {
                    Texture        texture;
                    OpenGlFontChar data;
                    i += font.GetCharacterData(text, i, out texture, out data) - 1;

                    if (data.TypographicSize.Height > height)
                    {
                        height = data.TypographicSize.Height;
                    }
                }

                if ((alignment & TextAlignment.Bottom) != 0)
                {
                    top = location.Y - height;
                }
                else
                {
                    top = location.Y - height / 2;
                }
            }
            else
            {
                top = location.Y;
            }

            /*
             * Render the string.
             * */
            GL.Enable(EnableCap.Texture2D);

            GL.MatrixMode(MatrixMode.Projection);
            GL.PushMatrix();
            unsafe
            {
                fixed(double *matrixPointer = &renderer.CurrentProjectionMatrix.Row0.X)
                {
                    GL.LoadMatrix(matrixPointer);
                }

                GL.MatrixMode(MatrixMode.Modelview);
                GL.PushMatrix();
                fixed(double *matrixPointer = &renderer.CurrentViewMatrix.Row0.X)
                {
                    GL.LoadMatrix(matrixPointer);
                }
            }


            for (int i = 0; i < text.Length; i++)
            {
                Texture        texture;
                OpenGlFontChar data;
                i += font.GetCharacterData(text, i, out texture, out data) - 1;

                if (renderer.currentHost.LoadTexture(texture, OpenGlTextureWrapMode.ClampClamp))
                {
                    GL.BindTexture(TextureTarget.Texture2D, texture.OpenGlTextures[(int)OpenGlTextureWrapMode.ClampClamp].Name);

                    int x = left - (data.PhysicalSize.Width - data.TypographicSize.Width) / 2;
                    int y = top - (data.PhysicalSize.Height - data.TypographicSize.Height) / 2;

                    /*
                     * In the first pass, mask off the background with pure black.
                     * */
                    GL.BlendFunc(BlendingFactor.Zero, BlendingFactor.OneMinusSrcColor);
                    GL.Begin(PrimitiveType.Quads);
                    GL.Color4(color.A, color.A, color.A, 1.0f);
                    GL.TexCoord2(data.TextureCoordinates.Left, data.TextureCoordinates.Top);
                    GL.Vertex2(x, y);
                    GL.TexCoord2(data.TextureCoordinates.Right, data.TextureCoordinates.Top);
                    GL.Vertex2(x + data.PhysicalSize.Width, y);
                    GL.TexCoord2(data.TextureCoordinates.Right, data.TextureCoordinates.Bottom);
                    GL.Vertex2(x + data.PhysicalSize.Width, y + data.PhysicalSize.Height);
                    GL.TexCoord2(data.TextureCoordinates.Left, data.TextureCoordinates.Bottom);
                    GL.Vertex2(x, y + data.PhysicalSize.Height);
                    GL.End();

                    /*
                     * In the second pass, add the character onto the background.
                     * */
                    GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.One);
                    GL.Begin(PrimitiveType.Quads);
                    GL.Color4(color.R, color.G, color.B, color.A);
                    GL.TexCoord2(data.TextureCoordinates.Left, data.TextureCoordinates.Top);
                    GL.Vertex2(x, y);
                    GL.TexCoord2(data.TextureCoordinates.Right, data.TextureCoordinates.Top);
                    GL.Vertex2(x + data.PhysicalSize.Width, y);
                    GL.TexCoord2(data.TextureCoordinates.Right, data.TextureCoordinates.Bottom);
                    GL.Vertex2(x + data.PhysicalSize.Width, y + data.PhysicalSize.Height);
                    GL.TexCoord2(data.TextureCoordinates.Left, data.TextureCoordinates.Bottom);
                    GL.Vertex2(x, y + data.PhysicalSize.Height);
                    GL.End();
                }

                left += data.TypographicSize.Width;
            }

            renderer.RestoreBlendFunc();
            GL.Disable(EnableCap.Texture2D);

            GL.PopMatrix();

            GL.MatrixMode(MatrixMode.Projection);
            GL.PopMatrix();
        }