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
40 function CreateTexture (var tex: GLTexture; Width, Height, aFormat: Word; pData: Pointer; filter: Boolean = False): Boolean;
42 // Standard set of images loading functions
43 function LoadTexture (Filename: String; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil; filter: Boolean = False): Boolean;
44 function LoadTextureEx (Filename: String; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil; filter: Boolean = False): Boolean;
45 function LoadTextureMem (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil; filter: Boolean = False): Boolean;
46 function LoadTextureMemEx (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil; filter: Boolean = False): Boolean;
48 // `img` must be valid!
49 function LoadTextureImg (var img: TImageData; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil; filter: Boolean = False): Boolean;
52 implementation
54 uses
59 begin
71 // This is auxiliary function that creates OpenGL texture from raw image data
72 function CreateTexture (var tex: GLTexture; Width, Height, aFormat: Word; pData: Pointer; filter: Boolean = False): Boolean;
73 var
76 //buf: PByte;
77 //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 }
130 // for GL_TEXTURE_MAG_FILTER only first two can be used
132 // for GL_TEXTURE_MIN_FILTER all of the above can be used
135 // create empty texture
137 glTexImage2D(GL_TEXTURE_2D, 0, fmt, tex.glwidth, tex.glheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, nil);
139 (*
140 GetMem(buf, tex.glwidth*4*tex.glheight);
141 try
142 FillChar(buf^, tex.glwidth*4*tex.glheight, 255);
143 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, tex.glwidth, tex.glheight, fmt, GL_UNSIGNED_BYTE, buf);
144 if (tex.glheight = 128) and (tex.height = 80) then
145 begin
146 for f := 0 to tex.glheight-1 do
147 begin
148 for c := 0 to tex.glwidth-1 do
149 begin
150 buf[f*(tex.glwidth*4)+c*4+0] := 255;
151 buf[f*(tex.glwidth*4)+c*4+1] := 127;
152 buf[f*(tex.glwidth*4)+c*4+2] := 0;
153 end;
154 end;
155 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 82, tex.glwidth, {tex.glheight}1, fmt, GL_UNSIGNED_BYTE, buf);
156 end;
157 finally
158 FreeMem(buf);
159 end;
160 *)
163 //glTexSubImage2D(GL_TEXTURE_2D, 0, 0, tex.glheight-tex.height, Width, Height, fmt, GL_UNSIGNED_BYTE, pData);
167 // so driver will really upload the texture (this is *sometimes* required for buggy videodrivers)
174 // `img` must be valid!
175 function LoadTextureImg (var img: TImageData; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil; filter: Boolean = False): Boolean;
176 var
182 begin
189 begin
191 exit;
193 //ConvertImage(img, ifA8R8G8B8);
200 try
201 // it is slow, but i don't care for now
204 begin
206 begin
216 finally
222 function LoadTextureMem (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil; filter: Boolean = False): Boolean;
223 var
224 //image, ii: PByte;
225 //width, height: Integer;
226 //imageSize: Integer;
228 //x, y: Integer;
229 //clr: TColor32Rec;
230 begin
238 begin
240 exit;
242 try
244 finally
250 function LoadTextureMemEx (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil; filter: Boolean = False): Boolean;
251 var
253 //width, height: Integer;
258 begin
264 begin
266 exit;
268 try
270 begin
272 exit;
274 //ConvertImage(img, ifA8R8G8B8);
279 //writeln('fX=', fX, '; fY=', fY, '; fWidth=', fWidth, '; fHeight=', fHeight);
282 try
283 // it is slow, but i don't care for now
286 begin
288 begin
298 finally
301 finally
307 function LoadTexture (filename: AnsiString; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil; filter: Boolean = False): Boolean;
308 var
312 begin
319 try
321 except
325 begin
327 exit;
330 try
333 try
336 finally
339 finally
345 function LoadTextureEx (filename: AnsiString; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil; filter: Boolean = False): Boolean;
346 var
350 begin
355 try
357 except
361 begin
363 exit;
366 try
369 try
372 finally
375 finally