I started a litte project to learn the basics of 3d. Right now you can load 3 different models with keys '1', '2', '3' pyramid, sphere and cube.
You can scale them up with the arrow keys 'up' and 'down'. Klick and drag rotates the object.
Right now there is no camera view matrix or something like that. Its just an object at the center that can be rotated around it's origin.
I wrote a little script that creates two tables nodes and edges out of .obj files. However it still has some flaws so I recommend to always triangulate your faces. Otherwise the text in .obj could be interpreted the wrong way.
I'm open for some tips and improvements. I would like to implement "simple" lighting. Maybe the using matrices would also make more sense for the whole transforming stuff. I know how they work but not how to implement them.
I don't know if thats the way 3d is normally done. This way just made sense to me.
Recommendations or tips are welcome.
Script to serialize .obj Points and Faces to Points and Edges stored in two tables.
Code: Select all
readFrom = "object.obj"
writeTo = "object.lua"
fo = io.open(writeTo, "w")
PATTERN = '^v (%S+) (%S+) (%S+)$'
PATTERN2= '^f (%S+)//(%S+) (%S+)//(%S+) (%S+)//(%S+) (%S+)//(%S+)'
PATTERN3 = '%d?%d?%d//%d%d?%d?'
PATTERN4 = '^f (%S+)//(%S+) (%S+)//(%S+) (%S+)//(%S+)'
i = 1
for line in io.lines(readFrom) do
if line:match(PATTERN) then
line = line:gsub(PATTERN,'Node'..i..' = { x=%1, y=%2, z=%3}')
print(line)
fo:write(line,'\n')
i = i + 1
end
end
fo:write("nodes = {")
for n=1, i-1, 1 do
if n < i-1 then
fo:write("Node"..n..", ")
else
fo:write("Node"..n.."}\n\n")
end
end
--read Edges
e = 1
for line in io.lines(readFrom) do
if line:match(PATTERN2) then
f = e + 1
g = f + 1
h = g + 1
line = line:gsub(PATTERN2,'Edge'..e..' = { %1, %3}\n Edge'..f..' = {%3, %5}\n Edge'..g..' = { %5, %7}\n Edge'..h..' = {%7, %1}\n')
fo:write(line)
e = e + 4
end
end
for line in io.lines(readFrom) do
if line:match(PATTERN4) then
f = e + 1
g = f + 1
line = line:gsub(PATTERN4,'Edge'..e..' = { %1, %3}\n Edge'..f..' = {%3, %5}\n Edge'..g..' = { %5, %1}\n')
fo:write(line)
e = e + 3
end
end
fo:write("edges = {")
for n=1, e-1, 1 do
if n < e-1 then
fo:write("Edge"..n..", ")
else
fo:write("Edge"..n.."}\n\n")
end
end
fo:close()
I don't know why but I alway have to zip the .love to be able to attach it to the post.