Ejemplo n.º 1
0
        public override void Initialize()
        {
            var vertexBuffer = Context.Create.Buffer(BufferTarget.Array, 3 * 8 * sizeof(float), BufferUsageHint.StaticDraw, new[]
            {
                new Vertex {
                    Position = new Vector4(-0.5f, -0.5f, 0f, 1f), Color = new Color4(1, 0, 0, 1)
                },
                new Vertex {
                    Position = new Vector4(0.0f, 0.5f, 0f, 1f), Color = new Color4(0, 1, 0, 1)
                },
                new Vertex {
                    Position = new Vector4(0.5f, -0.5f, 0f, 1f), Color = new Color4(1, 1, 0, 1)
                }
            });

            var indexBuffer = Context.Create.Buffer(BufferTarget.ElementArray, 3 * sizeof(ushort), BufferUsageHint.StaticDraw, new ushort[] { 0, 1, 2 });

            vertexArray = Context.Create.VertexArray();
            vertexArray.SetElementArrayBuffer(indexBuffer);
            vertexArray.SetVertexAttributeF(0, vertexBuffer, VertexAttributeDimension.Four, VertexAttribPointerType.Float, false, 32, 0);
            vertexArray.SetVertexAttributeF(1, vertexBuffer, VertexAttributeDimension.Four, VertexAttribPointerType.Float, false, 32, 16);

            IVertexShader   vsh = Context.Create.VertexShader(VertexShaderText);
            IFragmentShader fsh = Context.Create.FragmentShader(FragmentShaderText);

            program = Context.Create.Program(new ShaderProgramDescription
            {
                VertexShaders        = new[] { vsh },
                FragmentShaders      = new[] { fsh },
                VertexAttributeNames = new[] { "in_position", "in_color" }
            });
        }
Ejemplo n.º 2
0
        public override void Initialize()
        {
            var vertexData = new Vertex[ParticleCount];

            for (int i = 0; i < vertexData.Length; i++)
            {
                vertexData[i] = new Vertex((float)i / ParticleCount);
            }

            var vertexBuffer = Context.Create.Buffer(BufferTarget.Array, ParticleCount * Vertex.SizeInBytes, BufferUsageHint.StaticDraw, vertexData);

            vertexArray = Context.Create.VertexArray();
            vertexArray.SetVertexAttributeF(0, vertexBuffer, VertexAttributeDimension.Three, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 0);
            vertexArray.SetVertexAttributeF(1, vertexBuffer, VertexAttributeDimension.Three, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 3 * sizeof(float));

            timeBuffer   = Context.Create.Buffer(BufferTarget.Uniform, sizeof(float), BufferUsageHint.DynamicDraw);
            cameraBuffer = Context.Create.Buffer(BufferTarget.Uniform, sizeof(float), BufferUsageHint.DynamicDraw);

            var vsh = Context.Create.VertexShader(VertexShaderText);
            var gsh = Context.Create.GeometryShader(GeometryShaderText);
            var fsh = Context.Create.FragmentShader(FragmentShaderText);

            program = Context.Create.Program(new ShaderProgramDescription
            {
                VertexShaders        = new[] { vsh },
                GeometryShaders      = new[] { gsh },
                FragmentShaders      = new[] { fsh },
                VertexAttributeNames = new[] { "in_position", "in_color" },
                UniformBufferNames   = new[] { "Time", "Camera" }
            });
        }
Ejemplo n.º 3
0
        public override void Initialize()
        {
            var vertexBuffer = Context.Create.Buffer(BufferTarget.Array, 12 * Vertex.SizeInBytes, BufferUsageHint.StaticDraw, new Vertex[]
            {
                new Vertex(-1f, 1 / 3f), new Vertex(4f, 1f), new Vertex(-4f, 1f), new Vertex(1f, 1 / 3f),
                new Vertex(-1f, -1 / 3f), new Vertex(4f, 1 / 3f), new Vertex(-4f, 1 / 3f), new Vertex(1f, -1 / 3f),
                new Vertex(-1f, -1f), new Vertex(4f, -1 / 3f), new Vertex(-4f, -1 / 3f), new Vertex(1f, -1f)
            });

            vertexArray = Context.Create.VertexArray();
            vertexArray.SetVertexAttributeF(0, vertexBuffer, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 0);

            tessFactorBuffer = Context.Create.Buffer(BufferTarget.Uniform, sizeof(int), BufferUsageHint.DynamicDraw);

            var vsh  = Context.Create.VertexShader(VertexShaderText);
            var tcsh = Context.Create.TesselationControlShader(TessControlShaderText);
            var tesh = Context.Create.TesselationEvaluationShader(TessEvaluationShaderText);
            var fsh  = Context.Create.FragmentShader(FragmentShaderText);

            program = Context.Create.Program(new ShaderProgramDescription
            {
                VertexShaders                = new[] { vsh },
                TesselationControlShaders    = new[] { tcsh },
                TesselationEvaluationShaders = new[] { tesh },
                FragmentShaders              = new[] { fsh },
                VertexAttributeNames         = new[] { "in_position" },
                UniformBufferNames           = new[] { "TessFactor" }
            });
        }
Ejemplo n.º 4
0
        public FullTextureProcessor(IClientSettings settings, IContext context, string fragmentShaderText, string[] samplerNames)
        {
            var header = settings.IsEs ? EsHeader : DesktopHeader;
            var vertexShader = context.Create.VertexShader(header + VertexShaderText);

            var decodeFragmentShader = context.Create.FragmentShader(header + fragmentShaderText);
            program = context.Create.Program(new ShaderProgramDescription
            {
                VertexShaders = new[] { vertexShader },
                FragmentShaders = new[] { decodeFragmentShader },
                VertexAttributeNames = new[] { "in_position" },
                SamplerNames = samplerNames
            });

            var vertexBuffer = context.Create.Buffer(BufferTarget.ArrayBuffer, 4 * Vertex.Size, BufferUsageHint.StaticDraw, new[]
            {
                new Vertex(-1f, 1f),
                new Vertex(1f, 1f),
                new Vertex(1f, -1f),
                new Vertex(-1f, -1f)
            });

            var elementArrayBuffer = context.Create.Buffer(BufferTarget.ElementArrayBuffer, 6 * sizeof(ushort), BufferUsageHint.StaticDraw, new ushort[]
            {
                0, 1, 2, 0, 2, 3
            });

            vertexArray = context.Create.VertexArray();
            vertexArray.SetVertexAttributeF(0, vertexBuffer, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, Vertex.Size, 0);
            vertexArray.SetElementArrayBuffer(elementArrayBuffer);
        }
