1 module polyplex.core.content.gl.textures; 2 import polyplex.core.content.textures; 3 import polyplex.utils.logging; 4 import polyplex.core.render; 5 import polyplex.core.render.gl.gloo; 6 import polyplex.core.render.gl.shader; 7 import polyplex.core.color; 8 import std.stdio; 9 10 11 public class GlTexture2D : Texture2D { 12 private: 13 Texture tex; 14 15 static int MAX_TEX_UNITS = -1; 16 17 protected: 18 override void rebuffer() { 19 int mode = GL_RGBA; 20 21 if (tex !is null) destroy(tex); 22 tex = new Texture(); 23 24 Bind(); 25 tex.Image2D(TextureType.Tex2D, 0, mode, image.Width, image.Height, 0, mode, GL_UNSIGNED_BYTE, image.Pixels.ptr); 26 Unbind(); 27 28 // Set max texture units. 29 if (MAX_TEX_UNITS == -1) { 30 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &MAX_TEX_UNITS); 31 Logger.Info("Set max texture units to: {0}", MAX_TEX_UNITS); 32 } 33 } 34 public: 35 override uint Id() { 36 return tex.Id; 37 } 38 39 Texture GLTexture() { 40 return tex; 41 } 42 43 this(TextureImg img) { 44 Logger.VerboseDebug("Created GL Texture from image: {0}", img.InternalName); 45 super(img); 46 } 47 48 this(Color[][] colors) { 49 super(colors); 50 } 51 52 ~this() { 53 destroy(tex); 54 } 55 56 /** 57 Binds the texture. 58 */ 59 override void Bind(int attachPos = 0, Shader s = null) { 60 if (s is null) { 61 tex.Bind(TextureType.Tex2D); 62 return; 63 } 64 if (!s.HasUniform("ppTexture")) throw new Exception("Texture2D requires ppTexture sampler2d uniform to attach to!"); 65 if (attachPos > MAX_TEX_UNITS) attachPos = MAX_TEX_UNITS; 66 if (attachPos < 0) attachPos = 0; 67 68 tex.Bind(TextureType.Tex2D); 69 tex.AttachTo(attachPos); 70 } 71 72 /** 73 Unbinds the texture. 74 */ 75 override void Unbind() { 76 tex.Unbind(); 77 } 78 }