OpenGL ES

Kamac

Active Member
Licensed User
Longtime User
Hi.

I'm sorry if i am wrong, but i couldn't work this out.

There is OpenGL library for B4A, but what about OpenGL ES?

From what i've heard it's easier and made specially for mobile phones.

Now, i am sorry if that library for B4A made by Agraham is actually ES version, but as i said:
i couldn't work this out.


And if there is no such library, maybe someone would think of implementing it?


Thanks.
 

alfcen

Well-Known Member
Licensed User
Longtime User
Hi Kamac

The OpenGL library created by agraham wraps "OpenGL ES" which is a subset of OpenGL and the only version currently running on Android phones, as far as I am aware.

Jim Brown has been so very kind as to post numerous working samples.

Cheers
Robert
 
Upvote 0

alfcen

Well-Known Member
Licensed User
Longtime User
There are OpenGL tutorials in the internet. To be honest, I am doing lots of try-and-error work in the jungle of endless functions, but thanks to Jim's samples I am getting somewhere, whilst concentrating on the basics.
 
Upvote 0

alfcen

Well-Known Member
Licensed User
Longtime User
Hi Kamac,
The performance is formidable, I fail to recognize any difference to Java implementation.
 
Upvote 0

Jim Brown

Active Member
Licensed User
Longtime User
Kamac,

I have put this demo together for you. It's a very basic example to show the creation and drawing a square. There are lots of comments in the code to help you get started:

Grab Andrews latest library first --> OpenGL Library


B4X:
' OpenGL ES - Simple Demo - Yellow quad (square) on a violet background

' Requires Andrew Grahams OpenGL Library:
' http://www.b4x.com/forum/additional-libraries-official-updates/9036-opengl-library.html

' This demo will show how to render a basic OpenGL object on screen
' What you will see is a yellow quad (square) on a violet background
' The quad is constructed from 4 vertex coordinate points
' Each vertex is positioned in space by X/Y values **
'    ** NOTE: In this demo the Z (depth) coordinate is not required
'             because the square is 'flat' and does not need depth (Z)

' Here is how the quad is contructed and rendered
' Rendering starts at vert 0 (-1,-1) and finishes at vert 3 (1,1)
' 
'    Verts            X/Y Values         Rendering order (like backwards Z)
'   2--------3      -1,1 ----- 1, 1            ----->
'   |        |        |         |              \
'   |        |        |         |                 \
'   |        |        |         |                   \
'   0--------1      -1,1 ----- 1,-1            *---->

Sub Globals
   ' the main surfaceview holder
   Dim glsv As GLSurfaceView
   ' this array will hold the X/Y vertex coordinates of the quad (square)
   Dim verts(0) As Float
End Sub

Sub Activity_Create(FirstTime As Boolean)
   ' set up the OpenGL display - Draw mode is as "only when needed"
   ' the "glsv" is the event name part. See associated subs:
   '   glsv_Draw()
   '   glsv_SurfaceCreated()
   '   glsv_SurfaceChanged()
   glsv.Initialize(glsv.RENDERMODE_WHEN_DIRTY, "glsv")
   ' add the GL surface to the view
   Activity.AddView(glsv,0,0,100%x,100%y)
   ' define vertex coordinates for the quad (4 sets of x/y pairs)
   verts = Array As Float(-1,-1 , 1,-1 , -1,1 , 1,1)
End Sub

Sub Activity_Resume
   ' Must call GLSurfaceView.Resume to restart the rendering thread
   glsv.Resume 
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   ' Must call GLSurfaceView.Pause to halt the rendering thread
   glsv.Pause
End Sub

Sub glsv_Draw(gl As GL1)
   ' clear the display
   '' gl.glClear(Bit.Or(gl.GL_COLOR_BUFFER_BIT), gl.GL_DEPTH_BUFFER_BIT)
   gl.glClear(gl.GL_COLOR_BUFFER_BIT)
   ' set the background color of the display (purple)
   gl.glClearColor(0.5,0.2,0.5,1)
   ' set a rendering color (yellow)
   gl.glColor4f(1,1,0.2,1)
   ' reset all previous transformations back to default (rotation, postion, scaling)
   gl.glLoadIdentity
   ' move the camera back so that the square will be visible
   gl.glTranslatef(0,0,-9.0)
   ' enable the VERTEX_ARRAY since DrawArrays() is using Array buffer
   gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
   ' set pointer to the verts array
   gl.glVertexPointerf(2, verts)
   ' render the quad (square) by using a TRIANGLE_STRIP method
   ' this will produce a solid render
   gl.glDrawArrays(gl.GL_TRIANGLE_STRIP,0,4)
End Sub 

' Called whenever the GL surface is created or re-created
' You should load rescources here (textures) since these are destroyed
' whenever the surface is lost
Sub glsv_SurfaceCreated(gl As GL1)
   ' nohing to set up here for this demo
End Sub

' This routine is called everytime the surface changes size
' Rotating the device will cause such a change for example
Sub glsv_SurfaceChanged(gl As GL1, width As Int, height As Int)
   ' set the rendering area to the full screen
   gl.glViewport(0, 0, width, height)
   ' switch to orthographic projection
   gl.glMatrixMode(gl.GL_PROJECTION)
   ' reset all previous transformations back to default (rotation, postion, scaling)
   gl.glLoadIdentity
   ' calculate the correct aspect ratio (so that objects do not appear distorted)
   Dim ratio As Float : ratio = width/height
   ' set the perspective view parameters
   ' fov=45 degrees view angle
   ' near=1 how near an object can get to the front of the display **
   ' near=100 how far an object can go into the distance **
   ' ** if objects z position falls outside these ranges the object does not render
   gl.gluPerspective(45,ratio,0.5,100)
   ' switch back to 3d model view
   gl.glMatrixMode(gl.GL_MODELVIEW)
End Sub
 
Upvote 0

Victor Fung

Member
Licensed User
Jim, thank you very much for this, the absolute simplest opengl program, from which I have started to build more complex exercises. I would not have been able to get started without this.
 
Upvote 0
Top