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,578
  • OpenGL2_v1.5_lib.zip
    38.2 KB · Views: 741
Last edited:

NeoTechni

Well-Known Member
Licensed User
Longtime User
I get a blank screen on my device, Xperia Play

i get, Testing Queue event, and 60 frames shown
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
Could you find the API needed to poll the states of hardware buttons?
We cant make realtime games using just the keypress event...
 

GDO

Member
Licensed User
Longtime User
glBufferData (target As Int, size As Int, data As java.nio.Buffer, usage As Int)

Hello,

Could you please tell me how to define data as java.nio.Buffer ?

Thank you
 

agraham

Expert
Licensed User
Longtime User
I know nothing about OpenGL but I exposed as much of Android's OpenGL capability as I could in the OpenGL and OpenGL2 libraries in the hope that someone with OpenGL experience could help me get them working properly. This happened with the OpenGL library, thanks to Jim Brown and others. Unfortunately however in the twenty months since no-one, until now, has shown any interest in using this library. Either that or it's working perfectly well for them! :)

In this library glBufferData, glBufferSubData, glCompressedTexImage2D and glCompressedTexSubImage2D all want a java.nio.Buffer. Buffer is the superclass of a set of typed buffers, ByteBuffer, IntBuffer, FloatBuffer etc.

Some of the OpenGL methods require a specific type of buffer and in this case I have made the method I exposed accept the required type of array and converted the array to a buffer of the required type.

I think that the reason the calls above specify the base Buffer type of parameter is that they can then accept various types of typed buffers depending on the required use, about which I could not find any information. I left these methods exposed using the Buffer type in the hope that an OpenGL expert would tell me what is required for these methods and I could then provide variations of the methods that accept arrays of the required types.
 

GDO

Member
Licensed User
Longtime User
Hi,
An array of GL_FLOAT (FLOAT) will be a good beginning.
Could you, please ?
 

GDO

Member
Licensed User
Longtime User
Hi,
Thanks for the new version.
I am trying to use Opengl es 2 but it seems to me that something is wrong with glVertexAttribPointer.

You define it like that : glVertexAttribPointer (
indx As Int,
size As Int,
normalized As Boolean,
stride As Int,
ptr() As Float)

but it should be (I think) like that :void glVertexAttribPointer(
GLuint index,
GLint size,
GLenum type,
GLboolean normalized,
GLsizei stride,
const GLvoid * pointer);

GLenum type is missing and
const GLvoid * pointer (often 0) is not possible with ptr() As Float

Am I right ?

Thank you for your help.
 

agraham

Expert
Licensed User
Longtime User
The Android definition of glVertexAttribPointer is
B4X:
public void  glVertexAttribPointer(int indx, int size, int type, boolean normalized, int stride, Buffer ptr)

Because Basic4android cannot cope with buffers I implemented it to accept a float array and pass a FloatBuffer
B4X:
   public void glVertexAttribPointer(int indx, int size, boolean normalized, int stride, float[] ptr)
   {
      ByteBuffer pbb = ByteBuffer.allocateDirect(ptr.length * 4);
      pbb.order(ByteOrder.nativeOrder());
      FloatBuffer pfb = pbb.asFloatBuffer();
      pfb.put(ptr);
      pfb.position(0);
      GLES20.glVertexAttribPointer(indx, size, GL_FLOAT, false, 0, pfb);
   }
It looks like passing "false" rather passing the normalised argument is a bug.

As Java doesn't allow pointer manipulation you can't pass 0 like in C. You could try either Null or a zero length array.
 
Last edited:

GDO

Member
Licensed User
Longtime User
Hello,
It seems to me that "int offset" is defined, too :
Added in API level 9
public static void glVertexAttribPointer (int indx, int size, int type, boolean normalized, int stride, int offset)

For now, nothing is working, for me, with "Buffer ptr" :

in API level 8
public static void glVertexAttribPointer (int indx, int size, int type, boolean normalized, int stride, Buffer ptr)

Could you change/add glVertexAttribPointer with "int offset", please.
 

agraham

Expert
Licensed User
Longtime User
In version 1.2 now posted the previous version of glVertexAttribPointer is corrected and named glVertexAttribPointerF

B4X:
public void glVertexAttribPointerF(int indx, int size, boolean normalized, int stride, float[] ptr)
   {
      ByteBuffer pbb = ByteBuffer.allocateDirect(ptr.length * 4);
      pbb.order(ByteOrder.nativeOrder());
      FloatBuffer pfb = pbb.asFloatBuffer();
      pfb.put(ptr);
      pfb.position(0);
      GLES20.glVertexAttribPointer(indx, size, GL_FLOAT, normalized, stride, pfb);
   }

A new glVertexAttribPointer method is added.
B4X:
public void glVertexAttribPointer(int indx, int size, int type, boolean normalized, int stride, int offset)
   {
      GLES20.glVertexAttribPointer(indx, size, type, normalized, stride, offset);
   }
 

GDO

Member
Licensed User
Longtime User
Thank you for the change.

There is a problem with buffer :

main_v7 (java line: 317)
java.lang.IllegalArgumentException: remaining() < size


at android.opengl.GLES20.glBufferData(Native Method)
at anywheresoftware.b4a.agraham.opengl2.GLWrapper2.glBufferDataF(GLWrapper2.java:470)


at gdo.opengl2.main._v7(main.java:317)
at gdo.opengl2.main._glsv_surfacecreated(main.java:477)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:155)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:151)
at anywheresoftware.b4a.agraham.opengl2.GLWrapper2$GLSurfaceViewWrapper2$myRenderer.onSurfaceCreated(GLWrapper2.java:1668)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1348)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1118)
java.lang.IllegalArgumentException: remaining() < size



java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()



at android.os.Handler.<init>(Handler.java:121)
at android.app.Dialog.<init>(Dialog.java:101)
at android.app.AlertDialog.<init>(AlertDialog.java:63)
at android.app.AlertDialog.<init>(AlertDialog.java:59)
at android.app.AlertDialog$Builder.create(AlertDialog.java:786)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:204)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:155)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:151)
at anywheresoftware.b4a.agraham.opengl2.GLWrapper2$GLSurfaceViewWrapper2$myRenderer.onSurfaceCreated(GLWrapper2.java:1668)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1348)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1118)
** Activity (main) Pause, UserClosed = true **


Thanks
 

GDO

Member
Licensed User
Longtime User
It's : gl.GL_FLOAT * 3*3

gl.glBufferDataF(gl.GL_ARRAY_BUFFER, gl.GL_FLOAT * 3*3, triVertices, gl.GL_STATIC_DRAW)

where triVertices is Dim triVertices(9) As Float
 
Top