B4A Library 3D Bodies - Four libraries

Reuse of something I did in B4ppc days and got a round tuit now for B4a.
There are three libraries, doing similar thing with variations:
1. A 3D cube, with or without dice numbers on, can roll by touch or by command or provide a throw result by click.
2. A 3D library for any body, provided that you know how to define its verteces and polygons. There are examples of several bodies which may help, see photos.
3. A 3D library for any body (like 2 above), using Open_GL 1 library (thanks to Agraham). Note that I have very limited knowledge on the use of Open_GL and this implementation is very basic, however the user of this 3D library does not need any knowhow of Open_gl at all.

In all libraries you can create a view which is a base panel and the body on it. The panel may be transparent or with a bitmap.

A demo application is attached, including the bodies in the photos and a surprise.
The documentation is inherent and simple.
Important: The demo with the BodyGL library has some code with a timer - the specific item will fail when running in rapid debugger or even in legacy, but runs well in release mode (probably due to heavy load of Open_GL).

The three libraries use a coordinate system in which X is to the right, Y is up and Z is into the device. Flattened to 2D, z is diagonal from bottom-left to top-right.
The units of this system are much larger than the pixels, few units cover the display (depends on the z coordinate and the ViewAngle.

Edit: Cube3D updated to ver 1.01 , now its possible to define it as custom view.
see post 3 for details.

Edit: Ver 1.02 - CubeGL added , doing what Cube3D does but using openGL.
Cube3D and CubeGL both are also CustomView in the designer. See post #5 for details.
Edit: Frame line option added to BodyGL.
Edit: Body3D version is updated to 1.1, removing a limit of the number of points in a body and adding ViewAngle method. It is updated in 3D.zip file.
Edit: all libraries updayed to ver 1.2, inclusion of LineWidth method.

Note: The examples use openGL library , not updated to openGL2 !
 

Attachments

  • Body3Demo.zip
    448 KB · Views: 846
  • Prism3d.png
    Prism3d.png
    11.2 KB · Views: 1,026
  • dode3d.png
    dode3d.png
    10.9 KB · Views: 940
  • UAV3d.png
    UAV3d.png
    139.8 KB · Views: 1,073
  • dode.png
    dode.png
    114.3 KB · Views: 1,042
  • Cube3Demo.zip
    447.7 KB · Views: 685
  • CubeGlDemo.zip
    502.8 KB · Views: 750
  • Cube3ddemo.png
    Cube3ddemo.png
    115.9 KB · Views: 1,032
  • BodyGlDemo.zip
    62.8 KB · Views: 645
  • 3D.zip
    37.6 KB · Views: 764
Last edited:

derez

Expert
Licensed User
Longtime User
The attached is a b4ppc program for windows, enabling view and rotation of some of the bodies in the demo above. It also enables to see the body with the verteces numbers without color and to move the verteces for new design of the body. When design is complete - save, the file is "body.txt" in the application folder.

A special for Hanuka - a Svivon photo :) and code:
B4X:
Sub svivon_Click
    vert.x = Array As Float(2, 2, 2, 2,-2,-2,-2,-2, 0 , .5, .5, .5, .5,-.5,-.5,-.5,-.5 )
    vert.y = Array As Float(2, 2,-2,-2, 2, 2,-2,-2, -5,  5, 5,2,2, 5, 5,2,2 )
    vert.z = Array As Float(2,-2,-2, 2, 2,-2,-2, 2, 0, .5,-.5,-.5, .5, .5,-.5,-.5, .5  )
    pitch = 0
    Yaw = 45
    roll = 45
    posx = 0
    posy = 1.5
    posz = 30
viewangle = 45
    posx = 0
    posy = 1.5
    posz = 30

    Dim pol(14) As String
    Dim color(14) As Int
    pol(0) = "3,2,1,0"
    pol(1) = "2,6,5,1"
    pol(2) = "6,7,4,5"
    pol(3) = "0,4,7,3"
    pol(4) = "0,1,5,4"
'    pol(5) = "2,3,7,6"
    pol(5) = "2,3,8"
    pol(6) = "6,2,8"
    pol(7) = "7,6,8"
    pol(8) = "3,7,8"
    pol(9) = "12,11,10,9"
    pol(10) = "11,15,14,10"
    pol(11) = "15,16,13,14"
    pol(12) = "9,13,16,12"
    pol(13) = "9,10,14,13"
 
    color(0) = Colors.Blue
    color(1) = Colors.cyan
    color(2) = Colors.Red
    color(3) = Colors.Rgb(255,128,192)
    color(4) = Colors.Green
    color(5) = Colors.Rgb(255,128,192)
    color(6) = Colors.Red
    color(7) =  Colors.White
    color(8) = Colors.blue
    color(9) = Colors.blue
    color(10) = Colors.cyan
    color(11) = Colors.Red
    color(12) = Colors.Rgb(255,128,192)
    color(13) = Colors.Red
    Set_body
End Sub
 

Attachments

  • tool.png
    tool.png
    29.2 KB · Views: 709
  • rotate_D5.zip
    29.4 KB · Views: 519
  • Svivon.png
    Svivon.png
    128.6 KB · Views: 683
Last edited:

derez

Expert
Licensed User
Longtime User
Cube3D updated to ver 1.01 , now its possible to define it as custom view.
In case of definition by IDE as custom view the first method should be Start.
In case of creation by code the sequence of commands is like this:

B4X:
c3d.Initialize("Main","c3d")
c3d.CodeCreateView(wh)
Activity.AddView(c3d.AsView, 50%x,10%y,wh,wh)
c3d.Start( color, Colors.black, 0.10)
 

derez

Expert
Licensed User
Longtime User
Ver 1.02 - CubeGL added , doing what Cube3D does but using openGL.
Cube3D and CubeGL both are also CustomView in the designer.
Instructions:
- create CubeGL object by code
you cannot set the background to color, it is either transparent (when bmp Is Null) or a Bitmap.
- creation by designer :
Set the drawable to bitmap and set the bitmap to blank if you want the background To be transparent, otherwise set the picture File from the assets as bitmap.
If you want a colored background - set a color drawable with the color.
Set Textcolor to be the Dot color.
- Linecolor default is black, you can set it by a method to another color.
 

Attachments

  • CubeGLDemo.png
    CubeGLDemo.png
    124 KB · Views: 475

Sia

Member
Licensed User
Longtime User
@derez ,
hi,
i saw your example code for drawing surface chart with axis and title, it's very nice.

can you shear you code ?
very thanks.
 

Attachments

  • surface2.png
    surface2.png
    137.2 KB · Views: 583

derez

Expert
Licensed User
Longtime User
Sia said:
i saw your example code for drawing surface chart with axis and title, it's very nice.

can you shear you code ?
The work I do there is being paid for so I will not share it.
The background of the chart is just "hard work" of drawing lines and text to form the 3D axis.
The surface is a 3d body which can be created and rotated by the published library (the body3D). Currently there is a limit on the number of points but I'll remove it soon by new versin.
 

derez

Expert
Licensed User
Longtime User
Body3D version is updated to 1.1, removing a limit of the number of points in a body and adding ViewAngle method. It is updated in 3D.zip file in the first post.
 
Last edited:

derez

Expert
Licensed User
Longtime User
All libraries updayed to ver 1.2, inclusion of LineWidth method.
 
  • Like
Reactions: Sia

Sia

Member
Licensed User
Longtime User
Sia said:

I got permission to share. Send me your database and I'll show you how to do it with Body3D.

in my file first line "10" is x and "60" is Y and after it every 60 line is Y Mean Y1,Y2,Y3
you must use for to read this and insert to your array
 

Attachments

  • a1.txt
    2.9 KB · Views: 412

tonga

Member
Licensed User
Longtime User
Hi Derez,

I see your good works. Now I wish to translate in the screen the cube of "Body3Demo", but is very difficult for me to understand the mean of coordinates of verteces in the "Sub Cube_Click":

vert.x = Array As Float(2, 2, 2, 2,-2,-2,-2,-2)
vert.y = Array As Float(2, 2,-2,-2, 2, 2,-2,-2)
vert.z = Array As Float(2,-2,-2, 2, 2,-2,-2,2)

Can you explain me or show me where I can find any documentations?

Thanks!
 

rkwan

Member
Licensed User
Hello, I am a newbie trying to use this good example to plot 3D scatter points like this:-
https://matplotlib.org/examples/mplot3d/scatter3d_demo.html
I can get the elevation and azimuth view change with sliders.

However, when I tried the Surface example with
cnvs.DrawCircle() or cnvs.DrawOval(),
I cannot get good effect like as in matplot3d.

Any suggestion please?

Thanks & Best Regards,
 

derez

Expert
Licensed User
Longtime User
Hello, I am a newbie trying to use this good example to plot 3D scatter points like this:-
https://matplotlib.org/examples/mplot3d/scatter3d_demo.html
I can get the elevation and azimuth view change with sliders.

However, when I tried the Surface example with
cnvs.DrawCircle() or cnvs.DrawOval(),
I cannot get good effect like as in matplot3d.

Any suggestion please?

Thanks & Best Regards,

The library is for drawing 3D bodies or surfaces, not for showing scatter points. Each object that you want to show is made of surfaces, unlike points.
Seems to me that you should use something else.
 

rkwan

Member
Licensed User
Oh, I see. Thanks for your advice, Derez! By the way, any idea on what else I can try please? I am also trying the LibGDX now... Thanks & Best Regards,
 

derez

Expert
Licensed User
Longtime User
It is Agraham's opengl (not 2 ) library, search the forum for it.
 
Top