Ejemplo n.º 1
0
        public SpriteBatch()
        {
            var accessor       = Properties.Access();
            var bufferCapacity = accessor.GetInt("sprite.batch.buffer.capacity");
            var indexCapacity  = accessor.GetInt("sprite.batch.index.capacity");

            buffer = new PrimitiveBuffer(bufferCapacity, indexCapacity);

            GLUtilities.AllocateBuffers(bufferCapacity, indexCapacity, out bufferId, out indexId, GL_DYNAMIC_DRAW);

            // These two shaders (owned by the sprite batch) can be completed here (in terms of binding a buffer).
            // External shaders are bound when first applied.
            spriteShader = new Shader(bufferId, indexId);
            spriteShader.Attach(ShaderTypes.Vertex, "Sprite.vert");
            spriteShader.Attach(ShaderTypes.Fragment, "Sprite.frag");
            spriteShader.AddAttribute <float>(2, GL_FLOAT);
            spriteShader.AddAttribute <float>(2, GL_FLOAT);
            spriteShader.AddAttribute <byte>(4, GL_UNSIGNED_BYTE, ShaderAttributeFlags.IsNormalized);
            spriteShader.Initialize();

            primitiveShader = new Shader(bufferId, indexId);
            primitiveShader.Attach(ShaderTypes.Vertex, "Primitives2D.vert");
            primitiveShader.Attach(ShaderTypes.Fragment, "Primitives.frag");
            primitiveShader.AddAttribute <float>(2, GL_FLOAT);
            primitiveShader.AddAttribute <byte>(4, GL_UNSIGNED_BYTE, ShaderAttributeFlags.IsNormalized);
            primitiveShader.Initialize();
        }
Ejemplo n.º 2
0
        public SpriteBatch()
        {
            const int BufferCapacity = 30000;
            const int IndexCapacity  = 3000;

            buffer = new PrimitiveBuffer(BufferCapacity, IndexCapacity);

            GLUtilities.AllocateBuffers(BufferCapacity, IndexCapacity, out bufferId, out indexBufferId,
                                        GL_DYNAMIC_DRAW);

            // These two shaders (owned by the sprite batch) can be completed here (in terms of binding a buffer).
            // External shaders are bound when first applied.
            spriteShader = new Shader();
            spriteShader.Attach(ShaderTypes.Vertex, "Sprite.vert");
            spriteShader.Attach(ShaderTypes.Fragment, "Sprite.frag");
            spriteShader.AddAttribute <float>(2, GL_FLOAT);
            spriteShader.AddAttribute <float>(2, GL_FLOAT);
            spriteShader.AddAttribute <byte>(4, GL_UNSIGNED_BYTE, false, true);
            spriteShader.CreateProgram();
            spriteShader.Bind(bufferId, indexBufferId);

            primitiveShader = new Shader();
            primitiveShader.Attach(ShaderTypes.Vertex, "Primitives2D.vert");
            primitiveShader.Attach(ShaderTypes.Fragment, "Primitives.frag");
            primitiveShader.CreateProgram();
            primitiveShader.AddAttribute <float>(2, GL_FLOAT);
            primitiveShader.AddAttribute <byte>(4, GL_UNSIGNED_BYTE, false, true);
            primitiveShader.Bind(bufferId, indexBufferId);

            MessageSystem.Subscribe(this, CoreMessageTypes.ResizeWindow, (messageType, data, dt) => { OnResize(); });
        }
Ejemplo n.º 3
0
        public ModelBatch(int bufferSize, int indexBufferSize)
        {
            const int ShadowMapSize = 2048;

            GLUtilities.AllocateBuffers(bufferSize, indexBufferSize, out bufferId, out indexBufferId, GL_STATIC_DRAW);

            modelShader = new Shader();
            modelShader.Attach(ShaderTypes.Vertex, "ModelShadow.vert");
            modelShader.Attach(ShaderTypes.Fragment, "ModelShadow.frag");
            modelShader.AddAttribute <float>(3, GL_FLOAT);
            modelShader.AddAttribute <float>(2, GL_FLOAT);
            modelShader.AddAttribute <float>(3, GL_FLOAT);
            modelShader.CreateProgram();
            modelShader.Bind(bufferId, indexBufferId);
            modelShader.Use();
            modelShader.SetUniform("shadowSampler", 0);
            modelShader.SetUniform("textureSampler", 1);

            shadowMapShader = new Shader();
            shadowMapShader.Attach(ShaderTypes.Vertex, "ShadowMap.vert");
            shadowMapShader.Attach(ShaderTypes.Fragment, "ShadowMap.frag");
            shadowMapShader.AddAttribute <float>(3, GL_FLOAT, false, false, sizeof(float) * 5);
            shadowMapShader.CreateProgram();
            shadowMapShader.Bind(bufferId, indexBufferId);

            shadowMapTarget = new RenderTarget(ShadowMapSize, ShadowMapSize, RenderTargetFlags.Depth);
            defaultTexture  = ContentCache.GetTexture("Grey.png");
            handles         = new List <ModelHandle>();

            // These default values are arbitrary, just to make sure something shows up.
            LightDirection   = vec3.UnitX;
            LightColor       = Color.White;
            AmbientIntensity = 0.1f;
        }
Ejemplo n.º 4
0
        public FilterCommand(Sprite sprite) : base("filter", "filters")
        {
            const float Gamma = 2.2f;

            this.sprite = sprite;

            currentType = FilterTypes.None;

            // See https://github.com/nvkelso/color-oracle-java/blob/master/src/ika/colororacle/Simulator.java.
            var gammaToLinear = new int[256];
            var linearToGamma = new int[256];

            for (int i = 0; i < gammaToLinear.Length; i++)
            {
                var f = 0.992052f * (float)Math.Pow(i / 255f, Gamma) + 0.003974f;

                gammaToLinear[i] = (int)(f * 32767);
                linearToGamma[i] = (int)(255 * Math.Pow(i / 255f, 1 / Gamma));
            }

            shader = new Shader();
            shader.Attach(ShaderTypes.Vertex, "Sprite.vert");
            shader.Attach(ShaderTypes.Fragment, "Accessibility/Colorblind.frag");
            shader.AddAttribute <float>(2, GL_FLOAT);
            shader.AddAttribute <float>(2, GL_FLOAT);
            shader.AddAttribute <byte>(4, GL_UNSIGNED_BYTE, true);
            shader.Initialize();
            shader.Use();
            shader.SetUniform("gammaToLinear", gammaToLinear);
            shader.SetUniform("linearToGamma", linearToGamma);
        }
Ejemplo n.º 5
0
        public ModelRenderer(GlobalLight light) : base(light, "model")
        {
            shader = new Shader();
            shader.Attach(ShaderTypes.Vertex, "ModelShadow.vert");
            shader.Attach(ShaderTypes.Fragment, "ModelShadow.frag");
            shader.AddAttribute <float>(3, GL_FLOAT);
            shader.AddAttribute <float>(2, GL_FLOAT);
            shader.AddAttribute <float>(3, GL_FLOAT);

            Bind(bufferId, indexId);
        }
Ejemplo n.º 6
0
        public ModelRenderer(Camera3D camera, GlobalLight3D light, uint bufferCapacity = 0, uint indexCapacity = 0) :
            base(camera, light, bufferCapacity, indexCapacity)
        {
            shader = new Shader();
            shader.Attach(ShaderTypes.Vertex, "ModelShadow.vert");
            shader.Attach(ShaderTypes.Fragment, "ModelShadow.frag");
            shader.AddAttribute <float>(3, GL_FLOAT);
            shader.AddAttribute <float>(2, GL_FLOAT);
            shader.AddAttribute <float>(1, GL_FLOAT);
            shader.AddAttribute <float>(3, GL_FLOAT);

            Bind(bufferId, indexId);
        }
Ejemplo n.º 7
0
        public PrimitiveRenderer3D(Camera3D camera = null, uint bufferCapacity = 0, uint indexCapacity = 0)
        {
            this.camera = camera;

            buffer = new PrimitiveBuffer(BufferFrequencies.Static, BufferUsages.Draw, out bufferId, out indexId,
                                         bufferCapacity, indexCapacity);

            defaultShader = new Shader(bufferId, indexId);
            defaultShader.Attach(ShaderTypes.Vertex, "Primitives3D.vert");
            defaultShader.Attach(ShaderTypes.Fragment, "Primitives.frag");
            defaultShader.AddAttribute <float>(3, GL_FLOAT);
            defaultShader.AddAttribute <byte>(4, GL_UNSIGNED_BYTE, true);
            defaultShader.Initialize();
        }
Ejemplo n.º 8
0
        public RaindropTester()
        {
            grid         = new vec3[Width, Height];
            renderTarget = new RenderTarget(Width, Height, RenderTargetFlags.Color);
            texture      = new Texture();

            shader = new Shader();
            shader.Attach(ShaderTypes.Vertex, "Sprite.vert");
            shader.Attach(ShaderTypes.Fragment, "Raindrops.frag");
            shader.AddAttribute <float>(2, GL_FLOAT);
            shader.AddAttribute <float>(2, GL_FLOAT);
            shader.AddAttribute <byte>(4, GL_UNSIGNED_BYTE, ShaderAttributeFlags.IsNormalized);
            shader.Initialize();
        }
Ejemplo n.º 9
0
        public PrimitiveRenderer3D(Camera3D camera, int bufferSize, int indexSize)
        {
            this.camera = camera;

            buffer = new PrimitiveBuffer(bufferSize, indexSize);

            GLUtilities.AllocateBuffers(bufferSize, indexSize, out bufferId, out indexId, GL_DYNAMIC_DRAW);

            shader = new Shader(bufferId, indexId);
            shader.Attach(ShaderTypes.Vertex, "Primitives3D.vert");
            shader.Attach(ShaderTypes.Fragment, "Primitives.frag");
            shader.AddAttribute <float>(3, GL_FLOAT);
            shader.AddAttribute <byte>(4, GL_UNSIGNED_BYTE, ShaderAttributeFlags.IsNormalized);
            shader.Initialize();
        }
Ejemplo n.º 10
0
        public PrimitiveRenderer3D(Camera3D camera)
        {
            const int BufferCapacity = 2048;
            const int IndexCapacity  = 256;

            this.camera = camera;

            buffer = new PrimitiveBuffer(BufferCapacity, IndexCapacity);

            GLUtilities.AllocateBuffers(BufferCapacity, IndexCapacity, out bufferId, out indexBufferId,
                                        GL_DYNAMIC_DRAW);

            primitiveShader = new Shader();
            primitiveShader.Attach(ShaderTypes.Vertex, "Primitives3D.vert");
            primitiveShader.Attach(ShaderTypes.Fragment, "Primitives.frag");
            primitiveShader.CreateProgram();
            primitiveShader.AddAttribute <float>(3, GL_FLOAT);
            primitiveShader.AddAttribute <byte>(4, GL_UNSIGNED_BYTE, true);
            primitiveShader.Bind(bufferId, indexBufferId);
        }
Ejemplo n.º 11
0
        public SkeletonRenderer(GlobalLight light) : base(light, "skeletal")
        {
            shader = new Shader();
            shader.Attach(ShaderTypes.Vertex, "Skeletal.vert");
            shader.Attach(ShaderTypes.Fragment, "ModelShadow.frag");
            shader.AddAttribute <float>(3, GL_FLOAT);
            shader.AddAttribute <float>(2, GL_FLOAT);
            shader.AddAttribute <float>(3, GL_FLOAT);
            shader.AddAttribute <float>(2, GL_FLOAT);
            shader.AddAttribute <short>(2, GL_SHORT, ShaderAttributeFlags.IsInteger);

            shadowShader = new Shader();
            shadowShader.Attach(ShaderTypes.Vertex, "ShadowMapSkeletal.vert");
            shadowShader.Attach(ShaderTypes.Fragment, "ShadowMap.frag");
            shadowShader.Initialize();
            shadowShader.Use();
            shadowShader.SetUniform("image", 0);

            Bind(bufferId, indexId);
        }
Ejemplo n.º 12
0
        public Rain()
        {
            buffer = new GLBuffer(2000, 200);

            shader = new Shader();
            shader.Attach(ShaderTypes.Vertex, "Rain.vert");
            shader.Attach(ShaderTypes.Geometry, "Rain.geom");
            shader.Attach(ShaderTypes.Fragment, "Rain.frag");
            shader.AddAttribute <float>(3, GL_FLOAT);
            shader.Initialize();
        }
Ejemplo n.º 13
0
        public ShadowMapVisualizer(RenderTarget shadowMapTarget)
        {
            Shader shader = new Shader();

            shader.Attach(ShaderTypes.Vertex, "Sprite.vert");
            shader.Attach(ShaderTypes.Fragment, "ShadowMapVisualization.frag");
            shader.AddAttribute <float>(2, GL_FLOAT);
            shader.AddAttribute <float>(2, GL_FLOAT);
            shader.AddAttribute <byte>(4, GL_UNSIGNED_BYTE, true);
            shader.CreateProgram();

            sprite        = new Sprite(shadowMapTarget, null, Alignments.Left | Alignments.Bottom);
            sprite.Shader = shader;

            DisplaySize = DefaultSize;

            MessageSystem.Subscribe(this, CoreMessageTypes.ResizeWindow, (messageType, data, dt) =>
            {
                sprite.Position = new vec2(0, Resolution.WindowHeight);
            });
        }
Ejemplo n.º 14
0
        public SpriteBatch(uint bufferCapacity = 0, uint indexCapacity = 0)
        {
            buffer = new PrimitiveBuffer(BufferFrequencies.Dynamic, BufferUsages.Draw, out var bufferId,
                                         out var indexId, bufferCapacity, indexCapacity);

            // These two shaders (owned by the sprite batch) can be completed here (in terms of binding a buffer).
            // External shaders are bound when first applied.
            spriteShader = new Shader(bufferId, indexId);
            spriteShader.Attach(ShaderTypes.Vertex, "Sprite.vert");
            spriteShader.Attach(ShaderTypes.Fragment, "Sprite.frag");
            spriteShader.AddAttribute <float>(2, GL_FLOAT);
            spriteShader.AddAttribute <float>(2, GL_FLOAT);
            spriteShader.AddAttribute <byte>(4, GL_UNSIGNED_BYTE, true);
            spriteShader.Initialize();

            primitiveShader = new Shader(bufferId, indexId);
            primitiveShader.Attach(ShaderTypes.Vertex, "Primitives2D.vert");
            primitiveShader.Attach(ShaderTypes.Fragment, "Primitives.frag");
            primitiveShader.AddAttribute <float>(2, GL_FLOAT);
            primitiveShader.AddAttribute <byte>(4, GL_UNSIGNED_BYTE, true);
            primitiveShader.Initialize();
        }
Ejemplo n.º 15
0
        public ShadowMapVisualizer(RenderTarget shadowMapTarget)
        {
            Shader shader = new Shader();

            shader.Attach(ShaderTypes.Vertex, "Sprite.vert");
            shader.Attach(ShaderTypes.Fragment, "ShadowMapVisualization.frag");
            shader.AddAttribute <float>(2, GL_FLOAT);
            shader.AddAttribute <float>(2, GL_FLOAT);
            shader.AddAttribute <byte>(4, GL_UNSIGNED_BYTE, true);
            shader.Initialize();

            sprite        = new Sprite(shadowMapTarget, Alignments.Left | Alignments.Bottom);
            sprite.Shader = shader;
            //sprite.Color = Color.Yellow;

            DisplaySize = DefaultSize;

            MessageSystem.Subscribe(this, (int)CoreMessageTypes.ResizeWindow, data =>
            {
                sprite.Position.SetValue(new vec2(0, Resolution.WindowHeight), false);
            });
        }
Ejemplo n.º 16
0
        public SkeletalTester()
        {
            const int ShadowMapSize = 2048;

            GLUtilities.AllocateBuffers(10000, 1000, out bufferId, out indexId, GL_STATIC_DRAW);

            skeletalShader = new Shader(bufferId, indexId);
            skeletalShader.Attach(ShaderTypes.Vertex, "Skeletal.vert");
            skeletalShader.Attach(ShaderTypes.Fragment, "ModelShadow.frag");
            skeletalShader.AddAttribute <float>(3, GL_FLOAT);
            skeletalShader.AddAttribute <float>(2, GL_FLOAT);
            skeletalShader.AddAttribute <float>(3, GL_FLOAT);
            skeletalShader.AddAttribute <float>(2, GL_FLOAT);
            skeletalShader.AddAttribute <short>(2, GL_SHORT, ShaderAttributeFlags.IsInteger);
            skeletalShader.AddAttribute <int>(1, GL_INT, ShaderAttributeFlags.IsInteger);
            skeletalShader.Initialize();
            skeletalShader.Use();
            skeletalShader.SetUniform("shadowSampler", 0);
            skeletalShader.SetUniform("textureSampler", 1);

            shadowMapShader = new Shader(bufferId, indexId);
            shadowMapShader.Attach(ShaderTypes.Vertex, "ShadowMapSkeletal.vert");
            shadowMapShader.Attach(ShaderTypes.Fragment, "ShadowMap.frag");
            shadowMapShader.AddAttribute <float>(3, GL_FLOAT, ShaderAttributeFlags.None, 20);
            shadowMapShader.AddAttribute <float>(2, GL_FLOAT);
            shadowMapShader.AddAttribute <short>(2, GL_SHORT, ShaderAttributeFlags.IsInteger);
            shadowMapShader.AddAttribute <int>(1, GL_INT, ShaderAttributeFlags.IsInteger);
            shadowMapShader.Initialize();

            shadowMapTarget = new RenderTarget(ShadowMapSize, ShadowMapSize, RenderTargetFlags.Depth);
            defaultTexture  = ContentCache.GetTexture("Grey.png");
            lightDirection  = -vec3.UnitY;

            model = new Model("Tree.dae");

            BufferMesh(model.Mesh);
        }
Ejemplo n.º 17
0
        public SkeletonRenderer(Camera3D camera, GlobalLight3D light, uint bufferCapacity, uint indexCapacity) :
            base(camera, light, bufferCapacity, indexCapacity)
        {
            shader = new Shader();
            shader.Attach(ShaderTypes.Vertex, "Skeletal.vert");
            shader.Attach(ShaderTypes.Fragment, "ModelShadow.frag");
            shader.AddAttribute <float>(3, GL_FLOAT);
            shader.AddAttribute <float>(2, GL_FLOAT);
            shader.AddAttribute <float>(1, GL_FLOAT);
            shader.AddAttribute <float>(3, GL_FLOAT);
            shader.AddAttribute <float>(2, GL_FLOAT);
            shader.AddAttribute <ushort>(2, GL_UNSIGNED_SHORT);

            Bind(bufferId, indexId);

            shadowShader = new Shader();
            shadowShader.Attach(ShaderTypes.Vertex, "ShadowMapSkeletal.vert");
            shadowShader.Attach(ShaderTypes.Fragment, "ShadowMap.frag");
            shadowShader.Initialize();
            shadowShader.Use();
            shadowShader.SetUniform("images", new[] { 0, 1, 2, 3, 4, 5 });
        }
Ejemplo n.º 18
0
        public unsafe SpriteBatch3D(GlobalLight light) : base(light)
        {
            GLUtilities.GenerateBuffers(out bufferId, out indexId);

            shader = new Shader();
            shader.Attach(ShaderTypes.Vertex, "Sprite3D.vert");
            shader.Attach(ShaderTypes.Fragment, "Sprite3D.frag");
            shader.AddAttribute <float>(3, GL_FLOAT);
            shader.AddAttribute <float>(2, GL_FLOAT);

            Bind(bufferId, indexId);

            // Buffer vertex data.
            vec2[] points =
            {
                -vec2.Ones,
                new vec2(-1,  1),
                new vec2(1,  -1),
                vec2.Ones
            };

            // Dividing each point by two gives the entire quad a unit length of one.
            for (int i = 0; i < points.Length; i++)
            {
                points[i] /= 2;
            }

            // Modifying the source order causes sprites to appear the right way up (and not flipped horizontally).
            vec2[] sources =
            {
                vec2.UnitY,
                vec2.Zero,
                vec2.Ones,
                vec2.UnitX
            };

            float[] data = new float[20];

            for (int i = 0; i < points.Length; i++)
            {
                var p = points[i];
                var s = sources[i];

                int start = i * 5;

                data[start]     = p.x;
                data[start + 1] = p.y;
                data[start + 2] = 0;
                data[start + 3] = s.x;
                data[start + 4] = s.y;
            }

            glBindBuffer(GL_ARRAY_BUFFER, bufferId);

            fixed(float *address = &data[0])
            {
                glBufferData(GL_ARRAY_BUFFER, sizeof(float) * (uint)data.Length, address, GL_STATIC_DRAW);
            }

            // Buffer index data.
            ushort[] indices = new ushort[4];

            for (int i = 0; i < indices.Length; i++)
            {
                indices[i] = (ushort)i;
            }

            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexId);

            fixed(ushort *address = &indices[0])
            {
                glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(ushort) * 4, address, GL_STATIC_DRAW);
            }
        }