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
26 {$IFDEF USE_NANOGL}
27 nanoGL,
28 {$ELSE}
30 {$ENDIF}
34 type
43 var
47 function CreateTexture (var tex: GLTexture; Width, Height, aFormat: Word; pData: Pointer): Boolean;
49 // Standard set of images loading functions
50 function LoadTexture (Filename: String; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
51 function LoadTextureEx (Filename: String; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
52 function LoadTextureMem (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
53 function LoadTextureMemEx (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
55 // `img` must be valid!
56 function LoadTextureImg (var img: TImageData; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
59 implementation
61 uses
66 begin
78 // This is auxiliary function that creates OpenGL texture from raw image data
79 function CreateTexture (var tex: GLTexture; Width, Height, aFormat: Word; pData: Pointer): Boolean;
80 var
83 //buf: PByte;
84 //f, c: Integer;
85 begin
94 begin
101 //if (tex.glwidth <> tex.width) or (tex.glheight <> tex.height) then
102 // 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);
105 begin
108 Exit;
116 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);
118 // texture blends with object background
120 // texture does NOT blend with object background
121 //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
123 {
124 Select a filtering type.
125 BiLinear filtering produces very good results with little performance impact
127 GL_NEAREST - Basic texture (grainy looking texture)
128 GL_LINEAR - BiLinear filtering
129 GL_LINEAR_MIPMAP_NEAREST - Basic mipmapped texture
130 GL_LINEAR_MIPMAP_LINEAR - BiLinear Mipmapped texture
131 }
133 // for GL_TEXTURE_MAG_FILTER only first two can be used
135 // for GL_TEXTURE_MIN_FILTER all of the above can be used
138 // create empty texture
140 glTexImage2D(GL_TEXTURE_2D, 0, fmt, tex.glwidth, tex.glheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, nil);
142 (*
143 GetMem(buf, tex.glwidth*4*tex.glheight);
144 try
145 FillChar(buf^, tex.glwidth*4*tex.glheight, 255);
146 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, tex.glwidth, tex.glheight, fmt, GL_UNSIGNED_BYTE, buf);
147 if (tex.glheight = 128) and (tex.height = 80) then
148 begin
149 for f := 0 to tex.glheight-1 do
150 begin
151 for c := 0 to tex.glwidth-1 do
152 begin
153 buf[f*(tex.glwidth*4)+c*4+0] := 255;
154 buf[f*(tex.glwidth*4)+c*4+1] := 127;
155 buf[f*(tex.glwidth*4)+c*4+2] := 0;
156 end;
157 end;
158 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 82, tex.glwidth, {tex.glheight}1, fmt, GL_UNSIGNED_BYTE, buf);
159 end;
160 finally
161 FreeMem(buf);
162 end;
163 *)
166 //glTexSubImage2D(GL_TEXTURE_2D, 0, 0, tex.glheight-tex.height, Width, Height, fmt, GL_UNSIGNED_BYTE, pData);
170 // so driver will really upload the texture (this is *sometimes* required for buggy videodrivers)
177 // `img` must be valid!
178 function LoadTextureImg (var img: TImageData; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
179 var
185 begin
192 begin
194 exit;
196 //ConvertImage(img, ifA8R8G8B8);
203 try
204 // it is slow, but i don't care for now
207 begin
209 begin
219 finally
225 function LoadTextureMem (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
226 var
227 //image, ii: PByte;
228 //width, height: Integer;
229 //imageSize: Integer;
231 //x, y: Integer;
232 //clr: TColor32Rec;
233 begin
241 begin
243 exit;
245 try
247 finally
253 function LoadTextureMemEx (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
254 var
256 //width, height: Integer;
261 begin
267 begin
269 exit;
271 try
273 begin
275 exit;
277 //ConvertImage(img, ifA8R8G8B8);
282 //writeln('fX=', fX, '; fY=', fY, '; fWidth=', fWidth, '; fHeight=', fHeight);
285 try
286 // it is slow, but i don't care for now
289 begin
291 begin
301 finally
304 finally
310 function LoadTexture (filename: AnsiString; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
311 var
315 begin
322 try
324 except
328 begin
330 exit;
333 try
336 try
339 finally
342 finally
348 function LoadTextureEx (filename: AnsiString; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
349 var
353 begin
358 try
360 except
364 begin
366 exit;
369 try
372 try
375 finally
378 finally