OpenGL tiling background and some general thoughts

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

OpenGL tiling background and some general thoughts

Postby hardtofi » Wed Apr 28, 2010 3:52 pm

I've been forced over to OpenGL due to not getting enough performance using the 2D API, and I'm a bit at a loss. I've had a look at several examples and much code, like NeHe, Replica Island and Rokon. Another example I've been looking at is OpenGL tiling. I've figured out how to draw (probably not optimized) and add textures in a very basic way I suppose. One thing I haven't been able to figure out so far is how to crop the texture instead of resizing it.

My hands on example is:
I've got a large "map", background of the whole game level. I build it up from background tiles drawing on a Bitmap through a Canvas:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. bgImage = Bitmap.createBitmap(lvl.baseMap[0].length*frameW, lvl.baseMap.length, Bitmap.Config.ARGB_8888);
  3.  
  4. Canvas canvas = new Canvas(bgImage);
  5.  
  6. for (int i = 0; i < lvl.baseMap.length; i++)
  7.  
  8. {
  9.  
  10.     y = i * tileH;
  11.  
  12.     for (int j = 0; j < lvl.baseMap[i].length; j++)
  13.  
  14.     {
  15.  
  16.     x = j * tileW;
  17.  
  18.     type = lvl.baseMap[i][j];
  19.  
  20.     canvas.drawBitmap(bgTilesBitmap,
  21.  
  22.               bgTileData[type].srcRect,
  23.  
  24.               new Rect (x, y, x + bgTileData[type].width, y + bgTileData[type].height),
  25.  
  26.               null);
  27.  
  28.     }
  29.  
  30. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


The whole thing is persistent during the game so drawing every tile individually seems like a big waste, so I figure this way is a sane route to walk down. This large bitmap gives me a bit of headache though:

1. It's very likely to not be a POW 2, can I still load it up with GLUtils.texImage2D?

2. How to only draw the visible part of it? What I've tried (with small images only) is to load them into IntBuffers, together with an Intbuffer of squares then do
[syntax="java"]
gl.glVertexPointer(2, GL10.GL_FIXED, 0, mVertexBuffer);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, mTextureBuffer);
gl.glColor4f(1, 1, 1, 1);
for (int i = 0; i < yTiles*xTiles*8; i += 4) // 8 being 4 pairs of x,y for a square
{
gl.glNormal3f(0, 0, 0);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, i, 4);
}
If the vertexes are larger than the texture will it stretch, and shrink the texture if it's small. I suppose this will be the case with my map too? If I make a square covering the full screen will it shrink my map to fit, how to prevent that?

3. In general, is it possible and practical to have several layers on the screen? In my case am I thinking one for the map, which only changes if there is a scroll event, one which I draw sprites of the player/other things moving around (updated very often) and one with explosions. In my mind would that make things more optimized, I'd only need to redraw the layer having a change. My plan was to have the game 3D with background furthest back and only clear the depth of what's needed. Or is this just wishful thinking?

Sorry for the length and I'd really appreciate if someone could shed some light on these problems for me.
hardtofi
Freshman
Freshman
 
Posts: 4
Joined: Sun Apr 04, 2010 10:55 pm

Top

Postby impi » Sun May 02, 2010 12:32 am

I cannot really help you with the GL stuff, because im facing similar problems right now but my first idea of solving the multiple layers and performance problem with canvas, was very very simple. But it cost a HELL of memory.

I just loaded my map and drawed the WHOLE map into ONE bitmap, then i draw the bitmap at the camera's position.

That's it, for an 48x48 map with 16bit depth and 20x20 tiles you need ~1,75MB memory (assumed that there is no internal compression in android or other overhead). For an Tower Defense game i think thats okay, or for an RPG that has small levels.

This approach gives me 60fps, smooth scrolling and so much layers i want. With this approach you can have 1000 layers, it wont matter at all, it would just increase the loading time.

But then you must be really careful with the memory, because it's just an mobile phone. I think i have read something about that you can use ~12MB memory effectivly. Because of this i try to figure out how to speed up the direct drawing with opengl, but i fail at this :|

Another sidenote of this is, if your map is not changing you dont need the TextureAtlas anymore, and spare memory because of this, so the tradeoff is not that hard.

If your map has animated water or something similar, this approach won't work good.
impi
Developer
Developer
 
Posts: 29
Joined: Wed Apr 28, 2010 11:17 pm

Top

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

Who is online

Users browsing this forum: No registered users and 3 guests