' 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