9dd9192c6128331dac154c196071e97afcac172c
3 { This unit provides interface to load 24-bit and 32-bit uncompressed images
4 from Truevision Targa (TGA) graphic files, and create OpenGL textures
10 Windows
, dglOpenGL
, SysUtils
, e_log
;
13 fUseMipmaps
: Boolean = False;
14 TEXTUREFILTER
: Integer = GL_NEAREST
;
16 // Standard set of images loading functions
17 function LoadTexture( Filename
: String; var Texture
: GLuint
;
18 var pWidth
, pHeight
: Word ): Boolean;
20 function LoadTextureEx( Filename
: String; var Texture
: GLuint
;
21 fX
, fY
, fWidth
, fHeight
: Word ): Boolean;
23 function LoadTextureMem( pData
: Pointer; var Texture
: GLuint
;
24 var pWidth
, pHeight
: Word ): Boolean;
26 function LoadTextureMemEx( pData
: Pointer; var Texture
: GLuint
;
27 fX
, fY
, fWidth
, fHeight
: Word ): Boolean;
32 TTGAHeader
= packed record
36 ColorMapSpec
: array[0..4] of Byte;
37 OrigX
: array[0..1] of Byte;
38 OrigY
: array[0..1] of Byte;
39 Width
: array[0..1] of Byte;
40 Height
: array[0..1] of Byte;
45 // This is auxiliary function that creates OpenGL texture from raw image data
46 function CreateTexture( Width
, Height
, Format
: Word; pData
: Pointer ): Integer;
50 glGenTextures( 1, @Texture
);
51 glBindTexture( GL_TEXTURE_2D
, Texture
);
53 {Texture blends with object background}
54 glTexEnvi( GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
55 {Texture does NOT blend with object background}
56 // glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
59 Select a filtering type.
60 BiLinear filtering produces very good results with little performance impact
62 GL_NEAREST - Basic texture (grainy looking texture)
63 GL_LINEAR - BiLinear filtering
64 GL_LINEAR_MIPMAP_NEAREST - Basic mipmapped texture
65 GL_LINEAR_MIPMAP_LINEAR - BiLinear Mipmapped texture
68 // for GL_TEXTURE_MAG_FILTER only first two can be used
69 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, TEXTUREFILTER
);
70 // for GL_TEXTURE_MIN_FILTER all of the above can be used
71 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, TEXTUREFILTER
);
73 if Format
= GL_RGBA
then
76 gluBuild2DMipmaps( GL_TEXTURE_2D
, GL_RGBA
, Width
, Height
, GL_RGBA
,
77 GL_UNSIGNED_BYTE
, pData
)
79 glTexImage2D( GL_TEXTURE_2D
, 0, 4, Width
, Height
,
80 0, GL_RGBA
, GL_UNSIGNED_BYTE
, pData
);
84 gluBuild2DMipmaps( GL_TEXTURE_2D
, 3, Width
, Height
, GL_RGB
,
85 GL_UNSIGNED_BYTE
, pData
)
87 glTexImage2D( GL_TEXTURE_2D
, 0, 3, Width
, Height
,
88 0, GL_RGB
, GL_UNSIGNED_BYTE
, pData
);
94 function LoadTextureMem( pData
: Pointer; var Texture
: GLuint
;
95 var pWidth
, pHeight
: Word ): Boolean;
97 TGAHeader
: TTGAHeader
;
99 Width
, Height
: Integer;
112 CopyMemory( @TGAHeader
, pData
, SizeOf(TGAHeader
) );
114 if ( TGAHeader
.ImageType
<> 2 ) then
116 e_WriteLog( 'Error loading texture: Bad ImageType', MSG_WARNING
);
120 if ( TGAHeader
.ColorMapType
<> 0 ) then
122 e_WriteLog( 'Error loading texture: Bad ColorMapType', MSG_WARNING
);
126 if ( TGAHeader
.BPP
< 24 ) then
128 e_WriteLog( 'Error loading texture: BPP less than 24', MSG_WARNING
);
132 Width
:= TGAHeader
.Width
[0] + TGAHeader
.Width
[1] * 256;
133 Height
:= TGAHeader
.Height
[0] + TGAHeader
.Height
[1] * 256;
134 BPP
:= TGAHeader
.BPP
;
136 ImageSize
:= Width
* Height
* (BPP
div 8);
138 GetMem( Image
, ImageSize
);
139 CopyMemory( Image
, Pointer( Integer(pData
) + SizeOf(TGAHeader
) ), ImageSize
);
141 for i
:= 0 to Width
* Height
- 1 do
143 Front
:= Pointer( Integer(Image
) + i
*(BPP
div 8) );
144 Back
:= Pointer( Integer(Image
) + i
*(BPP
div 8) + 2 );
151 Texture
:= CreateTexture( Width
, Height
, GL_RGB
, Image
)
153 Texture
:= CreateTexture( Width
, Height
, GL_RGBA
, Image
);
163 function LoadTextureMemEx( pData
: Pointer; var Texture
: GLuint
;
164 fX
, fY
, fWidth
, fHeight
: Word ): Boolean;
166 TGAHeader
: TTGAHeader
;
167 image
, image2
: Pointer;
168 Width
, Height
: Integer;
180 CopyMemory( @TGAHeader
, pData
, SizeOf(TGAHeader
) );
182 if ( TGAHeader
.ImageType
<> 2 ) then
184 e_WriteLog( 'Error loading texture: Bad ImageType', MSG_WARNING
);
188 if ( TGAHeader
.ColorMapType
<> 0 ) then
190 e_WriteLog( 'Error loading texture: Bad ColorMapType', MSG_WARNING
);
194 if ( TGAHeader
.BPP
< 24 ) then
196 e_WriteLog( 'Error loading texture: BPP less than 24', MSG_WARNING
);
200 Width
:= TGAHeader
.Width
[0] + TGAHeader
.Width
[1] * 256;
201 Height
:= TGAHeader
.Height
[0] + TGAHeader
.Height
[1] * 256;
202 BPP
:= TGAHeader
.BPP
;
204 if fX
> Width
then Exit
;
205 if fY
> Height
then Exit
;
206 if fX
+fWidth
> Width
then Exit
;
207 if fY
+fHeight
> Height
then Exit
;
209 ImageSize
:= Width
* Height
* (BPP
div 8);
210 GetMem( Image2
, ImageSize
);
211 CopyMemory( Image2
, Pointer( Integer(pData
) + SizeOf(TGAHeader
) ), ImageSize
);
215 for i
:= 0 to Width
* Height
- 1 do
217 Front
:= Pointer( Integer(Image2
) + i
* a
);
218 Back
:= Pointer( Integer(Image2
) + i
* a
+ 2 );
224 fY
:= Height
- (fY
+ fHeight
);
226 ImageSize
:= fHeight
* fWidth
* (BPP
div 8);
227 GetMem( Image
, ImageSize
);
229 Base
:= Integer( Image2
) + fY
* Width
* (BPP
div 8) + fX
* (BPP
div 8);
230 a
:= fWidth
* (BPP
div 8);
231 b
:= Width
* (BPP
div 8);
233 for i
:= 0 to fHeight
-1 do
234 CopyMemory( Pointer( Integer(image
) + a
*i
), Pointer( Base
+ b
*i
), a
);
237 Texture
:= CreateTexture( fWidth
, fHeight
, GL_RGB
, image
)
239 Texture
:= CreateTexture( fWidth
, fHeight
, GL_RGBA
, image
);
247 function LoadTexture( Filename
: String; var Texture
: GLuint
;
248 var pWidth
, pHeight
: Word ): Boolean;
250 TGAHeader
: TTGAHeader
;
254 Width
, Height
: Integer;
267 if not FileExists(Filename
) then
269 e_WriteLog('Texture ' + Filename
+ ' not found', MSG_WARNING
);
273 AssignFile( TGAFile
, Filename
);
275 BlockRead( TGAFile
, TGAHeader
, SizeOf(TGAHeader
) );
277 if ( TGAHeader
.ImageType
<> 2 ) then
279 CloseFile( TGAFile
);
280 e_WriteLog( 'Error loading texture: Bad ImageType', MSG_WARNING
);
284 if ( TGAHeader
.ColorMapType
<> 0 ) then
286 CloseFile( TGAFile
);
287 e_WriteLog( 'Error loading texture: Bad ColorMapType', MSG_WARNING
);
291 if ( TGAHeader
.BPP
< 24 ) then
293 CloseFile( TGAFile
);
294 e_WriteLog( 'Error loading texture: BPP less than 24', MSG_WARNING
);
298 Width
:= TGAHeader
.Width
[0] + TGAHeader
.Width
[1] * 256;
299 Height
:= TGAHeader
.Height
[0] + TGAHeader
.Height
[1] * 256;
300 BPP
:= TGAHeader
.BPP
;
302 ImageSize
:= Width
* Height
* (BPP
div 8);
304 GetMem( Image
, ImageSize
);
306 BlockRead( TGAFile
, image
^, ImageSize
, bytesRead
);
307 if ( bytesRead
<> ImageSize
) then
309 CloseFile( TGAFile
);
313 CloseFile( TGAFile
);
315 for i
:= 0 to Width
* Height
- 1 do
317 Front
:= Pointer( Integer(Image
) + i
* (BPP
div 8) );
318 Back
:= Pointer( Integer(Image
) + i
* (BPP
div 8) + 2 );
325 Texture
:= CreateTexture( Width
, Height
, GL_RGB
, Image
)
327 Texture
:= CreateTexture( Width
, Height
, GL_RGBA
, Image
);
337 function LoadTextureEx( Filename
: String; var Texture
: GLuint
;
338 fX
, fY
, fWidth
, fHeight
: Word ): Boolean;
340 TGAHeader
: TTGAHeader
;
342 image
, image2
: Pointer;
343 Width
, Height
: Integer;
355 if not FileExists(Filename
) then
357 e_WriteLog( 'Texture ' + Filename
+ ' not found', MSG_WARNING
);
361 AssignFile( TGAFile
, Filename
);
363 BlockRead( TGAFile
, TGAHeader
, SizeOf(TGAHeader
) );
365 if ( TGAHeader
.ImageType
<> 2 ) then
367 CloseFile( TGAFile
);
368 e_WriteLog( 'Error loading texture: Bad ImageType', MSG_WARNING
);
372 if ( TGAHeader
.ColorMapType
<> 0 ) then
374 CloseFile( TGAFile
);
375 e_WriteLog( 'Error loading texture: Bad ColorMapType', MSG_WARNING
);
379 if ( TGAHeader
.BPP
< 24 ) then
381 CloseFile( TGAFile
);
382 e_WriteLog( 'Error loading texture: BPP less than 24', MSG_WARNING
);
386 Width
:= TGAHeader
.Width
[0] + TGAHeader
.Width
[1] * 256;
387 Height
:= TGAHeader
.Height
[0] + TGAHeader
.Height
[1] * 256;
388 BPP
:= TGAHeader
.BPP
;
390 if fX
> Width
then Exit
;
391 if fY
> Height
then Exit
;
392 if fX
+fWidth
> Width
then Exit
;
393 if fY
+fHeight
> Height
then Exit
;
395 ImageSize
:= Width
* Height
* (BPP
div 8);
396 GetMem( Image2
, ImageSize
);
397 BlockRead( TGAFile
, Image2
^, ImageSize
);
399 CloseFile( TGAFile
);
401 for i
:= 0 to Width
* Height
- 1 do
403 Front
:= Pointer( Integer(Image2
) + i
* (BPP
div 8) );
404 Back
:= Pointer( Integer(Image2
) + i
* (BPP
div 8) + 2 );
410 fY
:= Height
- (fY
+ fHeight
);
412 ImageSize
:= fHeight
* fWidth
* (BPP
div 8);
413 GetMem( Image
, ImageSize
);
415 Base
:= Integer(Image2
) + fY
* Width
* (BPP
div 8) + fX
* (BPP
div 8);
417 for i
:= 0 to fHeight
-1 do
419 CopyMemory( Pointer( Integer(image
) + fWidth
* (BPP
div 8) * i
),
420 Pointer( Base
+ Width
* (BPP
div 8) * i
), fWidth
* (BPP
div 8) );
424 Texture
:= CreateTexture( fWidth
, fHeight
, GL_RGB
, Image
)
426 Texture
:= CreateTexture( fWidth
, fHeight
, GL_RGBA
, Image
);