Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / Rendering basic 3D using only DBP's 2D line command

Author
Message
lower logic
18
Years of Service
User Offline
Joined: 15th Jun 2006
Location:
Posted: 2nd Feb 2007 00:00 Edited at: 2nd Feb 2007 12:59
This code snippet simulates 3D using just DBP's 2D lines for rendering. DX9 and OpenGL are of course much faster and have more functionality, but it was fun to make my own.
I get 50-70fps for the 432 lines that are drawn in the example.

I didn't add triangle support or a zbuffer because I don't think DBP would be able to compile it into fast enough code for any use (and I think it would be really hard to make a good zbuffer), but this example with lines is still interesting.

You can fly around using mouse look and WASD, and you can roll the camera using Q/E. The rotation method I used was YXZ.

Note I don't ever use DBP's camera or any objects.

Here is the code:


Edit:
1) Made the roll slowly spring back to 0 so you can't get confused/upside down as easily
2) Added some curveangle to the mouselook.
3) Realized the 3d world-space was being rendered upside down and fixed it
Kohaku
20
Years of Service
User Offline
Joined: 3rd May 2004
Location: The not very United Kingdom
Posted: 2nd Feb 2007 01:09
That's really impressive. Lines have never looked so good.

I wonder how much further you could take that.


You are not alone.
Mistrel
Retired Moderator
19
Years of Service
User Offline
Joined: 9th Nov 2005
Location:
Posted: 2nd Feb 2007 02:12
If you use Cloggy's d3dfunc dll you can make anti-aliased lines.

http://3dfolio.com
lower logic
18
Years of Service
User Offline
Joined: 15th Jun 2006
Location:
Posted: 2nd Feb 2007 03:13 Edited at: 2nd Feb 2007 12:43
Thanks. If I work on it more I could probably make my own "objects" with line data, rotation, scale, position, etc, using a type array. I think it would be fun to make some unusual effects/functionality such as a sort of black-hole space-warping effect, and edit the rendering function so that the lines are segmented so they could curve to the space warpage. I like the snippet because it shows how few lines of code are needed to create the illusion of 3D using just addition, multiplication, sine, cosine, tangent.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 2nd Feb 2007 20:22 Edited at: 2nd Feb 2007 20:25
what? no occlusion? pfft noob!

nah just kidding, nice code. Definitely gonna have to study that.


if you use pixel lock/unlock you might get more speed out of the drawing.

Kohaku
20
Years of Service
User Offline
Joined: 3rd May 2004
Location: The not very United Kingdom
Posted: 2nd Feb 2007 21:13
He doesz0rz.


You are not alone.
lower logic
18
Years of Service
User Offline
Joined: 15th Jun 2006
Location:
Posted: 2nd Feb 2007 22:47 Edited at: 2nd Feb 2007 22:54
Here's how the code works:
1) update_camera() translates the camera's rotation into the x/y/z components of the x/y/z axes (9 variables altogether). Note I apply the rotation in the opposite order. It is YXZ rotation but I apply Z first, then X, then Y because it is easier that way.

2) draw_line() first subtracts the camera's position from the 2 x/y/z endpoints of the line.

3) Next draw_line() transforms the 2 points to get the x/y/z points relative to the camera's rotation (using the camera's x/y/z axes calculated in update_camera()).

4) The ratio of the x/z and y/z calculate an x value and y value that are to be screen coordinates (but are not actual screen pixels yet).
5) These x and y values are then changed based on the FOV and aspect ratio.
6) Finally the x and y are mapped to their actual screen pixels. Steps 4-6 are combined into 1 command per coordinate in the actual code:

7) A line is drawn between these 2 points
HyperByte
17
Years of Service
User Offline
Joined: 3rd Jan 2007
Location: in a small kola bear costume
Posted: 2nd Feb 2007 23:08
thats awsome

On a scale from one to ten one being very smart and ten being an absolute genius your none of the above
Brain111
17
Years of Service
User Offline
Joined: 5th Feb 2007
Location: In my own little world.
Posted: 7th Feb 2007 03:24
Awesome, man... You should try to make a simple game that uses that type of stuff. Just like a fly-through-a-tunnel-and-don't-run-into-stuff type of game, or something like that. That would be awesome.
Terrorist Zero
18
Years of Service
User Offline
Joined: 29th Aug 2006
Location: Teh YouKai
Posted: 8th Feb 2007 19:34
Elite - Remake

That's some seriously good coding!

www.tornupgaming.com
The home of free games and game projects.
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 12th Feb 2007 03:07
That is amazing! Seriously, it is one of the best code snippets I have ever seen! NICE!

Xeridox
21
Years of Service
User Offline
Joined: 19th May 2003
Playing: Scrap Mechanic
Posted: 18th Feb 2007 17:42
I was wondering what it would take to do something like this in DBPro...

Very impressive.

Now, try texturing and occluding, lightmapping, etc., etc.

Kidding This is an awesome snippet!

Darin Snow
Xeridox Entertainment
Aaron Miller
18
Years of Service
User Offline
Joined: 25th Feb 2006
Playing: osu!
Posted: 24th Feb 2007 10:12
Lower Logic,

Do you mind if I convert that into an actual renderer?

It would be free, and credit would definately be given to you.


Please note, however, the renderer will not be a DBPro renderer, but an actual library for C++ (Perhaps, or a DLL), which renders to a window.


Cheers,

-db


Enter my forums here.
Whong
20
Years of Service
User Offline
Joined: 15th Jul 2004
Location: Turku Finland
Posted: 24th Feb 2007 10:55
Looks cool!

Great snippet!



Click to hear more about these spectacular programs for special effects!
Aaron Miller
18
Years of Service
User Offline
Joined: 25th Feb 2006
Playing: osu!
Posted: 28th Feb 2007 02:06
Hello. I have found this section of a site, which may be very useful

Learn to make a solid fill triangle

Learn how to make a gaurad filled triangle

Learn how to apply a texture to a triangle



Cheers,

-db


Enter my forums here.
lower logic
18
Years of Service
User Offline
Joined: 15th Jun 2006
Location:
Posted: 28th Feb 2007 04:08
DB user 2006+, sure you can use it. Though if you're going to be doing 3D rendering, I'd recomend something faster like OpenGL or DirectX.
Aaron Miller
18
Years of Service
User Offline
Joined: 25th Feb 2006
Playing: osu!
Posted: 2nd Mar 2007 08:34
Indeed OpenGL and Direct3D are faster. However, Im interested in this because ive always wanted to make my own 3D Renderer.... Every day I get more interested in something more advanced, or difficult.


Thank you.

Cheers,

-db


Enter my forums here.
VR2
19
Years of Service
User Offline
Joined: 14th Mar 2005
Location:
Posted: 2nd Mar 2007 13:49
Hey, nice work!

I can relate to how you feel about this project, as a couple of years ago I wrote something very similar in DB (to prototype), C++ (to get more speed), Javascript (just for a laugh to see if it could be done), C# (to use as a Web Control) and eventually Java (because C# Web Controls are .... lacking shall we say?) and was immensly proud of it .

During the iterations of development I added a bunch of stuff like 3d model loading (from xml), culling (object and face), lighting (point and directional), collision detection/response (ellip to polygon soup) and a scene graph...

...so for an idea of where you might want to take take this, here's a couple of Applet demos I made at the time:

Platformer: http://www.victory-road.co.uk/HTML3D/Level3.html
Racer: http://www.victory-road.co.uk/HTML3D/VRProto.html
Shooter: http://www.victory-road.co.uk/HTML3D/RedDoor.html

Notice the LACK of a decent Z-buffer - I used the "Painters" algorithm where you sort the triangles based on distance from the camera, which sounds ok but isn't very accurate.

It all looks so basic by todays DX10 standards but the satisfaction in doing it all yourself makes it all worthwhile
Image All
18
Years of Service
User Offline
Joined: 30th Dec 2005
Location: Home
Posted: 10th Mar 2007 20:13
I played the platformer, closed the tab and now the music won't stop playing

Roxas
19
Years of Service
User Offline
Joined: 11th Nov 2005
Location: http://forum.thegamecreators.com
Posted: 10th Mar 2007 20:57
WTF the music keeps playing -.-



