示例#1
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);
        }