1 (* Copyright (C) Doom 2D: Forever Developers
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.
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.
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/>.
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
32 ImagingTypes
, Imaging
, ImagingUtility
;
37 width
, height
: Word; // real
38 glwidth
, glheight
: Word; // powerof2
39 u
, v
: Single; // usually 1.0
44 e_DummyTextures
: Boolean = False;
45 TEXTUREFILTER
: Integer = GL_NEAREST
;
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;
62 Classes
, g_options
, utils
;
65 function AlignP2 (n
: Word): Word;
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;
89 tex
.glheight
:= Height
;
95 tex
.glwidth
:= AlignP2(Width
);
96 tex
.glheight
:= AlignP2(Height
);
97 if tex
.glwidth
<> tex
.width
then tex
.u
:= (tex
.width
+0.0)/(tex
.glwidth
+0.0);
98 if tex
.glheight
<> tex
.height
then tex
.v
:= (tex
.height
+0.0)/(tex
.glheight
+0.0);
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);
104 if e_DummyTextures
then
106 tex
.id
:= GLuint(-1);
111 glGenTextures(1, @Texture
);
113 glBindTexture(GL_TEXTURE_2D
, Texture
);
115 if (tex
.glwidth
<> tex
.width
) or (tex
.glheight
<> tex
.height
) then
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
119 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
120 // texture does NOT blend with object background
121 //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
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
133 // for GL_TEXTURE_MAG_FILTER only first two can be used
134 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, TEXTUREFILTER
);
135 // for GL_TEXTURE_MIN_FILTER all of the above can be used
136 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, TEXTUREFILTER
);
138 // create empty texture
139 if aFormat
= GL_RGBA
then fmt
:= GL_RGBA
else fmt
:= GL_RGB
; // silly, yeah?
140 glTexImage2D(GL_TEXTURE_2D
, 0, fmt
, tex
.glwidth
, tex
.glheight
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, nil);
143 GetMem(buf, tex.glwidth*4*tex.glheight);
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
149 for f := 0 to tex.glheight-1 do
151 for c := 0 to tex.glwidth-1 do
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;
158 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 82, tex.glwidth, {tex.glheight}1, fmt, GL_UNSIGNED_BYTE, buf);
165 glTexSubImage2D(GL_TEXTURE_2D
, 0, 0, 0, Width
, Height
, fmt
, GL_UNSIGNED_BYTE
, pData
);
166 //glTexSubImage2D(GL_TEXTURE_2D, 0, 0, tex.glheight-tex.height, Width, Height, fmt, GL_UNSIGNED_BYTE, pData);
168 glBindTexture(GL_TEXTURE_2D
, 0);
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;
181 width
, height
: Integer;
189 if Fmt
<> nil then Fmt
^ := GL_RGBA
; // anyway
191 if (img
.width
< 1) or (img
.width
> 32768) or (img
.height
< 1) or (img
.height
> 32768) then
193 e_WriteLog('Error loading texture: invalid image dimensions', TMsgType
.Warning
);
196 //ConvertImage(img, ifA8R8G8B8);
198 height
:= img
.height
;
201 imageSize
:= Width
*Height
*4;
202 GetMem(image
, imageSize
);
204 // it is slow, but i don't care for now
206 for y
:= height
-1 downto 0 do
208 for x
:= 0 to width
-1 do
210 clr
:= GetPixel32(img
, x
, y
);
211 ii
^ := clr
.r
; Inc(ii
);
212 ii
^ := clr
.g
; Inc(ii
);
213 ii
^ := clr
.b
; Inc(ii
);
214 ii
^ := clr
.a
; Inc(ii
);
217 CreateTexture(Texture
, width
, height
, GL_RGBA
, image
);
225 function LoadTextureMem (pData
: Pointer; dataSize
: LongInt; var Texture
: GLTexture
; var pWidth
, pHeight
: Word; Fmt
: PWord=nil): Boolean;
228 //width, height: Integer;
229 //imageSize: Integer;
237 if Fmt
<> nil then Fmt
^ := GL_RGBA
; // anyway
240 if not LoadImageFromMemory(pData
, dataSize
, img
) then
242 e_WriteLog('Error loading texture: unknown image format', TMsgType
.Warning
);
246 result
:= LoadTextureImg(img
, Texture
, pWidth
, pHeight
, Fmt
);
253 function LoadTextureMemEx (pData
: Pointer; dataSize
: LongInt; var Texture
: GLTexture
; fX
, fY
, fWidth
, fHeight
: Word; Fmt
: PWord=nil): Boolean;
256 //width, height: Integer;
263 if Fmt
<> nil then Fmt
^ := GL_RGBA
; // anyway
266 if not LoadImageFromMemory(pData
, dataSize
, img
) then
268 e_WriteLog('Error loading texture: unknown image format', TMsgType
.Warning
);
272 if (img
.width
< 1) or (img
.width
> 32768) or (img
.height
< 1) or (img
.height
> 32768) then
274 e_WriteLog('Error loading texture: invalid image dimensions', TMsgType
.Warning
);
277 //ConvertImage(img, ifA8R8G8B8);
278 if fX
> img
.width
then exit
;
279 if fY
> img
.height
then exit
;
280 if fX
+fWidth
> img
.width
then exit
;
281 if fY
+fHeight
> img
.height
then exit
;
282 //writeln('fX=', fX, '; fY=', fY, '; fWidth=', fWidth, '; fHeight=', fHeight);
283 imageSize
:= img
.width
*img
.height
*4;
284 GetMem(image
, imageSize
);
286 // it is slow, but i don't care for now
288 for y
:= fY
+fHeight
-1 downto fY
do
290 for x
:= fX
to fX
+fWidth
-1 do
292 clr
:= GetPixel32(img
, x
, y
);
293 ii
^ := clr
.r
; Inc(ii
);
294 ii
^ := clr
.g
; Inc(ii
);
295 ii
^ := clr
.b
; Inc(ii
);
296 ii
^ := clr
.a
; Inc(ii
);
299 CreateTexture(Texture
, fWidth
, fHeight
, GL_RGBA
, image
);
310 function LoadTexture (filename
: AnsiString; var Texture
: GLTexture
; var pWidth
, pHeight
: Word; Fmt
: PWord=nil): Boolean;
319 if Fmt
<> nil then Fmt
^ := GL_RGBA
; // anyway
323 fs
:= openDiskFileRO(filename
);
329 e_WriteLog('Texture "'+filename
+'" not found', TMsgType
.Warning
);
334 imageSize
:= fs
.size
;
335 GetMem(img
, imageSize
);
337 fs
.readBuffer(img
^, imageSize
);
338 result
:= LoadTextureMem(img
, imageSize
, Texture
, pWidth
, pHeight
, Fmt
);
348 function LoadTextureEx (filename
: AnsiString; var Texture
: GLTexture
; fX
, fY
, fWidth
, fHeight
: Word; Fmt
: PWord=nil): Boolean;
355 if Fmt
<> nil then Fmt
^ := GL_RGBA
; // anyway
359 fs
:= openDiskFileRO(filename
);
365 e_WriteLog('Texture "'+filename
+'" not found', TMsgType
.Warning
);
370 imageSize
:= fs
.size
;
371 GetMem(img
, imageSize
);
373 fs
.readBuffer(img
^, imageSize
);
374 result
:= LoadTextureMemEx(img
, imageSize
, Texture
, fX
, fY
, fWidth
, fHeight
, Fmt
);