budnhead/bnhdemo/TextureManager.cs

86 lines
1.7 KiB
C#

using System;
using System.Collections.Generic;
using OpenTK.Graphics.OpenGL4;
namespace bnhdemo
{
public class TextureManager
{
Dictionary<int, Texture> textures;
Texture defTexture;
public TextureManager()
{
textures = new Dictionary<int, Texture>();
createDefaultTexture();
}
private void createDefaultTexture()
{
defTexture = new Texture(128, 128);
for (int n = 0; n < 128;n++){
defTexture.setPixel(0, n, 0x00FFFF00);
defTexture.setPixel(n, 0, 0x00FFFF00);
defTexture.setPixel(127, n, 0x00FFFF00);
defTexture.setPixel(n, 127, 0x00FFFF00);
}
addTexture(defTexture);
}
public int addTexture(Texture t)
{
int id = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, id);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, t.Width, t.Height, 0, PixelFormat.Rgba, PixelType.Byte, t.Data);
GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
textures.Add(id, t);
return id;
}
public class Texture
{
int width,
height;
UInt32[] data;
public Texture(int width,int height)
{
this.width = width;
this.height = height;
this.data = new UInt32[width * height];
}
public UInt32[] Data {
get { return this.data; }
}
public int Width
{
get { return this.width; }
}
public int Height {
get { return this.height; }
}
public UInt32 getPixel(int x,int y){
return data[x + (y * width)];
}
public void setPixel(int x, int y, UInt32 rgba)
{
data[x + (y * width)] = rgba;
}
public void setPixel(int x, int y,uint r,uint g,uint b,uint a)
{
data[x + (y * width)] = (r << 0) | (g << 8) | (b << 16) | (a << 24);
}
}
}
}