L3D - The Starts of a 3D Lib

Showcase your libraries, tools and other projects that help your fellow love users.
10$man
Citizen
Posts: 77
Joined: Sun Apr 22, 2012 10:40 pm

L3D - The Starts of a 3D Lib

Post by 10$man » Mon Oct 15, 2012 8:48 pm

Love3D

Hello everyone!
I'm not a very frequent member, but I try to keep up with things.
Anyhow, I have been working for the last few days to create a 3d engine for Love2d!

Description:
This is a REAL 3d library. It's not pseudo. Objects can be rotated in every single way and .obj files can be loaded and displayed. The engine is very basic and minimal at the moment, but if you want to download it and add some functions in, go for it!

The engine is very basic so far and definitely far from finished, but I thought I would show some of it's functions.


Here's a list of functions:
-nil L3DSetup(Width, Height, FOV)
To be run when L3D is first started.
Define the screen width, height and field of view here.


-Vertices, Indices, Material L3DLoadObj(Path to Object)
Loads a .obj wavefront file and returns three arrays, Vertices, indices and material

-nil L3DSetClipping(Min X, Max X, Min Y, Max Y)
Set's the clipping to these arguments

-nil L3DLoadIdentity()
Resets the pointer to the origin (0, 0, 0)
Resets all rotations (Won't effect already drawn objects)

-nil L3DTranslate(Position X, Position Y, Position Z)
Moves the Pointer (Objects are drawn at the pointer)

-nil L3DRotate(Rotation X, Rotation Y, Rotation Z)
Rotates an object prior to drawing it.

-nil L3DDraw(Vertice Array, Indice Array, [Material])
Draws a element to the screen at the current pointer
position with the current rotation
There is functions that are only used by the library, namely "to2d()" and "toinds()" which are used to convert a 3d vertex to a 2d vertex and convert an array of faces to indices, respectively.

The library is attached and here is an example script:

Code: Select all

--Load our Library :) 
local L3D = require('/libs/L3D/l3d')

--Some tables to store our model in
Verts = {}
Indcs = {}
Mat = {}

--The Rotation is stored here
rot_y = 0

function love.load()
	
	--Let's set the mode (Mainly so I know how big the screen is
	love.graphics.setMode(640, 480, false, true, 0)
	
	--Set the screen width, height and field of view
	L3DSetup(640, 480, 256)
	
	--Load an object into the tables Verts, Indcs and Mat (Vertices, Indices, and material)
	Verts, Indcs, Mat = L3DLoadObj("Road.obj")
end

function love.update(dt)

	--Let's see the FPS
	love.graphics.setCaption("FPS: " .. tostring(love.timer.getFPS()))
end

function love.draw()

	--Reset Rotations and Translations
	L3DLoadIdentity()
	
	--Rotate the Y axis by rot_y
	L3DRotate(rot_y, 0, 0)
	
	--Translate on the Z axis by 20 units
	L3DTranslate(0, 0, 20)
	
	--Draw the .obj we loaded
	L3DDraw(Verts, Indcs, Mat, L3DDiffuse)
	
	--This will get how many materials are loaded
	love.graphics.print(#Mat .. " Materials", 0, 0)
	
	--This will show the names of all the materials that are loaded
	for a = 1, #Mat do
		love.graphics.print(Mat[a].ID, 0, a * 12)
	end
	
	rot_y = rot_y + 0.01
end
You can see some old example code in use here:
http://sience.schattenkind.net/love2d/l3d/
Thank you to SiENCe :)


Tell me what you think and if you'd like to analyze the code and suggest optimizations, I would be grateful!

Things you should NOT suggest:
• Adding texture and material support for OBJ loader.
• Frustum Culling
• Fog support
• Lookat function (There never will be one unless someone else wants to do it)
• Lighting support (Unless you want to help me with this one :) )


What I plan to do before I quit (In order of priority):
• Error handling
Frustum Culling
Material support :P
• Fog Support
• Lighting



For kicks and giggles, I rendered 100 cubes (Each contains 8 vertices and 6 faces (quads), so 800 faces) with an average fps of about 10. That translates to around 50-60 on a normal computer (Mine has the worst video card....).


