Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple3D sorted polygon drawing
#1
Alright, I don't want to hijack Rick's thread (http://www.naalaa.com/community/showthread.php?tid=84).

Here's an example of using "painter's algorithm" with the Simple3D library to render rectangles from back to front.

[Image: s3d.jpg]

I had do add two functions to the library (for transforming points and vectors without the screen space projection step). So I've included a new version of the library in the zip. I compiled it on Linux, and if the test program doesn't run on Windows (I'm not sure about this), open Simple3D.txt in NED and press F8 to re-compile the library.

Apart from the depth sort, I also do "backface culling" to skip rendering polygons that doesn't "face" the camera. And I also do some simple ambient and diffuse shading.

If you find any use of all this, I'll gladly explain things in greater detail, write some more examples and maybe even hide things away in a library.


Attached Files
.zip   s3d_test.zip (Size: 12.95 KB / Downloads: 8)
Reply
#2
This is awesome! Idea Heart

 Could take me awhile to figure out what's going on here.

  The idea is to grow this thing into something that could do 3d art, modeling and videos.
Reply
#3
(06-27-2018, 04:15 PM)Rick3137 Wrote: This is awesome! Idea Heart

 Could take me awhile to figure out what's going on here.

  The idea is to grow this thing into something that could do 3d art, modeling and videos.

It looks more complicated than it is.

There's one array that contains all the rectangles in the order in which they are added (with AddRect and AddCube).

Then there's a second array that is used for drawing the rectangles in the correct order (back to front). Every frame, every rectangle's distance to the camera is re-calculated. Then the distance and the rectangle's index (in the first array) is stored in the second array. When all rectangles have been processed, I use QuickSort to sort the second array based on the distance. Last, I loop through the second array and render the rectangles in the correct order.

Other than that it's just some maths that only make sense if you know linear algebra and the mathematical basics of 3D graphics. It's probably the only area in which I am very well educated Smile
Reply
#4
Don't use any of the code I posted. I did some more work on it before going to bed yesterday and will post something new tonight.

I'm adding functionality directly to the Simple3D library for drawing primitives (cubes, cylinders, spheres ...). For drawing a cube 6 "units" in front of the camera, you'll just write something like:

Code:
...
_S3D_ClearTransformation
_S3D_Translate 0.0, 0.0, 6.0
_S3D_AddCube 1.0, 255, 255, 255
...
_S3D_Render

This way it'll be easier to create animated and hierarchical stuff, like a solar system where planets rotate around a sun and moons rotate around the planets.
Reply
#5
I've attached an update.

[Image: s3d_2.jpg]

Most of the code in test will be moved to Simple3D. I'll add more primitives to play with, right now you can only use S3D_AddCube and S3D_AddCylinder. Maybe the addition of spheres, cones and toruses would be enough, and perhaps support for some simple 3d model format like OFF?

Is a longer  explanation of how 3D transformations work wanted? In short, they work a bit like "turtle graphics", I guess.

It's easier if we start with just looking at translation (moving around).

S3D_ClearTransformation clears all transformations and puts the "drawing position" at the "camera's" center. If you call S3D_AddCube directly after S3D_ClearTransformation, the cube will be put "around you", you will be at the center of the cube and actually won't see it at all (because of the backface culling thing you can only see its outside).

_S3D_ClearTransformation
_S3D_Translate 0.0, 0.0, 4.0
_S3D_AddCube 1.0, 255, 255, 255

Here we translate (move) 4 units "into" the screen (along the Z-axis) and THEN add the cube. This cube will be clearly visible in front of the camera. If we want to move it 2 units to the left (negative movement along the X-axis) as well, we could use:

_S3D_ClearTransformation
_S3D_Translate -2.0, 0.0, 4.0
_S3D_AddCube 1.0, 255, 255, 255

, OR, and this is important:

_S3D_ClearTransformation
_S3D_Translate 0.0, 0.0, 4.0
_S3D_Translate -2.0, 0.0, 0.0
_S3D_AddCube 1.0, 255, 255, 255

Here we move in two steps, first 4 units along Z and then, from that position, -2 units along X. Then we add our cube.

If we want to rotate the cube 45 degrees around the Y-axis, we call S3D_RotateY right before S3D_AddCube:

_S3D_ClearTransformation
_S3D_Translate 0.0, 0.0, 4.0
_S3D_Translate -2.0, 0.0, 0.0
_S3D_RotateY 45.0
_S3D_AddCube 1.0, 255, 255, 255


The code below would have given a completely different result:

_S3D_ClearTransformation
_S3D_Translate 0.0, 0.0, 4.0
_S3D_RotateY 45.0
_S3D_Translate -2.0, 0.0, 0.0
_S3D_AddCube 1.0, 255, 255, 255

Because this rotation would also have affected the second translation. Play around and you'll see Smile

I'm not sure if this helped. And there's a lot more, like scaling (not too complicated) and pushing and popping transformations to/from a stack.

Edit:  I added two examples, example1 and example2, that shows some basic transformations and pushing and popping.


Attached Files
.zip   s3d_test.zip (Size: 25.69 KB / Downloads: 8)
Reply
#6
This is very impressive .....I look forward to making some time to experiment with this......
Reply
#7
I hope that I am not out of line, and if so, I apologize....

Although all this 3D stuff is 'really' impressive, I just need to know, what is the ultimate goal? If Naalaa is primarily designed for 'retro' 2D, why the interest in 3D? Is it, "just to see if it can be done.", or perhaps, the intentions are to hopefully create 3D games?

Don't get me wrong. I'm not complaining. I think that what has been done so far is great... My reasons are selfish. I have SO much difficulty making 2D stuff, I'm afraid that if the 3D trend continues, I'm not going to be able to keep up... *sigh*
Reply
#8
(06-28-2018, 09:25 PM)johnno56 Wrote: Although all this 3D stuff is 'really' impressive, I just need to know, what is the ultimate goal? If Naalaa is primarily designed for 'retro' 2D, why the interest in 3D? Is it, "just to see if it can be done.", or perhaps, the intentions are to hopefully create 3D games?

Don't get me wrong. I'm not complaining. I think that what has been done so far is great... My reasons are selfish. I have SO much difficulty making 2D stuff, I'm afraid that if the 3D trend continues, I'm not going to be able to keep up... *sigh*

  I do it for the mental exercise. When TV and YouTube get too boring, I fall back to this.

  At the moment, I'm trying to build a game cube, and got side tracked. Maybe I'll do 2d again, later.
Reply
#9
I'm just doing it because it's fun, there's no "goal" Smile  I was happy to see that Rick actually managed to do something cool with Simple3D. I frankly didn't think that was possible - I've only used it for calculations myself, not for actually trying to draw 3D graphics. 

I've put, maybe, two hours into this 3D experiment, and I guess I'll give it about one more. I doubt the result of this minimal effort will be of much use to anyone, but it was still fun doing it Smile

And, hm, the SNES (Super Nintendo), a retro system by all means, actually could pull of 3D graphics at about this level, so I'm not that far off track Big Grin Here's Star Fox, a 3D game in the 16 bit era: https://youtu.be/ZnmnpWKoUS0
Reply
#10
I added spheres and tetrahedrons and you can load some OFF files (they must be pure triangle meshes or pure quad meshes).

[Image: s3d_3.jpg]

Even though one can't expect a software 3D renderer written in an interpreted language to be fast, a lot could be done to speed all of this up. If someone uses the library and wants better performance, I will gladly spend some time on that. But I'm leaving it as it is for now Smile If you need other primitives (cones, toruses, disks or what ever) I can of course write code for generating those too.

Use S3D_SetOutline true/false to turn outlines on/off, S3D_SetLighting true/false to turn lighting on/off and S3D_CullFace true/false (forgot to add a Set to that function name ...) to turn backface culling on/off.

Edit:  Naalaa 5 actually had a built in software 3D renderer that was fast and way more advanced than this:

https://www.youtube.com/watch?v=KihhpDwY9-U
https://www.youtube.com/watch?v=jWrxCphjAYo
https://www.youtube.com/watch?v=FPateInBfK0

I wrote most of it in assembler but couldn't keep it when moving from Borland C++ to Visual Studio.


Attached Files
.zip   s3d_test.zip (Size: 85 KB / Downloads: 5)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)