B4A Library OpenGL library

Here is a first stab at implementing an OpenGL library. The implementation is a subset of the full Android API but the actual functionality is pretty complete (I think!) as the omitted functions are mainly those that used scaled integer working, I have only implemented the equivalents that use floats.

However it is almost totally untested. I have got it to the point where it can draw a triangle on the screen but my total ignorance of OpenGL makes further progress challenging and I'm not really interested in doing graphics. As it can now draw on-screen most of the further functionality should be OK as it is a very thin wrapper of the OpenGL API. The hard part of integrating the GLSurfaceView with Basic4android and drawing on it is done.

The reason for posting it therefore is to see if there is any interest in it before I waste more time on it, and to see if there is an OpenGL expert out there who would collaborate on testing and debugging it - assuming anyone wants to use it at all!

As an aside I believe that the GLSurfaceView, being based on a standard View, can also be drawn on using the same methods as other Views as well as by the OpenGL renderer but I haven't tried that.

Note that the "demo" needs my Threading library.

EDIT :- Version 1.1 posted. See post #5 for details.

EDIT :- Version 1.2 posted. See post #10 for details.

EDIT :- Version 1.3 posted. See post #16 for details.

EDIT :- Version 1.4 posted. See post #21 for details.

EDIT :- Version 1.5 posted. See post #25 for details.

EDIT :- Version 1.6 posted. See post #35 for details.
EDIT :- Version 1.6 reposted with correct version number. See post #38 for details.

EDIT :- Version 1.7 posted. See post #39 for details.
 

Attachments

  • OpenGL1.7.zip
    51.3 KB · Views: 2,533
Last edited:

Jim Brown

Active Member
Licensed User
Longtime User
For anyone who wants a straight-forward example of a textured cube to play around with, see attached

Sliding your finger down and up on the touch screen will zoom in and out
OpenGLCube.png
 

Attachments

  • CubeDemo01.zip
    55.9 KB · Views: 460

alfcen

Well-Known Member
Licensed User
Longtime User
Hey Jim,
Congratulations, this is magnificent programming work!!!
Too bad that textures can't be wrapped around a sphere, but what it does already is exciting enough.

It runs smoothly on Sharp IS03.

To avoid oversizing, I added:

B4X:
Sub Activity_Touch (Action As Int, X As Float, Y As Float)
   Select Action
      Case Activity.ACTION_DOWN
         lastY=Y
      Case Activity.ACTION_MOVE
         distZ=distZ+(Y-lastY)/25
         [COLOR="Red"][B]If distZ > -2 Then distZ = -2[/B][/COLOR]
         Log(distZ)
         lastY=Y
      End Select
End Sub


Can't wait to see what's next :sign0098:
 

Jim Brown

Active Member
Licensed User
Longtime User
Hi gang,
Here's a new demo to try (attached). Use touch screen to rotate the Earth, and -/+ buttons to zoom

OpenGLEarth.png



The Earth object is a relatively low-poly job I created in Wings3D. Its a bit rough and hastily put together!
After exporting in DirectX format I wrote a routine in B4A to handle the loading of DirectX object files. Thankfully the file format is fairly easy to work with.

Earth object Tech specs
======================
992 faces (2976 vertex coords and 1984 uv coords)
Texture is 512x512
DirectX format

Eventually I want to release the code but its WIP at the moment. Only vertex and texture coordinate data are loaded for example. No other fancy stuff.

Let me know how performance feels. I have turned off backface rendering (faces you never see, such as inside the earth) but I believe futher speed improvements can be made, such as using fixed-point numbers instead of floats
 

Attachments

  • OpenGLObjectTest.apk
    223 KB · Views: 377
Last edited:

alfcen

Well-Known Member
Licensed User
Longtime User
Hey Jim,

Absolutely smooth on Sharp IS03! The only fancy thing I could think of for practical use is shadow projection, such as day and night on Earth. For outer planets, such as Jupiter, a shadow won't be needed as its phase to Earth is always low.

Actually, I am working on an app simulating the Jovian moons:

Requio Web Design Mobile

Looking at the screenshots, you will see what I am missing there, namely a globe of Jupiter in OpenGL. Presently, the app loads one of 36 images each centering on a 10th longitude, in that the accuracy of the central meridian shown is +/- 10 degs, thus not acceptable for serious observers.

Forgive me, I am not a 3D designer... would it be possible to convert a bitmap, such as a NASA surface map, to a DirectX file? Since you have already developed a routine that loads DirectX this might be the least painful method.

Anyway, your progress is truly amazing :sign0098:
 

Jim Brown

Active Member
Licensed User
Longtime User
Hi Robert
Forgive me, I am not a 3D designer... would it be possible to convert a bitmap, such as a NASA surface map ..
Yes, as long as its a nice power of 2 size (256x256, 512x512 etc)

If you need planets such as Jupiter I can this up, pretty much like the Earth object in the demo. I will have to look at how lighting and shadows work though. Its something I have not touched upon yet. I am mainly working on the DirectX model importer (and it's many gotcha's!)

Displaying the objects is easy enough. Do you need coordinates in 2D though (as in X across, Y down)?

===========================
More things to play with!

OpenGLObjectSet.png


For the new demo I have decided to attach the source code (which includes the DirectX importer). If you intend to import your own DirectX models and they fail let me know. I can give you a couple of pointers
 

Attachments

  • OpenGLObjectTest2.zip
    306 KB · Views: 422

alfcen

Well-Known Member
Licensed User
Longtime User
Hi Jim,
Great news. Some Jupiter surface maps are here:
http://www.requio.com/jupiter.zip
Any will do. Lighting and shadows are not required for Jupiter.
The ZIP contains 512 x 512 BMP's plus the original JPEGs.

I will look into your latest creations tomorrow, as it is past midnight in Okinawa.
Your screen snap looks exciting!
 

Jim Brown

Active Member
Licensed User
Longtime User
This new example ' OpenGL ES - Planets (Source code) ' demonstrates the ability to change an objects texture on the fly. Rather than create lots of planet 'objects' only one is loaded. The texture can then be changed dynamically as needed. The demo has textures for Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto

Hit MENU to Choose planet textures
 

alfcen

Well-Known Member
Licensed User
Longtime User
Truly awesome, Jim! This concept is exactly what I have been after and it's not yet time for birthday :sign0060:

I believe wrapping accuracy will increase by adding further details in the DirectX mesh file (sphere model). Earth continents look a bit stretched horizontally, which is less apparent on the planets, therefore, no big issue at all also considering that phone screens are rather tiny while a full screen display is not essential.

Out of curiosity, I tried it with a day and night terminator map of Earth - Nice!

I will be away to mainland Japan for a week and will get back to programming mid next week.

Thanks so much, indeed, for all your effort. Now, I think its up to me to tailor this to my specific needs and learn a bit about OpenGL myself.

Cheers

Robert
 

winjiadh

Active Member
Licensed User
Longtime User
error

I download it ,and copy the jar,xml files to the Additional Libraries folder.
and run the "OpenGLDemo.b4a"
error is :
Compiling code. Error
Error parsing program.
Error description: Unknown type: exceptionex
Are you missing a library reference?
Occurred on line: 186
Dim Ex As ExceptionEx
 

agraham

Expert
Licensed User
Longtime User
The documentation lacks, documentation. It lists a lot of subs but doesn't describe what they do/how to use them.
Here is a link to the Android documentation for OpenGL, the android.opengl package and the main GLES10 class.

As I noted here it is somewhat less than helpful.

I'd just like to be able to load a texture, then draw it using X,Y,Rotation,Alpha,Color,etc.
Better find an OpenGL ES resource and start reading.
 

Jim Brown

Active Member
Licensed User
Longtime User
@NeoTechni
I'd just like to be able to load a texture, then draw it using X,Y,Rotation,Alpha,Color,etc
There are no straightforward commands in OpenGL (ES) which let you simply "LoadImage", "RotateImage" etc but see my attachment, which is a 'getting started' demo for loading and displaying 2D textured images

The demo should display a rotating and fading Android logo in the center of the screen (with a static blue Android behind and to the upper/left):
androidgl2.png



** NOTE: Your own images must be in power of 2 sizes otherwise they may not show up. Example sizes == 256x128, 256,256 , 512,256 ...

Commands included:
ClearScreen
InitialiseTextureImage
DrawTextureImage
LoadTexture
FreeTexture
SetTextureImageColor
SetTextureImagePosition
SetTextureImageRotation


-- This example uses Graham's OpenGL 1 and Threading libraries --
 

Attachments

  • OpenGLStarter2DImages.zip
    12.8 KB · Views: 355
Last edited:

alfcen

Well-Known Member
Licensed User
Longtime User
Earth Sphere

Hey Jim,
Sorry, indeed, to bother again. As I mentioned earlier, the Earth globe is stretched horizontally. Attached two images for comparison.
I have tried manipulating parameters in the OpenGL code, but failed to draw a more accurate sphere. I am correct assuming that
the accuracy of the globe depends on the accuracy of the mesh (DirectX globe.x file), or did I simply overlook something?
Any hint is much appreciated.

Cheers
Robert
 

Attachments

  • earth-correct.jpg
    earth-correct.jpg
    8.4 KB · Views: 254
  • earth-stretched.jpg
    earth-stretched.jpg
    11.9 KB · Views: 221

Jim Brown

Active Member
Licensed User
Longtime User
Hi Robert

I have re-created the model and re-mapped it. It now appears to looks correct from what I can see:
OpenGLPlanets2.png


To use the model in your project you will need to obtain it from this updated example -- OpenGL Planets v1.1

NOTE: I have re-written the whole model handling code for my new *.model format. This is smaller and easier to parse compared to the Direct X output. Models should load a quicker than before (and take up less than half the storage space)
If you want to use this new code in your project search "GLObject" which should help to guide you in terms of what to change. See how all functions now start with GLObject_
 
Last edited:
Top