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
43 function CreateTexture (var tex: GLTexture; Width, Height, aFormat: Word; pData: Pointer): Boolean;
45 // Standard set of images loading functions
46 function LoadTexture (Filename: String; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
47 function LoadTextureEx (Filename: String; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
48 function LoadTextureMem (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
49 function LoadTextureMemEx (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
51 // `img` must be valid!
52 function LoadTextureImg (var img: TImageData; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
55 implementation
57 uses
62 begin
74 // This is auxiliary function that creates OpenGL texture from raw image data
75 function CreateTexture (var tex: GLTexture; Width, Height, aFormat: Word; pData: Pointer): Boolean;
76 var
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;
110 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);
112 // texture blends with object background
114 // texture does NOT blend with object background
115 //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
117 {
118 Select a filtering type.
119 BiLinear filtering produces very good results with little performance impact
121 GL_NEAREST - Basic texture (grainy looking texture)
122 GL_LINEAR - BiLinear filtering
123 GL_LINEAR_MIPMAP_NEAREST - Basic mipmapped texture
124 GL_LINEAR_MIPMAP_LINEAR - BiLinear Mipmapped texture
125 }
127 // for GL_TEXTURE_MAG_FILTER only first two can be used
129 // for GL_TEXTURE_MIN_FILTER all of the above can be used
132 // create empty texture
134 glTexImage2D(GL_TEXTURE_2D, 0, fmt, tex.glwidth, tex.glheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, nil);
136 (*
137 GetMem(buf, tex.glwidth*4*tex.glheight);
138 try
139 FillChar(buf^, tex.glwidth*4*tex.glheight, 255);
140 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, tex.glwidth, tex.glheight, fmt, GL_UNSIGNED_BYTE, buf);
141 if (tex.glheight = 128) and (tex.height = 80) then
142 begin
143 for f := 0 to tex.glheight-1 do
144 begin
145 for c := 0 to tex.glwidth-1 do
146 begin
147 buf[f*(tex.glwidth*4)+c*4+0] := 255;
148 buf[f*(tex.glwidth*4)+c*4+1] := 127;
149 buf[f*(tex.glwidth*4)+c*4+2] := 0;
150 end;
151 end;
152 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 82, tex.glwidth, {tex.glheight}1, fmt, GL_UNSIGNED_BYTE, buf);
153 end;
154 finally
155 FreeMem(buf);
156 end;
157 *)
160 //glTexSubImage2D(GL_TEXTURE_2D, 0, 0, tex.glheight-tex.height, Width, Height, fmt, GL_UNSIGNED_BYTE, pData);
167 // `img` must be valid!
168 function LoadTextureImg (var img: TImageData; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
169 var
175 begin
182 begin
184 exit;
186 //ConvertImage(img, ifA8R8G8B8);
193 try
194 // it's slow, but i don't care for now
197 begin
199 begin
209 finally
215 function LoadTextureMem (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
216 var
218 begin
226 begin
228 exit;
230 try
232 finally
238 function LoadTextureMemEx (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
239 var
245 begin
251 begin
253 exit;
255 try
257 begin
259 exit;
261 //ConvertImage(img, ifA8R8G8B8);
266 //writeln('fX=', fX, '; fY=', fY, '; fWidth=', fWidth, '; fHeight=', fHeight);
269 try
270 // it's slow, but i don't care for now
273 begin
275 begin
285 finally
288 finally
294 function LoadTexture (filename: AnsiString; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
295 var
299 begin
306 try
308 except
312 begin
314 exit;
317 try
320 try
323 finally
326 finally
332 function LoadTextureEx (filename: AnsiString; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
333 var
337 begin
342 try
344 except
348 begin
350 exit;
353 try
356 try
359 finally
362 finally