91 lines
3.1 KiB
C#
91 lines
3.1 KiB
C#
using Engine.Graphics.Framebuffer;
|
|
using Engine.Graphics.Pixel;
|
|
using OpenTK.Graphics.OpenGL;
|
|
using Serilog;
|
|
|
|
namespace Engine.Graphics.Texture;
|
|
|
|
/// <summary>
|
|
/// Represents a dynamic texture that can be resized and attached to a framebuffer.
|
|
/// </summary>
|
|
public class DynamicTexture : Texture, IFramebufferAttachment
|
|
{
|
|
/// <summary>
|
|
/// The pixel format of the texture.
|
|
/// </summary>
|
|
private readonly PixelFormat _format;
|
|
|
|
/// <summary>
|
|
/// The pixel type of the texture.
|
|
/// </summary>
|
|
private readonly PixelType _type;
|
|
|
|
/// <summary>
|
|
/// The internal format of the texture.
|
|
/// </summary>
|
|
private readonly PixelInternalFormat _internalFormat;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="DynamicTexture"/> class.
|
|
/// The texture is created with the specified format, type, and internal format.
|
|
/// </summary>
|
|
/// <param name="parWidth">The width of the texture.</param>
|
|
/// <param name="parHeight">The height of the texture.</param>
|
|
/// <param name="parFormat">The format of the texture.</param>
|
|
/// <param name="parType">The type of the texture.</param>
|
|
/// <param name="parInternalFormat">The internal format of the texture.</param>
|
|
internal DynamicTexture(int parWidth, int parHeight, PixelFormat parFormat, PixelType parType,
|
|
PixelInternalFormat parInternalFormat)
|
|
: base(parWidth, parHeight)
|
|
{
|
|
_format = parFormat;
|
|
_type = parType;
|
|
_internalFormat = parInternalFormat;
|
|
|
|
GL.BindTexture(TextureTarget.Texture2D, Handle);
|
|
GL.TexImage2D(TextureTarget.Texture2D, 0, _internalFormat, Width, Height, 0, _format, _type,
|
|
IntPtr.Zero);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a new <see cref="DynamicTexture"/> with the specified width, height, and pixel type.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of pixel used for the texture.</typeparam>
|
|
/// <param name="parWidth">The width of the texture.</param>
|
|
/// <param name="parHeight">The height of the texture.</param>
|
|
/// <returns>A new instance of <see cref="DynamicTexture"/>.</returns>
|
|
public static DynamicTexture Create<T>(int parWidth, int parHeight)
|
|
where T : struct, IPixel
|
|
{
|
|
var pixel = default(T);
|
|
return new DynamicTexture(parWidth, parHeight, pixel.Format, pixel.Type, pixel.InternalFormat);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Resizes the texture to the specified width and height.
|
|
/// </summary>
|
|
/// <param name="parWidth">The new width of the texture.</param>
|
|
/// <param name="parHeight">The new height of the texture.</param>
|
|
public void Resize(int parWidth, int parHeight)
|
|
{
|
|
if (Width == parWidth && Height == parHeight)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Width = parWidth;
|
|
Height = parHeight;
|
|
|
|
Bind();
|
|
GL.TexImage2D(TextureTarget.Texture2D, 0, _internalFormat, Width, Height, 0, _format, _type,
|
|
IntPtr.Zero);
|
|
|
|
Log.Debug("Texture {Handle} resized to {Width}x{Height}", Handle, Width, Height);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public void Attach(Framebuffer.Framebuffer parFramebuffer, FramebufferAttachment parAttachment)
|
|
{
|
|
GL.NamedFramebufferTexture(parFramebuffer.Handle, parAttachment, Handle, 0);
|
|
}
|
|
} |