simple 3D to 2D projection

Show off your games, demos and other (playable) creations.
Post Reply
adge
Citizen
Posts: 54
Joined: Mon Dec 14, 2015 8:50 pm

simple 3D to 2D projection

Post 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.
Attachments
3Dprojection.love.zip
(4.01 KiB) Downloaded 551 times
User avatar
qubodup
Inner party member
Posts: 775
Joined: Sat Jun 21, 2008 9:21 pm
Location: Berlin, Germany
Contact:

Re: simple 3D to 2D projection

Post by qubodup »

Image
That's nice. But it's funny how you .zip a .love which is a .zip already :)
Image
lg.newImage("cat.png") -- made possible by lg = love.graphics
-- Don't force fullscreen (it frustrates those who want to try your game real quick) -- Develop for 1280x720 (so people can make HD videos)
adge
Citizen
Posts: 54
Joined: Mon Dec 14, 2015 8:50 pm

Re: simple 3D to 2D projection

Post 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.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: simple 3D to 2D projection

Post 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?
adge
Citizen
Posts: 54
Joined: Mon Dec 14, 2015 8:50 pm

Re: simple 3D to 2D projection

Post by adge »

Safari :-)
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: simple 3D to 2D projection

Post by Jasoco »

Problem isn't here there either. You shouldn't be having trouble uploading a .love from anywhere.
erikgcl
Prole
Posts: 1
Joined: Mon Mar 02, 2020 10:46 am

Re: simple 3D to 2D projection

Post by erikgcl »

I did a 3d projection like OpenGL...
Attachments
lovegame.love
(4.04 KiB) Downloaded 423 times
noahnadai
Prole
Posts: 15
Joined: Sat Jun 27, 2020 10:43 am

Re: simple 3D to 2D projection

Post by noahnadai »

This is really cool bro. followed this through from your previous post. Can't wait to try it.
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests