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 {$MODE DELPHI}
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
29 type
37 var
41 function CreateTexture (var tex: GLTexture; Width, Height, aFormat: Word; pData: Pointer): Boolean;
43 // Standard set of images loading functions
44 function LoadTexture (Filename: String; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
45 function LoadTextureEx (Filename: String; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
46 function LoadTextureMem (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
47 function LoadTextureMemEx (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
49 // `img` must be valid!
50 function LoadTextureImg (var img: TImageData; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
53 implementation
55 uses
60 begin
72 {
73 type
74 TTGAHeader = packed record
75 FileType: Byte;
76 ColorMapType: Byte;
77 ImageType: Byte;
78 ColorMapSpec: array[0..4] of Byte;
79 OrigX: array[0..1] of Byte;
80 OrigY: array[0..1] of Byte;
81 Width: array[0..1] of Byte;
82 Height: array[0..1] of Byte;
83 BPP: Byte;
84 ImageInfo: Byte;
85 end;
86 }
89 // This is auxiliary function that creates OpenGL texture from raw image data
90 function CreateTexture (var tex: GLTexture; Width, Height, aFormat: Word; pData: Pointer): Boolean;
91 var
93 begin
97 begin
100 end
101 else
102 begin
112 begin
113 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);
117 begin
120 Exit;
127 // texture blends with object background
129 // texture does NOT blend with object background
130 //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
132 {
133 Select a filtering type.
134 BiLinear filtering produces very good results with little performance impact
136 GL_NEAREST - Basic texture (grainy looking texture)
137 GL_LINEAR - BiLinear filtering
138 GL_LINEAR_MIPMAP_NEAREST - Basic mipmapped texture
139 GL_LINEAR_MIPMAP_LINEAR - BiLinear Mipmapped texture
140 }
142 // for GL_TEXTURE_MAG_FILTER only first two can be used
144 // for GL_TEXTURE_MIN_FILTER all of the above can be used
147 // create empty texture
149 begin
150 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.glwidth, tex.glheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, nil);
152 end
153 else
154 begin
155 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex.glwidth, tex.glheight, 0, GL_RGB, GL_UNSIGNED_BYTE, nil);
159 // the following is ok too
160 //bindTexture(0);
161 //glTextureSubImage2D(tid, 0, 0, 0, img.width, img.height, GL_RGBA, GL_UNSIGNED_BYTE, img.imageData.bytes.ptr);
163 {
164 if (tex.glwidth = tex.glwidth) and (tex.glheight = tex.height) then
165 // easy case
166 if aFormat = GL_RGBA then
167 begin
168 glTexImage2D(GL_TEXTURE_2D, 0, 4, Width, Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pData);
169 end
170 else
171 begin
172 glTexImage2D(GL_TEXTURE_2D, 0, 3, Width, Height, 0, GL_RGB, GL_UNSIGNED_BYTE, pData);
173 end;
174 end
175 }
182 // `img` must be valid!
183 function LoadTextureImg (var img: TImageData; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
184 var
190 begin
197 begin
199 exit;
201 //ConvertImage(img, ifA8R8G8B8);
208 try
209 // it's slow, but i don't care for now
212 begin
214 begin
224 finally
230 function LoadTextureMem (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
231 var
238 begin
246 begin
248 exit;
250 try
252 finally
258 function LoadTextureMemEx (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
259 var
266 begin
272 begin
274 exit;
276 try
278 begin
280 exit;
282 //ConvertImage(img, ifA8R8G8B8);
289 try
290 // it's slow, but i don't care for now
293 begin
295 begin
305 finally
308 finally
314 function LoadTexture (filename: AnsiString; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
315 var
319 begin
326 try
328 except
332 begin
334 exit;
337 try
340 try
343 finally
346 finally
352 function LoadTextureEx (filename: AnsiString; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
353 var
357 begin
362 try
364 except
368 begin
370 exit;
373 try
376 try
379 finally
382 finally