• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps OpenGL ES 2.0 GLES20 context outside GLSurfaceView.Renderer

noskill

Lurker
I have set up a custom GLSurfaceView and its Renderer successfully.

I have another thread that handles the game logic and I would like to be able to dynamically add new objects to the scene.

However, it appears that the OpenGL ES 2.0 GLES20 context only exists in the three methods (onSurfaceCreated, onSurfaceChanged and onDrawFrame) of GLSurfaceView.Renderer class.

How can I use GLES20 functionality in the game thread? If I do this after the Renderer is running...

int shader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);

...it will always return the value 0.

If all programs, shaders and hooks must be defined in "onSurfaceCreated()", how is it possible to dynamically add e.g. new objects with hooks (matrix, color etc.) to the scene?
 
Just create a class representing a game object and then put a list of them in your GLSurfaceView subclass. Your game object class should have a render method that takes an instance of GLES20 as its parameter. you can then perform your rendering inside of the game object's render method. Inside the onDraw() method of GLSurfaceView.Renderer interface, loop through the list of game objects, calling the render method for each one, passing the GLES20 pointer you obtained from the onDraw() method's argument as its parameter. Lastly, you can access this list of game from the game thread and add/remove them from the list as you wish.
 
Thank you for your reply. I will do as you instructed.

One another thing is not yet clear to me.

There is this function in the class that implements GLSurfaceView.Renderer:

Code:
public void onDrawFrame(GL10 unUsed) {

        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT |
                        GLES20.GL_DEPTH_BUFFER_BIT);

}

So that GLES20 is a static context that can be passed to a custom function, right?

Is this simplified example the correct way? (This example has no matrix etc. stuff.)

Code:
public void onDrawFrame(GL10 unUsed) {

        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT |
                        GLES20.GL_DEPTH_BUFFER_BIT);

        for (Object obj: objects) {

                // The draw method
                // is in the Object class.
                // The method will have
                // the GLES20 static context.

                obj.draw();

        }

}
 
Back
Top Bottom