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:

NeoTechni

Well-Known Member
Licensed User
Longtime User
I tried to run the demo and got:

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
Anything else to do to have it transparent ?
It's probably because you are using the emulator which does not do OpenGL very well, or not at all for OpenGL 2.0, I believe.:(

Using gl.glClearColor(1, 0, 0, 0.3) on my Blade with Initialize gives a bright red background and with Initialize2 it gives a dark maroon background so it does seem to behave as I would expect with Initialize2 respecting alpha values. I don't know enough OpenGL to test it further - I have tried to change the demo cube color and failed dismally so I'll leave further testing to the experts!

There is a useful little app in the market called glInfo which displays a lot of arcane stuff about the device OpenGL cpabilities. I used it to check the supported display configs on my Blade and Xoom.
 

Jim Brown

Active Member
Licensed User
Longtime User
Hi folks

I am converting some of the excellent Nehe OpenGL tutorials at the moment. Although the demos appear to work fine on the emulator I am getting a black screen on my Galaxy S

Would others please test the attached? Any clues where I am going wrong?

EMULATOR SCREEN GRAB
Nehe01.png



For reference the Nehe tutorials are here - NEHE OPENGL TUTORIALS


.
 

Attachments

  • NeheDemo01.zip
    6.6 KB · Views: 368

agraham

Expert
Licensed User
Longtime User
It also drew a black screen on my ZTE Blade so although I know nothing about OpenGL by substituting bits of the demo (that works) with bits of your code I found that putting

gl.glMatrixMode(gl.GL_MODELVIEW)

in the drawing code made things work! I have no idea what it does but it seems important! :)

EDIT :- The order of these three lines of code in the glsv_SurfaceChanged event seems important, I've no idea why!
B4X:
   gl.glMatrixMode(gl.GL_PROJECTION)
   gl.gluPerspective(45.0,ratio,0.5,100.0)
   gl.glMatrixMode(gl.GL_MODELVIEW)
 
Last edited:

Jim Brown

Active Member
Licensed User
Longtime User
I have tided the Nehe demo up a bit and added a 3D spinning pyramid and cube

To avoid polygon fighting I constructed the cube as per the method shown in the image below. All faces are made up of the same zigzag "S" construction. Start at the bottom/left, then go to the bottom/right, next to top/left, and finally top/right

cube3D.png


See "05 - 3D Shapes" in the example attached

Also below is a reference coordinate system to show how verts are placed in 3D space

GLCoords.png
 

Attachments

  • NeheDemo02.zip
    7 KB · Views: 351
Last edited:

alfcen

Well-Known Member
Licensed User
Longtime User
Hey Jim,

This is fantastic!
My knowledge of OpenGL is absolute zero and I feel lost in the jungle of agraham's library functions, while I do respect his work.

Your practical approaches look great and perform fast on my Sharp IS03.

I wonder whether you are planning to try Nehe lesson 23:
NeHe Productions: OpenGL Lesson #23

By this it should be possible to wrap a texture, such as a world map, around a spherical globe and rotate it around XYZ axes. In my age it would probably take me months to get around this adventure, so I'd bette leave it up to the youth :sign0188:

Cheers
Robert
 

Jim Brown

Active Member
Licensed User
Longtime User
Thanks Robert. I'm only just getting my head around OpenGL ES (having dabbled only with OpenGL previously). Example 23 looks doable. I'm just trying to get Example 06 working at the moment (textures)

Graham,
Can you check glBindTexture (target As Int, texture As Int)
Should texture be a pointer instead of an int?
Most examples I have seen indicate a pointer is required
 

agraham

Expert
Licensed User
Longtime User
Can you check glBindTexture (target As Int, texture As Int)
Thats's correct for the Android implementation of OpenGL ES. Unfortunately the Android documentation gives no help whatsoever apart from the method signatures :( so I can't tell how it is intended to be used. The documentation unhelpfully states

A summary of how to actually write 3D applications using OpenGL is beyond the scope of this text and is left as an exercise for the reader.
Dead useful eh! It's the same for the OPenGL ES 2.0 documentation as well.
 

agraham

Expert
Licensed User
Longtime User
Robert. Be aware of what I said in my post #34 here.
Android implements OpenGL ES 1.0 with some capabilities from OpenGL ES 1.1. OpenGL ES is a simplified version of OpenGL for embedded systems.
The link you posted describes the full OpenGL so you will find some differences - like no Quads in OpenGL ES!
 

alfcen

Well-Known Member
Licensed User
Longtime User
Thanks Andrew, today, I failed indeed to find certain functions necessary to implement drawing of spheres, etc. It is not possible with OpenGL ES only.
Yet, some developpers managed a rotating Earth on Android like the app TerraTime.
What engine would they probably use?
 

Jim Brown

Active Member
Licensed User
Longtime User
Graham,

Here's a snippet of Android code I found which may shed some light on how BindTexture() is used:
B4X:
   /** Our texture pointer */
   private int[] textures = new int[1];

.....

      //Generate one texture pointer...
      gl.glGenTextures(1, textures, 0);
      //...and bind it to our array
      gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

See example 06 from Insanity Design

Any chance you can make a temporary 'GLBindTexzture', say for example GLBindTexture2(target as Int,texture() As Int)

I am trying to get a textured quad to appear but no luck at all so far. If you need an example to play around with let me know, I will knock something up
 

agraham

Expert
Licensed User
Longtime User
Any chance you can make a temporary 'GLBindTexzture', say for example GLBindTexture2(target as Int,texture() As Int).
I don't understand what that function would do, even if I could which I can't. As I understand it from a brief Google glBindTexture associates a single texture with a texture ID so why would it need an array?

I think you need to load the texture using glTexImage2D or glTexSubImage2D, they are the only calls I can find that use bitmaps. I found something like this Googling for "android glBindTexture".

Dim tmp(1) As Int ' one texture
gl.glGenTextures(1, tmp, 0) ' get array filled with texture IDs
gl.glBindTexture(gl.GL10.GL_TEXTURE_2D, tmp(0)) ' bind the texture to an ID
glTexImage2D(gl.GL10.GL_TEXTURE_2D, 0, bmp, 0) ' load the texture
 

Jim Brown

Active Member
Licensed User
Longtime User
Magic Graham. That's the solution. Passing tex(0) as a pointer. I was trying to pass the ID as an int
I have updated the Nehe demo with a texture example now. Happy days!

See NeheDemo3.zip attached. Example 06 should show a textured crate (as a 3D cube)
 

Attachments

  • NeheDemo03.zip
    49.7 KB · Views: 396
Last edited:
Top