Ejemplo n.º 5
0
        public override void Initialize()
        {
            var vertexData = new Vertex[MaxVertices];

            vertexData[0] = Vertex.Cannon;

            var vertexBufferSource = Context.Create.Buffer(BufferTarget.TransformFeedback, Vertex.SizeInBytes * MaxVertices, BufferUsageHint.StaticDraw, vertexData);
            var vertexBufferTarget = Context.Create.Buffer(BufferTarget.TransformFeedback, Vertex.SizeInBytes * MaxVertices, BufferUsageHint.StaticDraw);

            vertexArraySource = Context.Create.VertexArray();
            vertexArraySource.SetVertexAttributeF(0, vertexBufferSource, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 0);
            vertexArraySource.SetVertexAttributeF(1, vertexBufferSource, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 2 * sizeof(float));
            vertexArraySource.SetVertexAttributeF(2, vertexBufferSource, VertexAttributeDimension.One, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 4 * sizeof(float));
            vertexArraySource.SetVertexAttributeI(3, vertexBufferSource, VertexAttributeDimension.One, VertexAttribIPointerType.Int, Vertex.SizeInBytes, 5 * sizeof(float));
            vertexArraySource.SetVertexAttributeF(4, vertexBufferSource, VertexAttributeDimension.Three, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 6 * sizeof(float));

            vertexArrayTarget = Context.Create.VertexArray();
            vertexArrayTarget.SetVertexAttributeF(0, vertexBufferTarget, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 0);
            vertexArrayTarget.SetVertexAttributeF(1, vertexBufferTarget, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 2 * sizeof(float));
            vertexArrayTarget.SetVertexAttributeF(2, vertexBufferTarget, VertexAttributeDimension.One, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 4 * sizeof(float));
            vertexArrayTarget.SetVertexAttributeI(3, vertexBufferTarget, VertexAttributeDimension.One, VertexAttribIPointerType.Int, Vertex.SizeInBytes, 5 * sizeof(float));
            vertexArrayTarget.SetVertexAttributeF(4, vertexBufferTarget, VertexAttributeDimension.Three, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 6 * sizeof(float));

            transformFeedbackSource = Context.Create.TransformFeedback();
            transformFeedbackSource.SetBuffer(0, vertexBufferSource);

            transformFeedbackTarget = Context.Create.TransformFeedback();
            transformFeedbackTarget.SetBuffer(0, vertexBufferTarget);

            timeBuffer = Context.Create.Buffer(BufferTarget.Uniform, 2 * sizeof(float), BufferUsageHint.DynamicDraw);

            IVertexShader   vsh = Context.Create.VertexShader(VertexShaderText);
            IGeometryShader gsh = Context.Create.GeometryShader(GeometryShaderText);
            IFragmentShader fsh = Context.Create.FragmentShader(FragmentShaderText);

            program = Context.Create.Program(new ShaderProgramDescription
            {
                VertexShaders                   = new[] { vsh },
                GeometryShaders                 = new[] { gsh },
                FragmentShaders                 = new[] { fsh },
                VertexAttributeNames            = new[] { "in_position", "in_velocity", "in_timeToAction", "in_type", "in_color" },
                TransformFeedbackAttributeNames = new[] { "g_position", "g_velocity", "g_timeToAction", "g_type", "g_color" },
                TransformFeedbackMode           = TransformFeedbackMode.InterleavedAttribs,
                UniformBufferNames              = new[] { "Time" }
            });
        }
Ejemplo n.º 6
0
        public override void Initialize()
        {
            var vertexBuffer = Context.Create.Buffer(BufferTarget.Array, 4 * 8 * sizeof(float), BufferUsageHint.StaticDraw, new[]
            {
                new Vertex(-1f, -1f, 0f, 1f),
                new Vertex(-1f, 1f, 0f, 0f),
                new Vertex(1f, 1f, 1f, 0f),
                new Vertex(1f, -1f, 1f, 1f),
            });

            var indexBuffer = Context.Create.Buffer(BufferTarget.ElementArray, 6 * sizeof(ushort), BufferUsageHint.StaticDraw, new ushort[]
            {
                0, 1, 2, 0, 2, 3
            });

            vertexArray = Context.Create.VertexArray();
            vertexArray.SetElementArrayBuffer(indexBuffer);
            vertexArray.SetVertexAttributeF(0, vertexBuffer, VertexAttributeDimension.Four, VertexAttribPointerType.Float, false, 32, 0);
            vertexArray.SetVertexAttributeF(1, vertexBuffer, VertexAttributeDimension.Four, VertexAttribPointerType.Float, false, 32, 16);

            using (var textureLoader = new TextureLoader("../Textures/Chess256.png"))
            {
                diffuseMap = Context.Create.Texture2D(textureLoader.Width, textureLoader.Height, TextureHelper.CalculateMipCount(textureLoader.Width, textureLoader.Height, 1), Format.Srgb8Alpha8);
                for (int i = 0; i < diffuseMap.MipCount; i++)
                {
                    diffuseMap.SetData(i, textureLoader.GetMipData(i), FormatColor.Rgba, FormatType.UnsignedByte);
                }
            }

            sampler = Context.Create.Sampler();
            sampler.SetMagFilter(TextureMagFilter.Linear);
            sampler.SetMinFilter(TextureMinFilter.LinearMipmapLinear);
            sampler.SetMaxAnisotropy(16f);

            var vsh = Context.Create.VertexShader(VertexShaderText);
            var fsh = Context.Create.FragmentShader(FragmentShaderText);

            program = Context.Create.Program(new ShaderProgramDescription
            {
                VertexShaders        = new[] { vsh },
                FragmentShaders      = new[] { fsh },
                VertexAttributeNames = new[] { "in_position", "in_tex_coord" },
                SamplerNames         = new[] { "DiffuseMap" }
            });
        }
Ejemplo n.º 7
0
        public unsafe HighlightDrawer(IGraphicsInfra infra, IBlurDrawer blurDrawer, IQuadDrawer quadDrawer, IBleedDrawer bleedDrawer, IOffScreenContainer offScreenContainer)
        {
            this.infra              = infra;
            this.blurDrawer         = blurDrawer;
            this.quadDrawer         = quadDrawer;
            this.bleedDrawer        = bleedDrawer;
            this.offScreenContainer = offScreenContainer;

            #region Shader Text
            const string vertexShaderText =
                @"#version 150
in vec4 in_position;

void main()
{
    gl_Position = in_position;
}
";

            const string fragmentShaderText =
                @"#version 150
out vec4 out_color;

void main()
{
    out_color = vec4(1, 1, 1, 1);
}
";
            #endregion

            var vs = infra.GlContext.Create.VertexShader(vertexShaderText);
            var fs = infra.GlContext.Create.FragmentShader(fragmentShaderText);
            program = infra.GlContext.Create.Program(new ShaderProgramDescription
            {
                VertexShaders        = vs.EnumSelf(),
                FragmentShaders      = fs.EnumSelf(),
                VertexAttributeNames = new[] { "in_position" }
            });

            var vertices = new[]
            {
                new Vector4(-1, 1, 0.5f, 1),
                new Vector4(-1, -1, 0.5f, 1),
                new Vector4(1, 1, 0.5f, 1),
                new Vector4(1, -1, 0.5f, 1)
            };

            IBuffer vb;

            fixed(Vector4 *pVertices = vertices)
            vb = infra.GlContext.Create.Buffer(BufferTarget.Array, vertices.Length * sizeof(Vector4), BufferUsageHint.StaticDraw, (IntPtr)pVertices);

            vao = infra.GlContext.Create.VertexArray();
            vao.SetVertexAttributeF(0, vb, VertexAttributeDimension.Four, VertexAttribPointerType.Float, false, sizeof(Vector4), 0);

            drawSpotFramebuffer = infra.GlContext.Create.Framebuffer();
        }
Ejemplo n.º 8
0
        public override void Initialize()
        {
            var vertexData = new Vertex[ParticleCount];

            for (int i = 0; i < vertexData.Length; i++)
            {
                vertexData[i] = new Vertex(i * ParticleTicksDelta);
            }

            var vertexBufferSource = Context.Create.Buffer(BufferTarget.TransformFeedback, ParticleCount * Vertex.SizeInBytes, BufferUsageHint.StaticDraw, vertexData);
            var vertexBufferTarget = Context.Create.Buffer(BufferTarget.TransformFeedback, ParticleCount * Vertex.SizeInBytes, BufferUsageHint.StaticDraw, IntPtr.Zero);

            vertexArraySource = Context.Create.VertexArray();
            vertexArraySource.SetVertexAttributeF(0, vertexBufferSource, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 0);
            vertexArraySource.SetVertexAttributeF(1, vertexBufferSource, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 2 * sizeof(float));
            vertexArraySource.SetVertexAttributeF(2, vertexBufferSource, VertexAttributeDimension.One, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 4 * sizeof(float));
            vertexArraySource.SetVertexAttributeF(3, vertexBufferSource, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 5 * sizeof(float));
            vertexArraySource.SetVertexAttributeF(4, vertexBufferSource, VertexAttributeDimension.Three, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 7 * sizeof(float));

            vertexArrayTarget = Context.Create.VertexArray();
            vertexArrayTarget.SetVertexAttributeF(0, vertexBufferTarget, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 0);
            vertexArrayTarget.SetVertexAttributeF(1, vertexBufferTarget, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 2 * sizeof(float));
            vertexArrayTarget.SetVertexAttributeF(2, vertexBufferTarget, VertexAttributeDimension.One, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 4 * sizeof(float));
            vertexArrayTarget.SetVertexAttributeF(3, vertexBufferTarget, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 5 * sizeof(float));
            vertexArrayTarget.SetVertexAttributeF(4, vertexBufferTarget, VertexAttributeDimension.Three, VertexAttribPointerType.Float, false, Vertex.SizeInBytes, 7 * sizeof(float));

            transformFeedbackSource = Context.Create.TransformFeedback();
            transformFeedbackSource.SetBuffer(0, vertexBufferSource);

            transformFeedbackTarget = Context.Create.TransformFeedback();
            transformFeedbackTarget.SetBuffer(0, vertexBufferTarget);

            var vsh = Context.Create.VertexShader(VertexShaderText);
            var fsh = Context.Create.FragmentShader(FragmentShaderText);

            program = Context.Create.Program(new ShaderProgramDescription
            {
                VertexShaders                   = new[] { vsh },
                FragmentShaders                 = new[] { fsh },
                VertexAttributeNames            = new[] { "in_position", "in_velocity", "in_spawn_cooldown", "in_initial_velocity", "in_color" },
                TransformFeedbackAttributeNames = new[] { "v_position", "v_velocity", "v_spawn_cooldown", "v_initial_velocity", "v_color" },
                TransformFeedbackMode           = TransformFeedbackMode.InterleavedAttribs
            });
        }
Ejemplo n.º 9
0
        private void SetVertexAttrF(uint indexInVao, int indexInVertexSet, VertexAttributeDimension attrDimension, VertexAttribPointerType attrType)
        {
            var elemInfo      = vertexSet.ElementInfos[indexInVertexSet];
            var arraySubrange = vertexSet.ArraySubranges[elemInfo.ArrayIndex];
            var rawDataRes    = arraySubrange.RawDataResource;
            var arrayCache    = rawDataRes.CacheContainer.GetOrAddCache(Tuples.Pair(infra, rawDataRes), x => new RawDataResCache(x.First, x.Second));
            var glBuffer      = arrayCache.GetGlVertexBuffer();

            glVertexArray.SetVertexAttributeF(indexInVao, glBuffer, attrDimension, attrType, false, elemInfo.Stride, arraySubrange.StartOffset + elemInfo.Offset);
        }
Ejemplo n.º 10
0
        public override void Initialize()
        {
            var vertexBuffer = Context.Create.Buffer(BufferTarget.Array, 4 * 8 * sizeof(float), BufferUsageHint.StaticDraw, new[]
            {
                new Vertex(-1f, -1f, 0f, 1f),
                new Vertex(-1f, 1f, 0f, 0f),
                new Vertex(1f, 1f, 1f, 0f),
                new Vertex(1f, -1f, 1f, 1f),
            });

            var indexBuffer = Context.Create.Buffer(BufferTarget.ElementArray, 6 * sizeof(ushort), BufferUsageHint.StaticDraw, new ushort[]
            {
                0, 1, 2, 0, 2, 3
            });

            vertexArray = Context.Create.VertexArray();
            vertexArray.SetElementArrayBuffer(indexBuffer);
            vertexArray.SetVertexAttributeF(0, vertexBuffer, VertexAttributeDimension.Four, VertexAttribPointerType.Float, false, 32, 0);
            vertexArray.SetVertexAttributeF(1, vertexBuffer, VertexAttributeDimension.Four, VertexAttribPointerType.Float, false, 32, 16);

            pixelUnpackBuffer = Context.Create.Buffer(BufferTarget.PixelUnpack, 1024 * 1024 * 4, BufferUsageHint.StreamDraw);

            data = Enumerable.Range(0, 1024 * 1024 * 4 * 2).Select(x => (byte)(128.0 + 128.0 * Math.Sin((2.0 / 3.0) * Math.PI * (x % 4) + (double)x / 4 / 11111))).ToArray();

            diffuseMap = Context.Create.Texture2D(640, 360, 1, Format.Rgba8);

            sampler = Context.Create.Sampler();
            sampler.SetMagFilter(TextureMagFilter.Nearest);
            sampler.SetMinFilter(TextureMinFilter.Nearest);
            sampler.SetMaxAnisotropy(16f);

            var vsh = Context.Create.VertexShader(VertexShaderText);
            var fsh = Context.Create.FragmentShader(FragmentShaderText);

            program = Context.Create.Program(new ShaderProgramDescription
            {
                VertexShaders        = new[] { vsh },
                FragmentShaders      = new[] { fsh },
                VertexAttributeNames = new[] { "in_position", "in_tex_coord" },
                SamplerNames         = new[] { "DiffuseMap" }
            });
        }
Ejemplo n.º 11
0
        public unsafe IVertexArray GetVao()
        {
            if (!isDirty && NumPoints == lastPointCount)
            {
                return(vao);
            }

            if (aspect.VisualBorderCurve == null || aspect.VisualBorderCurve.Count == 0)
            {
                return(null);
            }

            if (vao == null)
            {
                vao = glContext.Create.VertexArray();
            }

            var newPointCount = NumPoints;

            if (vertexBuffer == null || bufferPointCapacity < newPointCount)
            {
                vertexBuffer?.Dispose();
                while (bufferPointCapacity < newPointCount)
                {
                    bufferPointCapacity *= 2;
                }
                vertexBuffer = glContext.Create.Buffer(BufferTarget.Array, bufferPointCapacity * sizeof(Vector4), BufferUsageHint.DynamicDraw);
                vao.SetVertexAttributeF(0, vertexBuffer, VertexAttributeDimension.Four, VertexAttribPointerType.Float, false, sizeof(Vector4), 0);
            }

            var rawData = aspect.BorderComplete
                ? aspect.VisualBorderCurve.Concat(aspect.VisualBorderCurve.First().EnumSelf()).Select(xy => new Vector4(xy, 0, 1)).ToArray()
                : aspect.VisualBorderCurve.Select(xy => new Vector4(xy, 0, 1)).ToArray();

            var sizeInBytes = rawData.Length * sizeof(Vector4);
            var dst         = vertexBuffer.Map(0, vertexBuffer.SizeInBytes, MapAccess.Write | MapAccess.InvalidateBuffer);

            fixed(Vector4 *src = rawData)
            PtrHelper.CopyBulk((byte *)dst, (byte *)src, sizeInBytes);

            vertexBuffer.Unmap();

            lastPointCount = newPointCount;
            isDirty        = false;
            return(vao);
        }
