Page 1 of 1

simple 3D to 2D projection

Posted: Mon May 02, 2016 1:59 pm
by adge
Hey there!

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()
This really needs some optimizing but it works for now.

I don't know why but I alway have to zip the .love to be able to attach it to the post.

Re: simple 3D to 2D projection

Posted: Mon May 02, 2016 6:21 pm
by qubodup
Image
That's nice. But it's funny how you .zip a .love which is a .zip already :)
Image

Re: simple 3D to 2D projection

Posted: Mon May 02, 2016 7:55 pm
by adge
Yeah my puter won't let me upload the .love file :D. I really don't know why. Maybe it's a mac thing.

Re: simple 3D to 2D projection

Posted: Tue May 03, 2016 10:46 pm
by Jasoco
adge wrote:Yeah my puter won't let me upload the .love file :D. I really don't know why. Maybe it's a mac thing.
Nope. Wouldn't be a Mac thing. What browser are you using?

Re: simple 3D to 2D projection

Posted: Fri May 06, 2016 3:40 pm
by adge
Safari :-)

Re: simple 3D to 2D projection

Posted: Fri May 06, 2016 5:38 pm
by Jasoco
Problem isn't here there either. You shouldn't be having trouble uploading a .love from anywhere.

Re: simple 3D to 2D projection

Posted: Mon Mar 02, 2020 10:51 am
by erikgcl
I did a 3d projection like OpenGL...

Re: simple 3D to 2D projection

Posted: Wed Jul 08, 2020 11:38 am
by noahnadai
This is really cool bro. followed this through from your previous post. Can't wait to try it.