I have an Android application with two actions, A and B. The application starts with A, then I press the screen to switch to B. B is displayed correctly, then I press the back button on my phone to return to A. Now the action runs correctly, except that I do not see my textures. The onResume function calls the GLSurfaceView onResume method, it calls my render on onSurfaceCreated, then onSurfaceChanged. After that, onDrawFrame calls each frame, but it only clears the screen with the specified color. I know that GLSurfaceView onPause destroys its contents, and onResume should rebuild it, but this will not work for me :(
My code is:
Rendering tool:
public class GlRenderer implements Renderer {
private Context context;
private CScene scene;
long mLastTime;
public GlRenderer(Context context, CScene scene) {
this.context = context;
this.scene=scene;
}
@Override
public void onDrawFrame(GL10 gl) {
long now = System.currentTimeMillis();
if (mLastTime > now) return;
float dt = (float) ((now - mLastTime) / 1000.0);
mLastTime = now;
scene.Update(dt);
scene.Draw(gl);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
if(height == 0) {
height = 1;
}
gl.glViewport(0, 0, width, height);
gl.glLoadIdentity();
gl.glMatrixMode(GL10.GL_PROJECTION);
GLU.gluOrtho2D(gl, 0, width, height, 0);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
scene.LoadTextures(gl);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
}
}
My Sprite class:
public class Sprite {
private FloatBuffer vertexBuffer;
private FloatBuffer textureBuffer;
private float texture[] = new float[8];
private int[] textures = new int[1];
private float width;
private float height;
private float x;
private float y;
public Sprite(float _width, float _height, float xpos, float ypos){
this(_width,_height,xpos,ypos,1.0f,1.0f);
}
public Sprite(float _width, float _height, float xpos, float ypos, float tex_width, float tex_height) {
}
public void loadGLTexture(GL10 gl, Context c, Bitmap bitmap) {
gl.glGenTextures(1, textures, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
}
public void draw(GL10 gl) {
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glFrontFace(GL10.GL_CW);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glLoadIdentity();
gl.glTranslatef((float)x, (float)y, 0);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0,4);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
}
My scene class:
public class CScene{
Context context;
public String name;
protected Activity activity;
public CScene(Context _context, Activity activity, String name){
context=_context;
this.name=name;
this.activity=activity;
}
public void Update(float dt){
}
public void Draw(GL10 gl){
}
public boolean TapControl(MotionEvent event)
{
return true;
}
public void LoadTextures(GL10 gl) {
}
}
:
GLSurfaceView, GLSurfaceView . , . GLSurfaceView, LoadTextures (from onSurfaceChagned), loadGLTexture. GlSurfaceView Draw Draw onDrawFrame, Draw Draw Sprite Draw.
//