VR2
19
Years of Service
User Offline
Joined: 14th Mar 2005
Location:
Posted: 10th Mar 2007 22:58
I just noticed that - sorry guys its a bug.

Still, jolly little number, no? It's from Croc.

I'd suggest just running one of the other two demos, that will stop the music from the first (and correctly dispose when finished).
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 11th Mar 2007 19:23
If you want to do a z-buffer, heres an easy way:

1) make an array of values, the size of the screen
2) When drawing a triangle, work out the z as well as the UV
3) Put the Z value into the array of depths
4) Only draw a dot or do 3 when the z value is less than the one currently in the array

A way for optimisation would be to use an array 1/4 of the screen size, but the lines will be more jagged when surrfaces overlap.

If you want to beat DBPros transparency issues, (but use loads of memory!) you can do this:

1) Make a 2d array of lists of rgba values instead of using the screen
2) Instead of using dot, insert an item into the correct list, each list sorted by zdepth
3) When finished, draw each item in each list to its screen pixel in the order it is in the list, (Including alpha values)



VR2
19
Years of Service
User Offline
Joined: 14th Mar 2005
Location:
Posted: 14th Mar 2007 13:08
Quote: "If you want to do a z-buffer, heres an easy way:

1) make an array of values, the size of the screen
2) When drawing a triangle, work out the z as well as the UV
3) Put the Z value into the array of depths
4) Only draw a dot or do 3 when the z value is less than the one currently in the array

A way for optimisation would be to use an array 1/4 of the screen size, but the lines will be more jagged when surrfaces overlap.
"


Nice one, Diggsey except, in my case, and I'm sure with alot of others, the approach you're advocating assumes that triangles are drawn per pixel, when in actual fact all implementations would use a native "FillPolygon" or "DrawPolygon" or "DrawLine" type of API call (this is not HW accelerated so we need all the speed we can get!)

It seemed to me that Z-buffering would mean writing a custom scan line renderer, testing every pixel for z values (as you say) and if you're gonna do that, then you may as well do texturing as well...and that would have meant texture coordinates for verts, meaning(ie - it's a bit of a can of worms needing a radical amount of work and possibly chocking the entire pipeline in the process).
Turoid
21
Years of Service
User Offline
Joined: 1st Nov 2003
Location: The Netherlands
Posted: 14th Mar 2007 21:32
Roxas
19
Years of Service
User Offline
Joined: 11th Nov 2005
Location: http://forum.thegamecreators.com
Posted: 14th Mar 2007 22:16
Hehee.. Got little speed boost with d3d.. 350 was static and 1000+ some times.. And dint change the code only the line command with d3d_line and the ink rgb with d3d_color and removed () bracets but i also modifed the code here's my try (Need Cloggy's d3d Plugin)





dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 19th Mar 2007 10:47
I made the code render triangles, and added support for objects and a few other things and thought it was worthy of a new topic, http://forum.thegamecreators.com/?m=forum_view&t=102348&b=6&p=0

Turoid
21
Years of Service
User Offline
Joined: 1st Nov 2003
Location: The Netherlands
Posted: 2nd Apr 2007 12:33
@lower logic:
I sort of grabbed your code and converted it into c++.. worked perfect Here's a video of it:

http://smg.photobucket.com/albums/v679/jopiedopie/?action=view¤t=3ddml.flv

I am awesome and always right.
TEH_CODERER
21
Years of Service
User Offline
Joined: 12th Nov 2003
Location: Right behind you!
Posted: 2nd Apr 2007 14:05
Cool, following the link though showed some of your other pics and this one is just about the coolest thing i have ever seen!
http://smg.photobucket.com/albums/v679/jopiedopie/?action=view¤t=bar.jpg

Turoid
21
Years of Service
User Offline
Joined: 1st Nov 2003
Location: The Netherlands
Posted: 3rd Apr 2007 10:26
thank you

I am awesome and always right.

Login to post a reply

Server time is: 2024-11-22 19:07:32
Your offset time is: 2024-11-22 19:07:32