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
3 changes: 2 additions & 1 deletion 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 Expand Up @@ -28,6 +29,6 @@ public void DrawTexture(ITexture texture, Box2 quad, Box2 uv)

public void DrawTexture(ITexture texture, Box2 quad, Box2 uv, Color color)
{
_renderer.DrawTexture(new TextureHandle(texture), quad, uv, color);
_renderer.DrawTexture(new TextureHandle(texture, new Texture2DCreationSettings()), quad, uv, color);
}
}
3 changes: 2 additions & 1 deletion Hypercube.Client/Graphics/Rendering/Renderer.Render.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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;
Expand Down Expand Up @@ -39,7 +40,7 @@ private void OnLoad()
_resourceManager.MountContentFolder("Resources/Shaders", "/");

_baseShader = new Shader("/base", _resourceManager);
_baseTexture = _textureManager.CreateHandler("/icon.png");
_baseTexture = _textureManager.CreateHandler("/icon.png", new Texture2DCreationSettings());
_baseTexture.Bind();

_cameraManager.SetMainCamera(_cameraManager.CreateCamera2D(MainWindow.Size));
Expand Down
7 changes: 4 additions & 3 deletions 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,6 @@ public interface ITextureManager
/// <returns>ITexture</returns>
ITexture Create(ResourcePath path, bool doFlip);

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

namespace Hypercube.Client.Graphics.Texturing;

Expand All @@ -7,20 +9,30 @@ public 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.ParameterName.ToOpenToolkit(), param.ParameterValue);
}

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
11 changes: 6 additions & 5 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 @@ -29,14 +30,14 @@ public ITexture Create(ResourcePath path, bool doFlip)
return texture;
}

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

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

private ITexture Create(ImageResult image)
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 class Texture2DCreationSettings : ITextureCreationSettings
JerryImMouse marked this conversation as resolved.
Show resolved Hide resolved
{
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 TextureParameterName ParameterName { get; } = name;
JerryImMouse marked this conversation as resolved.
Show resolved Hide resolved
public int ParameterValue { get; } = value;
JerryImMouse marked this conversation as resolved.
Show resolved Hide resolved
}
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