Share My Creation Basic 3D-Engine

A play around demo of a lightweight 3d-draw program. What it mainly does is display objects made of flat colored faces. The source may be useful, if you like an simpler-than-usual approach to basic 3d generation, where everything is coded 'by hand'. If the code looks a bit strange though, it's a pretty straight port from Visual Basic 2010. The complete project is available in the attachment and builds with B4a v2.52. Sorry, there are some minor multitouch-glitches i couldn't fix yet ^(.
(In case you ask, I kept the borders to check poly-clipping (while circles are not clipped)).

Controls:
Touch left side-bar to accelerate in eye direction
Touch screen to curve around
Touch right side-bar to switch renderstyle.

PlayStore: https://play.google.com/store/apps/details?id=b4a_test.engine21


IMG_5512_1008x594.jpg

Screenshot: Google Nexus 7

Known Issues:
Audio: On Nexus 7 Tablet (old 1280x800 version) updated to Android 4.3 JellyBean, the sound-loop (using 'SoundPool') stops after the first iteration. It still works with JB 4.2 and lower. Maybe it's just my device - glad to hear your experience.
 

Attachments

  • engine21_004_2.zip
    171.8 KB · Views: 635
Last edited:

keylab83

Member
Licensed User
Longtime User
I'm geting
B4X:
java.lang.RuntimeException: Object was not initialized.
on this line
B4X:
cDeepBlack.Initialize2(0, 0, 0)  '= New color5(0, 0, 0)
in colorsCLS module
 

kopperfeld

Member
Licensed User
Longtime User
I'm geting
B4X:
java.lang.RuntimeException: Object was not initialized.
on this line
B4X:
cDeepBlack.Initialize2(0, 0, 0)  '= New color5(0, 0, 0)
in colorsCLS module

I haven't got this error with B4a v2.52, maybe v2.71 is more strict. Do you get more such exception messages?
Adding these initializations might help (code update in the attachment):

cDeepBlack.Initialize: cLiteWood.Initialize: cStainSteel.Initialize: cLiteTar.Initialize

Otherwise I found these color5's aren't really used anywhere in the program, so you would likely 'get rid' of the problem by just outcommenting those lines ;)
 

Attachments

  • x2_004_ok3.zip
    172.9 KB · Views: 381
Last edited:

kopperfeld

Member
Licensed User
Longtime User
;) Thanks & Great to know it could be of any help for someone; even better if you can possibly use it for one of your projects alike!. The control code is enormously oud, like from 2ooo, and the mechanics follow simple ideas; no big magic here. From nowadays view, the code seriously needs refactor for better readability and understanding. (at least it somehow flows.. (??) ^)

Another idea was to tinker some of simple gameplay around it, with goals to reach / items to collect. Due to a bit scarce time i'd be glad for any help to push it forward better.) Admittedly for me, 3D stuff can be queerly complicated to handle for anything more than such a small demo (speaking of things beyond collision detection, i.e. proper physics or so). So I'd rather focus on 2D things first.
 
Last edited:

JordiCP

Expert
Licensed User
Longtime User
I am working on something similar. I started with this and now can travel "through the galaxies" with a control. I use camera transforms, view projections and some dirty tricks.

While diving into all this, one wonders why not to use an already made engine which will be surely faster, more reliable, .... But it is funny trying to code it by yourself.

I looked at your code, I must admit it is too difficult to understand for me and don't have much time neither. But I think you could use AcceleratedSurface that would speed up the drawing part in case the world would become more complex. If I find time I will try to see if it is easy to change this part

May I ask you where did you get the sounds? I need to find something similar effects
 

kopperfeld

Member
Licensed User
Longtime User
Wow ^^, Your project looks canny ! Somehow more 'textured' than mine.. Would be glad for the chance to take a look at the code someday. (I just saw the video already).
You're right, this Accelerated Surface should speed up drawPath considerably, wished i'd find more time to try it... Do you believe it'd be a big effort to add it to my program?, I know the code seems a bit bloated. ;(

Correct, why even bother to do it all by hand ? ^^ Err.. simply said, I had the old code already and wish to add stuff like physics or collision detection later. Also, i need the screen-position of points, for example to show a 2d-cross or distance-value on a 3d-target. With my home-made engine, 2d points are already there, just as a by-product of the calculation pipeline. Somehow i felt like needing full control over everything, and simply don't know how yet to emulate this with a readymade 3d-engine or openGL; albeit I believe and agree it'd be more easy than my approach in the end. ^^ .
 
Last edited:

kopperfeld

Member
Licensed User
Longtime User
For simpler sound effects (like sine-rectangle-sawtooth- or noise-waves) I used something from NHC, the tone/wave-generator (unluckily not free anymore, but you can still build and save some simple sounds I think). http://www.nch.com.au/tonegen/index.html
I just found these http://soundfx-free.com/ for readymade sound samples, but haven't tried it yet! ^

For little more advanced sounds like buttons, shots or explosions I used this online sound-fx generator: http://www.bfxr.net/. I saved the sounds in .mp3 and converted to the more space-saving .ogg-format later (with some other tool I sorrily forgot, but there must be plenty) ^^.
 
Last edited:

JordiCP

Expert
Licensed User
Longtime User
Thanks for the advice, will try it.

The sphere rendering part is not really 3D. The code uses a tricky UV mapping for the sphere textures and lighting addition/substraction on the borders to give a more realisteic effect. It all belongs to a set of utilities that I am developing for ARM devices with NEON coprocessors, which are the most common, but not all so it will be very limited in use.

While this approach allows me to get nice simple effects with a small library behind, it is itself limited to some kind of operations, as I don't have really a 3D set of sphere points and texture the polygons. Instead, on each frame I calculate the center position, the projected radius and orientation relative to the camera, and then do the UV mapping with a seamless texture using some X,Y offset which will achieve the rotation effect

Will take another look at your code but sadly I don't have enough time even for my projects :(

When I finish my game I will try to publish a small library and example with it :)
 

amorosik

Expert
Licensed User
I would very much like to understand how the posted code works
I am using on Ide B4A 11.50 + jdk 11.0.1
Loading the code at post # 4 and starting the compilation I get the following error

B4X:
init_screen()...
init_screen() done.
init_system()...
A. Build Sounds...
soundDir: AssetsDir
Load Sounds..
Load Sounds.. ok
Start Sounds..
Start Sounds.. ok
B. Build Components...
Error occurred on line: 23 (material3)
java.lang.RuntimeException: Class instance was not initialized (color5)
    at anywheresoftware.b4a.debug.Debug.shouldDelegate(Debug.java:242)
    at b4a_test.engine21.color5._initialize2(color5.java:231)
    at b4a_test.engine21.material3._init(material3.java:84)
    at b4a_test.engine21.material3._initialize(material3.java:128)
    at b4a_test.engine21.materialscls._init(materialscls.java:200)
    at b4a_test.engine21.materialscls._initialize(materialscls.java:92)
    at b4a_test.engine21.buildercls._create_components(buildercls.java:74)
    at b4a_test.engine21.maincls._init_system(maincls.java:257)
    at b4a_test.engine21.maincls._initialize(maincls.java:93)
    at b4a_test.engine21.main._initialize_main(main.java:409)
    at b4a_test.engine21.main._activity_create(main.java:395)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at b4a_test.engine21.main.afterFirstLayout(main.java:105)
    at b4a_test.engine21.main.access$000(main.java:17)
    at b4a_test.engine21.main$WaitForLayout.run(main.java:83)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
** Activity (main) Resume **

How to correct the problem and allow the code to start correctly?
 

amorosik

Expert
Licensed User
Modifing material3 sub with sideA and sideB inizialization, all compile and run fine

material3 code:
Private Sub init() 'create default material ->
    setup (0, 0, 0, 1, 1)
    sideA.Initialize()
    sideA.Initialize2(12, 12, 12) '= New color5(12, 12, 12)
    sideB.Initialize()
    sideB.Initialize2(8, 8, 8)    '= New color5(8, 8, 8)
End Sub
 
Top