Jasoco's 3D Engine Thread: Go to Page 6 Please!

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Jasoco's 3D Engine Thread: Go to Page 6 Please!

Post by Jasoco »

EDIT NOVEMBER 17TH, 2010: Go to page 6 for the latest version. The stuff below is old and busted.
viewtopic.php?f=5&t=1399&p=22601#p22601

Seems everyone's on a 3D kick these days, so here's a 3D Cube...

Image

But it's not just a cube. It's whatever the heck you want it to be! This, ladies and gentlemen, is how you do 3D with non 3D hardware. (Aside from raytracing of course)

Based on an old idea that's been around since the late 80's and 90's in the days of QBASIC.

Don't expect to use this for any real 3D worlds or anything. This is just for 3D models. Things go wonky if you try to move the lines out of the window. Think of this as good enough for a 3D model of the Death Star or a spinning wire-frame logo or something.

The code, while partially identical in maths to what I had in the 90's, is borrowed from a webpage about Flash and ActionScript of all places. And it worked so well I expanded it. With modifications to how things work of course. I added the ability to sort the points table so distant points are smaller than closer ones, which will actually help it be real 3D.

It's pretty fast too. I have tested up to 1000 points. With rectangles, it kept 60FPS even at 1000, with circles with a segment value of 100 It kept 60FPS all the way up to 500 and gradually lowered to 30FPS when I got to 1000. Not too shabby. Should go super fast with quads and sprites.

Each point has a scale factor which could be used to draw sprites/quads at a scale you want as the number is between 0 and 1. And if you multiply the scale by the zoom you can scale them up even more.

Lines are drawn by the location (z-index) of their first point. So it's not true 3D. If I could break the lines into segments and draw them separately as other depths, it would look right, but also take up a lot of power. It's only a visual problem when you have many lines crossing at weird angles anyway. Obviously if I could I'd make the lines have depth too with them being thick in the foreground and thin in the back with a smooth transition from front to back. But I can't.

Since a point's X, Y and Z can be changed during runtime, and the lines attach themselves to the point's table index, you can move any part of the 3D model independently.

It has a secret filled triangle-based mode that is in the experimental stages right now. It's buggy in that the triangles don't seem to sort correctly so triangles get drawn overlapping other ones even though they're behind the others. This is because most of the time they share a single point and the sorting algorithm sorts them but can't distinguish if a set of points should be sorted behind another set. 3D is hard maths. Press "P" to show this mode. Take a look at my code, maybe someone can help figure out how to get the clipping to stop. If I don't first that is.

Press "N" to toggle the points ID numbers. Useful when making new triangles to figure out what points to connect.

Shame Löve can't take an image, mask it to a shape at the scale and rotation you want and draw it in a polygon area or else we could do some cool stuff.

I'd like to add shading next to make further back triangles darker. Should be easy. Maybe.

I would not be surprised if I somehow stumbled onto a way to make a StarFox (SNES) clone one day.

I wish polygons and circles were antialised like lines are. I faked it for now by overlapping a lined shape on a filled one. But I shouldn't have to.
Attachments
3d.love
Newell's Teapot now featured!
(8.12 KiB) Downloaded 724 times
Last edited by Jasoco on Thu Nov 18, 2010 1:57 am, edited 9 times in total.
User avatar
schme16
Party member
Posts: 127
Joined: Thu Oct 02, 2008 2:46 am

Re: On the subject of three-dimensions...

Post by schme16 »

HOLY GOD ON A LOG!!!
That's pretty awesome stuff your playing with Jasoco.
I too have felt the anti-aliasing or... lack there of on poly/circles and have had to resort to using a lined version to add it in... I'm bleeding too bro.

You seriously need to try harder to `stumble` on a making a Starfox clone!!!!
My Development Diary - http://shanegadsby.info
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: On the subject of three-dimensions...

Post by kikito »

It is quite good looking. Congratulations!

Is there any chance that you include triangles, instead of just lines and dots?
When I write def I mean function.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: On the subject of three-dimensions...

Post by Jasoco »

Press P to show how buggy filled triangle mode is.

Since it's not real 3D, it doesn't have all the features a real 3D engine could have like backface culling and clipping. The best I can do is draw a triangle before I draw another as long as all the ones I draw first are behind the ones in the front.
User avatar
kalle2990
Party member
Posts: 245
Joined: Sat Sep 12, 2009 1:17 pm
Location: Sweden

