I am not new to programming (10+ years) but certainly new to OpenGL. I am trying to create a FPS meter that will display itself in a GLSurfaceView .. so in the Renderer I created a function to calculare FPS..
Using java Syntax Highlighting
- // member variable
- private List<Long> _stamps;
- // in constructor
- public StaticTriangleRenderer(Context context)
- {
- // snipped code
- _stamps = new ArrayList<Long>();
- }
- // function to calculate fps, called on each onDrawFrame
- private int calculateFps()
- {
- long now = new Date().getTime();
- _stamps.add(now);
- now -= 1000;
- long stamp = _stamps.get(0);
- while(stamp < now)
- {
- _stamps.remove(0);
- stamp = _stamps.get(0);
- }
- return _stamps.size();
- }
- // function to generate fps output "texture"
- protected Bitmap createFPSBitmap()
- {
- Bitmap bmp = Bitmap.createBitmap(128, 128, Config.ARGB_4444); // Config.ARGB_4444);
- Canvas canvas = new Canvas(bmp);
- Paint paint = new Paint();
- paint.setColor(0xFFFFFFFF);
- paint.setTextSize(32);
- canvas.drawText("" + calculateFps()+ " fps", 0, 16, paint);
- return bmp;
- }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
Now, I tried to reassign the texture using GLUtils.texImage2D in onDrawFrame, I understand this is far from a best practice but it would help me a lot if I could understand why it crashes after the second time.. I ended up doing the following inside of onSurfaceCreated. I copied the full code below so you can see the whole context..
Using java Syntax Highlighting
- public void onSurfaceCreated(GL10 gl, EGLConfig config)
- {
- /*
- * By default, OpenGL enables features that improve quality
- * but reduce performance. One might want to tweak that
- * especially on software renderer.
- */
- glDisable(GL_DITHER);
- /*
- * Some one-time OpenGL initialization can be made here
- * probably based on features of this particular context
- */
- glHint(GL_PERSPECTIVE_CORRECTION_HINT,
- GL_FASTEST);
- glClearColor(.5f, .5f, .5f, 1);
- glShadeModel(GL_SMOOTH);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_TEXTURE_2D);
- /*
- * Create our texture. This has to be done each time the
- * surface is created.
- */
- int[] textures = new int[1];
- glGenTextures(1, textures, 0);
- mTextureID = textures[0];
- glBindTexture(GL_TEXTURE_2D, mTextureID);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
- GL_NEAREST);
- glTexParameterf(GL_TEXTURE_2D,
- GL_TEXTURE_MAG_FILTER,
- GL_LINEAR);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
- GL_CLAMP_TO_EDGE);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
- GL_CLAMP_TO_EDGE);
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
- GL_REPLACE);
Parsed in 0.041 seconds, using GeSHi 1.0.8.4
This is where the faulty code starts..
Using java Syntax Highlighting
- Log.i("Renderer", "1st texture assignment");
- Bitmap bmp = createFPSBitmap();
- GLUtils.texImage2D(GL_TEXTURE_2D, 0, createFPSBitmap(), 0);
- bmp.recycle();
- // clean up after, not sure this is necessary
- glDeleteTextures(1, textures, 0);
- glGenTextures(1, textures, 0);
- glBindTexture(GL_TEXTURE_2D, mTextureID);
- Log.i("Renderer", "2nd texture assignment");
- bmp = createFPSBitmap();
- GLUtils.texImage2D(GL_TEXTURE_2D, 0, bmp, 0);
- bmp.recycle();
- Log.i("Renderer", "textureId i:"+textures[0]);
- Log.i("Renderer", "3rd texture assignment");
- bmp = createFPSBitmap();
- GLUtils.texImage2D(GL_TEXTURE_2D, 0, createFPSBitmap(), 0);
- bmp.recycle();
- glDeleteTextures(1, textures, 0);
- glGenTextures(1, textures, 0);
- glBindTexture(GL_TEXTURE_2D, mTextureID);
- Log.i("Renderer", "4th");
- bmp = createFPSBitmap();
- GLUtils.texImage2D(GL_TEXTURE_2D, 0, createFPSBitmap(), 0);
- bmp.recycle();
- glDeleteTextures(1, textures, 0);
- glGenTextures(1, textures, 0);
- glBindTexture(GL_TEXTURE_2D, mTextureID);
- Log.i("Renderer", "5th");
- bmp = createFPSBitmap();
- GLUtils.texImage2D(GL_TEXTURE_2D, 0, createFPSBitmap(), 0);
- bmp.recycle();
- glDeleteTextures(1, textures, 0);
- glGenTextures(1, textures, 0);
- glBindTexture(GL_TEXTURE_2D, mTextureID);
- Log.i("Renderer", "6th");
- bmp = createFPSBitmap();
- GLUtils.texImage2D(GL_TEXTURE_2D, 0, createFPSBitmap(), 0);
- bmp.recycle();
- glDeleteTextures(1, textures, 0);
- glGenTextures(1, textures, 0);
- glBindTexture(GL_TEXTURE_2D, mTextureID);
- Log.i("Renderer", "7th");
- bmp = createFPSBitmap();
- GLUtils.texImage2D(GL_TEXTURE_2D, 0, createFPSBitmap(), 0);
- bmp.recycle(); */
- Log.i("Renderer", "done with textures");
- }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4
For some reason it works twice, then crashes. What am I doing wrong?
Ultimately I don't want to do this in onSurfaceCreated, I want to update the texture inside of onDrawFrame so I can update the FPS meter texture to draw it on a square.
Also I tried the sample in APIDemos called "SpriteText" and it throws runtime errors.. weird.
I am testing everything on the Nexus One.
Thanks
Martin


