Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- stereoMultiplier = 0.1
- function renderAsTris(obj,pos,size)
- local xt = 1
- local n = 1
- while(obj.f[n]) do
- n = n + 1
- end
- local numfaces = n
- while(xt < numfaces) do
- local vertex1ind = obj.f[xt][1]["v"] -- Gets the number in the list of the current vertex
- 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
- local vertex2ind = obj.f[xt][2]["v"]
- 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)
- local vertex3ind = obj.f[xt][3]["v"]
- 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)
- drawLine(vertex1,vertex2)
- drawLine(vertex2,vertex3)
- drawLine(vertex3,vertex1)
- xt = xt + 1
- end
- end
- -- OBJ LOADER LIBRARY --
- local path ="."
- local loader = {}
- local fileString = ""
- loader.version = "0.0.2"
- function loader.load(file)
- local lines = {}
- fileStream = io.open(System.currentDirectory()..file,FREAD)
- size = io.size(fileStream)
- str = io.read(fileStream,0,size)
- io.close(fileStream)
- local x, a, b = 1;
- while x < string.len(str) do
- a, b = string.find(str, '.-\n', x);
- if not a then
- break;
- else
- if string.sub(str,a,b) then
- table.insert(lines,string.sub(str,a,b))
- end
- end;
- x = b + 1;
- end;
- --
- return loader.parse(lines)
- end
- function loader.parse(object)
- local obj = {
- v = {}, -- List of vertices - x, y, z, [w]=1.0
- vt = {}, -- Texture coordinates - u, v, [w]=0
- vn = {}, -- Normals - x, y, z
- vp = {}, -- Parameter space vertices - u, [v], [w]
- f = {}, -- Faces
- }
- for _, line in ipairs(object) do
- local l = string_split(line, "%s+")
- if l[1] == "v" then
- local v = {
- x = tonumber(l[2]),
- y = tonumber(l[3]),
- z = tonumber(l[4]),
- w = tonumber(l[5]) or 1.0
- }
- table.insert(obj.v, v)
- elseif l[1] == "vt" then
- local vt = {
- u = tonumber(l[2]),
- v = tonumber(l[3]),
- w = tonumber(l[4]) or 0
- }
- table.insert(obj.vt, vt)
- elseif l[1] == "vn" then
- local vn = {
- x = tonumber(l[2]),
- y = tonumber(l[3]),
- z = tonumber(l[4]),
- }
- table.insert(obj.vn, vn)
- elseif l[1] == "vp" then
- local vp = {
- u = tonumber(l[2]),
- v = tonumber(l[3]),
- w = tonumber(l[4]),
- }
- table.insert(obj.vp, vp)
- elseif l[1] == "f" then
- local f = {}
- for i=2, #l do
- local split = string_split(l[i], "/")
- local v = {}
- v.v = tonumber(split[1])
- if split[2] ~= "" then v.vt = tonumber(split[2]) end
- v.vn = tonumber(split[3])
- table.insert(f, v)
- end
- table.insert(obj.f, f)
- end
- end
- return obj
- end
- -- http://wiki.interfaceware.com/534.html
- 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
- function drawLine(vec1,vec2)
- 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)
- 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)
- end
- function round(num, idp)
- local mult = 10^(idp or 0)
- return math.floor(num * mult + 0.5) / mult
- end
- function drawPoint(x,y,z)
- Screen.drawPixel(round(x-z*stereoMultiplier ,0), round(y,0), Color.new(255,0,0), TOP_SCREEN, LEFT_EYE)
- Screen.drawPixel(round(x+z*stereoMultiplier ,0), round(y,0), Color.new(255,0,0), TOP_SCREEN, RIGHT_EYE)
- end
- Vertex = {};
- function Vertex.new(x,y,z)
- local self = {}
- self.x = x
- self.y = y
- self.z = z
- return self
- end
- local OBJModel = loader.load("/model.obj")
- local objPos = Vertex.new(180,100,0)
- Screen.enable3D() --Enabling 3D for the top screen
- while(true) do
- Screen.waitVblankStart() -- Screen related stuff
- Screen.refresh() -- Other Screen related stuff
- Screen.clear(TOP_SCREEN) -- Clear top screen
- Screen.clear(BOTTOM_SCREEN) -- Clear bottom screen
- pad = Controls.read() -- Read Controls
- if(Controls.check(pad, KEY_START)) then-- check if start is pressed
- System.exit()-- Exit back to HBL
- end
- 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
- renderAsTris(OBJModel,objPos,50)
- Screen.flip() -- More screen related stuff
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement