I'm having trouble to change my rendered mesh in my GLSurfaceView, and more precisely in the Renderer, I guess there is something I didn't understood in the workflow of the Renderer class.
So, let's consider we have this :
public class MyRenderer extends GLSurfaceView.Renderer{
private Mesh aMesh;
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config){
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
aMesh = new Mesh("TestMesh1");
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
...Creating viewport...
}
@Override
public void onDrawFrame(GL10 gl) {
GLES20.glClear(gl.GL_COLOR_BUFFER_BIT);
...all Maths to create mvpMatrix...
aMesh.draw(mvpMatrix);
}
}
So this is perfectly working, aMesh is display on the screen with all the good settings. Now, I want to change this mesh when the user is pressing a button, and to refresh my renderer I created a specific method in the renderer, as follow :
public class MyRenderer extends GLSurfaceView.Renderer{
private Mesh aMesh;
public void changeMesh(String newMeshName){
GLES20.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);
....
aMesh=new Mesh(newMeshName);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config){
....
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
...
}
@Override
public void onDrawFrame(GL10 gl) {
aMesh.draw(mvpMatrix);
}
}
So in my mind, calling changeMesh would make onDrawFrame method to redraw aMesh with the new model, right? My problem is that the result is an empty renderer. It's not even crashing or so. When I call changeMesh(), the previously well displayed mesh disappear (as expected), but the new one is not draw.
So I'm wondering, does it requires to create a new Renderer(which seems a little bit heavy)? Is there a way to ask a pass in OnSurfaceCreated manually? I'm a bit lost on this bug because it's kinda unexpected.
Part of the problem was the call to new, I couldn't explain exactly the reasons, but it seems it's somehow breaking links between OpenGL Objects and my buffers. I solved the problem with a kind of "setter" which just change all datas in an instance of Mesh to suit the datas of an other Mesh, this way I could save the memory address of the first instance.
It's a bit strange but well this solved my problem, maybe it can help someone else.