From 690cf0dabdfd87e6e0a481d3cc6f391d4a5f2500 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Mon, 25 Jan 2021 18:22:59 -0800 Subject: [PATCH] Texture --- content/Graphics/Resources/Buffer.md | 1 + content/Graphics/Resources/Texture.md | 51 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 content/Graphics/Resources/Texture.md diff --git a/content/Graphics/Resources/Buffer.md b/content/Graphics/Resources/Buffer.md index 75a408e..9995285 100644 --- a/content/Graphics/Resources/Buffer.md +++ b/content/Graphics/Resources/Buffer.md @@ -1,6 +1,7 @@ --- title: "Buffer" date: 2021-01-25T14:41:43-08:00 +weight: 1 --- Buffers are basically containers for data. They are the bread and butter of any renderer. diff --git a/content/Graphics/Resources/Texture.md b/content/Graphics/Resources/Texture.md new file mode 100644 index 0000000..ce001ea --- /dev/null +++ b/content/Graphics/Resources/Texture.md @@ -0,0 +1,51 @@ +--- +title: "Texture" +date: 2021-01-25T14:59:49-08:00 +weight: 2 +--- + +Textures are containers for pixel data. They always have a width, height, and color format. The most simple and common kind of texture is just a 2D texture. But textures can also have depth (3D textures), multiple layers (cubemaps), multiple resolution levels (mipmap levels), or a multisample count. Describing the use cases for all of these is beyond the scope of this tutorial, but they should come up as you learn how to execute certain rendering techniques. + +To create a new texture resource, you can set up a `TextureCreateInfo` struct: + +```cs +var textureCreateInfo = new TextureCreateInfo +{ + Width = 128, + Height = 128, + Depth = 1, + Format = ColorFormat.R8G8B8A8, + IsCube = false, + LevelCount = 1, + SampleCount = SampleCount.One, + UsageFlags = TextureUsageFlags.SamplerBit +}; + +var myTexture = new Texture(GraphicsDevice, textureCreateInfo); +``` + +This sets up 128x128 2D Texture that uses 32 bits per pixel: 8 bits each for red, green, blue, and alpha. +We also indicate that we intend to use the texture for sampling. + +To put pixels in the texture, we use the `SetData` method. + +```cs +var pixels = new Color[width * height]; +// set pixel array data here + +myTexture.SetData(pixels); +``` + +Of course, having to fill in all this data every time is kind of cumbersome, so we have some shortcut methods for common texture usages. + +```cs +var my2DTexture = Texture.CreateTexture2D(GraphicsDevice, 128, 128, ColorFormat.R8G8B8A8, TextureUsageFlags.SamplerBit); +var my3DTexture = Texture.CreateTexture3D(GraphicsDevice, 128, 128, ColorFormat.R8G8B8A8, TextureUsageFlags.SamplerBit); +var myCubeTexture = Texture.CreateTextureCube(GraphicsDevice, 128, ColorFormat.R8G8B8A8, TextureUsageFlags.SamplerBit); +``` + +There is also a built-in PNG loader that creates a 2D R8G8B8A8 texture and fills it with pixel data. + +```cs +var myPNGTexture = Texture.LoadPNG(GraphicsDevice, "coldsteel_the_hedgehog.png"); +```