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
14 type
22 var
26 function CreateTexture (var tex: GLTexture; Width, Height, aFormat: Word; pData: Pointer): Boolean;
28 // Standard set of images loading functions
29 function LoadTexture (Filename: String; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
30 function LoadTextureEx (Filename: String; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
31 function LoadTextureMem (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
32 function LoadTextureMemEx (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
34 // `img` must be valid!
35 function LoadTextureImg (var img: TImageData; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
38 implementation
40 uses
45 begin
57 {
58 type
59 TTGAHeader = packed record
60 FileType: Byte;
61 ColorMapType: Byte;
62 ImageType: Byte;
63 ColorMapSpec: array[0..4] of Byte;
64 OrigX: array[0..1] of Byte;
65 OrigY: array[0..1] of Byte;
66 Width: array[0..1] of Byte;
67 Height: array[0..1] of Byte;
68 BPP: Byte;
69 ImageInfo: Byte;
70 end;
71 }
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
78 begin
82 begin
85 end
86 else
87 begin
97 begin
98 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);
102 begin
105 Exit;
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 begin
135 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.glwidth, tex.glheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, nil);
137 end
138 else
139 begin
140 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex.glwidth, tex.glheight, 0, GL_RGB, GL_UNSIGNED_BYTE, nil);
144 // the following is ok too
145 //bindTexture(0);
146 //glTextureSubImage2D(tid, 0, 0, 0, img.width, img.height, GL_RGBA, GL_UNSIGNED_BYTE, img.imageData.bytes.ptr);
148 {
149 if (tex.glwidth = tex.glwidth) and (tex.glheight = tex.height) then
150 // easy case
151 if aFormat = GL_RGBA then
152 begin
153 glTexImage2D(GL_TEXTURE_2D, 0, 4, Width, Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pData);
154 end
155 else
156 begin
157 glTexImage2D(GL_TEXTURE_2D, 0, 3, Width, Height, 0, GL_RGB, GL_UNSIGNED_BYTE, pData);
158 end;
159 end
160 }
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
223 begin
231 begin
233 exit;
235 try
237 finally
243 function LoadTextureMemEx (pData: Pointer; dataSize: LongInt; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
244 var
251 begin
257 begin
259 exit;
261 try
263 begin
265 exit;
267 //ConvertImage(img, ifA8R8G8B8);
274 try
275 // it's slow, but i don't care for now
278 begin
280 begin
290 finally
293 finally
299 function LoadTexture (filename: AnsiString; var Texture: GLTexture; var pWidth, pHeight: Word; Fmt: PWord=nil): Boolean;
300 var
304 begin
311 try
313 except
317 begin
319 exit;
322 try
325 try
328 finally
331 finally
337 function LoadTextureEx (filename: AnsiString; var Texture: GLTexture; fX, fY, fWidth, fHeight: Word; Fmt: PWord=nil): Boolean;
338 var
342 begin
347 try
349 except
353 begin
355 exit;
358 try
361 try
364 finally
367 finally