Jasoco's 3D Engine Thread: Go to Page 6 Please!
Posted: Tue Mar 09, 2010 4:56 am
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...
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.
viewtopic.php?f=5&t=1399&p=22601#p22601
Seems everyone's on a 3D kick these days, so here's a 3D Cube...
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.