Beispiel #1
0
        public bool LoadFromKtx(string path)
        {
            string iblPath = path + "_ibl.ktx";

            if (!File.Exists(iblPath))
            {
                return(false);
            }

            string skyPath = path + "_skybox.ktx";

            if (!File.Exists(skyPath))
            {
                return(false);
            }

            var iblKtx = CreateKtx(iblPath);
            var skyKtx = CreateKtx(skyPath);

            _skyboxTexture = KtxUtility.CreateTexture(_engine, skyKtx, false);
            _texture       = KtxUtility.CreateTexture(_engine, iblKtx, false);

            if (!iblKtx.GetSphericalHarmonics(out _bands))
            {
                return(false);
            }

            IndirectLight = IndirectLightBuilder.Create()
                            .WithReflections(_texture)
                            .WithIntensity(Intensity)
                            .Build(_engine);

            Skybox = SkyboxBuilder.Create()
                     .WithEnvironment(_skyboxTexture)
                     .WithSun(true)
                     .Build(_engine);

            return(true);
        }
        static void Main(string[] args)
        {
            VertexBuffer     vertexBuffer     = null;
            IndexBuffer      indexBuffer      = null;
            Material         material         = null;
            MaterialInstance materialInstance = null;
            Texture          splatTexture     = null;
            int    renderable   = -1;
            int    cameraEntity = -1;
            Camera camera       = null;
            Skybox skybox       = null;

            var app = new Application(
                new WindowConfig()
            {
                Title = "point_sprites",
            },
                new ApplicationConfig()
                );

            app.Setup = (engine, view, scene) => {
                splatTexture = CreateSplatTexture(engine);

                Vertex[] kVertices   = new Vertex[NUM_POINTS];
                float[]  kPointSizes = new float[NUM_POINTS];
                ushort[] kIndices    = new ushort[NUM_POINTS];

                var dtheta = MathF.PI * 2 / NUM_POINTS;
                var dsize  = MAX_POINT_SIZE / NUM_POINTS;
                var dcolor = 256.0f / NUM_POINTS;

                byte[]       vbo       = new byte[NUM_POINTS * (8 + 4)];
                MemoryStream vboStream = new MemoryStream(vbo);
                BinaryWriter vboWriter = new BinaryWriter(vboStream);

                for (int i = 0; i < NUM_POINTS; i++)
                {
                    float theta = dtheta * i;
                    uint  c     = (uint)(dcolor * i);
                    kVertices[i].Position.X = MathF.Cos(theta);
                    kVertices[i].Position.Y = MathF.Sin(theta);
                    kVertices[i].Color      = 0xff000000u | c | (c << 8) | (c << 16);
                    kPointSizes[i]          = MIN_POINT_SIZE + dsize * i;
                    kIndices[i]             = (ushort)i;

                    vboWriter.Write(kVertices[i].Position.X);
                    vboWriter.Write(kVertices[i].Position.Y);
                    vboWriter.Write(kVertices[i].Color);
                }

                vertexBuffer = VertexBufferBuilder.Create()
                               .WithVertexCount(NUM_POINTS)
                               .WithBufferCount(2)
                               .WithAttribute(VertexAttribute.Position, 0, ElementType.Float2, 0, Marshal.SizeOf <Vertex>())
                               .WithAttribute(VertexAttribute.Color, 0, ElementType.UByte4, Marshal.SizeOf <Vector2>(), Marshal.SizeOf <Vertex>())
                               .WithNormalized(VertexAttribute.Color)
                               .WithAttribute(VertexAttribute.Custom0, 1, ElementType.Float, 0, Marshal.SizeOf <float>())
                               .Build(engine);
                vertexBuffer.SetBufferAt(engine, 0, vbo);
                vertexBuffer.SetBufferAt(engine, 1, kPointSizes);

                indexBuffer = IndexBufferBuilder.Create()
                              .WithIndexCount(NUM_POINTS)
                              .WithBufferType(IndexType.UShort)
                              .Build(engine);
                indexBuffer.SetBuffer(engine, kIndices);

                var sampleData = new SampleDataLoader();

                material = MaterialBuilder.Create()
                           .WithPackage(sampleData.LoadPointSprites())
                           .Build(engine);

                renderable = EntityManager.Create();

                materialInstance = material.CreateInstance();
                materialInstance.SetParameter("fade", splatTexture, new TextureSampler(SamplerMinFilter.Linear, SamplerMagFilter.Linear));

                RenderableBuilder.Create()
                .WithBoundingBox(
                    new Box(
                        new Vector3(-1, -1, -1),
                        new Vector3(1, 1, 1)
                        )
                    )
                .WithMaterial(0, materialInstance)
                .WithGeometry(0, PrimitiveType.Points, vertexBuffer, indexBuffer, 0, NUM_POINTS)
                .WithCulling(false)
                .WithReceiveShadows(false)
                .WithCastShadows(false)
                .Build(engine, renderable);

                scene.AddEntity(renderable);
                cameraEntity = EntityManager.Create();
                camera       = engine.CreateCamera(cameraEntity);

                view.Camera = camera;

                skybox = SkyboxBuilder.Create()
                         .WithColor(new Color(0.1f, 0.125f, 0.25f, 1.0f))
                         .Build(engine);
                scene.Skybox = skybox;
            };

            app.Animate = (engine, view, now) => {
                var zoom   = 1.5f;
                var width  = view.Viewport.Width;
                var height = view.Viewport.Height;
                var aspect = (float)width / height;

                camera.SetProjection(Projection.Ortho, -aspect * zoom, aspect * zoom, -zoom, zoom, 0, 1);

                var tcm = engine.TransformManager;
                tcm.SetTransform(tcm.GetInstance(renderable), Matrix4x4.CreateFromAxisAngle(Vector3.UnitZ, now));
            };

            app.Cleanup = (engine, view, scene) => {
                engine.Destroy(skybox);
                engine.Destroy(renderable);
                engine.Destroy(materialInstance);
                engine.Destroy(material);
                engine.Destroy(vertexBuffer);
                engine.Destroy(indexBuffer);

                engine.DestroyCameraComponent(cameraEntity);
                EntityManager.Destroy(cameraEntity);
            };

            app.Run();
        }
        static void Main(string[] args)
        {
            Skybox       skybox       = null;
            VertexBuffer vertexBuffer = null;
            IndexBuffer  indexBuffer  = null;
            Material     material     = null;
            int          renderable   = -1;
            Camera       camera       = null;
            int          cameraEntity = -1;

            var vbo = new VertexBufferObject();

            vbo.Write(new Vector2(1, 0));
            vbo.Write(0xffff0000u);
            vbo.Write(new Vector2(MathF.Cos(MathF.PI * 2 / 3), MathF.Sin(MathF.PI * 2 / 3)));
            vbo.Write(0xff00ff00u);
            vbo.Write(new Vector2(MathF.Cos(MathF.PI * 4 / 3), MathF.Sin(MathF.PI * 4 / 3)));
            vbo.Write(0xff0000ffu);

            var app = new Application(
                new WindowConfig()
            {
                Title = "hellotriangle",
            },
                new ApplicationConfig()
                );

            app.Setup = (engine, view, scene) => {
                skybox = SkyboxBuilder.Create()
                         .WithColor(new Color(0.1f, 0.125f, 0.25f, 1.0f))
                         .Build(engine);

                scene.Skybox = skybox;
                view.PostProcessingEnabled = false;

                vertexBuffer = VertexBufferBuilder.Create()
                               .WithVertexCount(3)
                               .WithBufferCount(1)
                               .WithAttribute(VertexAttribute.Position, 0, ElementType.Float2, 0, 12)
                               .WithAttribute(VertexAttribute.Color, 0, ElementType.UByte4, 8, 12)
                               .WithNormalized(VertexAttribute.Color)
                               .Build(engine);
                vertexBuffer.SetBufferAt(engine, 0, vbo);

                indexBuffer = IndexBufferBuilder.Create()
                              .WithIndexCount(3)
                              .WithBufferType(IndexType.UShort)
                              .Build(engine);
                indexBuffer.SetBuffer(engine, TRIANGLE_INDICES);

                var sampleData = new SampleDataLoader();

                material = MaterialBuilder.Create()
                           .WithPackage(sampleData.LoadBakedColor())
                           .Build(engine);

                renderable = EntityManager.Create();

                RenderableBuilder.Create()
                .WithBoundingBox(
                    new Box(
                        new Vector3(-1, -1, -1),
                        new Vector3(1, 1, 1)
                        )
                    )
                .WithMaterial(0, material.DefaultInstance)
                .WithGeometry(0, PrimitiveType.Triangles, vertexBuffer, indexBuffer, 0, 3)
                .WithCulling(false)
                .WithReceiveShadows(false)
                .WithCastShadows(false)
                .Build(engine, renderable);

                scene.AddEntity(renderable);

                cameraEntity = EntityManager.Create();
                camera       = engine.CreateCamera(cameraEntity);
                view.Camera  = camera;
            };

            app.Cleanup = (engine, view, scene) => {
                engine.Destroy(skybox);
                engine.Destroy(renderable);
                engine.Destroy(material);
                engine.Destroy(vertexBuffer);
                engine.Destroy(indexBuffer);
                engine.DestroyCameraComponent(cameraEntity);

                EntityManager.Destroy(cameraEntity);
            };

            app.Animate = (engine, view, now) => {
                var ZOOM = 1.5f;
                var w    = view.Viewport.Width;
                var h    = view.Viewport.Height;
                ;
                var aspect = (float)w / h;

                camera.SetProjection(Projection.Ortho, -aspect * ZOOM, aspect * ZOOM, -ZOOM, ZOOM, 0, 1);

                var tcm = engine.TransformManager;
                tcm.SetTransform(tcm.GetInstance(renderable), Matrix4x4.CreateFromAxisAngle(Vector3.UnitZ, now));
            };

            app.Run();
        }
        static void Main(string[] args)
        {
            Texture          texture          = null;
            Skybox           skybox           = null;
            VertexBuffer     vertexBuffer     = null;
            IndexBuffer      indexBuffer      = null;
            Material         material         = null;
            MaterialInstance materialInstance = null;
            Camera           camera           = null;
            int cameraEntity = -1;
            int renderable   = -1;

            var app = new Application(
                new WindowConfig()
            {
                Title = "texturedquad"
            },
                new ApplicationConfig()
                );

            app.Setup = (engine, view, scene) => {
                var path = Path.Combine(app.RootAssetPath, "textures/Moss_01/Moss_01_Color.png");

                if (!File.Exists(path))
                {
                    Console.WriteLine("The texture {0} does not exist", path);
                    Environment.Exit(1);
                }

                using (var stream = File.OpenRead(path)) {
                    var imageResult = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha);
                    Console.WriteLine("Loaded texture: {0}x{1}", imageResult.Width, imageResult.Height);

                    var pixelBuffer = new PixelBufferDescriptor(imageResult.Data, PixelDataFormat.Rgba, PixelDataType.UByte);

                    texture = TextureBuilder.Create()
                              .WithWidth(imageResult.Width)
                              .WithHeight(imageResult.Height)
                              .WithLevels(1)
                              .WithSampler(TextureSamplerType.Texture2d)
                              .WithFormat(TextureFormat.Rgba8)
                              .Build(engine);
                    texture.SetImage(engine, 0, pixelBuffer);
                }

                // Set up view
                skybox = SkyboxBuilder.Create()
                         .WithColor(new Color(0.1f, 0.125f, 0.25f, 1.0f))
                         .Build(engine);

                scene.Skybox = skybox;
                cameraEntity = EntityManager.Create();
                camera       = engine.CreateCamera(cameraEntity);

                view.PostProcessingEnabled = false;
                view.Camera = camera;

                // Create quad renderable
                var vbo = new VertexBufferObject();
                vbo.Write(new Vector2(-1, -1));
                vbo.Write(new Vector2(0, 0));
                vbo.Write(new Vector2(1, -1));
                vbo.Write(new Vector2(1, 0));
                vbo.Write(new Vector2(-1, 1));
                vbo.Write(new Vector2(0, 1));
                vbo.Write(new Vector2(1, 1));
                vbo.Write(new Vector2(1, 1));

                vertexBuffer = VertexBufferBuilder.Create()
                               .WithVertexCount(4)
                               .WithBufferCount(1)
                               .WithAttribute(VertexAttribute.Position, 0, ElementType.Float2, 0, 16)
                               .WithAttribute(VertexAttribute.Uv0, 0, ElementType.Float2, 8, 16)
                               .Build(engine);
                vertexBuffer.SetBufferAt(engine, 0, vbo);

                var sampleData = new SampleDataLoader();

                indexBuffer = IndexBufferBuilder.Create()
                              .WithIndexCount(6)
                              .WithBufferType(IndexType.UShort)
                              .Build(engine);
                indexBuffer.SetBuffer(engine, QUAD_INDICES);

                material = MaterialBuilder.Create()
                           .WithPackage(sampleData.LoadBakedTexture())
                           .Build(engine);

                var sampler = new TextureSampler(SamplerMinFilter.Linear, SamplerMagFilter.Linear);

                materialInstance = material.CreateInstance();
                materialInstance.SetParameter("albedo", texture, sampler);

                renderable = EntityManager.Create();

                RenderableBuilder.Create()
                .WithBoundingBox(
                    new Box(
                        new Vector3(-1, -1, -1),
                        new Vector3(1, 1, 1)
                        )
                    )
                .WithMaterial(0, materialInstance)
                .WithGeometry(0, PrimitiveType.Triangles, vertexBuffer, indexBuffer, 0, 6)
                .WithCulling(false)
                .WithReceiveShadows(false)
                .WithCastShadows(false)
                .Build(engine, renderable);

                scene.AddEntity(renderable);
            };

            app.Cleanup = (engine, view, scene) => {
                engine.Destroy(skybox);
                engine.Destroy(renderable);
                engine.Destroy(materialInstance);
                engine.Destroy(material);
                engine.Destroy(texture);
                engine.Destroy(vertexBuffer);
                engine.Destroy(indexBuffer);

                engine.DestroyCameraComponent(cameraEntity);
                EntityManager.Destroy(cameraEntity);
            };

            app.Animate = (engine, view, now) => {
                var zoom   = 2.0f + 2.0f * MathF.Sin(now);
                var width  = view.Viewport.Width;
                var height = view.Viewport.Height;
                var aspect = (float)width / (float)height;

                camera.SetProjection(Projection.Ortho,
                                     -aspect * zoom, aspect * zoom,
                                     -zoom, zoom,
                                     -1, 1
                                     );
            };

            app.Run();
        }
Beispiel #5
0
        public bool LoadFromDirectory(string path)
        {
            // First check if KTX files are available.
            if (LoadFromKtx(Path.Combine(path, Path.GetFileName(path))))
            {
                return(true);
            }

            // Read spherical harmonics
            string sh      = Path.Combine(path, "sh.txt");
            var    pattern = new Regex(@"^\([\s]{0,}(.+),[\s]{0,}(.+),[\s]{0,}(.+)\)", RegexOptions.Compiled);

            if (File.Exists(sh))
            {
                var lines = File.ReadAllLines(sh);

                if (lines.Length != _bands.Length)
                {
                    return(false);
                }

                for (var i = 0; i < _bands.Length; i++)
                {
                    var matches = pattern.Matches(lines[i]);

                    if (!pattern.IsMatch(lines[0]))
                    {
                        return(false);
                    }

                    var parts = matches[0].Groups;

                    if (!float.TryParse(parts[1].Value, out var r))
                    {
                        return(false);
                    }

                    if (!float.TryParse(parts[2].Value, out var g))
                    {
                        return(false);
                    }

                    if (!float.TryParse(parts[3].Value, out var b))
                    {
                        return(false);
                    }

                    _bands[i] = new Vector3(r, g, b);
                }
            }
            else
            {
                return(false);
            }

            // Read mip-mapped cubemap
            string prefix = "m";

            if (!LoadCubemapLevel(ref _texture, path, 0, prefix + "0_"))
            {
                return(false);
            }

            int numLevels = _texture.Levels;

            for (var i = 1; i < numLevels; i++)
            {
                LoadCubemapLevel(ref _texture, path, i, prefix + i + "_");
            }

            if (!LoadCubemapLevel(ref _skyboxTexture, path))
            {
                return(false);
            }

            IndirectLight = IndirectLightBuilder.Create()
                            .WithReflections(_texture)
                            .WithIrradiance(3, _bands)
                            .WithIntensity(Intensity)
                            .Build(_engine);

            Skybox = SkyboxBuilder.Create()
                     .WithEnvironment(_skyboxTexture)
                     .WithSun(true)
                     .Build(_engine);

            return(true);
        }