Ejemplo n.º 12
0
        public override void Initialize()
        {
            var vertexBuffer = Context.Create.Buffer(BufferTarget.Array, 4 * 8 * sizeof(float), BufferUsageHint.StaticDraw, new[]
            {
                new Vertex(-1f, -1f, 0f, 1f),
                new Vertex(-1f, 1f, 0f, 0f),
                new Vertex(1f, 1f, 1f, 0f),
                new Vertex(1f, -1f, 1f, 1f),
            });

            var indexBuffer = Context.Create.Buffer(BufferTarget.ElementArray, 6 * sizeof(ushort), BufferUsageHint.StaticDraw, new ushort[]
            {
                0, 1, 2, 0, 2, 3
            });

            vertexArray = Context.Create.VertexArray();
            vertexArray.SetElementArrayBuffer(indexBuffer);
            vertexArray.SetVertexAttributeF(0, vertexBuffer, VertexAttributeDimension.Four, VertexAttribPointerType.Float, false, 32, 0);
            vertexArray.SetVertexAttributeF(1, vertexBuffer, VertexAttributeDimension.Four, VertexAttribPointerType.Float, false, 32, 16);

            using (var textureLoader = new TextureLoader("../Textures/Chess256.png"))
            {
                diffuseMap = Context.Create.Texture2D(textureLoader.Width, textureLoader.Height, TextureHelper.CalculateMipCount(textureLoader.Width, textureLoader.Height, 1), Format.Srgb8Alpha8);
                for (int i = 0; i < diffuseMap.MipCount; i++)
                    diffuseMap.SetData(i, textureLoader.GetMipData(i), FormatColor.Rgba, FormatType.UnsignedByte);
            }

            sampler = Context.Create.Sampler();
            sampler.SetMagFilter(TextureMagFilter.Linear);
            sampler.SetMinFilter(TextureMinFilter.LinearMipmapLinear);
            sampler.SetMaxAnisotropy(16f);

            var vsh = Context.Create.VertexShader(VertexShaderText);
            var fsh = Context.Create.FragmentShader(FragmentShaderText);
            program = Context.Create.Program(new ShaderProgramDescription
            {
                VertexShaders = new[] {vsh},
                FragmentShaders = new[] {fsh},
                VertexAttributeNames = new[] {"in_position", "in_tex_coord"},
                SamplerNames = new[] {"DiffuseMap"}
            });
        }
Ejemplo n.º 13
0
        public unsafe QuadDrawer(IContext glContext)
        {
            this.glContext = glContext;

            #region Shader Text
            const string vertexShaderText =
                @"#version 150

in vec4 in_vertex;

out vec2 v_tex_coord;

void main()
{
    gl_Position = vec4(in_vertex.xy, 0.5, 1.0);
    v_tex_coord = in_vertex.zw;
}
";

            const string colFragmentShaderText =
                @"#version 150

layout(std140) uniform ColorUb
{
    vec4 Color;
};

out vec4 out_color;

void main()
{
    out_color = Color;
}
";

            const string texFragmentShaderText =
                @"#version 150

uniform sampler2D Texture;

in vec2 v_tex_coord;

out vec4 out_color;

void main()
{
    out_color = texture(Texture, v_tex_coord);
}
";
            #endregion

            var vs    = glContext.Create.VertexShader(vertexShaderText);
            var colFs = glContext.Create.FragmentShader(colFragmentShaderText);
            var texFs = glContext.Create.FragmentShader(texFragmentShaderText);
            colShaderProgram = glContext.Create.Program(new ShaderProgramDescription
            {
                VertexShaders        = new[] { vs },
                FragmentShaders      = new[] { colFs },
                VertexAttributeNames = new[] { "in_vertex" },
                UniformBufferNames   = new[] { "ColorUb" },
            });
            texShaderProgram = glContext.Create.Program(new ShaderProgramDescription
            {
                VertexShaders        = new[] { vs },
                FragmentShaders      = new[] { texFs },
                VertexAttributeNames = new[] { "in_vertex" },
                SamplerNames         = new[] { "Texture" }
            });

            var vertices = new[]
            {
                new Vector4(-1, -1, 0, 0),
                new Vector4(-1, 1, 0, 1),
                new Vector4(1, -1, 1, 0),
                new Vector4(1, 1, 1, 1),
            };

            IBuffer vertexBuffer;

            fixed(Vector4 *data = vertices)
            vertexBuffer = glContext.Create.Buffer(BufferTarget.Array, 4 * sizeof(float) * vertices.Length, BufferUsageHint.StaticDraw, (IntPtr)data);

            vao = glContext.Create.VertexArray();
            vao.SetVertexAttributeF(0, vertexBuffer, VertexAttributeDimension.Four, VertexAttribPointerType.Float, false, 4 * sizeof(float), 0);

            colorUb = new UniformBufferSugar <Color4>(glContext);
        }
Ejemplo n.º 14
0
        public unsafe BleedDrawer(IContext glContext)
        {
            this.glContext = glContext;

            #region Shader Text
            const string vertexShaderText =
                @"#version 150

in vec2 in_position;
in vec2 in_tex_coord;

out vec2 v_tex_coord;

void main()
{
    gl_Position = vec4(in_position, 0, 1);
    v_tex_coord = in_tex_coord;
}
";

            const string fragmentShaderText =
                @"#version 150
uniform sampler2D DiffuseMap;

layout(std140) uniform Params
{
    vec4 Color;
};

in vec2 v_tex_coord;

out vec4 out_color;

void main()
{
    ivec2 coord = ivec2(gl_FragCoord);
    
    float accumulator = 0;
    for (int i = -4; i <= 4; i++)
    for (int j = -4; j <= 4; j++)
    {
        vec4 col = texelFetch(DiffuseMap, coord + ivec2(i, j), 0);
        accumulator += col.r + col.g + col.b;
    }

    float s = step(0.1, accumulator);
    out_color = s * Color;
}
";
            #endregion

            var vs = glContext.Create.VertexShader(vertexShaderText);
            var fs = glContext.Create.FragmentShader(fragmentShaderText);
            program = glContext.Create.Program(new ShaderProgramDescription
            {
                VertexShaders        = new[] { vs },
                FragmentShaders      = new[] { fs },
                VertexAttributeNames = new[] { "in_position" },
                UniformBufferNames   = new [] { "Params" },
                SamplerNames         = new [] { "DiffuseMap" },
            });

            var vertices = new[]
            {
                new Vector4(-1, -1, 0, 0),
                new Vector4(-1, 1, 0, 1),
                new Vector4(1, -1, 1, 0),
                new Vector4(1, 1, 1, 1),
            };

            IBuffer vertexBuffer;

            fixed(Vector4 *data = vertices)
            vertexBuffer = glContext.Create.Buffer(BufferTarget.Array, 4 * sizeof(float) * vertices.Length, BufferUsageHint.StaticDraw, (IntPtr)data);

            vao = glContext.Create.VertexArray();
            vao.SetVertexAttributeF(0, vertexBuffer, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, 4 * sizeof(float), 0);
            //vao.SetVertexAttributeF(1, vertexBuffer, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, 4 * sizeof(float), 2 * sizeof(float));

            paramsUb = new UniformBufferSugar <Color4>(glContext);
        }
Ejemplo n.º 15
0
        public unsafe BlurDrawer(IContext glContext)
        {
            this.glContext = glContext;

            #region Shader Text
            const string vertexShaderText =
                @"#version 150

in vec2 in_position;
in vec2 in_tex_coord;

out vec2 v_tex_coord;

void main()
{
    gl_Position = vec4(in_position, 0, 1);
    v_tex_coord = in_tex_coord;
}
";

            const string fragmentShaderText =
                @"#version 150
uniform sampler2D DiffuseMap;

layout(std140) uniform Params
{
    int OffsetX;
    int OffsetY;
    int Width;
    int Height;
};

in vec2 v_tex_coord;

out vec4 out_color;

void main()
{
    ivec2 coord = ivec2(gl_FragCoord);
    //vec4 color = vec4(0, 0, 0, 0);
    //color += texelFetch(DiffuseMap, coord / 4 + ivec2(-1, -1), 2) / 16;
    //color += texelFetch(DiffuseMap, coord / 4 + ivec2(-1, 0), 2) / 8;
    //color += texelFetch(DiffuseMap, coord / 4 + ivec2(-1, 1), 2) / 16;
    //color += texelFetch(DiffuseMap, coord / 4 + ivec2(0, -1), 2) / 8;
    //color += texelFetch(DiffuseMap, coord / 4 + ivec2(0, 0), 2) / 4;
    //color += texelFetch(DiffuseMap, coord / 4 + ivec2(0, 1), 2) / 8;
    //color += texelFetch(DiffuseMap, coord / 4 + ivec2(1, -1), 2) / 16;
    //color += texelFetch(DiffuseMap, coord / 4 + ivec2(1, 0), 2) / 8;
    //color += texelFetch(DiffuseMap, coord / 4 + ivec2(1, 1), 2) / 16;
    //out_color = color;
    //out_color = vec4(1, 0, 1, 1);
    //out_color = texture(DiffuseMap, vec2(gl_FragCoord.x / 500, gl_FragCoord.y / 500));// vec4(gl_FragCoord.x / 500, gl_FragCoord.y / 500, 0, 1);
    out_color = texture(DiffuseMap, vec2((gl_FragCoord.x + OffsetX) / Width, (gl_FragCoord.y + OffsetY) / Height), 4);
}
";
            #endregion

            var vs = glContext.Create.VertexShader(vertexShaderText);
            var fs = glContext.Create.FragmentShader(fragmentShaderText);
            shaderProgram = glContext.Create.Program(new ShaderProgramDescription
            {
                VertexShaders        = new[] { vs },
                FragmentShaders      = new[] { fs },
                VertexAttributeNames = new[] { "in_position" },
                UniformBufferNames   = new [] { "Params" },
                SamplerNames         = new [] { "DiffuseMap" },
            });

            var vertices = new[]
            {
                new Vector4(-1, -1, 0, 0),
                new Vector4(-1, 1, 0, 1),
                new Vector4(1, -1, 1, 0),
                new Vector4(1, 1, 1, 1),
            };

            IBuffer vertexBuffer;

            fixed(Vector4 *data = vertices)
            vertexBuffer = glContext.Create.Buffer(BufferTarget.Array, 4 * sizeof(float) * vertices.Length, BufferUsageHint.StaticDraw, (IntPtr)data);

            vao = glContext.Create.VertexArray();
            vao.SetVertexAttributeF(0, vertexBuffer, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, 4 * sizeof(float), 0);
            //vao.SetVertexAttributeF(1, vertexBuffer, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, 4 * sizeof(float), 2 * sizeof(float));

            paramsUb = new UniformBufferSugar <IntVector4>(glContext);
        }
Ejemplo n.º 16
0
        public override void Initialize()
        {
            renderTarget = Context.Create.Texture2D(RenderTargetSize, RenderTargetSize, TextureHelper.CalculateMipCount(RenderTargetSize, 1, 1), Format.Rgba8);
            depthStencil = Context.Create.Renderbuffer(RenderTargetSize, RenderTargetSize, Format.Depth24Stencil8);

            framebuffer = Context.Create.Framebuffer();
            framebuffer.AttachTextureImage(FramebufferAttachmentPoint.Color0, renderTarget, 0);
            framebuffer.AttachRenderbuffer(FramebufferAttachmentPoint.DepthStencil, depthStencil);

            var vertexBuffer = Context.Create.Buffer(BufferTarget.Array, 24 * 8 * sizeof(float), BufferUsageHint.StaticDraw, new[]
            {
                new Vertex(1f, -1f, 1f, 1f, 0f, 0f, 0f, 0f),
                new Vertex(1f, 1f, 1f, 1f, 0f, 0f, 1f, 0f),
                new Vertex(1f, 1f, -1f, 1f, 0f, 0f, 1f, 1f),
                new Vertex(1f, -1f, -1f, 1f, 0f, 0f, 0f, 1f),

                new Vertex(1f, 1f, 1f, 0f, 1f, 0f, 0f, 0f),
                new Vertex(-1f, 1f, 1f, 0f, 1f, 0f, 1f, 0f),
                new Vertex(-1f, 1f, -1f, 0f, 1f, 0f, 1f, 1f),
                new Vertex(1f, 1f, -1f, 0f, 1f, 0f, 0f, 1f),

                new Vertex(-1f, 1f, 1f, -1f, 0f, 0f, 0f, 0f),
                new Vertex(-1f, -1f, 1f, -1f, 0f, 0f, 1f, 0f),
                new Vertex(-1f, -1f, -1f, -1f, 0f, 0f, 1f, 1f),
                new Vertex(-1f, 1f, -1f, -1f, 0f, 0f, 0f, 1f),

                new Vertex(-1f, -1f, 1f, 0f, -1f, 0f, 0f, 0f),
                new Vertex(1f, -1f, 1f, 0f, -1f, 0f, 1f, 0f),
                new Vertex(1f, -1f, -1f, 0f, -1f, 0f, 1f, 1f),
                new Vertex(-1f, -1f, -1f, 0f, -1f, 0f, 0f, 1f),

                new Vertex(-1f, -1f, 1f, 0f, 0f, 1f, 0f, 0f),
                new Vertex(-1f, 1f, 1f, 0f, 0f, 1f, 1f, 0f),
                new Vertex(1f, 1f, 1f, 0f, 0f, 1f, 1f, 1f),
                new Vertex(1f, -1f, 1f, 0f, 0f, 1f, 0f, 1f),

                new Vertex(-1f, 1f, -1f, 0f, 0f, -1f, 0f, 0f),
                new Vertex(-1f, -1f, -1f, 0f, 0f, -1f, 1f, 0f),
                new Vertex(1f, -1f, -1f, 0f, 0f, -1f, 1f, 1f),
                new Vertex(1f, 1f, -1f, 0f, 0f, -1f, 0f, 1f)
            });

            var indexBuffer = Context.Create.Buffer(BufferTarget.ElementArray, 36 * sizeof(ushort), BufferUsageHint.StaticDraw, new ushort[]
            {
                0, 1, 2, 0, 2, 3,
                4, 5, 6, 4, 6, 7,
                8, 9, 10, 8, 10, 11,
                12, 13, 14, 12, 14, 15,
                16, 17, 18, 16, 18, 19,
                20, 21, 22, 20, 22, 23
            });

            vertexArray = Context.Create.VertexArray();
            vertexArray.SetElementArrayBuffer(indexBuffer);
            vertexArray.SetVertexAttributeF(0, vertexBuffer, VertexAttributeDimension.Three, VertexAttribPointerType.Float, false, 32, 0);
            vertexArray.SetVertexAttributeF(1, vertexBuffer, VertexAttributeDimension.Three, VertexAttribPointerType.Float, false, 32, 12);
            vertexArray.SetVertexAttributeF(2, vertexBuffer, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, 32, 24);

            transformBuffer = Context.Create.Buffer(BufferTarget.Uniform, 64, BufferUsageHint.DynamicDraw);
            cameraBuffer = Context.Create.Buffer(BufferTarget.Uniform, 64, BufferUsageHint.DynamicDraw);
            #if INTEL_WORKAROUND
            cameraOutsideBuffer = Context.Create.Buffer(BufferTarget.Uniform, 64, BufferUsageHint.DynamicDraw);
            #endif
            cameraExtraBuffer = Context.Create.Buffer(BufferTarget.Uniform, 12, BufferUsageHint.DynamicDraw);
            lightBuffer = Context.Create.Buffer(BufferTarget.Uniform, 12, BufferUsageHint.DynamicDraw);

            using (var textureLoader = new TextureLoader("../Textures/DiffuseTest.png"))
            {
                diffuseMap = Context.Create.Texture2D(textureLoader.Width, textureLoader.Height, TextureHelper.CalculateMipCount(textureLoader.Width, textureLoader.Height, 1), Format.Rgba8);
                for (int i = 0; i < diffuseMap.MipCount; i++)
                    diffuseMap.SetData(i, textureLoader.GetMipData(i), FormatColor.Rgba, FormatType.UnsignedByte);
            }

            sampler = Context.Create.Sampler();
            sampler.SetMagFilter(TextureMagFilter.Linear);
            sampler.SetMinFilter(TextureMinFilter.LinearMipmapLinear);
            sampler.SetMaxAnisotropy(16f);

            IVertexShader vsh = Context.Create.VertexShader(VertexShaderText);
            IFragmentShader fsh = Context.Create.FragmentShader(FragmentShaderText);
            program = Context.Create.Program(new ShaderProgramDescription
            {
                VertexShaders = new[] {vsh},
                FragmentShaders = new[] {fsh},
                VertexAttributeNames = new[] {"in_position", "in_normal", "in_tex_coord"},
                UniformBufferNames = new[] {"Transform", "Camera", "Light"},
                SamplerNames = new[] {"DiffuseMap"}
            });
        }
