This repository was archived by the owner on Mar 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiqe.lua
678 lines (553 loc) · 14.9 KB
/
iqe.lua
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
local loader = {
_LICENSE = "Inter-Quake Export Loader is distributed under the terms of the MIT license. See LICENSE.md.",
_URL = "https://github.com/excessive/iqe",
_VERSION = "1.0.0",
_DESCRIPTION = "Load an IQE 3D model into Lua.",
}
local IQE = {}
--[[ Helper Functions ]]--
-- http://wiki.interfaceware.com/534.html
local function string_split(s, d)
local t = {}
local i = 0
local f
local match = '(.-)' .. d .. '()'
if string.find(s, d) == nil then
return {s}
end
for sub, j in string.gmatch(s, match) do
i = i + 1
t[i] = sub
f = j
end
if i ~= 0 then
t[i+1] = string.sub(s, f)
end
return t
end
local function merge_quoted(t)
local ret = {}
local merging = false
local buf = ""
for k, v in ipairs(t) do
local f, l = v:sub(1,1), v:sub(v:len())
if f == "\"" and l ~= "\"" then
merging = true
buf = v
else
if merging then
buf = buf .. " " .. v
if l == "\"" then
merging = false
table.insert(ret, buf:sub(2,-2))
end
else
if f == "\"" and l == f then
table.insert(ret, v:sub(2, -2))
else
table.insert(ret, v)
end
end
end
end
return ret
end
local function file_exists(file)
if love then return love.filesystem.isFile(file) end
local f = io.open(file, "r")
if f then f:close() end
return f ~= nil
end
--[[ Load File ]]--
function loader.load(file)
assert(file_exists(file), "File not found: " .. file)
local get_lines
if love then
local filetext = love.filesystem.read(file)
get_lines = function(file) return filetext:gmatch("[^\r\n]+") end
else
get_lines = io.lines
end
local lines = {}
for line in get_lines(file) do
if line == "# Inter-Quake Export" or line[1] ~= "#" then
line = string.gsub(line, "\t", "")
table.insert(lines, line)
end
end
if lines[1] == "# Inter-Quake Export" then
local model = setmetatable({}, {__index = IQE})
model:init(lines, file)
return model
end
end
--[[ Inter-Quake Export ]]--
function IQE:init(lines, file)
self.lines = lines
self.current_mesh = false
self.current_material = false
self.current_joint = false
self.current_animation = false
self.current_frame = false
self.current_vertexarray = false
self.paused = false
self.data = {}
self.materials = {}
self.vertex_buffer = {}
self.stats = {}
self:parse()
if love then
math.random = love.math.random
self:buffer()
end
end
function IQE:parse(lines)
self.lines = self.lines or lines
for _, line in ipairs(self.lines) do
local l = string_split(line, " ")
local cmd = l[1]
table.remove(l, 1)
if self[cmd] then
self[cmd](self, l)
end
end
self.lines = nil
self.current_mesh = nil
self.current_material = nil
self.current_joint = nil
self.current_animation = nil
self.current_frame = nil
self.current_vertexarray = nil
self.current_mtl = nil
end
--[[ Meshes ]]--
function IQE:mesh(line)
line = merge_quoted(line)
self.current_mesh = {}
self.current_mesh.name = line[1]
end
function IQE:material(line)
line = merge_quoted(line)
self.data.material = self.data.material or {}
self.data.material[line[1]] = self.data.material[line[1]] or {}
self.current_material = self.data.material[line[1]]
table.insert(self.current_material, self.current_mesh)
end
--[[ Vertex Attributes ]]--
function IQE:vp(line)
local mesh = self.current_material[#self.current_material]
mesh.vp = mesh.vp or {}
local vp = {}
for _, v in ipairs(line) do
table.insert(vp, tonumber(v))
end
if #vp == 3 then
table.insert(vp, 1)
end
table.insert(mesh.vp, vp)
end
function IQE:vt(line)
local mesh = self.current_material[#self.current_material]
mesh.vt = mesh.vt or {}
local vt = {}
for _, v in ipairs(line) do
table.insert(vt, tonumber(v))
end
table.insert(mesh.vt, vt)
end
function IQE:vn(line)
local mesh = self.current_material[#self.current_material]
mesh.vn = mesh.vn or {}
local vn = {}
for _, v in ipairs(line) do
table.insert(vn, tonumber(v))
end
table.insert(mesh.vn, vn)
end
function IQE:vx(line)
local mesh = self.current_material[#self.current_material]
mesh.vx = mesh.vx or {}
local vx = {}
for _, v in ipairs(line) do
table.insert(vx, tonumber(v))
end
table.insert(mesh.vx, vx)
end
function IQE:vb(line)
local mesh = self.current_material[#self.current_material]
self.rigged = true
mesh.vb = mesh.vb or {}
local vb = {}
for _, v in ipairs(line) do
table.insert(vb, tonumber(v))
end
table.insert(mesh.vb, vb)
end
function IQE:vc(line)
local mesh = self.current_material[#self.current_material]
mesh.vc = mesh.vc or {}
local vc = {}
for _, v in ipairs(line) do
table.insert(vc, tonumber(v))
end
if #vc == 3 then
table.insert(vc, 1)
end
table.insert(mesh.vc, vc)
end
function IQE:v0(line, cmd)
cmd = cmd or "v0"
local mesh = self.current_material[#self.current_material]
mesh[cmd] = mesh[cmd] or {}
local v0 = {}
for _, v in ipairs(line) do
table.insert(v0, tonumber(v))
end
table.insert(mesh[cmd], v0)
end
function IQE:v1(line)
IQE:v0(line, "v1")
end
function IQE:v2(line)
IQE:v0(line, "v2")
end
function IQE:v3(line)
IQE:v0(line, "v3")
end
function IQE:v4(line)
IQE:v0(line, "v4")
end
function IQE:v5(line)
IQE:v0(line, "v5")
end
function IQE:v6(line)
IQE:v0(line, "v6")
end
function IQE:v7(line)
IQE:v0(line, "v7")
end
function IQE:v8(line)
IQE:v0(line, "v8")
end
function IQE:v9(line)
IQE:v0(line, "v9")
end
--[[ Vertex Arrays ]]--
function IQE:vertexarray(line)
line = merge_quoted(line)
self.data.vertexarray = self.data.vertexarray or {}
local va = {}
va.type = line[1]
va.component = line[2]
va.size = tonumber(line[3])
va.name = line[4] or line[1]
table.insert(self.data.vertexarray, va)
self.current_vertexarray = self.data.vertexarray[#self.data.vertexarray]
end
--[[ Triangle ]]--
function IQE:fa(line)
local mesh = self.current_material[#self.current_material]
mesh.fa = mesh.fa or {}
local fa = {}
for k, v in ipairs(line) do
table.insert(fa, tonumber(v))
end
table.insert(mesh.fa, fa)
end
function IQE:fm(line)
local mesh = self.current_material[#self.current_material]
mesh.fm = mesh.fm or {}
mesh.indices = mesh.indices or {}
local fm = {}
-- CCW Winding
table.insert(fm, tonumber(line[1]+1))
table.insert(fm, tonumber(line[3]+1))
table.insert(fm, tonumber(line[2]+1))
table.insert(mesh.indices, tonumber(line[1]+1))
table.insert(mesh.indices, tonumber(line[3]+1))
table.insert(mesh.indices, tonumber(line[2]+1))
table.insert(mesh.fm, fm)
end
--[[ Smoothing ]]--
function IQE:smoothuv(line)
local mesh = self.current_material[#self.current_material]
local n = tonumber(line[1])
mesh.smoothuv = false
if n > 0 then
mesh.smoothuv = true
end
end
function IQE:smoothgroup(line)
local mesh = self.current_material[#self.current_material]
local n = tonumber(line[1])
mesh.smoothgroup = -1
if n then
mesh.smoothgroup = n
end
end
function IQE:smoothangle(line)
local mesh = self.current_material[#self.current_material]
local angle = tonumber(line[1])
mesh.smoothangle = 180
if angle then
mesh.smoothangle = angle
end
end
function IQE:fs(line)
local mesh = self.current_material[#self.current_material]
mesh.fs = mesh.fs or {}
local fs = {}
for k, v in ipairs(line) do
table.insert(fs, tonumber(v))
end
table.insert(mesh.fs, fs)
end
function IQE:vs(line)
local mesh = self.current_material[#self.current_material]
mesh.vs = mesh.vs or {}
local vs = tonumber(line[1])
table.insert(mesh.vs, vs)
end
--[[ Poses ]]--
function IQE:pq(line)
local pq = {}
for _, v in ipairs(line) do
table.insert(pq, tonumber(v))
end
if #pq == 6 then
table.insert(pq, -1)
end
if #pq == 7 then
table.insert(pq, 1)
table.insert(pq, 1)
table.insert(pq, 1)
end
local joint
if not self.current_animation then
joint = self.current_joint
joint.pq = pq
else
joint = self.current_frame
joint.pq = joint.pq or {}
table.insert(joint.pq, pq)
end
end
function IQE:pm(line)
local pm = {}
for _, v in ipairs(line) do
table.insert(pm, tonumber(v))
end
if #pm == 12 then
table.insert(pm, 1)
table.insert(pm, 1)
table.insert(pm, 1)
end
local joint
if not self.current_animation then
joint = self.current_joint
joint.pm = pm
else
joint = self.current_frame
joint.pm = joint.pm or {}
table.insert(joint.pm, pm)
end
end
function IQE:pa(line)
local pa = {}
for _, v in ipairs(line) do
table.insert(pa, tonumber(v))
end
if #pa == 6 then
table.insert(pa, 1)
table.insert(pa, 1)
table.insert(pa, 1)
end
local joint
if not self.current_animation then
joint = self.current_joint
joint.pa = pa
else
joint = self.current_frame
joint.pa = joint.pa or {}
table.insert(joint.pa, pa)
end
end
--[[ Skeleton ]]--
function IQE:joint(line)
line = merge_quoted(line)
self.data.joint = self.data.joint or {}
local joint = {}
joint.name = line[1]
joint.parent = tonumber(line[2]) + 1 -- fix offset
table.insert(self.data.joint, joint)
self.current_joint = joint
end
--[[ Animations ]]--
function IQE:animation(line)
line = merge_quoted(line)
self.data.animation = self.data.animation or {}
local name = line[1] or tostring(math.random(0, 99999))
self.data.animation[name] = {}
self.current_animation = self.data.animation[name]
self.current_frame = false
end
function IQE:loop(line)
local animation = self.current_animation
animation.loop = true
end
function IQE:framerate(line)
local animation = self.current_animation
animation.framerate = tonumber(line[1])
end
function IQE:frame(line)
local animation = self.current_animation
animation.frame = animation.frame or {}
table.insert(animation.frame, {})
self.current_frame = animation.frame[#animation.frame]
end
--[[ Render ]]--
function IQE:buffer()
local cpml = require "cpml"
local stats = self.stats
for k, material in pairs(self.data.material) do
for _, mesh in ipairs(material) do
local layout = {
{ "VertexPosition", "float", 3 },
{ "VertexTexCoord", "float", 2 },
{ "VertexColor", "byte", 4 },
{ "VertexNormal", "float", 3 }
}
if self.rigged then
table.insert(layout, { "VertexBone", "byte", 4 })
table.insert(layout, { "VertexWeight", "float", 4 })
end
local data = {}
local bounds = { min = {}, max = {} }
for i=1, #mesh.vp do
-- all meshes should have these things...
local vp = mesh.vp[i]
local vn = mesh.vn and mesh.vn[i] or {}
local vc = mesh.vc and { mesh.vc[i][1] * 255, mesh.vc[i][2] * 255, mesh.vc[i][3] * 255, mesh.vc[i][4] * 255 } or { 255, 255, 255, 255 }
local vt = mesh.vt and mesh.vt[i] or {}
local current = {
vp[1], vp[2], vp[3],
vt[1] or 0, vt[2] or 0,
vc[1] or 255, vc[2] or 255, vc[3] or 255, vc[4] or 255,
vn[1] or 0, vn[2] or 1, vn[3] or 0
}
bounds.min.x = bounds.min.x and math.min(bounds.min.x, vp[1]) or vp[1]
bounds.max.x = bounds.max.x and math.max(bounds.max.x, vp[1]) or vp[1]
bounds.min.y = bounds.min.y and math.min(bounds.min.y, vp[2]) or vp[2]
bounds.max.y = bounds.max.y and math.max(bounds.max.y, vp[2]) or vp[2]
bounds.min.z = bounds.min.z and math.min(bounds.min.z, vp[3]) or vp[3]
bounds.max.z = bounds.max.z and math.max(bounds.max.z, vp[3]) or vp[3]
-- ...but only rigged ones will have these.
if self.rigged then
local vb = mesh.vb[i]
table.insert(current, vb[1] or 0)
table.insert(current, vb[3] or 0)
table.insert(current, vb[5] or 0)
table.insert(current, vb[7] or 0)
table.insert(current, vb[2] or 0)
table.insert(current, vb[4] or 0)
table.insert(current, vb[6] or 0)
table.insert(current, vb[8] or 0)
end
table.insert(data, current)
end
bounds.min = cpml.vec3(bounds.min)
bounds.max = cpml.vec3(bounds.max)
stats.vertices = (stats.vertices or 0) + #mesh.vt
stats.triangles = (stats.triangles or 0) + #mesh.fm
local m = love.graphics.newMesh(layout, data, "triangles", "static")
m:setVertexMap(mesh.indices)
assert(m, "D:")
table.insert(self.vertex_buffer, { material=k, mesh=m, name=mesh.name, bounds=bounds })
end
end
self:calc_bounds()
-- everything after here only applies to models with skeletons.
if not self.rigged then return end
local anims = {}
local skeleton = {}
for i, joint in ipairs(self.data.joint) do
local v = joint.pq
local bone = {
parent = joint.parent,
name = joint.name,
position = cpml.vec3(v[1], v[2], v[3]),
rotation = cpml.quat(v[4], v[5], v[6], v[7]),
scale = cpml.vec3(v[8], v[9], v[10])
}
skeleton[i], skeleton[bone.name] = bone, bone
end
anims.skeleton = skeleton
-- it's entirely possible for a model to be rigged but not animated.
if not self.data.animation then return end
anims.frames = {}
for k, animation in pairs(self.data.animation) do
local a = {
name = k,
first = #anims.frames+1,
last = #anims.frames+#animation.frame,
framerate = animation.framerate,
loop = animation.loop or false
}
table.insert(anims, a)
anims[a.name] = a
for _, pose in ipairs(animation.frame) do
local frame = {}
for i, v in ipairs(pose.pq) do
table.insert(frame, {
translate = cpml.vec3(v[1], v[2], v[3]),
rotate = cpml.quat(v[4], v[5], v[6], v[7]),
scale = cpml.vec3(v[8], v[9], v[10])
})
end
table.insert(anims.frames, frame)
end
end
self.anims = anims
end
function IQE:dump()
local function create_dump(file, t)
local buffer = ""
local lines = 0
local function dump_r(t, level)
level = level or 1
for k,v in pairs(t) do
buffer = buffer .. rpad(" ", 12 * level) .. rpad(tostring(k), 3) .. " " .. tostring(v) .. "\n"
lines = lines + 1
if lines >= 2048 then
love.filesystem.append(file, buffer)
lines = 0
buffer = ""
end
if type(v) == "table" then
dump_r(v, level + 1)
end
end
end
dump_r(t)
love.filesystem.append(file, buffer)
end
local file = "dump.txt"
love.filesystem.write(file, "")
create_dump(file, self.data)
end
function IQE:calc_bounds()
local cpml = require "cpml"
self.bounds = { min = {}, max = {} }
for _, buffer in ipairs(self.vertex_buffer) do
local b = buffer.bounds
self.bounds.min.x = self.bounds.min.x and math.min(self.bounds.min.x, b.min.x) or b.min.x
self.bounds.max.x = self.bounds.max.x and math.max(self.bounds.max.x, b.max.x) or b.max.x
self.bounds.min.y = self.bounds.min.y and math.min(self.bounds.min.y, b.min.y) or b.min.y
self.bounds.max.y = self.bounds.max.y and math.max(self.bounds.max.y, b.max.y) or b.max.y
self.bounds.min.z = self.bounds.min.z and math.min(self.bounds.min.z, b.min.z) or b.min.z
self.bounds.max.z = self.bounds.max.z and math.max(self.bounds.max.z, b.max.z) or b.max.z
end
self.bounds.min = cpml.vec3(self.bounds.min)
self.bounds.max = cpml.vec3(self.bounds.max)
end
return loader