Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting 3D Math Functions in Android

as part of my project, I need to plot 2D and 3D functions in android using android studio. I know how to plot 2D functions but I'm struggling with 3D functions.

What is the best way to plot 3D functions? What do I need and where do I start?

I'm not looking for code or external libraries that do all the work, I just need to know what I need to learn to be able to do it myself.

Thanks in advance.

like image 870
DPDP Avatar asked Aug 29 '26 16:08

DPDP


1 Answers

I know how to plot 2D functions but I'm struggling with 3D functions.

What is the best way to plot 3D functions? What do I need and where do I start?

I'm not looking for code or external libraries that do all the work, I just need to know what I need to learn to be able to do it myself.

Since you already understand 2D and want to advance to 3D there's a simple and non-optimal method:

  • Decide on how much z depth you desire:

    EG: Currently your limits for x and y in your drawing functions are 1920 and 1080 (or even 4096x4096), if you want to save memory and have things a bit low resolution use a size of 1920x1080x1000 - that's going to use 1000x more memory and has the potential to increase the drawing time of some calls by 1000 times - but that's the cost of 3D.

    A more practical limit is matrices of 8192,8192,16384 but be aware that video games at that resolution need 4-6GB graphic cards to work half decently (more being a bit better) - so you'll be chewing up some main memory starting at that size.

    It's easy enough to implement a smaller drawing space and simply increase your allocation and limit variables later, not only does that test that future increases will go smoothly but it allows everything to run faster while you're ironing the bugs out.

  • Add a 3rd dimension to the functions:

    EG: Instead of a function that is simply line_draw(x,y) change it to line_draw(x,y,z), use the global variable z_limit (or whatever you decide to name it) to test that z doesn't exceed the limit.

    You'll need to decide if objects at the maximum distance are a dot or not visible. While testing having all z's past the limit changed to the limit value (thus making them a visible dot) is useful. For the finished product once it goes past the limit that you are implementing it's best that it isn't visible.

  • Start by allocating the drawing buffer and implementing a single function first, there's no point (and possibly great frustration) changing everything and hoping it's just going to work - it should but if it doesn't you'll have a lot on your plate if there's a common fault in every function.

  • Once you have this 3D buffer filled with an image (start with just a few 3D lines, such as a full screen sized "X" and "+") you draw to your 2D screen X,Y by starting at the largest value of Z first (EG: z=1000). Once you finish that layer decrement z and continue, drawing each layer until you reach zero, the objects closest to you.

    That's the simplest (and slowest) way to make certain that closest objects obscure the furthest objects.

Now does it look OK? You don't want distant objects the same size (or larger) than the closest objects, you want to make certain that you scale them.

The reason to choose numbers such as 8192 is because after writing your functions in C (or whichever language you choose) you'll want to optimize them with several versions each, written in assembly language, optimized for specific CPUs and GPU architectures. Without specifically optimized versions everything will be extremely slow.

I understand that you don't want to use a library but looking at several should give you an idea of the work involved and how you might implement your own. No need to copy, improve instead.

There are similar questions and answers that might fill in the blanks:

Reddit - "I want to create a 3D engine from scratch. Where do I start?"

Davrous - "Tutorial series: learning how to write a 3D soft engine from scratch in C#, TypeScript or JavaScript"

GameDev.StackExchange - "How to write my own 3-D graphics library for Windows? [closed]"

like image 192
Rob Avatar answered Aug 31 '26 05:08

Rob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!