Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Texture Creation Settings #16

Merged
merged 13 commits into from
Jul 13, 2024
1 change: 1 addition & 0 deletions Hypercube.Client/Graphics/Drawing/RenderDrawing.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Hypercube.Client.Graphics.Rendering;
using Hypercube.Client.Graphics.Texturing;
using Hypercube.Client.Graphics.Texturing.TextureSettings;
using Hypercube.Shared.Dependency;
using Hypercube.Shared.Math;
using Hypercube.Shared.Math.Box;
Expand Down
4 changes: 4 additions & 0 deletions Hypercube.Client/Graphics/Rendering/Renderer.Render.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using Hypercube.Client.Graphics.Event;
using Hypercube.Client.Graphics.Shading;
using Hypercube.Client.Graphics.Texturing;
using Hypercube.Client.Graphics.Texturing.TextureSettings;
using Hypercube.Client.Graphics.Viewports;
using Hypercube.Shared.Math;
using Hypercube.Shared.Math.Box;
using Hypercube.Client.Graphics.Windows;
using Hypercube.Shared.Math.Matrix;
using Hypercube.Shared.Runtimes.Loop.Event;
Expand Down
4 changes: 2 additions & 2 deletions Hypercube.Client/Graphics/Texturing/ITextureHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

public interface ITextureHandle
{
public int Handle { get; }
public ITexture Texture { get; }
int Handle { get; }
ITexture Texture { get; }

void Bind();
}
5 changes: 4 additions & 1 deletion Hypercube.Client/Graphics/Texturing/ITextureManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Hypercube.Shared.Resources;
using Hypercube.Client.Graphics.Texturing.TextureSettings;
using Hypercube.Shared.Resources;

namespace Hypercube.Client.Graphics.Texturing;

Expand All @@ -13,6 +14,8 @@ public interface ITextureManager
/// <returns>ITexture</returns>
ITexture Create(ResourcePath path, bool doFlip);

ITextureHandle CreateHandler(ResourcePath path, ITextureCreationSettings settings);
JerryImMouse marked this conversation as resolved.
Show resolved Hide resolved
ITextureHandle CreateHandler(ITexture texture, ITextureCreationSettings settings);
ITextureHandle GetHandler(ResourcePath path);
ITextureHandle GetHandler(ITexture texture);
}
32 changes: 22 additions & 10 deletions Hypercube.Client/Graphics/Texturing/TextureHandle.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
using OpenToolkit.Graphics.OpenGL4;
using Hypercube.Client.Graphics.Texturing.TextureSettings;
using Hypercube.Client.Utilities;
using OpenToolkit.Graphics.OpenGL4;

namespace Hypercube.Client.Graphics.Texturing;

public class TextureHandle : ITextureHandle
JerryImMouse marked this conversation as resolved.
Show resolved Hide resolved
public sealed class TextureHandle : ITextureHandle
{
public int Handle { get; init; }
public ITexture Texture { get; init; }
Tornado-Technology marked this conversation as resolved.
Show resolved Hide resolved

public TextureHandle(ITexture texture)
public TextureHandle(ITexture texture, ITextureCreationSettings settings)
{
Handle = GL.GenTexture();
Texture = texture;
var target = settings.TextureTarget.ToOpenToolkit();

GL.BindTexture(TextureTarget.Texture2D, Handle);
GL.BindTexture(target, Handle);

foreach (var param in settings.Parameters)
{
GL.TexParameter(target, param.Name.ToOpenToolkit(), param.Value);
}

GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexImage2D(
settings.TextureTarget.ToOpenToolkit(),
settings.Level,
settings.PixelInternalFormat.ToOpenToolkit(),
texture.Width, texture.Height,
settings.Border,
settings.PixelFormat.ToOpenToolkit(),
settings.PixelType.ToOpenToolkit(),
texture.Data);

GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, texture.Width, texture.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, texture.Data);
GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
GL.GenerateMipmap((GenerateMipmapTarget)settings.TextureTarget);
}

public void Bind()
Expand Down
16 changes: 14 additions & 2 deletions Hypercube.Client/Graphics/Texturing/TextureManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Hypercube.Shared.Dependency;
using Hypercube.Client.Graphics.Texturing.TextureSettings;
using Hypercube.Shared.Dependency;
using Hypercube.Shared.Resources;
using Hypercube.Shared.Resources.Manager;
using StbImageSharp;
Expand Down Expand Up @@ -43,12 +44,23 @@ public ITexture Create(ResourcePath path, bool doFlip)
return _cachedTextures[path] = texture;
}

public ITextureHandle CreateHandler(ITexture texture, ITextureCreationSettings settings)
{
return new TextureHandle(texture, settings);
}


