Is it possible to draw custom triangle in 3D?
-
Hi,
I was wondering if it's possible to create your own 3D scene consisting of triangles. I'm searching for a similar method like there's in 2D to draw a custom traingle but in 3D?
Thanks,
Michal -
No, but you can transform and even texture 2D triangles to fulfill your purpose if you're okay with not having perspective correction (I guess that is also possibly doable). I'm not sure if the cost of doing your own Z-buffer or sorting algorithm is worth it in terms of performance though.
-
@Doriphor Thank you for the reply, I think I got it. Basically I need to follow my camera settings, do my own 3d to 2d transformations and sort it "somehow" and draw it with the 2d triangle. I think for some tricks this is fine but custom z-buffer might be not possible to do due the very poor performance.
-
There are a few default shapes available in Fuze, which are cube, sphere, pyramid, cone, cylinder, wedge and hemisphere.
Obviously a pyramid isn't exactly a triangle, but you can make it almost appear like one. I admit it's not the nicest solution, but maybe that's easier than using 2D over 3D for this.
For example:
vector location = {0, 0, 0} vector scale = { 1, 1, 0.000001 } // by using a tiny scale for the z axis, the pyramid becomes very compact in that direction, making it approximate a triangle. placeObject(pyramid, location, scale)
The question is interesting though. With a triangle being the most simple polygon, you can approximate any 3D shape with triangles. However, if that's what you're trying to do here, then I think it might be a good idea to keep in mind you can also use the other available shapes. For example, if you'd form a pyramid out of triangle polygons, you'd need 6 per pyramid (2 triangles for the square face at the bottom).
-
@mixaal Indeed there is no efficient way to make a custom Z-buffer using the available 2D graphics commands. Looping over every pixel would be very slow.
However it is generally possible to cut faces and order them by using a binary space partition tree (BSPT). I have done this, although I admit that the amount of faces I can afford to draw is relatively small partly do to the costly way I cut faces into smaller pieces. More specialized methods of ordering faces might work better depending on what you are going for.
-
@Gothon The painter's algorithm might be a more efficient solution if you're okay with the PS1 look π€ Or not!
-
@mixaal g'day mate
This is the best I could come up with.
100 polygon model
A lot of optimization went into it butBackface culling using precalculated vectors
Originally had my models in 3 dimensional arrays converted to one dim and calculated the offsets
Originally calculated each point in each polygon switched to calculating only those points that weren't already calculated
Used a function call to calculate the 3d points and ran through twice to achieve rotation for the model and rotation in space. Had to unwind into one very long function 90 ? ( probably more ) Lines of code instead of 10 ? . This was helpful because the way you're looking doesn't change so you only have to set once.
Except for the models which are a global kept everything else local. Used vector to load the points from the array ( everything was stored as vectors in the array )
Used the vectors to calculate the 3d points. This was as fast as the other way. I was hoping for a speed increase as I thought it might calculate in parallel. I think because the I made the variables local.That's all I can think of.
Anyway if you download the latest wormhole game I made
Go down to main about line 170 there is
Create_3d_object
Uncomment
Then go into the loop and comment out everything out to just before the cabinet [ 3 ] about line 250
That will achieve what's in this video
So all the 3d code is towards the end of the program.
One last thing if I didn't Do all the things I said above 10 fps. -
@xevdev Fun fact: you could probably simulate Gouraud shading if you drew gradient triangles over your polys π€
What is the cost difference between triangle vs drawShape vs drawQuad with two points having the same coordinates?
Are outlines free?
Edit: I've answered my own question.
- triangle is twice as cheap as the other two methods, but it can't draw an outline unless you call the function twice with different settings, negating the advantage.
- drawShape is more costly than triangle but the outline is essentially free
- drawQuad takes a little less time to draw than drawShape. No outlines, but it's the only one offering texture mapping. Can also draw quads (obviously).
-
@Doriphor
Yes drawquad does textures at next to no cost and originally I had textures but eliminated as I was looking for speed . Not sure at the moment if it looses speed as i haven't implement textures. Also took out screen clipping for each polygon. Basically went for maximum speed to see how fast it could go. Only thing I kept was normals for lighting the polygons and even that doesn't work properly. To busy at the moment working in 40 degree heat building sheds to program anything. I'll be back ; { -
@Doriphor
As for the fastest sort I could program ( I'm not a programmer ) that I found on Wikipedia that worked was a comb sort line 1573 in wormhole.
I tried some others but recursion sorts bummed out at like 6 recursion?.
At 24 lines it was pretty !@#$ing good but if you or anyone can do a better one I'm all ears.
It worked on vectors again so it had to swap the vectors around.
It used the z axis for the sort
Used a pointer to the arrays position
And the x? Was the order all put into a list and churned out for the drawquad.
A sort would be good in fuze that that used an array of vectors like described. And that would be more helpful than sorting a list of numbers. -
@Doriphor
If you use drawquad as a triangle ie use the 4th coordinate as the 1st then it gives a better texture transform. Not ideal but there it is. -
Yes I knew about drawQuad being able to draw tris π sadly it doesn't have perspective correction for textures π
For the sorting, I would also like to run some tests but Wikipedia is a pretty good source. I think you'd usually want to sort the z coordinate of the center of each tri.
Also yes you are a programmer. You wrote a pretty great and very complex piece of software, especially considering the tools we were given. I have worked with devs who couldn't get 1/10th of the way for a game like yours. Of course if you wanted to get into professional dev you'd probably have to learn some of the more boring stuff, but you definitely are a programmer. Never let people tell you otherwise!
And finally: recursion is eeeeviiilll. Don't use it. Ever. It's taught in courses and yes, it's more readable, but recursion breaks down quickly (and you're pretty much guaranteed to cause a stack overflow after a relatively small number of iterations, even with very simple calculations like the Fibonacci sequence) and even performance-wise, it doesn't hold up against loop-based algorithms. π
-
Itβs baffling that thereβs no draw triangle function for 3d. Baffling!
-
Recursion is basically a trick to make function based loops and use the function call stack as a substitute for making a memory based stack of your own. For example it makes looping over tree like structures quite easy in many cases, because to loop over a tree you need to keep the path to the current node from the root of the tree in memory (eg in a stack). However, Fuze has a rather small call limit atm, so lazily coding anything recursively will likely hit the limit before you have a handful of calls on the call stack.
-
@Gothon Avoiding recursion still usually results in better performance from my experience :)
-
@toxibunny
I dunno ?
I had a a fun time with one function and learned a lot.I remember as a 14 year old on my cpc464 experimenting with sine and cosine to try and get a 3d cube and being surprised it didn't display properly. No perspective. And I was good at tech drawing and wondering why.
So all I've done is derived from an equasion I got from a PlayStation 2 demo called yabasic. It plots a point in 3d space. And I've run from there. And learnt a lot about the limitations and will try to learn more.
I think once you know the limitations then your keen to learn a better way and thankfully we live now where we can find it on the internet.
Unlike that bloke who come up with quaternions before computers.
Pure genius and hopefully I can underkerstumble it one day too. -
@xevdev Wow :) So you wrote it all by youself! Anyway it looks amazing!