예제 #1
0
 private void UpdateBuffer()
 {
     if (BackColor)
     {
         for (int i = 0; i < drawCount; i++)
         {
             Vector2 uv  = TiledTexture.GetUV(tiles[i]);
             int     i10 = i * 10;
             tileVertices[i10]     = uv[0];
             tileVertices[i10 + 1] = uv[1];
             tileVertices[i10 + 2] = colors[i][0];
             tileVertices[i10 + 3] = colors[i][1];
             tileVertices[i10 + 4] = colors[i][2];
             tileVertices[i10 + 5] = colors[i][3];
             tileVertices[i10 + 6] = backColors[i][0];
             tileVertices[i10 + 7] = backColors[i][1];
             tileVertices[i10 + 8] = backColors[i][2];
             tileVertices[i10 + 9] = backColors[i][3];
         }
     }
     else
     {
         for (int i = 0; i < drawCount; i++)
         {
             Vector2 uv  = TiledTexture.GetUV(tiles[i]);
             int     i10 = i * 6;
             tileVertices[i10]     = uv[0];
             tileVertices[i10 + 1] = uv[1];
             tileVertices[i10 + 2] = colors[i][0];
             tileVertices[i10 + 3] = colors[i][1];
             tileVertices[i10 + 4] = colors[i][2];
             tileVertices[i10 + 5] = colors[i][3];
         }
     }
     GLStates.BindBuffer(BufferTarget.ArrayBuffer, tileBO);
     GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(tileVertices.Length * sizeof(float)), tileVertices, BufferUsageHint.DynamicDraw);
 }
예제 #2
0
        private void UpdateBuffer()
        {
            for (int i = 0; i < Sprites.Count; i++)
            {
                int     i2 = i * 13;
                Vector2 uv = TiledTexture.GetUV(Sprites[i].tile);

                //sprite_position
                bufferObject.Data[i2]     = Sprites[i].position.X;
                bufferObject.Data[i2 + 1] = Sprites[i].position.Y;

                //Size
                bufferObject.Data[i2 + 2] = Sprites[i].size.X;
                bufferObject.Data[i2 + 3] = Sprites[i].size.Y;

                //Texture
                bufferObject.Data[i2 + 4] = uv.X;
                bufferObject.Data[i2 + 5] = uv.Y;

                //Scale
                bufferObject.Data[i2 + 6] = Sprites[i].scale.X;
                bufferObject.Data[i2 + 7] = Sprites[i].scale.Y;

                //Rotation
                bufferObject.Data[i2 + 8] = Sprites[i].rotation;

                //Color
                bufferObject.Data[i2 + 9]  = Sprites[i].color[0];
                bufferObject.Data[i2 + 10] = Sprites[i].color[1];
                bufferObject.Data[i2 + 11] = Sprites[i].color[2];
                bufferObject.Data[i2 + 12] = Sprites[i].color[3];
            }

            GLStates.BindBuffer(BufferTarget.ArrayBuffer, bufferObject.Id);
            GL.BufferSubData(BufferTarget.ArrayBuffer, (IntPtr)0, (IntPtr)(Sprites.Count * 13 * sizeof(float)), bufferObject.Data);
        }
예제 #3
0
        public TileGrid(TiledTexture tiledTexture, int x, int y, int z, int width, int height, float scaleX, float scaleY, bool backColor)
        {
            Width        = width;
            Height       = height;
            Scale        = new Vector2(scaleX, scaleY);
            drawCount    = width * height;
            TiledTexture = tiledTexture;
            BackColor    = backColor;
            X            = x;
            Y            = y;
            Z            = z;

            //Shader
            program = new Program();
            string vertexShader =
                @"
                in vec2 vertex_position;
                in vec2 tex_position;
                in vec4 vx_color;
                in vec4 vx_back_color;
                varying vec4 fg_color;
                varying vec2 tex_coord;
                uniform mat4 projection;
                uniform vec2 offset;
                uniform float grid_width;
                uniform vec2 tile_size;
                uniform mat2 uvTile;
                {0}

                void main()
                {
                    fg_color = vx_color;
                    {1}

                    float a = floor(gl_InstanceID / grid_width);

                    gl_Position = projection * 

                    mat4(vec4(tile_size.x, 0.0, 0.0, 0.0),
                         vec4(0.0, tile_size.y, 0.0, 0.0),
                         vec4(0.0, 0.0, 1.0, 0.0),
                         vec4((gl_InstanceID - (grid_width * a)) * tile_size.x, a * tile_size.y, 0.0, 1.0)) *

                    vec4(vertex_position + offset, 0.0, 1.0);

                    tex_coord = tex_position + (vertex_position * uvTile);
                }
                ";

            string fragShader =
                @"
                varying vec2 tex_coord;
                varying vec4 fg_color;
                uniform sampler2D tex_sampler;
                {0}

                void main()
                {
                    gl_FragColor = texture2D(tex_sampler, tex_coord).bgra;
                    {1}
                }
                ";

            if (BackColor)
            {
                vertexShader = vertexShader.Replace("{0}", "varying vec4 fg_back_color;");
                vertexShader = vertexShader.Replace("{1}", "fg_back_color = vx_back_color;");
                fragShader   = fragShader.Replace("{0}", "varying vec4 fg_back_color; uniform vec4 background;");
                fragShader   = fragShader.Replace("{1}", "if (gl_FragColor.rgb == background.rgb) gl_FragColor = fg_back_color; else gl_FragColor *= fg_color;");
            }
            else
            {
                vertexShader = vertexShader.Replace("{0}", string.Empty);
                vertexShader = vertexShader.Replace("{1}", string.Empty);
                fragShader   = fragShader.Replace("{0}", string.Empty);
                fragShader   = fragShader.Replace("{1}", "gl_FragColor *= fg_color;");
            }

            program.AddShader(ShaderType.VertexShader, vertexShader);
            program.AddShader(ShaderType.FragmentShader, fragShader);

            program.SetVariable("grid_width", (float)width);
            program.SetVariable("offset", new Vector2(X, Y));
            program.SetVariable("tile_size", tiledTexture.TileSize * Scale);
            program.SetVariable("uvTile", new Matrix2(tiledTexture.uTile, 0, 0, tiledTexture.vTile));
            program.SetVariable("projection", Projection.Matrix);
            program.SetVariable("tex_sampler", 0);

            if (BackColor)
            {
                program.SetVariable("background", tiledTexture.Background);
            }

            Projection.ProjectionChangeEvent += Projection_ProjectionChangeEvent;

            //Tile and Colors
            tiles      = new ushort[drawCount];
            colors     = new Vector4[drawCount];
            backColors = new Vector4[drawCount];

            if (BackColor)
            {
                tileVertices = new float[drawCount * 10];
            }
            else
            {
                tileVertices = new float[drawCount * 6];
            }

            tileBO = GL.GenBuffer();
            GLStates.BindBuffer(BufferTarget.ArrayBuffer, tileBO);
            GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(tileVertices.Length * sizeof(float)), IntPtr.Zero, BufferUsageHint.DynamicDraw);
        }
예제 #4
0
        public SpriteBatch(TiledTexture tiledTexture, int x, int y, int z, float scaleX, float scaleY)
        {
            bufferObject = new BufferObject();
            Scale = new Vector2(scaleX, scaleY);
            Sprites = new List<Sprite>();
            TiledTexture = tiledTexture;
            X = x;
            Y = y;
            Z = z;

            //Shader
            program = new Program();
            string vertexShader =
            @"
                in vec2 vertex_position;
                in vec2 sprite_position;
                in vec2 tex_position;
                in vec4 vx_color;
                in float rotation;
                in vec2 scale;
                in vec2 size;
                varying vec4 color;
                varying vec2 tex_coord;
                uniform mat4 projection;
                uniform vec2 offset;
                uniform vec2 tile_size;
                uniform mat2 uvTile;

                void main()
                {
                    color = vx_color;

                    float calc_cos = cos(rotation);
                    float calc_sin = sin(rotation);
                    vec2 size_total = size * tile_size * scale;

                    gl_Position =  projection *

                    //Object Translate
                    mat4(
                        vec4(1.0, 0.0, 0.0, 0.0),
                        vec4(0.0, 1.0, 0.0, 0.0),
                        vec4(0.0, 0.0, 1.0, 0.0),
                        vec4(offset + (sprite_position * tile_size) + (size_total * 0.5), 0.0, 1.0)) *

                    //Rotation
                    mat4(
                        vec4(calc_cos, calc_sin, 0.0, 0.0),
                        vec4(-calc_sin, calc_cos, 0.0, 0.0),
                        vec4(0.0, 0.0, 1.0, 0.0),
                        vec4(0.0, 0.0, 0.0, 1.0)) *

                    //Rotation Origin Translate
                    mat4(
                        vec4(1.0, 0.0, 0.0, 0.0),
                        vec4(0.0, 1.0, 0.0, 0.0),
                        vec4(0.0, 0.0, 1.0, 0.0),
                        vec4(size_total * -0.5, 0.0, 1.0)) *

                    //Scale
                    mat4(
                        vec4(size_total.x, 0.0, 0.0, 0.0),
                        vec4(0.0, size_total.y, 0.0, 0.0),
                        vec4(0.0, 0.0, 1.0, 0.0),
                        vec4(0.0, 0.0, 0.0, 1.0)) *

                    vec4(vertex_position, 0.0, 1.0);

                    tex_coord = tex_position + (vertex_position * uvTile * size);
                }
            ";

            string fragShader =
                @"
                varying vec2 tex_coord;
                varying vec4 color;
                uniform sampler2D tex_sampler;

                void main()
                {
                    gl_FragColor = texture2D(tex_sampler, tex_coord).bgra * color;
                }
                ";

            program.AddShader(ShaderType.VertexShader, vertexShader);
            program.AddShader(ShaderType.FragmentShader, fragShader);

            program.SetVariable("offset", new Vector2(X, Y));
            program.SetVariable("tile_size", new Vector2((float)tiledTexture.TileWidth, (float)tiledTexture.TileWidth) * Scale);
            program.SetVariable("uvTile", new Matrix2(tiledTexture.uTile, 0, 0, tiledTexture.vTile));
            program.SetVariable("projection", Projection.Matrix);
            program.SetVariable("tex_sampler", 0);

            Projection.ProjectionChangeEvent += Projection_ProjectionChangeEvent;
        }
예제 #5
0
        public SpriteBatch(TiledTexture tiledTexture, int x, int y, int z, float scaleX, float scaleY)
        {
            bufferObject = new BufferObject();
            Scale        = new Vector2(scaleX, scaleY);
            Sprites      = new List <Sprite>();
            TiledTexture = tiledTexture;
            X            = x;
            Y            = y;
            Z            = z;

            //Shader
            program = new Program();
            string vertexShader =
                @"
                in vec2 vertex_position;
                in vec2 sprite_position;
                in vec2 tex_position;
                in vec4 vx_color;
                in float rotation;
                in vec2 scale;
                in vec2 size;
                varying vec4 color;
                varying vec2 tex_coord;
                uniform mat4 projection;
                uniform vec2 offset;
                uniform vec2 tile_size;
                uniform mat2 uvTile;

                void main()
                {
                    color = vx_color;

                    float calc_cos = cos(rotation);
                    float calc_sin = sin(rotation);
                    vec2 size_total = size * tile_size * scale;

                    gl_Position =  projection *

                    //Object Translate
                    mat4(
                        vec4(1.0, 0.0, 0.0, 0.0), 
                        vec4(0.0, 1.0, 0.0, 0.0), 
                        vec4(0.0, 0.0, 1.0, 0.0), 
                        vec4(offset + (sprite_position * tile_size) + (size_total * 0.5), 0.0, 1.0)) *

                    //Rotation
                    mat4(
                        vec4(calc_cos, calc_sin, 0.0, 0.0), 
                        vec4(-calc_sin, calc_cos, 0.0, 0.0), 
                        vec4(0.0, 0.0, 1.0, 0.0), 
                        vec4(0.0, 0.0, 0.0, 1.0)) *

                    //Rotation Origin Translate
                    mat4(
                        vec4(1.0, 0.0, 0.0, 0.0), 
                        vec4(0.0, 1.0, 0.0, 0.0), 
                        vec4(0.0, 0.0, 1.0, 0.0), 
                        vec4(size_total * -0.5, 0.0, 1.0)) *

                    //Scale
                    mat4(
                        vec4(size_total.x, 0.0, 0.0, 0.0), 
                        vec4(0.0, size_total.y, 0.0, 0.0), 
                        vec4(0.0, 0.0, 1.0, 0.0), 
                        vec4(0.0, 0.0, 0.0, 1.0)) *

                    vec4(vertex_position, 0.0, 1.0);

                    tex_coord = tex_position + (vertex_position * uvTile * size);
                }
            ";

            string fragShader =
                @"
                varying vec2 tex_coord;
                varying vec4 color;
                uniform sampler2D tex_sampler;

                void main()
                {
                    gl_FragColor = texture2D(tex_sampler, tex_coord).bgra * color;
                }
                ";

            program.AddShader(ShaderType.VertexShader, vertexShader);
            program.AddShader(ShaderType.FragmentShader, fragShader);

            program.SetVariable("offset", new Vector2(X, Y));
            program.SetVariable("tile_size", new Vector2((float)tiledTexture.TileWidth, (float)tiledTexture.TileWidth) * Scale);
            program.SetVariable("uvTile", new Matrix2(tiledTexture.uTile, 0, 0, tiledTexture.vTile));
            program.SetVariable("projection", Projection.Matrix);
            program.SetVariable("tex_sampler", 0);

            Projection.ProjectionChangeEvent += Projection_ProjectionChangeEvent;
        }
예제 #6
0
        public TileGrid(TiledTexture tiledTexture, int x, int y, int z, int width, int height, float scaleX, float scaleY, bool backColor)
        {
            Width = width;
            Height = height;
            Scale = new Vector2(scaleX, scaleY);
            drawCount = width * height;
            TiledTexture = tiledTexture;
            BackColor = backColor;
            X = x;
            Y = y;
            Z = z;

            //Shader
            program = new Program();
            string vertexShader =
                @"
                in vec2 vertex_position;
                in vec2 tex_position;
                in vec4 vx_color;
                in vec4 vx_back_color;
                varying vec4 fg_color;
                varying vec2 tex_coord;
                uniform mat4 projection;
                uniform vec2 offset;
                uniform float grid_width;
                uniform vec2 tile_size;
                uniform mat2 uvTile;
                {0}

                void main()
                {
                    fg_color = vx_color;
                    {1}

                    float a = floor(gl_InstanceID / grid_width);

                    gl_Position = projection *

                    mat4(vec4(tile_size.x, 0.0, 0.0, 0.0),
                         vec4(0.0, tile_size.y, 0.0, 0.0),
                         vec4(0.0, 0.0, 1.0, 0.0),
                         vec4((gl_InstanceID - (grid_width * a)) * tile_size.x, a * tile_size.y, 0.0, 1.0)) *

                    vec4(vertex_position + offset, 0.0, 1.0);

                    tex_coord = tex_position + (vertex_position * uvTile);
                }
                ";

            string fragShader =
                @"
                varying vec2 tex_coord;
                varying vec4 fg_color;
                uniform sampler2D tex_sampler;
                {0}

                void main()
                {
                    gl_FragColor = texture2D(tex_sampler, tex_coord).bgra;
                    {1}
                }
                ";

            if (BackColor)
            {
                vertexShader = vertexShader.Replace("{0}", "varying vec4 fg_back_color;");
                vertexShader = vertexShader.Replace("{1}", "fg_back_color = vx_back_color;");
                fragShader = fragShader.Replace("{0}", "varying vec4 fg_back_color; uniform vec4 background;");
                fragShader = fragShader.Replace("{1}", "if (gl_FragColor.rgb == background.rgb) gl_FragColor = fg_back_color; else gl_FragColor *= fg_color;");
            }
            else
            {
                vertexShader = vertexShader.Replace("{0}", string.Empty);
                vertexShader = vertexShader.Replace("{1}", string.Empty);
                fragShader = fragShader.Replace("{0}", string.Empty);
                fragShader = fragShader.Replace("{1}", "gl_FragColor *= fg_color;");
            }

            program.AddShader(ShaderType.VertexShader, vertexShader);
            program.AddShader(ShaderType.FragmentShader, fragShader);

            program.SetVariable("grid_width", (float)width);
            program.SetVariable("offset", new Vector2(X, Y));
            program.SetVariable("tile_size", tiledTexture.TileSize * Scale);
            program.SetVariable("uvTile", new Matrix2(tiledTexture.uTile, 0, 0, tiledTexture.vTile));
            program.SetVariable("projection", Projection.Matrix);
            program.SetVariable("tex_sampler", 0);

            if (BackColor) program.SetVariable("background", tiledTexture.Background);

            Projection.ProjectionChangeEvent += Projection_ProjectionChangeEvent;

            //Tile and Colors
            tiles = new ushort[drawCount];
            colors = new Vector4[drawCount];
            backColors = new Vector4[drawCount];

            if (BackColor) tileVertices = new float[drawCount * 10];
            else tileVertices = new float[drawCount * 6];

            tileBO = GL.GenBuffer();
            GLStates.BindBuffer(BufferTarget.ArrayBuffer, tileBO);
            GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(tileVertices.Length * sizeof(float)), IntPtr.Zero, BufferUsageHint.DynamicDraw);
        }