Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.75 KB | None | 0 0
  1. stereoMultiplier = 0.1
  2.  
  3. function renderAsTris(obj,pos,size)
  4.  
  5.     local xt = 1
  6.  
  7.     local n = 1
  8.     while(obj.f[n]) do
  9.     n = n + 1
  10.     end
  11.     local numfaces = n
  12.  
  13.     while(xt < numfaces) do
  14.         local vertex1ind = obj.f[xt][1]["v"] -- Gets the number in the list of the current vertex
  15.         local vertex1 = Vertex.new(pos.x + size * obj.v[vertex1ind].x, pos.y + size * obj.v[vertex1ind].y, pos.z + size * obj.v[vertex1ind].z) -- creates vertex with position and size
  16.  
  17.         local vertex2ind = obj.f[xt][2]["v"]
  18.         local vertex2 = Vertex.new(pos.x + size *  obj.v[vertex2ind].x, pos.y + size *  obj.v[vertex2ind].y, pos.z + size *  obj.v[vertex2ind].z)
  19.  
  20.         local vertex3ind = obj.f[xt][3]["v"]
  21.         local vertex3 = Vertex.new(pos.x + size *  obj.v[vertex3ind].x, pos.y + size *  obj.v[vertex3ind].y, pos.z + size *  obj.v[vertex3ind].z)
  22.  
  23.         drawLine(vertex1,vertex2)
  24.         drawLine(vertex2,vertex3)
  25.         drawLine(vertex3,vertex1)
  26.  
  27.         xt = xt + 1
  28.  
  29.     end
  30. end
  31. -- OBJ LOADER LIBRARY --
  32.  
  33. local path ="."
  34. local loader = {}
  35. local fileString = ""
  36.  
  37. loader.version = "0.0.2"
  38.  
  39. function loader.load(file)
  40.     local lines = {}
  41.     fileStream = io.open(System.currentDirectory()..file,FREAD)
  42.     size = io.size(fileStream)
  43.     str = io.read(fileStream,0,size)
  44.     io.close(fileStream)
  45.      local x, a, b = 1;
  46.       while x < string.len(str) do
  47.         a, b = string.find(str, '.-\n', x);
  48.         if not a then
  49.             break;
  50.         else
  51.             if string.sub(str,a,b) then
  52.                 table.insert(lines,string.sub(str,a,b))
  53.             end
  54.         end;
  55.         x = b + 1;
  56.       end;
  57.             --
  58.     return loader.parse(lines)
  59. end
  60.  
  61. function loader.parse(object)
  62.     local obj = {
  63.         v   = {}, -- List of vertices - x, y, z, [w]=1.0
  64.         vt  = {}, -- Texture coordinates - u, v, [w]=0
  65.         vn  = {}, -- Normals - x, y, z
  66.         vp  = {}, -- Parameter space vertices - u, [v], [w]
  67.         f   = {}, -- Faces
  68.     }
  69.     for _, line in ipairs(object) do
  70.         local l = string_split(line, "%s+")
  71.         if l[1] == "v" then
  72.             local v = {
  73.                 x = tonumber(l[2]),
  74.                 y = tonumber(l[3]),
  75.                 z = tonumber(l[4]),
  76.                 w = tonumber(l[5]) or 1.0
  77.             }
  78.             table.insert(obj.v, v)
  79.         elseif l[1] == "vt" then
  80.             local vt = {
  81.                 u = tonumber(l[2]),
  82.                 v = tonumber(l[3]),
  83.                 w = tonumber(l[4]) or 0
  84.             }
  85.             table.insert(obj.vt, vt)
  86.         elseif l[1] == "vn" then
  87.             local vn = {
  88.                 x = tonumber(l[2]),
  89.                 y = tonumber(l[3]),
  90.                 z = tonumber(l[4]),
  91.             }
  92.             table.insert(obj.vn, vn)
  93.         elseif l[1] == "vp" then
  94.             local vp = {
  95.                 u = tonumber(l[2]),
  96.                 v = tonumber(l[3]),
  97.                 w = tonumber(l[4]),
  98.             }
  99.             table.insert(obj.vp, vp)
  100.         elseif l[1] == "f" then
  101.             local f = {}
  102.             for i=2, #l do
  103.                 local split = string_split(l[i], "/")
  104.                 local v = {}
  105.                 v.v = tonumber(split[1])
  106.                 if split[2] ~= "" then v.vt = tonumber(split[2]) end
  107.                 v.vn = tonumber(split[3])
  108.                 table.insert(f, v)
  109.             end
  110.             table.insert(obj.f, f)
  111.         end
  112.     end
  113.     return obj
  114. end
  115.  
  116. -- http://wiki.interfaceware.com/534.html
  117. function string_split(s, d)
  118.     local t = {}
  119.     local i = 0
  120.     local f
  121.     local match = '(.-)' .. d .. '()'
  122.     if string.find(s, d) == nil then
  123.         return {s}
  124.     end
  125.     for sub, j in string.gmatch(s, match) do
  126.         i = i + 1
  127.         t[i] = sub
  128.         f = j
  129.     end
  130.     if i ~= 0 then
  131.         t[i+1] = string.sub(s, f)
  132.     end
  133.     return t
  134. end
  135.  
  136. function drawLine(vec1,vec2)
  137.  
  138.     Screen.drawLine(round(vec1.x-vec1.z*stereoMultiplier,0), round(vec2.x-vec2.z*stereoMultiplier,0), round(vec1.y,0),round(vec2.y,0), Color.new(255,255,255),TOP_SCREEN, LEFT_EYE)
  139.     Screen.drawLine(round(vec1.x+vec1.z*stereoMultiplier,0), round(vec2.x+vec2.z*stereoMultiplier,0), round(vec1.y,0),round(vec2.y,0), Color.new(255,255,255),TOP_SCREEN, RIGHT_EYE)
  140. end
  141.  
  142.  
  143. function round(num, idp)
  144.   local mult = 10^(idp or 0)
  145.   return math.floor(num * mult + 0.5) / mult
  146. end
  147.  
  148.  
  149. function drawPoint(x,y,z)
  150.     Screen.drawPixel(round(x-z*stereoMultiplier ,0), round(y,0), Color.new(255,0,0), TOP_SCREEN, LEFT_EYE)
  151.     Screen.drawPixel(round(x+z*stereoMultiplier ,0), round(y,0), Color.new(255,0,0), TOP_SCREEN, RIGHT_EYE)
  152. end
  153.  
  154. Vertex = {};
  155. function Vertex.new(x,y,z)
  156.     local self = {}
  157.     self.x = x
  158.     self.y = y
  159.     self.z = z
  160.     return self
  161. end
  162.  
  163.  
  164. local OBJModel = loader.load("/model.obj")
  165. local objPos = Vertex.new(180,100,0)
  166. Screen.enable3D() --Enabling 3D for the top screen
  167. while(true) do
  168.  
  169.     Screen.waitVblankStart() -- Screen related stuff
  170.     Screen.refresh() -- Other Screen related stuff
  171.     Screen.clear(TOP_SCREEN) -- Clear top screen
  172.     Screen.clear(BOTTOM_SCREEN) -- Clear bottom screen
  173.     pad = Controls.read() -- Read Controls
  174.     if(Controls.check(pad, KEY_START)) then-- check if start is pressed
  175.  
  176.         System.exit()-- Exit back to HBL
  177.  
  178.     end
  179.     Screen.debugPrint(0,0,"Hello World",Color.new(255,255,255),BOTTOM_SCREEN) -- Print onto the bottom screen at (0,0), "Hello World" in white
  180.  
  181.     renderAsTris(OBJModel,objPos,50)
  182.     Screen.flip() -- More screen related stuff
  183.  
  184. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement