1 {$MODE DELPHI}
4 { This unit provides interface to load 24-bit and 32-bit uncompressed images
5 from Truevision Targa (TGA) graphic files, and create OpenGL textures
6 from it's data. }
8 interface
10 uses
13 type
21 var
25 function CreateTexture (var tex: GLTexture; Width, Height, aFormat: Word; pData: Pointer): Boolean;
27 // Standard set of images loading functions
28 function LoadTexture (Filename: String; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
29 function LoadTextureEx (Filename: String; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
30 function LoadTextureMem (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
31 function LoadTextureMemEx (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
33 implementation
35 uses
41 begin
53 {
54 type
55 TTGAHeader = packed record
56 FileType: Byte;
57 ColorMapType: Byte;
58 ImageType: Byte;
59 ColorMapSpec: array[0..4] of Byte;
60 OrigX: array[0..1] of Byte;
61 OrigY: array[0..1] of Byte;
62 Width: array[0..1] of Byte;
63 Height: array[0..1] of Byte;
64 BPP: Byte;
65 ImageInfo: Byte;
66 end;
67 }
70 // This is auxiliary function that creates OpenGL texture from raw image data
71 function CreateTexture (var tex: GLTexture; Width, Height, aFormat: Word; pData: Pointer): Boolean;
72 var
74 begin
78 begin
81 end
82 else
83 begin
93 begin
94 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;
108 // texture blends with object background
110 // texture does NOT blend with object background
111 //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
113 {
114 Select a filtering type.
115 BiLinear filtering produces very good results with little performance impact
117 GL_NEAREST - Basic texture (grainy looking texture)
118 GL_LINEAR - BiLinear filtering
119 GL_LINEAR_MIPMAP_NEAREST - Basic mipmapped texture
120 GL_LINEAR_MIPMAP_LINEAR - BiLinear Mipmapped texture
121 }
123 // for GL_TEXTURE_MAG_FILTER only first two can be used
125 // for GL_TEXTURE_MIN_FILTER all of the above can be used
128 // create empty texture
130 begin
131 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.glwidth, tex.glheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, nil);
133 end
134 else
135 begin
136 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex.glwidth, tex.glheight, 0, GL_RGB, GL_UNSIGNED_BYTE, nil);
140 // the following is ok too
141 //bindTexture(0);
142 //glTextureSubImage2D(tid, 0, 0, 0, img.width, img.height, GL_RGBA, GL_UNSIGNED_BYTE, img.imageData.bytes.ptr);
144 {
145 if (tex.glwidth = tex.glwidth) and (tex.glheight = tex.height) then
146 // easy case
147 if aFormat = GL_RGBA then
148 begin
149 glTexImage2D(GL_TEXTURE_2D, 0, 4, Width, Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pData);
150 end
151 else
152 begin
153 glTexImage2D(GL_TEXTURE_2D, 0, 3, Width, Height, 0, GL_RGB, GL_UNSIGNED_BYTE, pData);
154 end;
155 end
156 }
163 function LoadTextureMem (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
164 var
171 begin
179 begin
181 exit;
183 try
185 begin
187 exit;
189 //ConvertImage(img, ifA8R8G8B8);
196 try
197 // it's slow, but i don't care for now
200 begin
202 begin
212 finally
215 finally
221 function LoadTextureMemEx (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
222 var
229 begin
235 begin
237 exit;
239 try
241 begin
243 exit;
245 //ConvertImage(img, ifA8R8G8B8);
252 try
253 // it's slow, but i don't care for now
256 begin
258 begin
268 finally
271 finally
277 function LoadTexture (filename: AnsiString; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
278 var
282 begin
289 try
291 except
295 begin
297 exit;
300 try
303 try
306 finally
309 finally
315 function LoadTextureEx (filename: AnsiString; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
316 var
320 begin
325 try
327 except
331 begin
333 exit;
336 try
339 try
342 finally
345 finally