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, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
18 { This unit provides interface to load 24-bit and 32-bit uncompressed images
19 from Truevision Targa (TGA) graphic files, and create OpenGL textures
20 from it's data. }
22 interface
24 uses
25 {$INCLUDE ../nogl/noGLuses.inc}
28 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 // This is auxiliary function that creates OpenGL texture from raw image data
73 function CreateTexture (var tex: GLTexture; Width, Height, aFormat: Word; pData: Pointer): Boolean;
74 var
77 //buf: PByte;
78 //f, c: Integer;
79 begin
88 begin
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);
99 begin
102 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]), TMsgType.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);
165 // so driver will really upload the texture (this is *sometimes* required for buggy videodrivers)
172 // `img` must be valid!
173 function LoadTextureImg (var img: TImageData; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
174 var
180 begin
187 begin
189 exit;
191 //ConvertImage(img, ifA8R8G8B8);
198 try
199 // it is slow, but i don't care for now
202 begin
204 begin
214 finally
220 function LoadTextureMem (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
221 var
222 //image, ii: PByte;
223 //width, height: Integer;
224 //imageSize: Integer;
226 //x, y: Integer;
227 //clr: TColor32Rec;
228 begin
236 begin
238 exit;
240 try
242 finally
248 function LoadTextureMemEx (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
249 var
251 //width, height: Integer;
256 begin
262 begin
264 exit;
266 try
268 begin
270 exit;
272 //ConvertImage(img, ifA8R8G8B8);
277 //writeln('fX=', fX, '; fY=', fY, '; fWidth=', fWidth, '; fHeight=', fHeight);
280 try
281 // it is slow, but i don't care for now
284 begin
286 begin
296 finally
299 finally
305 function LoadTexture (filename: AnsiString; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
306 var
310 begin
317 try
319 except
323 begin
325 exit;
328 try
331 try
334 finally
337 finally
343 function LoadTextureEx (filename: AnsiString; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
344 var
348 begin
353 try
355 except
359 begin
361 exit;
364 try
367 try
370 finally
373 finally