B4A Library OpenGL 2.0 library

Android 2.2 and later support OpenGL 2.0 ES as well as OpenGL 1.0 ES. Having produced an OpenGL library for 1.0 I thought I may as well do one for 2.0 as well so here it is!

The included "demo" is not really a demo at all and does little more than test the device OpenGL capability and initialise a drawing surface. It needs someone more interested in graphics than I to take it further.

Note that the demo needs my Threading library.

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

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

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

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

EDIT:- Version 1.5 posted. See post #83 for details.
User @walterf25 has added two methods to the the library to create version 1.5. The new jar and xml are posted below in OpenGL2_v1.5_lib.zip. I have left version 1.4 as the zip for that contains a somewhat trivial demo program. Just use the new v1.5 libs and ignore the v1.4 ones.
 

Attachments

  • OpenGL2_1.4.zip
    43.7 KB · Views: 1,462
  • OpenGL2_v1.5_lib.zip
    38.2 KB · Views: 649
Last edited:

GDO

Member
Licensed User
Longtime User
Hi,

Could you please add something like that :

"...In order to make your OpenGL ES application respond to touch events, you must implement the onTouchEvent() method in your GLSurfaceView class..."

Now I can draw a triangle, I want to move it ...

:)

Thanks.
 

agraham

Expert
Licensed User
Longtime User
This potentially opens up a whole can of worms :( UI events are raised on the main application thread but the renderer events Draw, SurfaceChanged and SurfaceCreated run on a separate rendering thread. I need to think about this.

So that I can read it do you have the link for that quote you gave above?

What did you want to do in a Touch event? Changing global variables to be picked up by the Draw event would be OK but you might have to implement some resource locking or system of flags so the cross thread access from both the main and rendering threads to those variables doesn't cause problems. I don't think that you can call OpenGL functions outside of those rendering events, though I might be wrong. Even so those calls should only be made on the rendering thread.

One way of handling events that is "ready-rolled" would be to put a transparent Panel on top of the GLSurfaceView.
 

GDO

Member
Licensed User
Longtime User
Hello,

My triangle is now moving and I got new problem.

It seems to me that Opengl2 is not hardware accelerated.
I added, into the manifest, hardware Accelerated (application level), minSdk=9 and targetSdk=11 but it is not accelerated on my 2 real devices (Galaxy Notes and Asus Transformer TF300).

Is there something like "double bufered" inside your library to enable ?

Thanks.
 

agraham

Expert
Licensed User
Longtime User
I got the reflection code above reporting hardware acceleration on my Xoom by putting
B4X:
SetApplicationAttribute(android:hardwareAccelerated, "true")
in the Manifest Editor. I was already compiling with "...\android-13\android.jar" in my android.jar path.

EDIT :- the minSdkVersion and targetSdkVersion don't seem to matter. I've set both to 4 and I still get hardware acceleration reported.
 
Last edited:

GDO

Member
Licensed User
Longtime User
Hi,

hardware Accelerated is OK BUT it seems that opengl2 doesn't use double buffers.

I don't kow why ...
 

agraham

Expert
Licensed User
Longtime User
From what I can see from Googling GLSurfaceView automatically double-buffers. See the second post here screen flickering in GLSurfaceView - android-discuss for a description of how it works. Are you not completely redrawing each frame? This can cause flicker as described in that link.
 

GDO

Member
Licensed User
Longtime User
Yes I read it, too but what I see on my device, makes me think there is a trouble with double-buffers.

In my OnDraw event, I clear color and depth, draw my rotating triangle and that's all.

And on my device from, time to time, you can see the new triangle (this one is ok) AND a piece of the old one (not OK) as if the screen was not clear before the drawing.
 

GDO

Member
Licensed User
Longtime User
Don't forget I am a newbie ...
 

Attachments

  • OGL2-TRSTt.zip
    4.3 KB · Views: 304

agraham

Expert
Licensed User
Longtime User
I have been playing with various combinations of speed and colours for about an hour now. I see the effect but I wonder if it is an artefact of persistence of vision.

If you speed it up by setting the Timer interval to 10mS then you see lots of triangle points.

If you slow it down by setting the timer to 1000mS and set the background to black or white then you don't really notice any effect but when the background is gray it obvious but, to me, only on the red and the blue points but not on the green point of the large triangle. If you make the small triangle white or green I see no effect but if it is blue then I see it.
 

GDO

Member
Licensed User
Longtime User
Yes, I wonder too.

I don't know if the trouble is about my display devices.
But I tried other demo (not in b4a and from better programmer than I am) which run fine on my display devices.

I'll try to make a better demo and see if the problem is still there.

Thanks.
 

GDO

Member
Licensed User
Longtime User
Hello,

I am playing with textures but nothing is working ...
I have 3 questions/remarks about the library

1- gActiveTexture should be glActiveTexture
2-glCompressedTexImage2D and glCompressedTexSubImage2D are defined with java.nio.Buffer

and the most important point,

3- with glTexImage2D (target As Int, level As Int, bitmap As android.graphics.Bitmap, border As Int), how do you manage the missing parameters (internalFormat, width, height, format and type) ?

from here : glTexImage2D - OpenGL 4 Reference Pages


Thank you
 

agraham

Expert
Licensed User
Longtime User
gActiveTexture should be glActiveTexture
Oops!, sorry I will fix it.
2-glCompressedTexImage2D and glCompressedTexSubImage2D are defined with java.nio.Buffer
Like those other method that I left needing a Buffer, those fall into the class of needing someone who knows what they are doing to tell me what type you want to use. So what types of arrays do you want pass?
with glTexImage2D (target As Int, level As Int, bitmap As android.graphics.Bitmap, border As Int), how do you manage the missing parameters (internalFormat, width, height, format and type) ?
As Informatix has pointed out Android uses OpenGl ES. There is a class in Android called GLUtils of which the documentations says "Utility class to help bridging OpenGL ES and Android APIs.". With my prevailing ignorance all things OpenGL I exposed
B4X:
texImage2D(int target, int level, int internalformat, Bitmap bitmap, int type, int border)
and
texSubImage2D(int target, int level, int xoffset, int yoffset, Bitmap bitmap)
and again was waiting feedback from anyone trying to use them to see if any more of the GLUtils methods were needed. This library does nothing clever at all - that's a bit beyond me! It's a very thin wrapper to expose the API calls to Basic4android so there should be almost a one to one correspondence between this library methods and those in the Android API documentation
 

GDO

Member
Licensed User
Longtime User
Hello,

1- ;-)

2- glCompressedTexImage2D and glCompressedTexSubImage2D
I am not using them, for now, but I'll ask you for modifications as soon as possible, if you don't mind.

3- You're right, Android + Opengl ES is not Opengl.
I have to read the doc more carrefully.

I can now see my texture on my square.

Thank you for your help.
 

GDO

Member
Licensed User
Longtime User
Hello,

1-
I can test my programs on 3 devices (Samsung Galaxy Notes, Nexus 7 and Asus Transformer TF300).

My first program (colored triangle with OGL2) runs OK on the 3 devices .
My second program (square with one texture OGL2) doesn't run on my Asus but runs OK on the 2 others devices.

Do you know if there is something special to do with OGL2 and Asus ?
Could you, please, try my program ?

2-
Could you add this, in Matrix2 :
// Create a camera view matrix
Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
( from OpenGL | Android Developers )

Thank you.
 

Attachments

  • OGL2Bitmap.zip
    40.9 KB · Views: 302
Top