Ejemplo n.º 1
0
        //
        // SETUP GRAPHICS
        // Sets up the transforms for the 2d render and setup the basic effect
        //
        public PrimitiveRenderer(Platform platform)
        {
            var shaderDecl =
                new ShaderDeclaration {
                    Name = "Primitive Batch Shader",
                    InputDeclarations = new List<ShaderInputDeclaration> {
                        new ShaderInputDeclaration {
                            Name = "a_vertPosition",
                            NiceName = "Position",
                            Optional = false,
                            Usage = VertexElementUsage.Position,
                            DefaultValue = Vector3.Zero
                        },
                        new ShaderInputDeclaration {
                            Name = "a_vertTexcoord",
                            NiceName = "TextureCoordinate",
                            Optional = false,
                            Usage = VertexElementUsage.TextureCoordinate,
                            DefaultValue = Vector2.Zero
                        },
                        new ShaderInputDeclaration {
                            Name = "a_vertColour",
                            NiceName = "Colour",
                            Optional = false,
                            Usage = VertexElementUsage.Colour,
                            DefaultValue = Rgba32.White
                        }
                    },
                    VariableDeclarations = new List<ShaderVariableDeclaration> {
                        new ShaderVariableDeclaration {
                            Name = "u_view",
                            NiceName = "View",
                            DefaultValue = Matrix44.Identity
                        },
                        new ShaderVariableDeclaration {
                            Name = "u_proj",
                            NiceName = "Projection",
                            DefaultValue = Matrix44.Identity
                        }
                    },
                    SamplerDeclarations = new List<ShaderSamplerDeclaration> {
                        new ShaderSamplerDeclaration {
                            Name = "s_tex0",
                            NiceName = "TextureSampler",
                            Optional = true
                        }
                    }
                };

                String source = "";

                var runtimeShaderFormat = platform.Graphics.GetRuntimeShaderFormat ();

                if (runtimeShaderFormat == ShaderFormat.HLSL)
                {
                    throw new NotImplementedException ();
                }
                else if (runtimeShaderFormat == ShaderFormat.GLSL)
                {
                    source =
            @"Primitive Batch Shader
            =VSH=
            attribute vec4 a_vertPosition;
            attribute vec2 a_vertTexcoord;
            attribute vec4 a_vertColour;
            uniform mat4 u_view;
            uniform mat4 u_proj;
            varying vec2 v_texCoord;
            varying vec4 v_tint;
            void main()
            {
            gl_Position = u_proj * u_view * a_vertPosition;
            v_texCoord = a_vertTexcoord;
            v_tint = a_vertColour;
            }
            =FSH=
            uniform sampler2D s_tex0;
            varying vec2 v_texCoord;
            varying vec4 v_tint;
            void main()
            {
            vec4 a = texture2D(s_tex0, v_texCoord);
            gl_FragColor = v_tint * a;
            }
            ";
            }
            else if (runtimeShaderFormat == ShaderFormat.GLSL_ES)
            {
                source =
            @"Primitive Batch Shader
            =VSH=
            attribute mediump vec4 a_vertPosition;
            attribute mediump vec2 a_vertTexcoord;
            attribute mediump vec4 a_vertColour;
            uniform mediump mat4 u_view;
            uniform mediump mat4 u_proj;
            varying mediump vec2 v_texCoord;
            varying mediump vec4 v_tint;
            void main()
            {
            gl_Position = u_proj * u_view * a_vertPosition;
            v_texCoord = a_vertTexcoord;
            v_tint = a_vertColour;
            }
            =FSH=
            uniform mediump sampler2D s_tex0;
            varying mediump vec2 v_texCoord;
            varying mediump vec4 v_tint;
            void main()
            {
            mediump vec4 a = texture2D(s_tex0, v_texCoord);
            gl_FragColor = v_tint * a;
            }
            ";
            }
            else throw new NotSupportedException ();

            Byte[] shaderUTF8 = System.Text.Encoding.UTF8.GetBytes (source);

            using (var mem = new System.IO.MemoryStream ())
            {
                using (var bin = new System.IO.BinaryWriter (mem))
                {
                    bin.Write ((Byte)1);
                    bin.Write (shaderUTF8.Length);
                    bin.Write (shaderUTF8);
                }

                shader = platform.Graphics.CreateShader (
                    shaderDecl,
                    runtimeShaderFormat,
                    mem.GetBuffer ());
            }

            // Set the index buffer for each vertex, using
            // clockwise winding
            for (int i = 0, vertex = 0; i < quadIndices.Length; i += 6, vertex += 4)
            {
                quadIndices[i + 0] = vertex + 0;
                quadIndices[i + 1] = vertex + 1;
                quadIndices[i + 2] = vertex + 2;
                quadIndices[i + 3] = vertex + 0;
                quadIndices[i + 4] = vertex + 2;
                quadIndices[i + 5] = vertex + 3;
            }
        }