Here's the Library:
l3d.lua
(8.95 KiB) Downloaded 739 times
This is an example demonstrating that color's are being successfully set (:D :D :D)
L3D0.02.love
0.02 - Materials
(11.19 KiB) Downloaded 935 times

This is showing a whole bunch of cubes falling from the heavons :)
CubesFromHeavon.love
Falling Cubes
(4.01 KiB) Downloaded 969 times
Last edited by 10$man on Thu Oct 18, 2012 10:47 am, edited 9 times in total.

User avatar
josefnpat
Inner party member
Posts: 955
Joined: Wed Oct 05, 2011 1:36 am
Location: your basement
Contact:

Re: L3D - The Starts of a 3D Lib

Post by josefnpat » Mon Oct 15, 2012 9:50 pm

I get this error in your sample code:

Code: Select all

Error: l3d.lua:86: Could not open file coin.obj. Does not exist.
stack traceback:
	[C]: in function 'lines'
	l3d.lua:86: in function 'L3DLoadObj'
	main.lua:11: in function 'load'
	[string "boot.lua"]:378: in function <[string "boot.lua"]:373>
	[C]: in function 'xpcall'
Any chance you can package up a .love that demos this project?
Missing Sentinel Software | Twitter

FORCIBLY IGNORED.
<leafo> when in doubt delete all of your code
<bartbes> git rm -r *
<bartbes> git commit -m "Fixed all bugs"
<bartbes> git push

10$man
Citizen
Posts: 77
Joined: Sun Apr 22, 2012 10:40 pm

Re: L3D - The Starts of a 3D Lib

Post by 10$man » Mon Oct 15, 2012 10:16 pm

josefnpat wrote:I get this error in your sample code:

Code: Select all

Error: l3d.lua:86: Could not open file coin.obj. Does not exist.
stack traceback:
	[C]: in function 'lines'
	l3d.lua:86: in function 'L3DLoadObj'
	main.lua:11: in function 'load'
	[string "boot.lua"]:378: in function <[string "boot.lua"]:373>
	[C]: in function 'xpcall'
Any chance you can package up a .love that demos this project?
No... Not really.
This was mainly meant to preview what I am working on without being vaporware (which is why I included the source).
If you see what I have completed and what I intend to complete, I am probably only 50% finished or less depending on how I do lighting.
I don't want to create demos and package them for each individual update. I was expecting that the user would be able to look at the functions and the example code and write up there own test.

I'm sorry, the code I posted was just showing all the uses of the library. It was not meant to be a demo that you could just copy and run.
You could go online and google cube.obj or something and load that, or I'l even upload the object I used to test (I tested with many different objects in triangle, quad and n-gon rendering).

Unpack the attached file into the directory of your main.lua that you are using with the code I gave in the example and run it.
Attachments
Coin.rar
(5.01 KiB) Downloaded 353 times

User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: L3D - The Starts of a 3D Lib

Post by substitute541 » Tue Oct 16, 2012 2:06 am

Frusturm Culling? I don't know what that is, but I think that removes faces that are not visible... Anyways, this inspired me to continue my own Love3D Lib o.o
..

Did I notice something? My Rotate functions are almost the same as yours

Code: Select all

--My Rotate functions
function love3D.func.rotateX(y, z, angleX, centerY, centerZ)
	local cosX = math.cos(angleX)
	local sinX = math.sin(angleX)
	local y1 = (y - centerY) * cosX - (z - centerZ) * sinX
	local z1 = (z - centerZ) * cosX + (y - centerY) * sinX
	return y1 + centerY, z1 + centerZ
end

function love3D.func.rotateY(x, z, angleY, centerX, centerZ)
	local cosY = math.cos(angleY)
	local sinY = math.sin(angleY)
	local x1 = (x - centerX) * cosY - (z - centerZ) * sinY
	local z1 = (z - centerZ) * cosY + (x - centerX) * sinY
	return x1 + centerX, z1 + centerZ
end

function love3D.func.rotateZ(x, y, angleZ, centerX, centerY)
	local cosZ = math.cos(angleZ)
	local sinZ = math.sin(angleZ)
	local x1 = (x - centerX) * cosZ - (y - centerY) * sinZ
	local y1 = (y - centerY) * cosZ + (x - centerX) * sinZ
	return x1 + centerX, y1 + centerY
end

