DEADSOFTWARE

6055678f343d8e55dafc2c24ff518c666d21863b
[d2df-editor.git] / src / engine / e_textures.pas
1 (* Copyright (C) DooM 2D:Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit e_textures;
19 { This unit provides interface to load 24-bit and 32-bit uncompressed images
20 from Truevision Targa (TGA) graphic files, and create OpenGL textures
21 from it's data. }
23 interface
25 uses
26 GL, GLExt, SysUtils, e_log,
27 ImagingTypes, Imaging, ImagingUtility;
29 type
30 GLTexture = record
31 id: GLuint;
32 width, height: Word; // real
33 glwidth, glheight: Word; // powerof2
34 u, v: Single; // usually 1.0
35 fmt: GLuint;
36 end;
38 var
39 e_DummyTextures: Boolean = False;
40 e_glLegacyNPOT: Boolean = False;
41 TEXTUREFILTER: Integer = GL_NEAREST;
43 function CreateTexture (var tex: GLTexture; Width, Height, aFormat: Word; pData: Pointer): Boolean;
45 // Standard set of images loading functions
46 function LoadTexture (Filename: String; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
47 function LoadTextureEx (Filename: String; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
48 function LoadTextureMem (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
49 function LoadTextureMemEx (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
51 // `img` must be valid!
52 function LoadTextureImg (var img: TImageData; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
55 implementation
57 uses
58 Classes, BinEditor, utils;
61 function AlignP2 (n: Word): Word;
62 begin
63 Dec(n);
64 n := n or (n shr 1);
65 n := n or (n shr 2);
66 n := n or (n shr 4);
67 n := n or (n shr 8);
68 n := n or (n shr 16);
69 Inc(n);
70 Result := n;
71 end;
74 // This is auxiliary function that creates OpenGL texture from raw image data
75 function CreateTexture (var tex: GLTexture; Width, Height, aFormat: Word; pData: Pointer): Boolean;
76 var
77 Texture: GLuint;
78 fmt: GLenum;
79 begin
80 tex.width := Width;
81 tex.height := Height;
82 tex.glwidth := Width;
83 tex.glheight := Height;
84 tex.u := 1;
85 tex.v := 1;
87 if e_glLegacyNPOT then
88 begin
89 tex.glwidth := AlignP2(Width);
90 tex.glheight := AlignP2(Height);
91 if tex.glwidth <> tex.width then tex.u := (tex.width+0.0)/(tex.glwidth+0.0);
92 if tex.glheight <> tex.height then tex.v := (tex.height+0.0)/(tex.glheight+0.0);
93 end;
95 //if (tex.glwidth <> tex.width) or (tex.glheight <> tex.height) then
96 // e_WriteLog(Format('NPOT: orig is %ux%u; gl is %ux%u; u=%f; v=%f', [Width, Height, tex.glwidth, tex.glheight, tex.u, tex.v]), MSG_NOTIFY);
98 if e_DummyTextures then
99 begin
100 tex.id := GLuint(-1);
101 Result := True;
102 Exit;
103 end;
105 glGenTextures(1, @Texture);
106 tex.id := Texture;
107 glBindTexture(GL_TEXTURE_2D, Texture);
109 if (tex.glwidth <> tex.width) or (tex.glheight <> tex.height) then
110 e_WriteLog(Format('NPOT: %u is %ux%u; gl is %ux%u; u=%f; v=%f', [tex.id, Width, Height, tex.glwidth, tex.glheight, tex.u, tex.v]), MSG_NOTIFY);
112 // texture blends with object background
113 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
114 // texture does NOT blend with object background
115 //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
118 Select a filtering type.
119 BiLinear filtering produces very good results with little performance impact
121 GL_NEAREST - Basic texture (grainy looking texture)
122 GL_LINEAR - BiLinear filtering
123 GL_LINEAR_MIPMAP_NEAREST - Basic mipmapped texture
124 GL_LINEAR_MIPMAP_LINEAR - BiLinear Mipmapped texture
127 // for GL_TEXTURE_MAG_FILTER only first two can be used
128 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, TEXTUREFILTER);
129 // for GL_TEXTURE_MIN_FILTER all of the above can be used
130 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, TEXTUREFILTER);
132 // create empty texture
133 if aFormat = GL_RGBA then fmt := GL_RGBA else fmt := GL_RGB; // silly, yeah?
134 glTexImage2D(GL_TEXTURE_2D, 0, fmt, tex.glwidth, tex.glheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, nil);
136 (*
137 GetMem(buf, tex.glwidth*4*tex.glheight);
138 try
139 FillChar(buf^, tex.glwidth*4*tex.glheight, 255);
140 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, tex.glwidth, tex.glheight, fmt, GL_UNSIGNED_BYTE, buf);
141 if (tex.glheight = 128) and (tex.height = 80) then
142 begin
143 for f := 0 to tex.glheight-1 do
144 begin
145 for c := 0 to tex.glwidth-1 do
146 begin
147 buf[f*(tex.glwidth*4)+c*4+0] := 255;
148 buf[f*(tex.glwidth*4)+c*4+1] := 127;
149 buf[f*(tex.glwidth*4)+c*4+2] := 0;
150 end;
151 end;
152 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 82, tex.glwidth, {tex.glheight}1, fmt, GL_UNSIGNED_BYTE, buf);
153 end;
154 finally
155 FreeMem(buf);
156 end;
157 *)
159 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, Width, Height, fmt, GL_UNSIGNED_BYTE, pData);
160 //glTexSubImage2D(GL_TEXTURE_2D, 0, 0, tex.glheight-tex.height, Width, Height, fmt, GL_UNSIGNED_BYTE, pData);
162 glBindTexture(GL_TEXTURE_2D, 0);
164 Result := true;
165 end;
167 // `img` must be valid!
168 function LoadTextureImg (var img: TImageData; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
169 var
170 image, ii: PByte;
171 width, height: Integer;
172 imageSize: Integer;
173 x, y: Integer;
174 clr: TColor32Rec;
175 begin
176 result := false;
177 pWidth := 0;
178 pHeight := 0;
179 if Fmt <> nil then Fmt^ := GL_RGBA; // anyway
181 if (img.width < 1) or (img.width > 32768) or (img.height < 1) or (img.height > 32768) then
182 begin
183 e_WriteLog('Error loading texture: invalid image dimensions', MSG_WARNING);
184 exit;
185 end;
186 //ConvertImage(img, ifA8R8G8B8);
187 width := img.width;
188 height := img.height;
189 pWidth := width;
190 pHeight := height;
191 imageSize := Width*Height*4;
192 GetMem(image, imageSize);
193 try
194 // it's slow, but i don't care for now
195 ii := image;
196 for y := height-1 downto 0 do
197 begin
198 for x := 0 to width-1 do
199 begin
200 clr := GetPixel32(img, x, y);
201 ii^ := clr.r; Inc(ii);
202 ii^ := clr.g; Inc(ii);
203 ii^ := clr.b; Inc(ii);
204 ii^ := clr.a; Inc(ii);
205 end;
206 end;
207 CreateTexture(Texture, width, height, GL_RGBA, image);
208 result := true;
209 finally
210 FreeMem(image);
211 end;
212 end;
215 function LoadTextureMem (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
216 var
217 img: TImageData;
218 begin
219 result := false;
220 pWidth := 0;
221 pHeight := 0;
222 if Fmt <> nil then Fmt^ := GL_RGBA; // anyway
224 InitImage(img);
225 if not LoadImageFromMemory(pData, dataSize, img) then
226 begin
227 e_WriteLog('Error loading texture: unknown image format', MSG_WARNING);
228 exit;
229 end;
230 try
231 result := LoadTextureImg(img, Texture, pWidth, pHeight, Fmt);
232 finally
233 FreeImage(img);
234 end;
235 end;
238 function LoadTextureMemEx (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
239 var
240 image, ii: PByte;
241 imageSize: Integer;
242 img: TImageData;
243 x, y: Integer;
244 clr: TColor32Rec;
245 begin
246 result := false;
247 if Fmt <> nil then Fmt^ := GL_RGBA; // anyway
249 InitImage(img);
250 if not LoadImageFromMemory(pData, dataSize, img) then
251 begin
252 e_WriteLog('Error loading texture: unknown image format', MSG_WARNING);
253 exit;
254 end;
255 try
256 if (img.width < 1) or (img.width > 32768) or (img.height < 1) or (img.height > 32768) then
257 begin
258 e_WriteLog('Error loading texture: invalid image dimensions', MSG_WARNING);
259 exit;
260 end;
261 //ConvertImage(img, ifA8R8G8B8);
262 if fX > img.width then exit;
263 if fY > img.height then exit;
264 if fX+fWidth > img.width then exit;
265 if fY+fHeight > img.height then exit;
266 //writeln('fX=', fX, '; fY=', fY, '; fWidth=', fWidth, '; fHeight=', fHeight);
267 imageSize := img.width*img.height*4;
268 GetMem(image, imageSize);
269 try
270 // it's slow, but i don't care for now
271 ii := image;
272 for y := fY+fHeight-1 downto fY do
273 begin
274 for x := fX to fX+fWidth-1 do
275 begin
276 clr := GetPixel32(img, x, y);
277 ii^ := clr.r; Inc(ii);
278 ii^ := clr.g; Inc(ii);
279 ii^ := clr.b; Inc(ii);
280 ii^ := clr.a; Inc(ii);
281 end;
282 end;
283 CreateTexture(Texture, fWidth, fHeight, GL_RGBA, image);
284 result := true;
285 finally
286 FreeMem(image);
287 end;
288 finally
289 FreeImage(img);
290 end;
291 end;
294 function LoadTexture (filename: AnsiString; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
295 var
296 fs: TStream;
297 img: Pointer;
298 imageSize: LongInt;
299 begin
300 result := False;
301 pWidth := 0;
302 pHeight := 0;
303 if Fmt <> nil then Fmt^ := GL_RGBA; // anyway
304 fs := nil;
306 try
307 fs := openDiskFileRO(filename);
308 except
309 fs := nil;
310 end;
311 if fs = nil then
312 begin
313 e_WriteLog('Texture "'+filename+'" not found', MSG_WARNING);
314 exit;
315 end;
317 try
318 imageSize := fs.size;
319 GetMem(img, imageSize);
320 try
321 fs.readBuffer(img^, imageSize);
322 result := LoadTextureMem(img, imageSize, Texture, pWidth, pHeight, Fmt);
323 finally
324 FreeMem(img);
325 end;
326 finally
327 fs.Free();
328 end;
329 end;
332 function LoadTextureEx (filename: AnsiString; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
333 var
334 fs: TStream;
335 img: Pointer;
336 imageSize: LongInt;
337 begin
338 result := False;
339 if Fmt <> nil then Fmt^ := GL_RGBA; // anyway
340 fs := nil;
342 try
343 fs := openDiskFileRO(filename);
344 except
345 fs := nil;
346 end;
347 if fs = nil then
348 begin
349 e_WriteLog('Texture "'+filename+'" not found', MSG_WARNING);
350 exit;
351 end;
353 try
354 imageSize := fs.size;
355 GetMem(img, imageSize);
356 try
357 fs.readBuffer(img^, imageSize);
358 result := LoadTextureMemEx(img, imageSize, Texture, fX, fY, fWidth, fHeight, Fmt);
359 finally
360 FreeMem(img);
361 end;
362 finally
363 fs.Free();
364 end;
365 end;
367 end.