public ITextureHandle CreateHandler(ResourcePath path, ITextureCreationSettings settings)
{
return CreateHandler(Create(path), settings);
}

public ITextureHandle GetHandler(ITexture texture)
{
if (_cachedHandles.TryGetValue(texture, out var result))
return result;

return _cachedHandles[texture] = new TextureHandle(texture);
return _cachedHandles[texture] = new TextureHandle(texture, new Texture2DCreationSettings());
}

public ITextureHandle GetHandler(ResourcePath path)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Hypercube.Client.Graphics.Texturing.TextureSettings.TextureParameters;

namespace Hypercube.Client.Graphics.Texturing.TextureSettings;

public interface ITextureCreationSettings
{
TextureTarget TextureTarget { get; }
HashSet<TextureParameter> Parameters { get; }
PixelInternalFormat PixelInternalFormat { get; }
int Level { get; }
int Border { get; }
PixelFormat PixelFormat { get; }
PixelType PixelType { get; }

bool Flip { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using OpenToolkit.Graphics.OpenGL4;
using PixelFormat = Hypercube.Client.Graphics.Texturing.TextureSettings.TextureParameters.PixelFormat;
using PixelInternalFormat = Hypercube.Client.Graphics.Texturing.TextureSettings.TextureParameters.PixelInternalFormat;
using PixelType = Hypercube.Client.Graphics.Texturing.TextureSettings.TextureParameters.PixelType;
using TextureParameterName = Hypercube.Client.Graphics.Texturing.TextureSettings.TextureParameters.TextureParameterName;
using TextureTarget = Hypercube.Client.Graphics.Texturing.TextureSettings.TextureParameters.TextureTarget;

namespace Hypercube.Client.Graphics.Texturing.TextureSettings;

public struct Texture2DCreationSettings : ITextureCreationSettings
{
public Texture2DCreationSettings(
TextureTarget textureTarget,
HashSet<TextureParameter> parameters,
PixelInternalFormat pixelInternalFormat,
int level,
int border,
PixelFormat pixelFormat,
PixelType pixelType,
bool flip = true)
{
TextureTarget = textureTarget;
Parameters = parameters;
PixelInternalFormat = pixelInternalFormat;
Level = level;
Border = border;
PixelFormat = pixelFormat;
PixelType = pixelType;
Flip = flip;
}

public Texture2DCreationSettings()
{
TextureTarget = TextureTarget.Texture2D;
Parameters = new()
{
new TextureParameter(TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat),
new TextureParameter(TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat),
new TextureParameter(TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear),
new TextureParameter(TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear),
};
PixelInternalFormat = PixelInternalFormat.Rgba;
Level = 0;
Border = 0;
PixelFormat = PixelFormat.Rgba;
PixelType = PixelType.UnsignedByte;
Flip = true;
}
public TextureTarget TextureTarget { get; }
public HashSet<TextureParameter> Parameters { get; }
public PixelInternalFormat PixelInternalFormat { get; }
public int Level { get; }
public int Border { get; }
public PixelFormat PixelFormat { get; }
public PixelType PixelType { get; }

public bool Flip { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Hypercube.Client.Graphics.Texturing.TextureSettings.TextureParameters;

namespace Hypercube.Client.Graphics.Texturing.TextureSettings;

public readonly struct TextureParameter(TextureParameterName name, int value)
JerryImMouse marked this conversation as resolved.
Show resolved Hide resolved
{
public readonly TextureParameterName Name = name;
public readonly int Value = value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace Hypercube.Client.Graphics.Texturing.TextureSettings.TextureParameters;

public enum PixelFormat
{
UnsignedShort = 5123,
UnsignedInt = 5125,
ColorIndex = 6400,
StencilIndex = 6401,
DepthComponent = 6402,
Red = 6403,
RedExt = 6403,
Green = 6404,
Blue = 6405,
Alpha = 6406,
Rgb = 6407,
Rgba = 6408,
Luminance = 6409,
LuminanceAlpha = 6410,
AbgrExt = 32768,
CmykExt = 32780,
CmykaExt = 32781,
Bgr = 32992,
Bgra = 32993,
Ycrcb422Sgix = 33211,
Ycrcb444Sgix = 33212,
Rg = 33319,
RgInteger = 33320,
R5G6B5IccSgix = 33894,
R5G6B5A8IccSgix = 33895,
Alpha16IccSgix = 33896,
Luminance16IccSgix = 33897,
Luminance16Alpha8IccSgix = 33899,
DepthStencil = 34041,
RedInteger = 36244,
GreenInteger = 36245,
BlueInteger = 36246,
AlphaInteger = 36247,
RgbInteger = 36248,
RgbaInteger = 36249,
BgrInteger = 36250,
BgraInteger = 36251,
}
Loading
Loading