[PART. SOLVED] Resuming openGL application -> no textures

Problems with Canvas, OpenGL, etc...

[PART. SOLVED] Resuming openGL application -> no textures

Postby Quipeace » Wed Sep 22, 2010 5:41 pm

Hello all,

I'm currently working on an openGL launcher and it's all working pretty well so far. However, I ran into a little problem I can't figure out.

Here's the renderer:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class LauncherRenderer implements GLSurfaceView.Renderer
  2. {
  3.         static int screenSize[] = new int[2];
  4.         static float virtualSize[] = new float[2];
  5.         shortcutTile shortcut[] = new shortcutTile[150];
  6.         ScreenDynamics dynamics;
  7.  
  8.         public static ArrayList<AppInfo> appInfos;
  9.        
  10.         long start = 0;
  11.         long current = 0;
  12.         int frames = 0;
  13.         float screenY = 0;
  14.  
  15.         float yPadding = 0;
  16.         float xPadding = 0;
  17.         float tileSpacingX = 0;
  18.         float tileSpacingY = 0;
  19.        
  20.         final int nAppsX = 4;
  21.         final int nAppsY = 5;
  22.        
  23.         @Override
  24.         public void onDrawFrame(GL10 gl)
  25.         {
  26.                 getFPS(true);
  27.                
  28.                 gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  29.                 gl.glLoadIdentity();
  30.                
  31.                 screenY = dynamics.getCurrentYCoord();
  32.                 gl.glTranslatef(0, screenY, 0);
  33.                 gl.glTranslatef(xPadding, yPadding, -1);
  34.  
  35.                 int x = 0;
  36.                 for (int i = 0; i < appInfos.size(); i++)
  37.                 {
  38.                         if (x == 400)
  39.                         {
  40.                                 gl.glTranslatef(-tileSpacingX*nAppsX, tileSpacingY , 0);
  41.                                 x = 0;
  42.                         }
  43.                         gl.glBindTexture(GL10.GL_TEXTURE_2D, appInfos.get(i).texID);
  44.                         shortcut[i].draw(gl);
  45.                         gl.glTranslatef(tileSpacingX, 0, 0);
  46.                         x += 100f;
  47.                 }
  48.                 frames++;
  49.         }
  50.  
  51.         @Override
  52.         public void onSurfaceChanged(GL10 gl, int w, int h)
  53.         {
  54.                 gl.glMatrixMode(GL10.GL_PROJECTION);
  55.                 float ratio = (float) w / (float) h;
  56.                 screenSize[0] = w;
  57.                 screenSize[1] = h;
  58.                 virtualSize[0] = 400;
  59.                 virtualSize[1] = (float) 400.0f / (float) ratio;
  60.                 gl.glOrthof(0.0f, virtualSize[0], virtualSize[1], 0.0f, 0.01f, 100.0f);
  61.                 gl.glViewport(0, 0, screenSize[0], screenSize[1]);
  62.                
  63.                 tileSpacingX = (virtualSize[0]/nAppsX);
  64.                 tileSpacingY = (virtualSize[1]/nAppsY);
  65.                 xPadding = (tileSpacingX-70)/2;
  66.                 yPadding = (tileSpacingY-70)/2;
  67.                 dynamics.setLimits((int) ((int) (Math.ceil(appInfos.size()/nAppsX)*tileSpacingY)-virtualSize[1]+tileSpacingY), 0);     
  68.                
  69.                 gl.glMatrixMode(GL10.GL_MODELVIEW);
  70.         }
  71.  
  72.         @Override
  73.         public void onSurfaceCreated(GL10 gl, EGLConfig config)
  74.         {
  75.                 start = System.currentTimeMillis();
  76.                 dynamics = new ScreenDynamics();
  77.                
  78.                 gl.glClearColor(0f, 0f, 0f, 1.0f);
  79.                 gl.glEnable(GL10.GL_CULL_FACE);
  80.                 gl.glFrontFace(GL10.GL_CCW);
  81.                 gl.glCullFace(GL10.GL_BACK);
  82.  
  83.                 gl.glEnable(GL10.GL_TEXTURE_2D);
  84.                 gl.glShadeModel(GL10.GL_SMOOTH);
  85.                 gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
  86.                 gl.glEnable(GL10.GL_BLEND);
  87.        
  88.                
  89.                 for (int i = 0; i < appInfos.size() && i < 150; i++)
  90.                 {
  91.                         appInfos.get(i).texID = loadTexture(gl, appInfos.get(i).icon);
  92.                         shortcut[i] = new shortcutTile();
  93.                 }
  94.  
  95.         }
  96.        
  97.         private static int newTextureID(GL10 gl)
  98.         {
  99.                 int[] temp = new int[1];
  100.                 gl.glGenTextures(1, temp, 0);
  101.                 return temp[0];        
  102.         }
  103.  
  104.         private int loadTexture(GL10 gl, Drawable drawable)
  105.         {
  106.                 int id = newTextureID(gl);
  107.  
  108.                 Bitmap temp = ((BitmapDrawable) drawable).getBitmap();
  109.                    
  110.                 Matrix flip = new Matrix();    
  111.                 float scaleWidth = ((float) 64) / temp.getWidth();
  112.                 float scaleHeight = ((float) 64) / temp.getHeight();
  113.                 flip.postRotate(270);
  114.                 flip.postScale(scaleWidth, -scaleHeight);      
  115.                    
  116.                 Bitmap bmp = Bitmap.createBitmap(temp, 0, 0, temp.getWidth(), temp.getHeight(), flip, true);
  117.                 temp.recycle();
  118.                 gl.glBindTexture(GL10.GL_TEXTURE_2D, id);
  119.                    
  120.                 gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
  121.                 gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
  122.                 gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
  123.                 gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
  124.                    
  125.                 GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
  126.                 bmp.recycle();
  127.                    
  128.                 return id;
  129.         }
  130. // GETTERS AND SETTERS FOLLOWING, IRRELEVANT
  131.  
Parsed in 0.044 seconds, using GeSHi 1.0.8.4


and here's my activity:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class LauncherGL extends Activity
  2. {
  3.         private LauncherView _LauncherView;
  4.     private LauncherRenderer renderer;
  5.    
  6.         static WallpaperManager wallmanager;
  7.     @Override
  8.     public void onCreate(Bundle savedInstanceState)
  9.     {
  10.         super.onCreate(savedInstanceState);        
  11.                
  12.         _LauncherView = new LauncherView(this);
  13.         renderer = _LauncherView.getRenderer();
  14.         setContentView(_LauncherView);
  15.         getAllPackages();
  16.     }
  17.    
  18.     @Override
  19.     protected void onPause()
  20.     {
  21.         super.onPause();
  22.         _LauncherView.onPause();
  23.     }
  24.     @Override
  25.     protected void onResume()
  26.     {
  27.         super.onResume();
  28.         _LauncherView.onResume();      
  29.     }
  30. // IRRELEVANT STUFF FOLLOWING
  31.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Now first of all, I'm using a small method to transform a drawable to an openGL texture I modified from the internet. One of the bitmaps called temp gets recycled, and causes problems when resuming (attempt to use recycled bitmap error). Besides just leaving it out, what would be the solution to this?

Now the main problem is that when I resume the activity (just forgetting about recycling the bitmap described above) my squares -are- drawn, but without a texture, the squares are simply transparent, showing the background beneath them. I can make the textures appear by launching the activity again without navigating away from it first, or calling "setContentView(_LauncherView);" from onResume()

I really hope that someone could help me with this, as it's the only thing holding me back at the moment.

Thank you very much,
Quipeace
Last edited by Quipeace on Thu Sep 23, 2010 6:57 am, edited 1 time in total.
Quipeace
Developer
Developer
 
Posts: 27
Joined: Tue Feb 23, 2010 4:00 pm

Top

Re: Resuming openGL application -> no textures

Postby mark@project8games.com » Wed Sep 22, 2010 8:24 pm

You're losing your textures on the OpenGL side. What you need to do, onSurfaceLost(), reset the state of your textures to unloaded (maybe add a boolean "loaded" or something to your AppInfo object). Then onSurfaceChanged(), call your loadTexturesMethod again, reset texture id and set state to loaded.
User avatar
mark@project8games.com
Developer
Developer
 
Posts: 41
Joined: Tue Mar 02, 2010 8:33 pm

Re: Resuming openGL application -> no textures

Postby Quipeace » Wed Sep 22, 2010 9:50 pm

when and where is onSurfaceLost() supposed to be called? Eclipse isn't giving me any hints :(.
With unloading I suppose you mean calling gl.glDeleteTextures(...)? Running this code from onPause doesn't seem to work :/
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public void deleteTextures()
  2.         {
  3.                 int texArray[] = new int[appInfos.size()];
  4.                
  5.                 for (int i = 0; i < appInfos.size(); i++)
  6.                         texArray[i] = appInfos.get(i).texID;
  7.        
  8.                 _gl.glDeleteTextures(texArray.length, texArray, 0);
  9.         }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Thank you for your reply by the way :)
Quipeace
Developer
Developer
 
Posts: 27
Joined: Tue Feb 23, 2010 4:00 pm

Re: Resuming openGL application -> no textures

Postby mark@project8games.com » Wed Sep 22, 2010 11:02 pm

Have you tried doing this in your onSurfaceChange() method

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  for (int i = 0; i < appInfos.size() && i < 150; i++)
  2. {
  3.      appInfos.get(i).texID = loadTexture(gl, appInfos.get(i).icon);
  4. }
  5.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
User avatar
mark@project8games.com
Developer
Developer
 
Posts: 41
Joined: Tue Mar 02, 2010 8:33 pm

Re: Resuming openGL application -> no textures

Postby Quipeace » Thu Sep 23, 2010 6:56 am

I kind of solved my problem.
I've added a couple of debug lines to track what's going on.
When I start my application I'm getting:
onCreate done
onChange done

When I move away I get:
Delete done

Now when I press the home button and launch my app from launcher list:
onCreate done
onChange done
onChange done

Now here's the thing, if I check whether the texture was loaded before, and skip the complete onChange method if they were, my textures show fine and the problem is gone. Is this what I was supposed to do in the first place? :P

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public void onSurfaceChanged(GL10 gl, int w, int h)
  2.         {
  3.                 if(!appInfos.get(0).loaded)
  4.                 {
  5.                         for (int i = 0; i < appInfos.size() && i < 150; i++)
  6.                         {
  7.                                 appInfos.get(i).texID = loadTexture(gl, appInfos.get(i).icon);
  8.                                 shortcut[i] = new shortcutTile();
  9.                         }
  10.                         Log.i("LauncherGL", "Loading Done");
  11.                         appInfos.get(0).loaded = true;
  12.                
  13.                         gl.glMatrixMode(GL10.GL_PROJECTION);
  14.                         float ratio = (float) w / (float) h;
  15.                         screenSize[0] = w;
  16.                         screenSize[1] = h;
  17.                         virtualSize[0] = 400;
  18.                         virtualSize[1] = (float) 400.0f / (float) ratio;
  19.                         gl.glOrthof(0.0f, virtualSize[0], virtualSize[1], 0.0f, 0.01f, 100.0f);
  20.                         gl.glViewport(0, 0, screenSize[0], screenSize[1]);
  21.                        
  22.                         tileSpacingX = (virtualSize[0]/nAppsX);
  23.                         tileSpacingY = (virtualSize[1]/nAppsY);
  24.                         xPadding = (tileSpacingX-70)/2;
  25.                         yPadding = (tileSpacingY-70)/2;
  26.                         dynamics.setLimits((int) ((int) (Math.ceil(appInfos.size()/nAppsX)*tileSpacingY)-virtualSize[1]+tileSpacingY), 0);     
  27.                        
  28.                         gl.glMatrixMode(GL10.GL_MODELVIEW);
  29.                         start = System.currentTimeMillis();
  30.                         longstart = System.currentTimeMillis();
  31.                         Log.i("LauncherGL", "OnChange Done");
  32.                 }
  33.         }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
Quipeace
Developer
Developer
 
Posts: 27
Joined: Tue Feb 23, 2010 4:00 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 1 guest