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,508
Last edited:

alfcen

Well-Known Member
Licensed User
Longtime User
Hi Jim

Your new *.model concept looks great and is fast, indeed, even with 1k and 2k textures!

I must appear like splitting hairs. While the stretch in longitude looks fine, now the
latitudes appear to be stretched.

Compared with a correct image of the globe, you can't look down to the southern tip of Africa.

Also when rotated to poles the sphere looks like a "16tagon", rather than a circle.
The image of Neptune shows this phenomena most clearly.
Perhaps the model needs to be a finer mesh, but I realize this is much work.

I am fascinated by your model viewer demo!
 

Attachments

  • Image1.jpg
    Image1.jpg
    39.1 KB · Views: 242
  • Image2.jpg
    Image2.jpg
    58.1 KB · Views: 241
  • Image3.jpg
    Image3.jpg
    44.2 KB · Views: 247

Jim Brown

Active Member
Licensed User
Longtime User
I will take another stab at this later. The mesh is relatively low-poly but I can easily increase this. Bear in mind the processing overhead though

One thought regarding appearance - try messing around with the gluPerspective() setting. I am wondering whether this is causing a kind of fish eye distortion on the texture(?)
I also tried switching to an Orthographical display but nothing would render
 

alfcen

Well-Known Member
Licensed User
Longtime User
Hi Jim

"Fish eye". Bingo!
I played with the zNear parameter in gluPerspective() and came to significantly improved results.

Processing overhead is a compromise for higher accuracy. If you can spare the time I'd very much appreciate a high-poly mesh.

You did great!
Robert
 

alfcen

Well-Known Member
Licensed User
Longtime User
Superb!

That was in no time, Jim, THANKS SO MUCH!
The accuarcy of your high-poly model is :sign0060:

Playing with distZ against fov in:
gl.gluPerspective(fov,ratio,0.5,100.0)

reveals excellent proportions of the continents
while the pole views are nicely circular.
This opens exciting possibilities.

Complements, you are doing truly great with OpenGL ES.

Today, I came across an example in Java that shows how to lighten a globe
(for the purpose of rendering a day/night terminator). I'll will try it out
as well as another example of blending two textures, such as for
overlaying a coordinate grid.

Cheers
Robert
 

Jim Brown

Active Member
Licensed User
Longtime User
Thanks Robert. Its all a learning curve for me so no problem. I am glad we got there in the end

>Today, I came across an example in Java that shows how to lighten a globe

Sounds interesting. I don't know if you will need Normals data for your object. I have implemented normals but when playing around with lighting settings appear, there is no change the models visual appearance. Perhaps my normals data is whack. If you have a link it might help me in this field
 
Last edited:

alfcen

Well-Known Member
Licensed User
Longtime User
Grid on sphere

Hi Jim,

I wrapped a coordinate grid texture around a sphere and noticed that the poles are having accuracy problems. Is this inherent to the model or could this be tuned?

How far did you get to Saturn's Rings? I am afraid that ain't easy.

Cheers
Robert
 

Attachments

  • device.jpg
    device.jpg
    11.2 KB · Views: 248

Jim Brown

Active Member
Licensed User
Longtime User
Hi Robert,
I sent you a PM

Hi Andrew,
Is there a way to get the current GL context/object? At the moment I pass the GL1 object from the glsv_Draw() sub to my subs but want to make them self-contained

For example, I would like to do something like:
B4X:
Sub MySub()
   Dim g As GL1   *** <- Error (see below)
   g=CurrentGLContext()
   g.glEnable(blah)
End Sub
The above results in the following error:
B4X:
src\basic\abdroid\GLPlanets\main.java:428: cannot find symbol
symbol  : constructor GLWrapper()
location: class anywheresoftware.b4a.agraham.opengl.GLWrapper
mostCurrent._g = new anywheresoftware.b4a.agraham.opengl.GLWrapper();
                 ^
1 error
 

agraham

Expert
Licensed User
Longtime User
Is there a way to get the current GL context/object?
No. A GL1 instance is only valid in the scope of the Draw event to which it is passed so you cannot save and reuse it elsewhere. You will have to keep on passing it to Subs called from the Draw event that need it.

Related to the above is why you can't "Dim As GL1". It is because it does not posess a default (no parameter) constructor. Basic4android does not let variables be uninitialised so its uses the default construcor on any variable that is Dimmed. This is one reason why Basic4android objects often have an Initialize method to pass them parameters not available when they are intantiated.

The GL1 instance passed to you is initialised within the library with an OpenGL GL10 instance parameter passed to it by OpenGL which is what you are really invoking methods on. You can't get a GL10 so you can't create a GL1 instance in Basic4android.

So that's clear then! :)
 

Jim Brown

Active Member
Licensed User
Longtime User
Excellent and detailed response. I can live with passing the GL1 object to the subs
Thanks Andrew!
 

acorrias

Member
Licensed User
Longtime User
Difference between opengl and opengl2

Hi
Could you explain the differente between the two libraries.
I've have compiled the two demo provided with the libraries but the second one (opengl2) seems not working on my asus transformer tablet.

I've a bit confused. I read:
a) 1st library >> andoird supports OpenGL ES 1.0, which corresponds to OpenGL 1.3
b) 2nd library >> android supports OpenGL ES 2.0, which corresponds to OpenGL 1.5.

But 2ns library has 1.0 version, while 1st has 1.7. I intend this numbers as library version.

Which library should I use? which is best for tablets and which for phones? Do they exist in two flavours for techincal reasons? Then another question: to use tegra2 graphic engine should I use opengl or games with excellent graphics I've seen are hardcoding programmed? thanks in advance
 

alfcen

Well-Known Member
Licensed User
Longtime User
Hi acorrias,

agraham cannot explain OpenGL in this tread because this would lie far beyond the scope of the forum, since OpenGL is a very complex concept.

The library agraham posted in the first post of this thread contains Ver 1.7 which you can safely use for all your experiments. So far I have not yet encountered any problems with V 1.7 :icon_clap:

Please also note that Google included a subset of OpenGL, not the latest full set, perhaps because they don't want us to create another Google Earth in B4A :)
Seriously, it would require dramatic graphic processing power.

Cheers
Robert
 

acorrias

Member
Licensed User
Longtime User
difference betwwen OpenGL ES 2.0 & OpenGL ES 1.0. Some consideration

Hi Robert
I apologize for my silly question.

I don't need to learn OpenGL (I developed with OpenGLwith mfc and c++ ten years ago - if you look fo "boidi volanti" with google, you could find my old course university project). I only needed to re orient myself to understand quickly when to choose OpenGL es 1.0 and when OpenGL es 2.0 and how to update my old knowledge.

But no problem. I have followed agraham's suggestion and found what I needed. I hope that the following and concise definition would be useful for everyone interested in the same question.

"OpenGL ES 2.0 allows for fancier graphical effects such as per-pixel lighting, reflective surfaces, and overall better shader support." and again "OpenGL ES 1.X is for fixed function hardware and offers acceleration, image quality and performance. OpenGL ES 2.X enables full programmable 3D graphics".

I take this opportunity to write this post to ask a question. Is there a library to render with OpenGL one or more OBJ (wavefront) object loading it as external file?

best regards
alex
 
Last edited:

alfcen

Well-Known Member
Licensed User
Longtime User
Hi Alex,

There are no silly questions. I must apologize as I did not realize that you already have experience with OpenGL, unlike myself.

I take this opportunity to write this post to ask a question. Is there a library to render with OpenGL one or more OBJ (wavefront) object loading it as external file?

Sorry, not to my knowledge, but something may come up as demand increases.

Cheers
Robert
 

acorrias

Member
Licensed User
Longtime User
Custom Model

Hi Jim
I've tested your impressive application.
I've installed wings3d but my files seeem a bit different from the one you have provided with the source (model format).
Would you lite to expaing how do you have processed format x?

A question. Have you managed to import obj objects? I've found this piece of code URL="http://potatoland.org/code/gl/source/glmodel/GL_OBJ_Importer.java"]http://potatoland.org/code/gl/source/glmodel/GL_OBJ_Importer.java[/URL].
Do you think it would be possible to port it to b4a to read obj objects?

cheers
alex
 

Jim Brown

Active Member
Licensed User
Longtime User
Hi Alex

The *.model format is my own custom file generated by a Win32 app I wrote, which converts Direct X models into the model format
In Wings3D I export the "selected" model as Direct X (ensuring UV coords option is picked). My model util app can then load/convert/display the model.
I will release this Win32 app soon

I will also take a look at the Wavefont object importer. Originally, I started with Wavefont format but Wings3D seemed to mess up the UV texture coordinates when exporting the model
 

alfcen

Well-Known Member
Licensed User
Longtime User
Hi Jim,

I found a minor issue with your latest globe model. See screen snaps.
The latitude is a bit off. The error accumulates as latitude increases.
You can also see that the land areas in northern regions are stretched.

The previous model with distorted poles is pin-point.
If it won't cause too much trouble...

Cheers
Robert
 

Attachments

  • globe1.jpg
    globe1.jpg
    13.7 KB · Views: 269
  • globe2.jpg
    globe2.jpg
    12.3 KB · Views: 250
Top