예제 #1
0
        public unsafe ITexture2D GetGlTexture2D()
        {
            if (image is Ogl3TextureImage oglCgImage)
            {
                return(oglCgImage.GlTexture);
            }

            if (!isDirty)
            {
                return(glTexture2D);
            }

            var size = image.Size;

            if (glTexture2D == null || glTexture2D.Width != size.Width || glTexture2D.Height != size.Height)
            {
                glTexture2D?.Dispose();
                var mipCount = image.Volatility != ResourceVolatility.Volatile
                    ? GraphicsHelper.TextureMipCount(size.Width, size.Height)
                    : 1; // To side-step the sRGB issue with GenerateMipmaps
                glTexture2D = infra.GlContext.Create.Texture2D(size.Width, size.Height, mipCount, format);
            }

            fixed(byte *pRawData = image.GetRawData())
            glTexture2D.SetData(0, (IntPtr)pRawData, FormatColor.Bgra, FormatType.UnsignedByte);

            if (glTexture2D.MipCount != 1)
            {
                glTexture2D.GenerateMipmap();
            }
            isDirty = false;

            return(glTexture2D);
        }
예제 #2
0
        public void Attach()
        {
            _renderer.Init();

            var vertexBuffer = _factory.Create <IVertexBuffer>();

            vertexBuffer.SetData(_vertices);
            vertexBuffer.AddLayout <float>(3);
            vertexBuffer.AddLayout <float>(2);

            var indexBuffer = _factory.Create <IIndexBuffer>();

            indexBuffer.AddData(_indices);

            _vertexArray = _factory.Create <IVertexArray>();
            _vertexArray.VertexBuffer = vertexBuffer;
            _vertexArray.IndexBuffer  = indexBuffer;

            _shader = _factory.Create <IShader>();
            _shader.Compile("Assets/shader.vert", "Assets/shader.frag");
            _shader.SetInt("texture0", 0);

            _texture = _factory.Create <ITexture2D>();
            _texture.SetData("Assets/picture.png");

            _position = _camera.Position;
        }
예제 #3
0
        public void InitializeTexture(ITexture2D texture)
        {
            if (data.Length < texture.Width * texture.Height * 4)
                data = Enumerable.Range(0, texture.Width * texture.Height * 4).Select(x => (byte)255).ToArray();

            for (int i = 0; i < texture.MipCount; i++)
                texture.SetData(0, data, FormatColor.Bgra, FormatType.UnsignedByte);
        }
예제 #4
0
        private void RenderTexture()
        {
            var desiredWidth  = (int)RenderSurface.ActualWidth;
            var desiredHeight = (int)RenderSurface.ActualHeight;

            // Redraw occurs on dispose
            using (var rc = RenderSurface.GetRenderContext())
            {
                // Copy pixel data
                _texture.SetData(_pixels);

                // Render
                rc.DrawTexture(_texture, new Rect(new Size(desiredWidth, desiredHeight)));
            }
        }
예제 #5
0
        public void Initialize()
        {
            _onePixel = new PlatformTexture2D(0, _device, new Texture2D(_device, 1, 1));
            _onePixel.SetData(new Color[1] {
                new Color(Microsoft.Xna.Framework.Color.Magenta.PackedValue)
            });

            var files = _fileSystem.FindAssets("textures/**/*", FileType.IMAGE);

            foreach (var it in files)
            {
                LoadFromFile(it);
            }

            Track(files.Observe(file => _modifiedFiles.Add(file)));
        }
예제 #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" }
            });
        }
예제 #7
0
        public unsafe override void OnNewFrame(float totalSeconds, float elapsedSeconds)
        {
            fixed(byte *pData = data)
            pixelUnpackBuffer.SetDataByMapping((IntPtr)pData + offset);

            offset = (offset + 1024) % (data.Length / 2);
            diffuseMap.SetData(0, IntPtr.Zero, FormatColor.Rgba, FormatType.UnsignedByte, pixelUnpackBuffer);

            Context.Actions.ClearWindowColor(new Color4(0, 0, 0, 0));
            Context.Actions.ClearWindowDepthStencil(DepthStencil.Both, 1f, 0);

            Context.States.ScreenClipping.United.Viewport.Set(GameWindow.Width, GameWindow.Height);

            Context.Bindings.Program.Set(program);
            Context.Bindings.VertexArray.Set(vertexArray);

            Context.Bindings.Textures.Units[0].Set(diffuseMap);
            Context.Bindings.Samplers[0].Set(sampler);

            Context.Actions.Draw.Elements(BeginMode.Triangles, 6, DrawElementsType.UnsignedShort, 0);
        }
예제 #8
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"}
            });
        }
예제 #9
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"}
            });
        }
예제 #10
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" }
            });
        }
예제 #11
0
 public static void SetData(this ITexture2D texture, int level, IntPtr data, FormatColor format, FormatType type, IBuffer pixelUnpackBuffer = null)
 {
     texture.SetData(level, 0, 0, texture.CalculateMipWidth(level), texture.CalculateMipHeight(level), data, format, type, pixelUnpackBuffer);
 }
예제 #12
0
파일: Layer2D.cs 프로젝트: ejrich/Pretend
        public void Attach()
        {
            _texture = _factory.Create <ITexture2D>();
            _texture.SetData("Assets/picture.png");

            _texture2 = _factory.Create <ITexture2D>();
            _texture2.SetData("Assets/picture2.png");

            _scene.Init();
            _physicsContainer.Gravity = new Vector3(0, -800, 0);

            var entity = _scene.CreateEntity();

            _scene.AddComponent(entity, new CameraComponent {
                Camera = _camera, Active = true
            });
            _scene.AddComponent(entity, new CameraScript(_camera));

            entity = _scene.CreateEntity();
            _scene.AddComponent(entity, new PositionComponent {
                Position = new Vector3(-100, 400, 0)
            });
            _scene.AddComponent(entity, new SizeComponent {
                Width = 300, Height = 300
            });
            _scene.AddComponent(entity, new ColorComponent {
                Color = new Vector4(0.5f, 0.5f, 0.5f, 1f)
            });

            entity = _scene.CreateEntity();
            var positionComponent = new PositionComponent {
                Position = new Vector3(400, -100, 0)
            };

            _scene.AddComponent(entity, positionComponent);
            _scene.AddComponent(entity, new SizeComponent {
                Width = 400, Height = 300
            });
            _scene.AddComponent(entity, new ColorComponent {
                Color = new Vector4(1, 0, 1, 1)
            });
            _scene.AddComponent(entity, new TextureComponent {
                Texture = _texture
            });
            _scene.AddComponent(entity, new DiceScript(positionComponent));

            entity = _scene.CreateEntity();
            _scene.AddComponent(entity, new PositionComponent {
                Position = new Vector3(-400, -100, 0)
            });
            _scene.AddComponent(entity, new SizeComponent {
                Width = 300, Height = 300
            });
            _scene.AddComponent(entity, new TextureComponent {
                Texture = _texture2
            });
            _scene.AddComponent(entity, new PhysicsComponent {
                Velocity = new Vector3(300, 500, 0)
            });

            entity = _scene.CreateEntity();
            _scene.AddComponent(entity, new PositionComponent {
                Position = new Vector3(0, -360, 0)
            });
            _scene.AddComponent(entity, new SizeComponent {
                Width = 1280, Height = 10
            });
            _scene.AddComponent(entity, new PhysicsComponent {
                Fixed = true
            });
        }