-- Your rotate functions :
local ca = math.cos(L3D.ROTATE[1])
	local sa = math.sin(L3D.ROTATE[1])
	
	local y_temp = y1
	y1 = y1 * ca - z1 * sa
	z1 = y_temp * sa + z1 * ca
	
	ca = math.cos(L3D.ROTATE[2])
	sa = math.sin(L3D.ROTATE[2])
	
	local z_temp = z1
	z1 = z1 * ca - x1 * sa
	x1 = z_temp * sa + x1 * ca
	
	ca = math.cos(L3D.ROTATE[3])
	sa = math.sin(L3D.ROTATE[3])
	
	local x_temp = x1
	x1 = x1 * ca - y1 * sa
	y1 = x_temp * sa + y1 * ca
Currently designing themes for WordPress.

Sometimes lurks around the forum.

10$man
Citizen
Posts: 77
Joined: Sun Apr 22, 2012 10:40 pm

Re: L3D - The Starts of a 3D Lib

Post by 10$man » Tue Oct 16, 2012 2:22 am

OK cool :)
Please share ideas and thoughts as you go.

Yes, that's probably because it's the simplest way to rotate an object without rotation matrices. It's not a new trick.

Oh wait.... I think yours look a bit like mine... http://bb.waratteka.net/viewtopic.php?f=21&t=573
Notice, that is 8 months before you posted about your library.

User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: L3D - The Starts of a 3D Lib

Post by substitute541 » Tue Oct 16, 2012 3:25 am

10$man wrote:OK cool :)
Please share ideas and thoughts as you go.

Yes, that's probably because it's the simplest way to rotate an object without rotation matrices. It's not a new trick.

Oh wait.... I think yours look a bit like mine... http://bb.waratteka.net/viewtopic.php?f=21&t=573
Notice, that is 8 months before you posted about your library.
I used a book about Actionscript 3.0 (the programming language for making flash games) for my 3D projection. It was written in.. 2008 maybe? It's called "Actionscript 3.0 : Making Things Move".
Currently designing themes for WordPress.

Sometimes lurks around the forum.

User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: L3D - The Starts of a 3D Lib

Post by substitute541 » Tue Oct 16, 2012 3:29 am

I would like to help you about lighting support, although it will take lots of vector calculations for that. Fortunately my book has it :3
Currently designing themes for WordPress.

Sometimes lurks around the forum.

10$man
Citizen
Posts: 77
Joined: Sun Apr 22, 2012 10:40 pm

Re: L3D - The Starts of a 3D Lib

Post by 10$man » Tue Oct 16, 2012 3:36 am

I would prefer to not go straight from a book.
It helps to really understand it and going from a book just doesn't get it for me.
If you want to try helping, I'm happy to have an extra hand.

First I need to worry about getting materials from the .mtl file.

User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: L3D - The Starts of a 3D Lib

Post by substitute541 » Tue Oct 16, 2012 3:52 am

10$man wrote:I would prefer to not go straight from a book.
It helps to really understand it and going from a book just doesn't get it for me.
If you want to try helping, I'm happy to have an extra hand.

First I need to worry about getting materials from the .mtl file.
I do understand what's in the book...

.mtl.... Is that from 3Ds Max or Autocad. And one thing, the Love engine DOES NOT support distorting pics, if you meant that your adding textures. It only supports skewing/shearing.
Currently designing themes for WordPress.

Sometimes lurks around the forum.

10$man
Citizen
Posts: 77
Joined: Sun Apr 22, 2012 10:40 pm

Re: L3D - The Starts of a 3D Lib

Post by 10$man » Tue Oct 16, 2012 10:36 am

substitute541 wrote:
10$man wrote:I would prefer to not go straight from a book.
It helps to really understand it and going from a book just doesn't get it for me.
If you want to try helping, I'm happy to have an extra hand.

First I need to worry about getting materials from the .mtl file.
I do understand what's in the book...

.mtl.... Is that from 3Ds Max or Autocad. And one thing, the Love engine DOES NOT support distorting pics, if you meant that your adding textures. It only supports skewing/shearing.
I meant me. I want to understand too ;)
(
.obj files are wavefront. If they have a material they will also have a .mtl file that contains the information for the material. Materials can be textures but they can also be solid colors (which is what I am going to implement first)

Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests