Advertisement
MoonlightOwl

OBJ library

Aug 29th, 2015
979
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.38 KB | None | 0 0
  1. --    OBJ model library 1.0
  2. --    ---------------------
  3. -- by Totoro (computercraft.ru)
  4.  
  5. local obj = {}
  6. local vertex = {}
  7. local faces = {}
  8.  
  9. local color = {r = 1, g = 0, b = 0}
  10. local position = {x = 0, y = 0, z = 0}
  11. local scale = 1.0
  12.  
  13. local function getVertex(data)
  14.   return tonumber(data:match("(-?%d+)/"))
  15. end
  16.  
  17. obj.load = function(filename)
  18.   file = io.open(filename, "r")
  19.   if file ~= nil then
  20.     vertex = {}
  21.    
  22.     for line in file:lines() do
  23.       if line ~= nil and line ~= '' then
  24.         local key, a, b, c, d = line:match("(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s*(%S*)")
  25.        
  26.         -- load vertex data / skip normals and textures
  27.         if key == 'v' then
  28.           local x, y, z = tonumber(a), tonumber(b), tonumber(c)
  29.           table.insert(vertex, {x,y,z})
  30.          
  31.         -- load faces data
  32.         elseif key == 'f' then
  33.           local v1, v2, v3 = getVertex(a), getVertex(b), getVertex(c)
  34.           if v1 < 0 then
  35.             local len = #vertex + 1
  36.             v1, v2, v3 = len + v1, len + v2, len + v3
  37.           end
  38.           table.insert(faces, {vertex[v1], vertex[v2], vertex[v3]})
  39.         end
  40.       end
  41.     end
  42.     file:close()
  43.   else
  44.     error("[OBJ.load] File not found!")
  45.   end
  46. end
  47.  
  48. obj.setColor = function(r, g, b)
  49.   color.r = r; color.g = g; color.b = b
  50. end
  51. obj.setPosition = function(x, y, z)
  52.   position.x = x; position.y = y; position.z = z
  53. end
  54. obj.setScale = function(s)
  55.   scale = s
  56. end
  57.  
  58. obj.getColor = function()
  59.   return {color.r, color.g, color.b}
  60. end
  61. obj.getPosition = function()
  62.   return {position.x, position.y, position.z}
  63. end
  64. obj.getScale = function()
  65.   return scale
  66. end
  67.  
  68. obj.draw = function(glasses)
  69.   local count = 0
  70.   for n, face in pairs(faces) do
  71.     local triangle = glasses.addTriangle3D()
  72.     triangle.setVertex(1, face[1][1]*scale + position.x, face[1][2]*scale + position.y, face[1][3]*scale + position.z)
  73.     triangle.setVertex(2, face[2][1]*scale + position.x, face[2][2]*scale + position.y, face[2][3]*scale + position.z)
  74.     triangle.setVertex(3, face[3][1]*scale + position.x, face[3][2]*scale + position.y, face[3][3]*scale + position.z)
  75.     triangle.setColor(color.r, color.g, color.b)
  76.     ---
  77.     count = count + 1
  78.     if count > 50 then count = 0; os.sleep(0.1) end
  79.   end
  80. end
  81.  
  82. obj.getVertexNum = function()
  83.   return #vertex
  84. end
  85. obj.getPolyNum = function()
  86.   return #faces
  87. end
  88.  
  89. return obj
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement