1 /* 2 Copyright (c) 2017-2018 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 Permission is hereby granted, free of charge, to any person or organization 6 obtaining a copy of the software and accompanying documentation covered by 7 this license (the "Software") to use, reproduce, display, distribute, 8 execute, and transmit the Software, and to prepare derivative works of the 9 Software, and to permit third-parties to whom the Software is furnished to 10 do so, all subject to the following: 11 12 The copyright notices in the Software and this entire statement, including 13 the above license grant, this restriction and the following disclaimer, 14 must be included in all copies of the Software, in whole or in part, and 15 all derivative works of the Software, unless such copies or derivative 16 works are solely in the form of machine-executable object code generated by 17 a source language processor. 18 19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 DEALINGS IN THE SOFTWARE. 26 */ 27 28 module dagon.graphics.texture; 29 30 import std.stdio; 31 import std.math; 32 33 import dlib.core.memory; 34 import dlib.image.color; 35 import dlib.image.image; 36 import dlib.math.vector; 37 38 import dagon.core.libs; 39 import dagon.core.ownership; 40 41 class Texture: Owner 42 { 43 SuperImage image; 44 45 GLuint tex; 46 GLenum format; 47 GLint intFormat; 48 GLenum type; 49 50 int width; 51 int height; 52 int numMipmapLevels; 53 54 Vector2f translation; 55 Vector2f scale; 56 float rotation; 57 58 bool useMipmapFiltering = true; 59 bool useLinearFiltering = true; 60 61 this(Owner o) 62 { 63 super(o); 64 translation = Vector2f(0.0f, 0.0f); 65 scale = Vector2f(1.0f, 1.0f); 66 rotation = 0.0f; 67 } 68 69 this(SuperImage img, Owner o, bool genMipmaps = false) 70 { 71 super(o); 72 translation = Vector2f(0.0f, 0.0f); 73 scale = Vector2f(1.0f, 1.0f); 74 rotation = 0.0f; 75 createFromImage(img, genMipmaps); 76 } 77 78 void createFromImage(SuperImage img, bool genMipmaps = true) 79 { 80 image = img; 81 width = img.width; 82 height = img.height; 83 84 switch (img.pixelFormat) 85 { 86 case PixelFormat.L8: intFormat = GL_R8; format = GL_RED; type = GL_UNSIGNED_BYTE; break; 87 case PixelFormat.LA8: intFormat = GL_RG8; format = GL_RG; type = GL_UNSIGNED_BYTE; break; 88 case PixelFormat.RGB8: intFormat = GL_RGB8; format = GL_RGB; type = GL_UNSIGNED_BYTE; break; 89 case PixelFormat.RGBA8: intFormat = GL_RGBA8; format = GL_RGBA; type = GL_UNSIGNED_BYTE; break; 90 case PixelFormat.RGBA_FLOAT: intFormat = GL_RGBA32F; format = GL_RGBA; type = GL_FLOAT; break; 91 default: 92 writefln("Unsupported pixel format %s", img.pixelFormat); 93 return; 94 } 95 96 glGenTextures(1, &tex); 97 glActiveTexture(GL_TEXTURE0); 98 glBindTexture(GL_TEXTURE_2D, tex); 99 100 //glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 101 102 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 103 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 104 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 105 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 106 107 //if (DerelictGL.isExtSupported("GL_EXT_texture_filter_anisotropic")) 108 // glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0f); 109 110 glTexImage2D(GL_TEXTURE_2D, 0, intFormat, width, height, 0, format, type, cast(void*)img.data.ptr); 111 112 //if (genMipmaps) 113 glGenerateMipmap(GL_TEXTURE_2D); 114 115 glBindTexture(GL_TEXTURE_2D, 0); 116 } 117 118 void bind() 119 { 120 if (glIsTexture(tex)) 121 glBindTexture(GL_TEXTURE_2D, tex); 122 123 if (useMipmapFiltering) 124 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 125 else if (useLinearFiltering) 126 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 127 else 128 { 129 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 130 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 131 } 132 } 133 134 void unbind() 135 { 136 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 137 glBindTexture(GL_TEXTURE_2D, 0); 138 } 139 140 bool valid() 141 { 142 return cast(bool)glIsTexture(tex); 143 } 144 145 Color4f sample(float u, float v) 146 { 147 int x = cast(int)floor(u * width); 148 int y = cast(int)floor(v * height); 149 return image[x, y]; 150 } 151 152 void release() 153 { 154 if (glIsTexture(tex)) 155 glDeleteTextures(1, &tex); 156 if (image) 157 { 158 Delete(image); 159 image = null; 160 } 161 } 162 163 ~this() 164 { 165 release(); 166 } 167 }