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
28 type
37 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 begin
87 begin
94 //if (tex.glwidth <> tex.width) or (tex.glheight <> tex.height) then
95 // 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 begin
101 Exit;
109 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);
111 // texture blends with object background
113 // texture does NOT blend with object background
114 //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
116 {
117 Select a filtering type.
118 BiLinear filtering produces very good results with little performance impact
120 GL_NEAREST - Basic texture (grainy looking texture)
121 GL_LINEAR - BiLinear filtering
122 GL_LINEAR_MIPMAP_NEAREST - Basic mipmapped texture
123 GL_LINEAR_MIPMAP_LINEAR - BiLinear Mipmapped texture
124 }
126 // for GL_TEXTURE_MAG_FILTER only first two can be used
128 // for GL_TEXTURE_MIN_FILTER all of the above can be used
131 // create empty texture
133 glTexImage2D(GL_TEXTURE_2D, 0, fmt, tex.glwidth, tex.glheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, nil);
135 (*
136 GetMem(buf, tex.glwidth*4*tex.glheight);
137 try
138 FillChar(buf^, tex.glwidth*4*tex.glheight, 255);
139 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, tex.glwidth, tex.glheight, fmt, GL_UNSIGNED_BYTE, buf);
140 if (tex.glheight = 128) and (tex.height = 80) then
141 begin
142 for f := 0 to tex.glheight-1 do
143 begin
144 for c := 0 to tex.glwidth-1 do
145 begin
146 buf[f*(tex.glwidth*4)+c*4+0] := 255;
147 buf[f*(tex.glwidth*4)+c*4+1] := 127;
148 buf[f*(tex.glwidth*4)+c*4+2] := 0;
149 end;
150 end;
151 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 82, tex.glwidth, {tex.glheight}1, fmt, GL_UNSIGNED_BYTE, buf);
152 end;
153 finally
154 FreeMem(buf);
155 end;
156 *)
159 //glTexSubImage2D(GL_TEXTURE_2D, 0, 0, tex.glheight-tex.height, Width, Height, fmt, GL_UNSIGNED_BYTE, pData);
166 // `img` must be valid!
167 function LoadTextureImg (var img: TImageData; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
168 var
174 begin
181 begin
183 exit;
185 //ConvertImage(img, ifA8R8G8B8);
192 try
193 // it's slow, but i don't care for now
196 begin
198 begin
208 finally
214 function LoadTextureMem (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
215 var
217 begin
225 begin
227 exit;
229 try
231 finally
237 function LoadTextureMemEx (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
238 var
244 begin
250 begin
252 exit;
254 try
256 begin
258 exit;
260 //ConvertImage(img, ifA8R8G8B8);
265 //writeln('fX=', fX, '; fY=', fY, '; fWidth=', fWidth, '; fHeight=', fHeight);
268 try
269 // it's slow, but i don't care for now
272 begin
274 begin
284 finally
287 finally
293 function LoadTexture (filename: AnsiString; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
294 var
298 begin
305 try
307 except
311 begin
313 exit;
316 try
319 try
322 finally
325 finally
331 function LoadTextureEx (filename: AnsiString; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
332 var
336 begin
341 try
343 except
347 begin
349 exit;
352 try
355 try
358 finally
361 finally