forked from harrisonpartch/spasim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGLTextures.Mod
More file actions
351 lines (275 loc) · 12.1 KB
/
GLTextures.Mod
File metadata and controls
351 lines (275 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
MODULE GLTextures; (** AUTHOR "fnecati"; PURPOSE "opengl texture loading"; *)
IMPORT
WMGraphics, Raster, SYSTEM, Files,
KernelLog, gl := OpenGL, GLC := OpenGLConst (*, glu := GLU*);
CONST
debug = FALSE;
CONST
MAGIC_DDS = 20534444H; (* "DDS "*)
FOURCC_DXT1 = 31545844H; (* Equivalent to "DXT1" in ASCII *)
FOURCC_DXT3 = 33545844H; (* Equivalent to "DXT3" in ASCII *)
FOURCC_DXT5 = 35545844H; (* Equivalent to "DXT5" in ASCII *)
(* Structures from Direct3D 9 *)
TYPE
D3DPixelFormat = RECORD (* DDPIXELFORMAT *)
size: LONGINT;
flags: LONGINT;
fourCC: LONGINT;
rgbBitCount: LONGINT;
rBitMask, gBitMask, bBitMask: LONGINT;
rgbAlphaBitMask: LONGINT;
END
TYPE D3DCaps2 = RECORD
caps1: LONGINT;
caps2: LONGINT;
reserved: ARRAY 2 OF LONGINT;
END;
TYPE
D3DSurfaceDesc2 = RECORD
size: LONGINT;
flags: LONGINT;
height: LONGINT;
width: LONGINT;
pitchOrLinearSize: LONGINT;
depth: LONGINT;
mipMapCount: LONGINT;
reserved1: ARRAY 11 OF LONGINT;
pixelFormat: D3DPixelFormat;
caps: D3DCaps2;
reserved2: LONGINT;
END;
DDSImage* = OBJECT
VAR
width*, height*, blockSize*, mipMaps*: LONGINT;
format*: LONGINT;
pixels*: POINTER TO ARRAY OF CHAR;
END DDSImage;
(** This function loads an image file and return the OpenGL reference ID to use that texture *)
PROCEDURE LoadTexture* (CONST filename: ARRAY OF CHAR): LONGINT;
VAR
img0, img: Raster.Image;
textureNumber: LONGINT;
BEGIN
img0:= WMGraphics.LoadImage(filename, FALSE); (* loaded image format is BGRA8888 - > GL.GL_BGRA *)
IF img0 = NIL THEN
KernelLog.String(filename); KernelLog.String(" Not Loaded"); KernelLog.Ln;
RETURN -1; (* Open the file for reading *)
END;
img := TransposeImage(img0);
gl.GenTextures(1, ADDRESSOF(textureNumber));
gl.BindTexture(GLC.GL_TEXTURE_2D, textureNumber); (* Bind the ID texture specified by the 2nd parameter *)
gl.TexEnvi(GLC.GL_TEXTURE_ENV, GLC.GL_TEXTURE_ENV_MODE, GLC.GL_MODULATE); (* Texture blends with object background *)
(* gl.TexEnvi(GLC.GL_TEXTURE_ENV, GLC.GL_TEXTURE_ENV_MODE, GLC.GL_DECAL); *) (* Texture does NOT blend with object background *)
(* Select a filtering type. BiLinear filtering produces very good results with little performance impact
GL_NEAREST - Basic texture (grainy looking texture)
GL_LINEAR - BiLinear filtering
GL_LINEAR_MIPMAP_NEAREST - Basic mipmapped texture
GL_LINEAR_MIPMAP_LINEAR - BiLinear Mipmapped texture
*)
(* The next commands sets the texture parameters *)
gl.TexParameterf(GLC.GL_TEXTURE_2D, GLC.GL_TEXTURE_MAG_FILTER, GLC.GL_LINEAR); (* only first two can be used *)
gl.TexParameterf(GLC.GL_TEXTURE_2D, GLC.GL_TEXTURE_MIN_FILTER, GLC.GL_LINEAR); (* all of the above can be used*)
(* And create 2d mipmaps for the minifying function *)
(* res := glu.Build2DMipmaps(GLC.GL_TEXTURE_2D, GLC.GL_RGBA, img.width, img.height, GLC.GL_BGRA, GLC.GL_UNSIGNED_BYTE, img.adr); *)
gl.TexImage2D(GLC.GL_TEXTURE_2D, 0, GLC.GL_RGBA, img.width, img.height, 0, GLC.GL_BGRA, GLC.GL_UNSIGNED_BYTE, img.adr); (* use when not wanting mipmaps to be built by openGL *)
img0 := NIL;
img := NIL;
RETURN textureNumber; (* Returns the current texture OpenGL ID *)
END LoadTexture;
(** This function loads an image file and return the OpenGL reference ID to use that texture *)
PROCEDURE LoadTextureRGB* (CONST filename: ARRAY OF CHAR): LONGINT;
VAR
img0, img: Raster.Image;
textureNumber: LONGINT;
mode: Raster.Mode;
BEGIN
img0:= WMGraphics.LoadImage(filename, FALSE); (* loaded image format is BGRA8888 - > GL.GL_BGRA *)
IF img0 = NIL THEN
KernelLog.String(filename); KernelLog.String(" Not Loaded"); KernelLog.Ln;
RETURN -1; (* Open the file for reading *)
END;
NEW(img);
Raster.Create(img, img0.width, img0.height, Raster.BGR888);
Raster.InitMode(mode, Raster.srcCopy);
Raster.Copy(img0, img, 0,0,img.width, img.height, 0,0, mode);
img := TransposeImage(img0);
gl.GenTextures(1, ADDRESSOF(textureNumber));
gl.BindTexture(GLC.GL_TEXTURE_2D, textureNumber); (* Bind the ID texture specified by the 2nd parameter *)
(* gl.TexEnvi(GLC.GL_TEXTURE_ENV, GLC.GL_TEXTURE_ENV_MODE, GLC.GL_MODULATE); (* Texture blends with object background *)*)
(* gl.TexEnvi(GLC.GL_TEXTURE_ENV, GLC.GL_TEXTURE_ENV_MODE, GLC.GL_DECAL); *) (* Texture does NOT blend with object background *)
(* Select a filtering type. BiLinear filtering produces very good results with little performance impact
GL_NEAREST - Basic texture (grainy looking texture)
GL_LINEAR - BiLinear filtering
GL_LINEAR_MIPMAP_NEAREST - Basic mipmapped texture
GL_LINEAR_MIPMAP_LINEAR - BiLinear Mipmapped texture
*)
(* The next commands sets the texture parameters *)
gl.TexParameteri(GLC.GL_TEXTURE_2D, GLC.GL_TEXTURE_WRAP_S, GLC.GL_REPEAT);
gl.TexParameteri(GLC.GL_TEXTURE_2D, GLC.GL_TEXTURE_WRAP_T, GLC.GL_REPEAT);
gl.TexParameterf(GLC.GL_TEXTURE_2D, GLC.GL_TEXTURE_MAG_FILTER, GLC.GL_LINEAR); (* only first two can be used *)
gl.TexParameterf(GLC.GL_TEXTURE_2D, GLC.GL_TEXTURE_MIN_FILTER, GLC.GL_LINEAR); (* all of the above can be used*)
(* And create 2d mipmaps for the minifying function *)
(* res := glu.Build2DMipmaps(GLC.GL_TEXTURE_2D, GLC.GL_RGB, img.width, img.height, GLC.GL_RGB, GLC.GL_UNSIGNED_BYTE, img.adr); *)
gl.TexImage2D(GLC.GL_TEXTURE_2D, 0, GLC.GL_RGBA, img.width, img.height, 0, GLC.GL_BGRA, GLC.GL_UNSIGNED_BYTE, img.adr); (* use when not wanting mipmaps to be built by openGL *)
img0 := NIL;
img := NIL;
RETURN textureNumber; (* Returns the current texture OpenGL ID *)
END LoadTextureRGB;
PROCEDURE LoadTextureCustom* (CONST filename: ARRAY OF CHAR): LONGINT;
VAR
img0, img: Raster.Image;
textureNumber: LONGINT;
BEGIN
img0:= WMGraphics.LoadImage(filename, FALSE); (* loaded image format is BGRA8888 - > GL.GL_BGRA *)
IF img0 = NIL THEN
KernelLog.String(filename); KernelLog.String(" Not Loaded"); KernelLog.Ln;
RETURN -1; (* Open the file for reading *)
END;
img := img0;
gl.GenTextures(1, ADDRESSOF(textureNumber));
gl.BindTexture(GLC.GL_TEXTURE_2D, textureNumber); (* Bind the ID texture specified by the 2nd parameter *)
gl.TexImage2D(GLC.GL_TEXTURE_2D, 0, GLC.GL_RGBA, img.width, img.height, 0, GLC.GL_BGRA, GLC.GL_UNSIGNED_BYTE, img.adr);
(* ... nice trilinear filtering. *)
gl.TexParameteri(GLC.GL_TEXTURE_2D, GLC.GL_TEXTURE_WRAP_S, GLC.GL_REPEAT);
gl.TexParameteri(GLC.GL_TEXTURE_2D, GLC.GL_TEXTURE_WRAP_T, GLC.GL_REPEAT);
gl.TexParameteri(GLC.GL_TEXTURE_2D, GLC.GL_TEXTURE_MAG_FILTER, GLC.GL_LINEAR);
gl.TexParameteri(GLC.GL_TEXTURE_2D, GLC.GL_TEXTURE_MIN_FILTER, GLC.GL_LINEAR_MIPMAP_LINEAR);
gl.GenerateMipmap(GLC.GL_TEXTURE_2D);
img0 := NIL;
img := NIL;
RETURN textureNumber; (* Returns the current texture OpenGL ID *)
END LoadTextureCustom;
PROCEDURE LoadTextureCustomFlipped* (CONST filename: ARRAY OF CHAR): LONGINT;
VAR
img0, img: Raster.Image;
textureNumber: LONGINT;
BEGIN
img0:= WMGraphics.LoadImage(filename, FALSE); (* loaded image format is BGRA8888 - > GL.GL_BGRA *)
IF img0 = NIL THEN
KernelLog.String(filename); KernelLog.String(" Not Loaded"); KernelLog.Ln;
RETURN -1; (* Open the file for reading *)
END;
img := TransposeImage(img0);
gl.GenTextures(1, ADDRESSOF(textureNumber));
gl.BindTexture(GLC.GL_TEXTURE_2D, textureNumber); (* Bind the ID texture specified by the 2nd parameter *)
gl.TexImage2D(GLC.GL_TEXTURE_2D, 0, GLC.GL_RGBA, img.width, img.height, 0, GLC.GL_BGRA, GLC.GL_UNSIGNED_BYTE, img.adr);
(* ... nice trilinear filtering. *)
gl.TexParameteri(GLC.GL_TEXTURE_2D, GLC.GL_TEXTURE_WRAP_S, GLC.GL_REPEAT);
gl.TexParameteri(GLC.GL_TEXTURE_2D, GLC.GL_TEXTURE_WRAP_T, GLC.GL_REPEAT);
gl.TexParameteri(GLC.GL_TEXTURE_2D, GLC.GL_TEXTURE_MAG_FILTER, GLC.GL_LINEAR);
gl.TexParameteri(GLC.GL_TEXTURE_2D, GLC.GL_TEXTURE_MIN_FILTER, GLC.GL_LINEAR_MIPMAP_LINEAR);
gl.GenerateMipmap(GLC.GL_TEXTURE_2D);
img0 := NIL;
img := NIL;
RETURN textureNumber; (* Returns the current texture OpenGL ID *)
END LoadTextureCustomFlipped;
PROCEDURE LoadDDS*(CONST imagepath: ARRAY OF CHAR): gl.Uint;
VAR
file: Files.File;
read: Files.Reader;
cnt: LONGINT;
textureID: gl.Uint;
dumy, height, width: LONGINT;
buffer: POINTER TO ARRAY OF CHAR;
components, format: LONGINT;
blockSize, offset, level, bufSize: LONGINT;
header: ARRAY 124 OF CHAR;
dds: D3DSurfaceDesc2;
BEGIN
(* try to open the file *)
file := Files.Old(imagepath);
IF file = NIL THEN
RETURN 0;
END;
Files.OpenReader(read, file, 0);
(* verify the type of file *)
read.RawLInt(dumy);
IF dumy # MAGIC_DDS THEN
KernelLog.String(" not a DDS file "); KernelLog.Ln;
RETURN 0;
END;
read.Bytes(header, 0,124, cnt);
dds := SYSTEM.VAL(D3DSurfaceDesc2, header);
(* 124 bytes read till now + 4 bytes for DDS*)
IF dds.pixelFormat.fourCC = FOURCC_DXT1 THEN components := 3 ELSE components := 4 END;
IF dds.pixelFormat.fourCC = FOURCC_DXT1 THEN
format := GLC.GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
ELSIF dds.pixelFormat.fourCC = FOURCC_DXT3 THEN
format := GLC.GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
ELSIF dds.pixelFormat.fourCC =FOURCC_DXT5 THEN
format := GLC.GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
ELSE
RETURN 0;
END;
IF format = GLC.GL_COMPRESSED_RGBA_S3TC_DXT1_EXT THEN
blockSize := 8
ELSE
blockSize := 16;
END;
IF debug THEN
KernelLog.String("size= "); KernelLog.Int(dds.size, 0); KernelLog.Ln;
KernelLog.String("height= "); KernelLog.Int(dds.height, 0); KernelLog.Ln;
KernelLog.String("width= "); KernelLog.Int(dds.width, 0); KernelLog.Ln;
KernelLog.String("pitchOrLinearSize= "); KernelLog.Int(dds.pitchOrLinearSize, 0); KernelLog.Ln;
KernelLog.String("mipMapCount= "); KernelLog.Int(dds.mipMapCount, 0); KernelLog.Ln;
KernelLog.String("dds.pixelFormat.fourCC= "); KernelLog.Int(dds.pixelFormat.fourCC, 0); KernelLog.Ln;
KernelLog.String("components= "); KernelLog.Int(components, 0); KernelLog.Ln;
KernelLog.String("format= "); KernelLog.Int(format, 0); KernelLog.Ln;
KernelLog.String("blockSize= "); KernelLog.Int(blockSize, 0); KernelLog.Ln;
END;
width := dds.width;
height := dds.height;
level := 0;
bufSize :=0;
(* How big is it going to be including all mipmaps? *)
WHILE (level < dds.mipMapCount) & ((width # 0) OR (height # 0)) DO
bufSize := bufSize + ((width+3) DIV 4)*((height+3) DIV 4)*blockSize;
width := width DIV 2; (* IF width = 0 THEN width := 1; END;*)
height := height DIV 2; (* IF height = 0 THEN height := 1; END;*)
INC(level);
END;
IF debug THEN KernelLog.String("bufSize (bytes)= "); KernelLog.Int(bufSize, 0); KernelLog.Ln; END;
(* alocate pixels memory *)
NEW(buffer, bufSize);
read.Bytes(buffer^, 0, bufSize, cnt);
IF debug THEN KernelLog.String("read.Bytes cnt= "); KernelLog.Int(cnt, 0); KernelLog.Ln; END;
(* upload the texture *)
(* Create one OpenGL texture *)
gl.GenTextures(1, ADDRESSOF(textureID));
(* "Bind" the newly created texture : all future texture functions will modify this texture *)
gl.BindTexture(GLC.GL_TEXTURE_2D, textureID);
(* missing mipmaps won't be a problem anymore. *)
gl.TexParameteri(GLC.GL_TEXTURE_2D, GLC.GL_TEXTURE_MAX_LEVEL, dds.mipMapCount - 1);
(* Upload each mipmaps *)
width := dds.width;
height := dds.height;
offset := 0;
level := 0;
WHILE (level < dds.mipMapCount) & ((width # 0) OR (height # 0)) DO
bufSize := ((width+3) DIV 4)*((height+3) DIV 4)*blockSize;
gl.CompressedTexImage2D(GLC.GL_TEXTURE_2D, level, format, width, height, 0, bufSize, ADDRESSOF(buffer[offset ]) );
offset := offset + bufSize;
width := width DIV 2; (* IF width = 0 THEN width := 1; END; *)
height := height DIV 2; (* IF height = 0 THEN height := 1; END; *)
INC(level);
END;
RETURN textureID;
END LoadDDS;
PROCEDURE TransposeImage(im: Raster.Image): Raster.Image;
VAR i, j: LONGINT;
tim: Raster.Image;
pix: Raster.Pixel;
mode: Raster.Mode;
BEGIN
Raster.InitMode(mode, Raster.srcCopy);
NEW(tim);
Raster.Create(tim, im.width, im.height, im.fmt);
FOR j :=0 TO im.height-1 DO
FOR i:=0 TO im.width-1 DO
Raster.Get(im,i, j, pix, mode);
Raster.Put(tim, i, im.height-j-1, pix, mode); (* flip vertical *)
END;
END;
RETURN tim;
END TransposeImage;
END GLTextures.