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}
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
38 var
42 function CreateTexture (var tex: GLTexture; Width, Height, aFormat: Word; pData: Pointer): Boolean;
44 // Standard set of images loading functions
45 function LoadTexture (Filename: String; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
46 function LoadTextureEx (Filename: String; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
47 function LoadTextureMem (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
48 function LoadTextureMemEx (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
50 // `img` must be valid!
51 function LoadTextureImg (var img: TImageData; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
54 implementation
56 uses
61 begin
73 // This is auxiliary function that creates OpenGL texture from raw image data
74 function CreateTexture (var tex: GLTexture; Width, Height, aFormat: Word; pData: Pointer): Boolean;
75 var
78 //buf: PByte;
79 //f, c: Integer;
80 begin
89 begin
96 //if (tex.glwidth <> tex.width) or (tex.glheight <> tex.height) then
97 // 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);
100 begin
103 Exit;
111 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);
113 // texture blends with object background
115 // texture does NOT blend with object background
116 //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
118 {
119 Select a filtering type.
120 BiLinear filtering produces very good results with little performance impact
122 GL_NEAREST - Basic texture (grainy looking texture)
123 GL_LINEAR - BiLinear filtering
124 GL_LINEAR_MIPMAP_NEAREST - Basic mipmapped texture
125 GL_LINEAR_MIPMAP_LINEAR - BiLinear Mipmapped texture
126 }
128 // for GL_TEXTURE_MAG_FILTER only first two can be used
130 // for GL_TEXTURE_MIN_FILTER all of the above can be used
133 // create empty texture
135 glTexImage2D(GL_TEXTURE_2D, 0, fmt, tex.glwidth, tex.glheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, nil);
137 (*
138 GetMem(buf, tex.glwidth*4*tex.glheight);
139 try
140 FillChar(buf^, tex.glwidth*4*tex.glheight, 255);
141 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, tex.glwidth, tex.glheight, fmt, GL_UNSIGNED_BYTE, buf);
142 if (tex.glheight = 128) and (tex.height = 80) then
143 begin
144 for f := 0 to tex.glheight-1 do
145 begin
146 for c := 0 to tex.glwidth-1 do
147 begin
148 buf[f*(tex.glwidth*4)+c*4+0] := 255;
149 buf[f*(tex.glwidth*4)+c*4+1] := 127;
150 buf[f*(tex.glwidth*4)+c*4+2] := 0;
151 end;
152 end;
153 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 82, tex.glwidth, {tex.glheight}1, fmt, GL_UNSIGNED_BYTE, buf);
154 end;
155 finally
156 FreeMem(buf);
157 end;
158 *)
161 //glTexSubImage2D(GL_TEXTURE_2D, 0, 0, tex.glheight-tex.height, Width, Height, fmt, GL_UNSIGNED_BYTE, pData);
168 // `img` must be valid!
169 function LoadTextureImg (var img: TImageData; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
170 var
176 begin
183 begin
185 exit;
187 //ConvertImage(img, ifA8R8G8B8);
194 try
195 // it's slow, but i don't care for now
198 begin
200 begin
210 finally
216 function LoadTextureMem (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
217 var
218 //image, ii: PByte;
219 //width, height: Integer;
220 //imageSize: Integer;
222 //x, y: Integer;
223 //clr: TColor32Rec;
224 begin
232 begin
234 exit;
236 try
238 finally
244 function LoadTextureMemEx (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
245 var
247 //width, height: Integer;
252 begin
258 begin
260 exit;
262 try
264 begin
266 exit;
268 //ConvertImage(img, ifA8R8G8B8);
273 //writeln('fX=', fX, '; fY=', fY, '; fWidth=', fWidth, '; fHeight=', fHeight);
276 try
277 // it's slow, but i don't care for now
280 begin
282 begin
292 finally
295 finally
301 function LoadTexture (filename: AnsiString; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
302 var
306 begin
313 try
315 except
319 begin
321 exit;
324 try
327 try
330 finally
333 finally
339 function LoadTextureEx (filename: AnsiString; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
340 var
344 begin
349 try
351 except
355 begin
357 exit;
360 try
363 try
366 finally
369 finally