Ejemplo n.º 17
0
        public unsafe VeilDrawer(IContext glContext)
        {
            this.glContext = glContext;

            #region Shader Text
            const string vertexShaderText =
                @"#version 150

in vec4 in_position;

void main()
{
    gl_Position = in_position;
}
";

            const string fragmentShaderText =
                @"#version 150

layout(std140) uniform ColorUb
{
    vec4 Color;
};

out vec4 out_color;

void main()
{
    out_color = Color;
}
";
            #endregion

            var vs = glContext.Create.VertexShader(vertexShaderText);
            var fs = glContext.Create.FragmentShader(fragmentShaderText);
            shaderProgram = glContext.Create.Program(new ShaderProgramDescription
            {
                VertexShaders        = new[] { vs },
                FragmentShaders      = new[] { fs },
                VertexAttributeNames = new[] { "in_position" },
                UniformBufferNames   = new[] { "ColorUb" },
            });

            var vertices = new[]
            {
                new Vector4(-1, -1, 0, 1),
                new Vector4(-1, 1, 0, 1),
                new Vector4(1, -1, 0, 1),
                new Vector4(1, 1, 0, 1),
            };

            IBuffer vertexBuffer;

            fixed(Vector4 *data = vertices)
            vertexBuffer = glContext.Create.Buffer(BufferTarget.Array, 4 * sizeof(float) * vertices.Length, BufferUsageHint.StaticDraw, (IntPtr)data);

            vao = glContext.Create.VertexArray();
            vao.SetVertexAttributeF(0, vertexBuffer, VertexAttributeDimension.Four, VertexAttribPointerType.Float, false, 4 * sizeof(float), 0);

            colorUb = new UniformBufferSugar <Color4>(glContext);
        }
Ejemplo n.º 18
0
        public override void Initialize()
        {
            renderTarget = Context.Create.Texture2D(RenderTargetSize, RenderTargetSize, TextureHelper.CalculateMipCount(RenderTargetSize, 1, 1), Format.Rgba8);
            depthStencil = Context.Create.Renderbuffer(RenderTargetSize, RenderTargetSize, Format.Depth24Stencil8);

            framebuffer = Context.Create.Framebuffer();
            framebuffer.AttachTextureImage(FramebufferAttachmentPoint.Color0, renderTarget, 0);
            framebuffer.AttachRenderbuffer(FramebufferAttachmentPoint.DepthStencil, depthStencil);

            var vertexBuffer = Context.Create.Buffer(BufferTarget.Array, 24 * 8 * sizeof(float), BufferUsageHint.StaticDraw, new[]
            {
                new Vertex(1f, -1f, 1f, 1f, 0f, 0f, 0f, 0f),
                new Vertex(1f, 1f, 1f, 1f, 0f, 0f, 1f, 0f),
                new Vertex(1f, 1f, -1f, 1f, 0f, 0f, 1f, 1f),
                new Vertex(1f, -1f, -1f, 1f, 0f, 0f, 0f, 1f),

                new Vertex(1f, 1f, 1f, 0f, 1f, 0f, 0f, 0f),
                new Vertex(-1f, 1f, 1f, 0f, 1f, 0f, 1f, 0f),
                new Vertex(-1f, 1f, -1f, 0f, 1f, 0f, 1f, 1f),
                new Vertex(1f, 1f, -1f, 0f, 1f, 0f, 0f, 1f),

                new Vertex(-1f, 1f, 1f, -1f, 0f, 0f, 0f, 0f),
                new Vertex(-1f, -1f, 1f, -1f, 0f, 0f, 1f, 0f),
                new Vertex(-1f, -1f, -1f, -1f, 0f, 0f, 1f, 1f),
                new Vertex(-1f, 1f, -1f, -1f, 0f, 0f, 0f, 1f),

                new Vertex(-1f, -1f, 1f, 0f, -1f, 0f, 0f, 0f),
                new Vertex(1f, -1f, 1f, 0f, -1f, 0f, 1f, 0f),
                new Vertex(1f, -1f, -1f, 0f, -1f, 0f, 1f, 1f),
                new Vertex(-1f, -1f, -1f, 0f, -1f, 0f, 0f, 1f),

                new Vertex(-1f, -1f, 1f, 0f, 0f, 1f, 0f, 0f),
                new Vertex(-1f, 1f, 1f, 0f, 0f, 1f, 1f, 0f),
                new Vertex(1f, 1f, 1f, 0f, 0f, 1f, 1f, 1f),
                new Vertex(1f, -1f, 1f, 0f, 0f, 1f, 0f, 1f),

                new Vertex(-1f, 1f, -1f, 0f, 0f, -1f, 0f, 0f),
                new Vertex(-1f, -1f, -1f, 0f, 0f, -1f, 1f, 0f),
                new Vertex(1f, -1f, -1f, 0f, 0f, -1f, 1f, 1f),
                new Vertex(1f, 1f, -1f, 0f, 0f, -1f, 0f, 1f)
            });

            var indexBuffer = Context.Create.Buffer(BufferTarget.ElementArray, 36 * sizeof(ushort), BufferUsageHint.StaticDraw, new ushort[]
            {
                0, 1, 2, 0, 2, 3,
                4, 5, 6, 4, 6, 7,
                8, 9, 10, 8, 10, 11,
                12, 13, 14, 12, 14, 15,
                16, 17, 18, 16, 18, 19,
                20, 21, 22, 20, 22, 23
            });

            vertexArray = Context.Create.VertexArray();
            vertexArray.SetElementArrayBuffer(indexBuffer);
            vertexArray.SetVertexAttributeF(0, vertexBuffer, VertexAttributeDimension.Three, VertexAttribPointerType.Float, false, 32, 0);
            vertexArray.SetVertexAttributeF(1, vertexBuffer, VertexAttributeDimension.Three, VertexAttribPointerType.Float, false, 32, 12);
            vertexArray.SetVertexAttributeF(2, vertexBuffer, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, 32, 24);

            transformBuffer = Context.Create.Buffer(BufferTarget.Uniform, 64, BufferUsageHint.DynamicDraw);
            cameraBuffer    = Context.Create.Buffer(BufferTarget.Uniform, 64, BufferUsageHint.DynamicDraw);
#if INTEL_WORKAROUND
            cameraOutsideBuffer = Context.Create.Buffer(BufferTarget.Uniform, 64, BufferUsageHint.DynamicDraw);
#endif
            cameraExtraBuffer = Context.Create.Buffer(BufferTarget.Uniform, 12, BufferUsageHint.DynamicDraw);
            lightBuffer       = Context.Create.Buffer(BufferTarget.Uniform, 12, BufferUsageHint.DynamicDraw);

            using (var textureLoader = new TextureLoader("../Textures/DiffuseTest.png"))
            {
                diffuseMap = Context.Create.Texture2D(textureLoader.Width, textureLoader.Height, TextureHelper.CalculateMipCount(textureLoader.Width, textureLoader.Height, 1), Format.Rgba8);
                for (int i = 0; i < diffuseMap.MipCount; i++)
                {
                    diffuseMap.SetData(i, textureLoader.GetMipData(i), FormatColor.Rgba, FormatType.UnsignedByte);
                }
            }

            sampler = Context.Create.Sampler();
            sampler.SetMagFilter(TextureMagFilter.Linear);
            sampler.SetMinFilter(TextureMinFilter.LinearMipmapLinear);
            sampler.SetMaxAnisotropy(16f);

            IVertexShader   vsh = Context.Create.VertexShader(VertexShaderText);
            IFragmentShader fsh = Context.Create.FragmentShader(FragmentShaderText);
            program = Context.Create.Program(new ShaderProgramDescription
            {
                VertexShaders        = new[] { vsh },
                FragmentShaders      = new[] { fsh },
                VertexAttributeNames = new[] { "in_position", "in_normal", "in_tex_coord" },
                UniformBufferNames   = new[] { "Transform", "Camera", "Light" },
                SamplerNames         = new[] { "DiffuseMap" }
            });
        }
Ejemplo n.º 19
0
        public void Draw()
        {
            var sketches        = sketchService.GetSketches();
            var totalPointCount = sketches.Sum(x => x.Count);

            if (totalPointCount == 0)
            {
                return;
            }

            if (data == null || totalPointCount > data.Length)
            {
                vertexBuffer?.Dispose();
                var size = data?.Length ?? 1024;
                while (size < totalPointCount)
                {
                    size *= 2;
                }

                data         = new Vector2[size];
                vertexBuffer = glContext.Create.Buffer(BufferTarget.Array, data.Length * sizeof(Vector2), BufferUsageHint.DynamicDraw);
                vao.SetVertexAttributeF(0, vertexBuffer, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, sizeof(Vector2), 0);
            }

            int offset = 0;

            foreach (var sketch in sketches)
            {
                foreach (var point in sketch)
                {
                    data[offset++] = point;
                }
            }

            var sketchSize = totalPointCount * sizeof(Vector2);
            var map        = vertexBuffer.Map(0, sketchSize, MapAccess.Write | MapAccess.InvalidateRange);

            fixed(Vector2 *pData = data)
            PtrHelper.CopyBulk((byte *)map, (byte *)pData, sketchSize);

            vertexBuffer.Unmap();

            glContext.Bindings.Program.Set(shaderProgram);
            glContext.Bindings.VertexArray.Set(vao);

            glContext.States.DepthStencil.DepthTestEnable.Set(false);
            glContext.States.DepthStencil.DepthMask.Set(false);

            OpenTK.Graphics.OpenGL4.GL.LineWidth(6f);

            offset = 0;
            foreach (var sketch in sketches)
            {
                glContext.Actions.Draw.Arrays(BeginMode.LineStrip, offset, sketch.Count);
                offset += sketch.Count;
            }

            OpenTK.Graphics.OpenGL4.GL.LineWidth(1f);

            glContext.States.DepthStencil.DepthTestEnable.Set(true);
            glContext.States.DepthStencil.DepthMask.Set(true);
        }
Ejemplo n.º 20
0
        public unsafe SkyboxDrawer(IContext glContext)
        {
            this.glContext = glContext;
            sampler        = glContext.Create.Sampler();
            sampler.SetMagFilter(TextureMagFilter.Linear);
            sampler.SetMinFilter(TextureMinFilter.LinearMipmapLinear);
            sampler.SetWrapR(TextureWrapMode.Clamp);
            sampler.SetWrapS(TextureWrapMode.Clamp);
            sampler.SetWrapT(TextureWrapMode.Clamp);
            sampler.SetMaxAnisotropy(16f);

            #region Shader Text
            const string vertexShaderText =
                @"#version 150
layout(std140) uniform Camera
{
    mat4 ViewProjection;
};

in vec3 in_position;

out vec3 v_tex_coord;

void main()
{
    vec4 worldPosition = vec4(in_position, 1.0f);

    gl_Position = worldPosition * ViewProjection;
    gl_Position.y = -gl_Position.y;
    gl_Position.z = 2.0 * gl_Position.z - gl_Position.w;

    v_tex_coord = vec3(-worldPosition.x, worldPosition.y, worldPosition.z);
}
";

            const string fragmentShaderText =
                @"#version 150
uniform samplerCube Texture;

in vec3 v_tex_coord;

out vec4 out_color;

void main()
{
    out_color = texture(Texture, v_tex_coord);
}
";
            #endregion

            var vs = glContext.Create.VertexShader(vertexShaderText);
            var fs = glContext.Create.FragmentShader(fragmentShaderText);
            shaderProgram = glContext.Create.Program(new ShaderProgramDescription
            {
                VertexShaders        = new[] { vs },
                FragmentShaders      = new[] { fs },
                VertexAttributeNames = new[] { "in_position" },
                UniformBufferNames   = new[] { "Camera" },
                SamplerNames         = new[] { "Texture" }
            });

            var vertices = new[]
            {
                new Vector4(-10, -10, -1, 1),
                new Vector4(-10, 10, -1, 1),
                new Vector4(10, 10, -1, 1),
                new Vector4(10, -10, -1, 1),

                new Vector4(-10, -10, 1, 1),
                new Vector4(-10, 10, 1, 1),
                new Vector4(10, 10, 1, 1),
                new Vector4(10, -10, 1, 1),
            };

            var indices = new[]
            {
                0, 1, 2, 0, 2, 3,
                4, 5, 6, 4, 6, 7,
                0, 4, 7, 0, 7, 3,
                0, 4, 5, 0, 5, 1,
                1, 5, 6, 1, 6, 2,
                3, 7, 6, 3, 6, 2
            };

            IBuffer vertexBuffer;

            fixed(Vector4 *data = vertices)
            vertexBuffer = glContext.Create.Buffer(BufferTarget.Array, 4 * sizeof(float) * vertices.Length, BufferUsageHint.StaticDraw, (IntPtr)data);

            IBuffer indexBuffer;

            fixed(int *data = indices)
            indexBuffer = glContext.Create.Buffer(BufferTarget.ElementArray, sizeof(int) * indices.Length, BufferUsageHint.StaticDraw, (IntPtr)data);

            vao = glContext.Create.VertexArray();
            vao.SetVertexAttributeF(0, vertexBuffer, VertexAttributeDimension.Three, VertexAttribPointerType.Float, false, 4 * sizeof(float), 0);
            vao.SetElementArrayBuffer(indexBuffer);

            cameraUb = new UniformBufferSugar <Matrix4x4>(glContext);
        }
Ejemplo n.º 21
0
        public CursorRenderer(IClientSettings settings, IContext context, ITextureLoader textureLoader)
        {
            var header = settings.IsEs ? EsHeader : DesktopHeader;
            var vertexShader = context.Create.VertexShader(header + VertexShaderText);
            var fragmentShader = context.Create.FragmentShader(header + FragmentShaderText);
            program = context.Create.Program(new ShaderProgramDescription
            {
                VertexShaders = new[] { vertexShader },
                FragmentShaders = new[] { fragmentShader },
                VertexAttributeNames = new[] { "in_position", "in_tex_coord" },
                SamplerNames = new[] { "DiffTexture" }
            });

            var vertexBuffer = context.Create.Buffer(BufferTarget.ArrayBuffer, 4 * Vertex.Size, BufferUsageHint.StaticDraw, new[]
            {
                new Vertex(-1f, 1f, 0f, 0f),
                new Vertex(1f, 1f, 1f, 0f),
                new Vertex(1f, -1f, 1f, 1f),
                new Vertex(-1f, -1f, 0f, 1f)
            });

            var indexBuffer = context.Create.Buffer(BufferTarget.ElementArrayBuffer, 6 * sizeof(ushort), BufferUsageHint.StaticDraw, new ushort[]
            {
                0, 1, 2, 0, 2, 3
            });

            vertexArray = context.Create.VertexArray();
            vertexArray.SetVertexAttributeF(0, vertexBuffer, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, Vertex.Size, 0);
            vertexArray.SetVertexAttributeF(1, vertexBuffer, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, Vertex.Size, 2 * sizeof(float));
            vertexArray.SetElementArrayBuffer(indexBuffer);

            texture = textureLoader.LoadTexture(context, "Cursor.png");
            sampler = context.Create.Sampler();
            sampler.SetMagFilter(TextureMagFilter.Nearest);
        }
Ejemplo n.º 22
0
        public override void Initialize()
        {
            var vertexBuffer = Context.Create.Buffer(BufferTarget.Array, 3 * 8 * sizeof(float), BufferUsageHint.StaticDraw, new[]
            {
                new Vertex { Position = new Vector4(-0.5f, -0.5f, 0f, 1f), Color = new Color4(1, 0, 0, 1)},
                new Vertex { Position = new Vector4(0.0f, 0.5f, 0f, 1f), Color = new Color4(0, 1, 0, 1)},
                new Vertex { Position = new Vector4(0.5f, -0.5f, 0f, 1f), Color = new Color4(1, 1, 0, 1)}
            });

            var indexBuffer = Context.Create.Buffer(BufferTarget.ElementArray, 3 * sizeof(ushort), BufferUsageHint.StaticDraw, new ushort[] { 0, 1, 2 });

            vertexArray = Context.Create.VertexArray();
            vertexArray.SetElementArrayBuffer(indexBuffer);
            vertexArray.SetVertexAttributeF(0, vertexBuffer, VertexAttributeDimension.Four, VertexAttribPointerType.Float, false, 32, 0);
            vertexArray.SetVertexAttributeF(1, vertexBuffer, VertexAttributeDimension.Four, VertexAttribPointerType.Float, false, 32, 16);

            IVertexShader vsh = Context.Create.VertexShader(VertexShaderText);
            IFragmentShader fsh = Context.Create.FragmentShader(FragmentShaderText);
            program = Context.Create.Program(new ShaderProgramDescription
            {
                VertexShaders = new[] {vsh},
                FragmentShaders = new[] {fsh},
                VertexAttributeNames = new[] {"in_position", "in_color"}
            });
        }
Ejemplo n.º 23
0
        public override void Initialize()
        {
            var vertexBuffer = Context.Create.Buffer(BufferTarget.Array, 4 * 8 * sizeof(float), BufferUsageHint.StaticDraw, new[]
            {
                new Vertex(-1f, -1f, 0f, 1f),
                new Vertex(-1f, 1f, 0f, 0f),
                new Vertex(1f, 1f, 1f, 0f),
                new Vertex(1f, -1f, 1f, 1f),
            });

            var indexBuffer = Context.Create.Buffer(BufferTarget.ElementArray, 6 * sizeof(ushort), BufferUsageHint.StaticDraw, new ushort[]
            {
                0, 1, 2, 0, 2, 3
            });

            vertexArray = Context.Create.VertexArray();
            vertexArray.SetElementArrayBuffer(indexBuffer);
            vertexArray.SetVertexAttributeF(0, vertexBuffer, VertexAttributeDimension.Four, VertexAttribPointerType.Float, false, 32, 0);
            vertexArray.SetVertexAttributeF(1, vertexBuffer, VertexAttributeDimension.Four, VertexAttribPointerType.Float, false, 32, 16);

            pixelUnpackBuffer = Context.Create.Buffer(BufferTarget.PixelUnpack, 1024 * 1024 * 4, BufferUsageHint.StreamDraw);

            data = Enumerable.Range(0, 1024 * 1024 * 4 * 2).Select(x => (byte)(128.0 + 128.0 * Math.Sin((2.0 / 3.0) * Math.PI * (x % 4) + (double)x / 4 / 11111))).ToArray();

            diffuseMap = Context.Create.Texture2D(640, 360, 1, Format.Rgba8);

            sampler = Context.Create.Sampler();
            sampler.SetMagFilter(TextureMagFilter.Nearest);
            sampler.SetMinFilter(TextureMinFilter.Nearest);
            sampler.SetMaxAnisotropy(16f);

            var vsh = Context.Create.VertexShader(VertexShaderText);
            var fsh = Context.Create.FragmentShader(FragmentShaderText);
            program = Context.Create.Program(new ShaderProgramDescription
            {
                VertexShaders = new[] {vsh},
                FragmentShaders = new[] {fsh},
                VertexAttributeNames = new[] {"in_position", "in_tex_coord"},
                SamplerNames = new[] {"DiffuseMap"}
            });
        }