File: loadmodel.lua

package info (click to toggle)
spring 103.0%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,720 kB
  • ctags: 63,685
  • sloc: cpp: 368,283; ansic: 33,988; python: 12,417; java: 12,203; awk: 5,879; sh: 1,846; xml: 655; perl: 405; php: 211; objc: 194; makefile: 77; sed: 2
file content (344 lines) | stat: -rw-r--r-- 9,075 bytes parent folder | download | duplicates (8)
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
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  file:    loadmodel.lua
--  brief:   utilities for the custom lua 3d model format
--  author:  Dave Rodgers
--
--  Copyright (C) 2007.
--  Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local insert = table.insert

local debug = 1

--------------------------------------------------------------------------------

local currMat = nil

local defMat = {
  ambient   = { 0.2, 0.2, 0.2, 1.0 },
  diffuse   = { 0.8, 0.8, 0.8, 1.0 },
  emission  = { 0.0, 0.0, 0.0, 1.0 },
  specular  = { 0.0, 0.0, 0.0, 1.0 },
  shininess = 0.0,
  texture   = "",
  lighting  = true
}


local function SanitizeMaterial(mat)
  for k,v in pairs(defMat) do
    if (mat[k] == nil) then
      mat[k] = v
    end
    if ((type(v) == "table") and (mat[k][4] == nil)) then
      mat[k][4] = 1.0 -- make the colors r/g/b/A
    end
  end
end


--------------------------------------------------------------------------------

local function AlmostEqual(a, b)
  return (math.abs(a - b) < 0.001)
end
  

local function SameColor(a, b)
  if (AlmostEqual(a[1], b[1]) and
      AlmostEqual(a[2], b[2]) and
      AlmostEqual(a[3], b[3]) and
      AlmostEqual(a[4], b[4])) then
    return true
  end
  return false  
end


local function CompareColor(a, b)
  if (not AlmostEqual(a[1], b[1])) then return (a[1] < b[1]) and -1 or 1 end
  if (not AlmostEqual(a[2], b[2])) then return (a[2] < b[2]) and -1 or 1 end
  if (not AlmostEqual(a[3], b[3])) then return (a[3] < b[3]) and -1 or 1 end
  if (not AlmostEqual(a[4], b[4])) then return (a[4] < b[4]) and -1 or 1 end
  return 0
end


local function CompareMaterials(matPairA, matPairB)
  local matA = matPairA[1]
  local matB = matPairB[1]
  local alphaA = matA.diffuse[4]
  local alphaB = matB.diffuse[4]
  if (not AlmostEqual(alphaA, alphaB)) then return (alphaA > alphaB) end
  if (matA.texture < matB.texture)     then return false end
  if (matA.texture > matB.texture)     then return true  end
  if (matA.lighting ~= matB.lighting)  then return matA.lighting end
  if (matA.lighting) then
    if (not AlmostEqual(matA.shininess, matB.shininess)) then
      return (matA.shininess < matB.shininess)
    end
    local cmpAmbient = CompareColor(matA.ambient, matB.ambient)
    if (cmpAmbient ~= 0)  then return (cmpAmbient > 0) end
    local cmpDiffuse = CompareColor(matA.diffuse, matB.diffuse)
    if (cmpDiffuse ~= 0)  then return (cmpDiffuse > 0) end
    local cmpSpecular = CompareColor(matA.specular, matB.specular)
    if (cmpSpecular ~= 0) then return (cmpSpecular > 0) end
    local cmpEmission = CompareColor(matA.emission, matB.emission)
    if (cmpEmission ~= 0) then return (cmpEmission > 0) end
  end
  return false
end


local function SortMaterials(obj, mats)
  local sorted = {}
  for matname in pairs(obj.mats) do
    local mat = mats[matname]
    if (mat == nil) then
      mat = defMat
    end
    insert(sorted, { mat, matname })
  end
  table.sort(sorted, CompareMaterials)
  if (debug > 0) then
    for k,v in ipairs(sorted) do
      print(k, v[2])
    end
  end
  return sorted
end


local function SetupMaterial(mat, useDepthMask)
  if (currmat == nil) then
    -- set all our states
    if (mat.texture ~= "") then
      gl.Texture(mat.texture)
    else
      gl.Texture(false)
    end
    if (useDepthMask) then
      gl.DepthMask(mat.diffuse[4] >= 1.0)
    end
    gl.Lighting(mat.lighting)
    if (not mat.lighting) then  
      gl.Color(mat.diffuse)
    else
      gl.Material({
        ambient   = mat.ambient,
        diffuse   = mat.diffuse,
        specular  = mat.specular,
        emission  = mat.emission,
        shininess = mat.shininess
      })
    end
    currmat = mat
    return
  end

  if (mat.texture ~= currmat.texture) then
    if (mat.texture ~= "") then
      gl.Texture(mat.texture)
    else
      gl.Texture(false)
    end
  end

  if (useDepthMask) then
    local next = (mat.diffuse[4]     >= 1.0)
    local curr = (currmat.diffuse[4] >= 1.0)
    if (next ~= curr) then
      gl.DepthMask(next)
    end
  end

  if (mat.lighting ~= currmat.lighting) then
    gl.Lighting(mat.lighting)
  end

  if (not mat.lighting) then
    if ((currmat.lighting) or
        (not SameColor(currmat.diffuse, mat.diffuse))) then
      gl.Color(mat.diffuse)
    end
  else
    changeAll = (not currmat.lighting)
    local changes = {}
    if (changeAll or (not SameColor(currmat.ambient, mat.ambient))) then
      changes.ambient = mat.ambient
    end
    if (changeAll or (not SameColor(currmat.diffuse, mat.diffuse))) then
      changes.diffuse = mat.diffuse
    end
    if (changeAll or (not SameColor(currmat.specular, mat.specular))) then
      changes.specular = mat.specular
    end
    if (changeAll or (not SameColor(currmat.emission, mat.emission))) then
      changes.emission = mat.emission
    end
    if (changeAll or (not AlmostEqual(currmat.shininess, mat.shininess))) then
      changes.shininess = mat.shininess
    end
    gl.Material(changes)
  end

  currmat = mat
end


local function SetParam(index, array, name, elem)
  if ((index == nil) or (index <= 0)) then
    return
  end
  local data = array[index]
  if (data == nil) then
    print("indexing error: " .. index .. "[" .. name .. "]")
  else
    elem[name] = data
  end
end


local function DrawObject(obj, mats, v, t, n, c, noMaterials, noDepthMask)
  currmat = nil

  if (debug > 0) then print("obj.name = " .. obj.name) end

  local sortedMats = SortMaterials(obj, mats)

  for _,matPair in ipairs(sortedMats) do

    local mat     = matPair[1]
    local matname = matPair[2]
    local faces = obj.mats[matname]

    if (not noMaterials) then
      SetupMaterial(mat, not noDepthMask)
    end

    local glBeginEnd = gl.BeginEnd
    local glVertex   = gl.Vertex
    local glNormal   = gl.Normal
    local glTexCoord = gl.TexCoord
    local glColor    = gl.Color

    for fi,face in ipairs(faces) do

      glBeginEnd(GL.TRIANGLE_FAN, function()
        for _,elem in ipairs(face) do
          local vi, ti, ni, ci = elem[1], elem[2], elem[3], elem[4]
          if (ci and (ci > 0)) then glColor    (c[ci]) end
          if (ni and (ni > 0)) then glNormal   (n[ni]) end
          if (ti and (ti > 0)) then glTexCoord (t[ti]) end
          if (vi and (vi > 0)) then glVertex   (v[vi]) end
        end
      end)

      if (debug > 1) then
        print("SHAPE: " .. obj.name .. " || " .. matname .. " || face:" .. fi)
        for k1,v1 in ipairs(faceTbl) do
          print(k1,v1)
          for k2,v2 in pairs(v1) do
            print("  " .. k2,v2)
            for k3,v3 in pairs(v2) do
              print("    " .. k3,v3)
            end
          end
        end
      end
    end
  end
  return
end


--------------------------------------------------------------------------------

local function Append(old, new)
  for _,f in ipairs(new) do
    insert(old, f)
  end
end


local function MergeObjects(objs)
  if (objs == nil)    then return nil end
  if (objs[1] == nil) then return {}  end

  local obj = objs[1]
  for i,nextobj in ipairs(objs) do
    if (i > 1) then
      for matname,faces in pairs(nextobj.mats) do
        local oldfaces = obj.mats[matname]
        if (oldfaces) then
          Append(oldfaces, faces)
        else
          obj.mats[matname] = faces
        end
      end
    end
  end
  return obj
end


--------------------------------------------------------------------------------

local function LoadModel(data)
  local version, objs, mats, v, t, n, c, exts
  if (type(data) == 'table') then
    version, objs, mats, v, t, n, c, exts = unpack(data)
  elseif (type(data) == 'string') then
    version, objs, mats, v, t, n, c, exts = include(data)
  else
    print("bad data passed to LoadModel(string|table)")
    return nil
  end

  if (objs == nil) then
    print("LoadModel() failed to load:  " .. tostring(data))
    return nil
  end

  -- make sure the materials are valid
  for name,mat in pairs(mats) do
    SanitizeMaterial(mat)
  end

  -- cache the counts
  local vCount = #v
  local tCount = #t
  local nCount = #n
  local cCount = #c
  objs.vCount = vCount
  objs.tCount = tCount
  objs.nCount = nCount
  objs.cCount = cCount

  -- setup the draw calls
  for _,obj in ipairs(objs) do
    local obj = obj
    obj.Draw = function(useDepthMask)
      return DrawObject(obj, mats, v, t, n, c, useDepthMask)
    end
  end

  -- setup the merge call
  objs.Merge = MergeObjects

  return objs, mats, v, t, n, c, exts
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

return LoadModel

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------