Ejemplo n.º 2
0
        static Shader CreateShader(Platform platform)
        {
            var shaderDecl =
                new ShaderDeclaration {
                Name = "Primitive Batch Shader",
                InputDeclarations = new List<ShaderInputDeclaration> {
                    new ShaderInputDeclaration {
                        Name = "a_vertPosition",
                        NiceName = "Position",
                        Optional = false,
                        Usage = VertexElementUsage.Position,
                        DefaultValue = Vector3.Zero
                    },
                    new ShaderInputDeclaration {
                        Name = "a_vertColour",
                        NiceName = "Colour",
                        Optional = false,
                        Usage = VertexElementUsage.Colour,
                        DefaultValue = Rgba32.White
                    }
                },
                VariableDeclarations = new List<ShaderVariableDeclaration> {
                    new ShaderVariableDeclaration {
                        Name = "u_view",
                        NiceName = "View",
                        DefaultValue = Matrix44.Identity
                    },
                    new ShaderVariableDeclaration {
                        Name = "u_proj",
                        NiceName = "Projection",
                        DefaultValue = Matrix44.Identity
                    }
                },
                SamplerDeclarations = new List<ShaderSamplerDeclaration> ()
            };

            var runtimeShaderFormat = platform.Graphics.GetRuntimeShaderFormat ();

            String source = "";

            if (runtimeShaderFormat == ShaderFormat.HLSL)
            {
                throw new NotImplementedException ();
            }
            else if (runtimeShaderFormat == ShaderFormat.GLSL)
            {
                source =
            @"Debug Shader
            =VSH=
            attribute vec4 a_vertPosition;
            attribute vec4 a_vertColour;
            uniform mat4 u_view;
            uniform mat4 u_proj;
            varying vec4 v_tint;
            void main()
            {
            gl_Position = u_proj * u_view * a_vertPosition;
            gl_Position = u_proj * u_view * a_vertPosition;
            v_tint = a_vertColour;
            }
            =FSH=
            varying vec4 v_tint;
            void main()
            {
            gl_FragColor = v_tint;
            }
            ";
            }
            else if (runtimeShaderFormat == ShaderFormat.GLSL_ES)
            {
                source =
            @"Debug Shader
            =VSH=
            attribute mediump vec4 a_vertPosition;
            attribute mediump vec4 a_vertColour;
            uniform mediump mat4 u_view;
            uniform mediump mat4 u_proj;
            varying mediump vec4 v_tint;
            void main()
            {
            gl_Position = u_proj * u_view * a_vertPosition;
            v_tint = a_vertColour;
            }
            =FSH=
            varying mediump vec4 v_tint;
            void main()
            {
            gl_FragColor = v_tint;
            }
            ";
            }
            else throw new NotSupportedException ();

            Byte[] shaderUTF8 = System.Text.Encoding.UTF8.GetBytes (source);

            using (var mem = new System.IO.MemoryStream ())
            {
                using (var bin = new System.IO.BinaryWriter (mem))
                {
                    bin.Write ((Byte)1);
                    bin.Write (shaderUTF8.Length);
                    bin.Write (shaderUTF8);
                }

                return platform.Graphics.CreateShader (
                    shaderDecl,
                    runtimeShaderFormat,
                    mem.GetBuffer ());
            }

            return null;
        }