Re: On the subject of three-dimensions...

Post by kalle2990 »

Jasoco wrote:The best I can do is draw a triangle before I draw another as long as all the ones I draw first are behind the ones in the front.
I bet there is some way of doing this correct, but I have no clue of how to... :(
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: On the subject of three-dimensions...

Post by Jasoco »

Doing a little debugging I see that my triangles "z index's" rapidly change places for some, I think when two or more share the same point they fight it out for superiority. If I can fix this jumping back and forth maybe I can get it to work right. It should end up working perfectly for simple 3D games that don't require too much going on at once. Even sprite-based 3D games should work fine. i.e. a flat ground with some people walking around and objects. If we could stretch images/quads between four points to make them look perspective, it could be pretty cool.

That's my goal today. To get the triangles to stop dancing.

Edit: Download the new version I just put up. It has debug text so you can see what I mean when I say jumping polygons.
snake
Citizen
Posts: 52
Joined: Tue Jul 14, 2009 6:59 pm

Re: On the subject of three-dimensions...

Post by snake »

Wow it is much faster than i thought it would be. :ultrashocked::
Awesome!
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: On the subject of three-dimensions...

Post by Jasoco »

Hmm.. Backface culling is harder than it would seem.
http://gpwiki.org/index.php/3D:Backface_Culling

It doesn't seem to be working right so far for me. It works fine in this JavaScript 3D model viewer:
http://www.nihilogic.dk/labs/canvas3dtexture_0.2/
Which gets its idea from here:
http://www.kirupa.com/developer/actions ... ulling.htm

The code for determining if a face is facing forward or backward is:

Code: Select all

if ((y3-y1)/(x3-x1) - (y2-y1)/(x2-x1) <= 0) ^ (x1 <= x3 == x1 > x2) {
But I can't figure out how I'd convert that to Lua. So I used the example from the GPWiki link above (First link) which uses this as its code:

Code: Select all

(p3 - p1) x (p3 - p2)
= ([0,0,0]-[1,1,0]) x ([0,0,0]-[2,0,0])
= [-1,-1,0] x [-2,0,0]
= [(-1*0)-(0*0), (0*2)-(1*0), (1*0)-(1*2)]
= [0-0, 0-0, 0-2]
= [0, 0, -2]
I came up with this:

Code: Select all

((x3-x1)+(y3-y1)+(z3-z1)) * ((x3-x2)+(y3-y2)+(z3-z2))
Which doesn't seem to work.

But another method would be to take the three points X and Y and figure out if they are in clockwise or counterclockwise order. But I have no idea how to do that.

So really, how do I determine if three points are in clockwise order? That will help determine if it is facing towards the camera or towards the back. If it is towards the camera, it is visible. If not, it is not.

Edit: Download the current version for debugging in the OP up top. Enjoy! And HELLLLLLP!!!!!
User avatar
bmelts
Party member
Posts: 380
Joined: Fri Jan 30, 2009 3:16 am
Location: Wiscönsin
Contact:

Re: On the subject of three-dimensions...

Post by bmelts »

For the record, this:

Code: Select all

if ((y3-y1)/(x3-x1) - (y2-y1)/(x2-x1) <= 0) ^ (x1 <= x3 == x1 > x2) {
in Lua would be this:

Code: Select all

if ((y3-y1)/(x3-x1) - (y2-y1)/(x2-x1) <= 0) ~= ((x1 <= x3) == (x1 > x2)) then
You could probably get rid of some of the parentheses, but that should work. (As the article explains, a bitwise xor is just a not-equals operation...)

Though your code looks a bit different from kirupa's. I see a < that became a <=, for example.
pekka
Party member
Posts: 206
Joined: Thu Jan 07, 2010 6:48 am
Location: Oulu, Finland
Contact:

Re: On the subject of three-dimensions...

Post by pekka »

Jasoco wrote:
But another method would be to take the three points X and Y and figure out if they are in clockwise or counterclockwise order. But I have no idea how to do that.
Cross product to the rescue. This is the first Google hit for the relevant search terms:

http://local.wasp.uwa.edu.au/~pbourke/g ... clockwise/

This presentation might be clearer:

http://www.lighthouse3d.com/opengl/math ... ossproduct
Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests