Drawing a 3D colored pyramid making use of
gl.glDrawElements in place of
gl.glDrawArrays:
[CODE lang="b4x" title="B4A"]#Region Module Attributes
#FullScreen: False
#IncludeTitle: True
#ApplicationLabel: OpenGLIndicesPyramid
#VersionCode: 1
#VersionName:
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim Timer1 As Timer
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim glsv As GLSurfaceView
Dim dosetup As Boolean = False
Dim vertices() As Float
Dim indices() As Short
Dim color() As Float
Dim lightAmbient() As Float
Dim lightDiffuse() As Float
Dim lightPosition() As Float
Dim lightDirection() As Float
Dim rot As Float = 0.0f
End Sub
Sub Timer1_Tick
' eyeangle=eyeangle+0.1
' If eyeangle>(2*3.14159) Then eyeangle=eyeangle-(2*3.14159)
glsv.RequestRender
rot = rot + 1.0f
End Sub
Sub Activity_Create(FirstTime As Boolean)
glsv.Initialize(glsv.RENDERMODE_WHEN_DIRTY, "glsv")
Activity.AddView(glsv,0,0,Activity.Width,Activity.Height)
End Sub
Sub Activity_Resume
'glsv.DebugFlags = glsv.DEBUG_CHECK_GL_ERROR + glsv.DEBUG_LOG_GL_CALLS ' Can set debug flags here if required
glsv.Resume ' Must call GLSurfaceView.Resume to restart the rendering thread
Timer1.Initialize("Timer1", 20)
Timer1.Enabled = True
End Sub
Sub Activity_Pause (UserClosed As Boolean)
glsv.Pause ' Must call GLSurfaceView.Pause to halt the rendering thread
Timer1.Enabled = False
End Sub
' The GLSurfaceView events run on a separate thread to the main thread.
' They therefore must not try to manipulate GUI elements.
' The OpenGL library does not allow the debugger to halt at code within these event Subs.
Sub glsv_Draw(gl As GL1)
'The view wants to be drawn using the suppllied GL10
If dosetup = True Then
ClearStates(gl)
'define cube vertices, indices, colors
vertices = Array As Float( 0.0f, 0.0f, -2.0f, _
1.0f, 1.0f, 0.0f, _
-1.0f, 1.0f, 0.0f, _
-1.0f, -1.0f, 0.0f, _
1.0f, -1.0f, 0.0f, _
1.0f, 1.0f, 0.0f)
indices = Array As Short(3, 2, 5, _
4, 3, 5, _
0, 1, 2, _
0, 2, 3, _
0, 3, 4, _
0, 4, 1)
color = Array As Float(0.0f, 1.0f, 1.0f, 1.0f, _
1.0f, 0.0f, 0.0f, 1.0f, _
1.0f, 1.0f, 0.0f, 1.0f, _
0.0f, 1.0f, 0.0f, 1.0f, _
0.0f, 0.0f, 1.0f, 1.0f, _
1.0f, 0.0f, 1.0f, 1.0f, _
1.0f, 1.0f, 1.0f, 1.0f, _
0.0f, 1.0f, 1.0f, 1.0f)
lightAmbient = Array As Float(0.1f, 0.3f, 0.3f, 0.1f)
lightDiffuse = Array As Float(0.7f, 0.7f, 0.7f, 1.0f)
lightPosition = Array As Float(1.0f, 2.0f, 0.5f, 2.0f)
lightDirection = Array As Float(0.0f, 1.0f, 0.5f)
dosetup = False
End If
gl.glFrontFace(gl.GL_CCW)
gl.glClearColor(0.0f, 0.0f, 0.3f, 0.0f)
gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
gl.glEnableClientState(gl.GL_COLOR_ARRAY)
gl.glClear(Bit.Or(gl.GL_COLOR_BUFFER_BIT, gl.GL_DEPTH_BUFFER_BIT))
Reset(gl)
gl.glTranslatef(0.0f,0.0f,-12.0)
gl.glRotatef(rot, 1.0f, 0.4f, 0.5f)
gl.glVertexPointerf(3,vertices)
gl.glColorPointerf(4, 0, color)
gl.glDrawElements(gl.GL_TRIANGLES,indices.Length,indices)
gl.glLightfv(gl.GL_LIGHT0, gl.GL_AMBIENT, lightAmbient, 0)
gl.glLightfv(gl.GL_LIGHT0, gl.GL_DIFFUSE, lightDiffuse, 0)
gl.glLightfv(gl.GL_LIGHT0, gl.GL_POSITION, lightPosition, 0)
gl.glLightfv(gl.GL_LIGHT0, gl.GL_SPOT_DIRECTION, lightDirection, 0)
gl.glMaterialf(gl.GL_FRONT, gl.GL_SHININESS, 180)
gl.glMaterialf(gl.GL_BACK, gl.GL_SHININESS, 90)
gl.glMaterialf(gl.GL_FRONT_AND_BACK, gl.GL_SHININESS, 45)
gl.glDisableClientState(gl.GL_VERTEX_ARRAY)
gl.glDisableClientState(gl.GL_COLOR_ARRAY)
End Sub
Sub glsv_SurfaceChanged(gl As GL1, width As Int, height As Int)
'Called when the surface has changed size.
gl.glViewport(0, 0, width, height)
gl.glMatrixMode(gl.GL_PROJECTION)
gl.glLoadIdentity
Dim aspect As Float = width/height 'the width / height is propotional to the width and height of the GLSURFACEVIEW
gl.gluPerspective(45.0f, aspect, 0.1f, 100.0f) 'this will make the square look square rather than rectangular should SV width and height not be the same
gl.glMatrixMode(gl.GL_MODELVIEW)
dosetup = True
End Sub
Sub glsv_SurfaceCreated(gl As GL1)
'Called when the surface is created or recreated.
gl.glClearDepthf(1.0f)
gl.glEnable(gl.GL_DEPTH_TEST)
gl.glDepthFunc(gl.GL_LEQUAL)
gl.glHint(gl.GL_PERSPECTIVE_CORRECTION_HINT, gl.GL_NICEST)
gl.glShadeModel(gl.GL_SMOOTH)
gl.glDisable(gl.GL_DITHER)
dosetup = True
End Sub
Sub Reset(gl As GL1)
gl.glLoadIdentity
gl.glScalef(0.5,0.5,0.5)
End Sub
Sub ClearStates(gl As GL1)
gl.glDisable(gl.GL_TEXTURE_2D)
gl.glDisable(gl.GL_BLEND)
gl.glDisableClientState(gl.GL_VERTEX_ARRAY)
gl.glDisableClientState(gl.GL_COLOR_ARRAY)
gl.glDisableClientState(gl.GL_TEXTURE_COORD_ARRAY)
gl.glDisableClientState(gl.GL_NORMAL_ARRAY)
End Sub[/CODE]