142 lines
4.3 KiB
C#
142 lines
4.3 KiB
C#
using StgSharp.Math;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace StgSharp.Graphics
|
|
{
|
|
|
|
public unsafe class Texture
|
|
{
|
|
internal static bool[] unitActivated = new bool[16];
|
|
internal glHandleSet _textureHandle;
|
|
internal Image[] sourceList;
|
|
private IglFunc GL;
|
|
|
|
internal Texture(uint n, IglFunc gl)
|
|
{
|
|
_textureHandle = new glHandleSet((int)n);
|
|
sourceList = new Image[n];
|
|
GL = gl;
|
|
GL.GenTextures(n, _textureHandle);
|
|
}
|
|
|
|
public void Activate(TextureUnit unit)
|
|
{
|
|
GL.ActiveTexture((uint)unit);
|
|
}
|
|
|
|
public void Bind2D(int index)
|
|
{
|
|
GL.BindTexture(GLconst.TEXTURE_2D, _textureHandle[index]);
|
|
}
|
|
|
|
/// <summary>
|
|
/// </summary>
|
|
/// <param name="name"> </param>
|
|
/// <param name="index"> </param>
|
|
/// <param name="loader"> </param>
|
|
public unsafe void LoadTexture(int index, string name, ImageLoader loader)
|
|
{
|
|
sourceList[index] = new Image();
|
|
fixed (Image.ImageInfo* textureInfo = &sourceList[index].info)
|
|
{
|
|
InternalIO.InternalLoadImage(name, textureInfo, loader);
|
|
}
|
|
|
|
if (sourceList[index].info.pixelPtr != IntPtr.Zero)
|
|
{
|
|
uint channel;
|
|
switch (sourceList[index].info.channel)
|
|
{
|
|
case 1:
|
|
channel = GLconst.RED; break;
|
|
case 2:
|
|
channel = GLconst.RG; break;
|
|
case 3:
|
|
channel = GLconst.RGB; break;
|
|
case 4:
|
|
channel = GLconst.RGBA; break;
|
|
default:
|
|
channel = 0;
|
|
break;
|
|
}
|
|
GL.TextureImage2d(
|
|
GLconst.TEXTURE_2D, 0, channel,
|
|
(uint)sourceList[index].Width, (uint)sourceList[index].Height,
|
|
channel, GLconst.UNSIGNED_BYTE, (byte*)sourceList[index].info.pixelPtr
|
|
);
|
|
}
|
|
else
|
|
{
|
|
InternalIO.InternalWriteLog($"Failed to load image file of \"{name}\".", LogType.Error);
|
|
}
|
|
}
|
|
|
|
internal void ChangeEnvironment(IglFunc gl)
|
|
{
|
|
GL = gl;
|
|
}
|
|
|
|
#region texture set
|
|
|
|
public void Set2dFilterProperty(
|
|
Filter onMinify, Filter onMagnify)
|
|
{
|
|
GL.TextureParameter(GLconst.TEXTURE_2D, GLconst.TEXTURE_MIN_FILTER, (int)onMinify);
|
|
GL.TextureParameter(GLconst.TEXTURE_2D, GLconst.TEXTURE_MAG_FILTER, (int)onMagnify);
|
|
}
|
|
|
|
public void Set2dWrapProperty(
|
|
Wrap onHorizontial, Wrap onVertical)
|
|
{
|
|
GL.TextureParameter(GLconst.TEXTURE_2D, GLconst.TEXTURE_WRAP_S, (int)onHorizontial);
|
|
GL.TextureParameter(GLconst.TEXTURE_2D, GLconst.TEXTURE_WRAP_T, (int)onVertical);
|
|
}
|
|
|
|
#endregion texture set
|
|
}
|
|
|
|
public enum Filter
|
|
{
|
|
Nearest = GLconst.NEAREST,
|
|
Linear = GLconst.LINEAR,
|
|
Nearest_LinearMipmap = GLconst.NEAREST_MIPMAP_LINEAR,
|
|
Linear_LinearMipmap = GLconst.LINEAR_MIPMAP_LINEAR,
|
|
Nearest_NearestMipmap = GLconst.NEAREST_MIPMAP_NEAREST,
|
|
Linear_NearestMipmap = GLconst.LINEAR_MIPMAP_NEAREST,
|
|
}
|
|
|
|
public enum TextureUnit
|
|
{
|
|
Unit0 = GLconst.TEXTURE0,
|
|
Unit1 = GLconst.TEXTURE1,
|
|
Unit2 = GLconst.TEXTURE2,
|
|
Unit3 = GLconst.TEXTURE3,
|
|
Unit4 = GLconst.TEXTURE4,
|
|
Unit5 = GLconst.TEXTURE5,
|
|
Unit6 = GLconst.TEXTURE6,
|
|
Unit7 = GLconst.TEXTURE7,
|
|
Unit8 = GLconst.TEXTURE8,
|
|
Unit9 = GLconst.TEXTURE9,
|
|
Unit10 = GLconst.TEXTURE10,
|
|
Unit11 = GLconst.TEXTURE11,
|
|
Unit12 = GLconst.TEXTURE12,
|
|
Unit13 = GLconst.TEXTURE13,
|
|
Unit14 = GLconst.TEXTURE14,
|
|
Unit15 = GLconst.TEXTURE15,
|
|
}
|
|
|
|
public enum Wrap
|
|
{
|
|
Repeat = GLconst.REPEAT,
|
|
MirroredRepeat = GLconst.MIRRORED_REPEAT,
|
|
ClampToEdge = GLconst.CLAMP_TO_EDGE,
|
|
ClampToBorder = GLconst.CLAMP_TO_BORDER,
|
|
}
|
|
}
|