1 /*
2 Copyright (c) 2013 Andrey Penechko
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license the "Software" to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 module anchovy.graphics.texture;
30 
31 public import derelict.opengl3.gl3;
32 
33 import std.conv, std.file;
34 import std.stdio;
35 import std..string;
36 
37 import anchovy.core.types;
38 import anchovy.graphics.glerrors;
39 import anchovy.graphics.bitmap;
40 
41 //version = debugTexture;
42 
43 enum TextureTarget : uint
44 {
45 	target1d = GL_TEXTURE_1D,
46 	target2d = GL_TEXTURE_2D,
47 	target3d = GL_TEXTURE_3D,
48 	targetRectangle = GL_TEXTURE_RECTANGLE,
49 	targetBuffer = GL_TEXTURE_BUFFER,
50 	targetCubeMap = GL_TEXTURE_CUBE_MAP,
51 	target1dArray = GL_TEXTURE_1D_ARRAY,
52 	target2dArray = GL_TEXTURE_2D_ARRAY,
53 	targetCubeMapArray = GL_TEXTURE_CUBE_MAP_ARRAY,
54 	target2dMultisample = GL_TEXTURE_2D_MULTISAMPLE,
55 	target2dMultisampleArray = GL_TEXTURE_2D_MULTISAMPLE_ARRAY,
56 }
57 
58 enum TextureFormat : uint
59 {
60 	r = GL_RED,
61 	rg = GL_RG,
62 	rgb = GL_RGB,
63 	rgba = GL_RGBA,
64 }
65 
66 class Texture
67 {
68 	this(string filename, TextureTarget target, TextureFormat format)
69 	{
70 		texTarget = target;
71 		texFormat = format;
72 		loadFromFile(filename);
73 	}
74 
75 	this(Bitmap textureData, TextureTarget target, TextureFormat format)
76 	{
77 		texTarget = target;
78 		texFormat = format;
79 		loadFromData(textureData);
80 	}
81 
82 	void validateBind(uint textureUnit = 0)
83 	{
84 		if (!isValid) reload();
85 
86 		texUnit = GL_TEXTURE0 + textureUnit;
87 
88 		glBindTexture(texTarget, glTextureHandle);
89 			checkGlError;
90 	}
91 
92 	void bind(uint textureUnit = 0)
93 	{
94 		texUnit = GL_TEXTURE0 + textureUnit;
95 
96 		glBindTexture(texTarget, glTextureHandle);
97 			checkGlError;
98 	}
99 
100 	void unbind()
101 	{
102 		glBindTexture(texTarget, 0);
103 	}
104 
105 	uvec2 size() @property
106 	{
107 		return _bitmap.size;
108 	}
109 
110 	ref const(ubyte[]) data()
111 	{
112 		return _bitmap.data;
113 	}
114 
115 	private void reload()
116 	{
117 		if (!isHandleCreated) genTexture();
118 
119 		bind;
120 		if (_bitmap.size != lastSize)
121 		{
122 			glTexImage2D(texTarget, 0, texFormat, _bitmap.size.x, _bitmap.size.y, 0, texFormat, GL_UNSIGNED_BYTE, null);
123 				checkGlError;
124 			glTexImage2D(texTarget, 0, texFormat, _bitmap.size.x, _bitmap.size.y, 0, texFormat, GL_UNSIGNED_BYTE, _bitmap.data.ptr);
125 				checkGlError;
126 
127 			lastSize = _bitmap.size;
128 		}
129 		else
130 		{
131 			glTexSubImage2D(texTarget, 0, 0, 0, _bitmap.size.x, _bitmap.size.y,  texFormat, GL_UNSIGNED_BYTE, _bitmap.data.ptr);
132 				checkGlError;
133 		}
134 
135 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
136 			checkGlError;
137 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
138 			checkGlError;
139 		unbind;
140 
141 		isValid = true;
142 	}
143 
144 	void invalidate()
145 	{
146 		isValid = false;
147 	}
148 
149 	void onBitmapChanged()
150 	{
151 		invalidate();
152 	}
153 
154 	void bitmap(Bitmap newBitmap) @property
155 	{
156 		if (newBitmap == _bitmap) return;
157 
158 		loadFromData(newBitmap);
159 	}
160 
161 	Bitmap bitmap() @property
162 	{
163 		return _bitmap;
164 	}
165 
166 	/////////////////
167 	//Private methods
168 	/////////////////
169 
170 	private void genTexture()
171 	{
172 		glGenTextures(1, &glTextureHandle);
173 			checkGlError;
174 
175 		isHandleCreated = true;
176 	}
177 
178 	/// Loads image from file in RGBA8 format
179 	private void loadFromFile(string filename)
180 	{
181 		if (!exists(filename)) throw new Exception("File not found: " ~ filename);
182 
183 		if (_bitmap)
184 		{
185 			_bitmap.dataChanged.disconnect(&onBitmapChanged);
186 		}
187 
188 		_bitmap = createBitmapFromFile(filename);
189 
190 		_bitmap.dataChanged.connect(&onBitmapChanged);
191 
192 		invalidate();
193 	}
194 
195 	private void loadFromData(Bitmap textureData)
196 	{
197 		if (_bitmap)
198 		{
199 			_bitmap.dataChanged.disconnect(&onBitmapChanged);
200 		}
201 
202 		this._bitmap = textureData;
203 		_bitmap.dataChanged.connect(&onBitmapChanged);
204 
205 		invalidate();
206 	}
207 
208 	private void unload()
209 	{
210 		glDeleteTextures(1, &glTextureHandle);
211 	}
212 
213 private:
214 	TextureFormat texFormat;
215 	uint glTextureHandle;
216 	TextureTarget texTarget;
217 	uvec2 lastSize;
218 	bool isValid = false;
219 	bool isHandleCreated = false;
220 
221 	uint texUnit = 0;
222 	Bitmap _bitmap;
223 }