Background images in opengl

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

Background images in opengl

Postby ameetnsharma » Thu Dec 10, 2009 6:58 am

What is the best way to display background images when using OpenGL (GLSurfaceView)?

The way I did it was by creating a texture with the image... and then rendering it... but this makes the performance extremely slow depending on the image size.

What other options do I have?

Thanks.
ameetnsharma
Developer
Developer
 
Posts: 30
Joined: Tue Dec 08, 2009 10:55 pm

Top

Postby hardcoras » Thu Dec 10, 2009 5:04 pm

You can use GL11 Ext function glDrawTexfOES(x, y, z, width, height).

But I think rendering backgroud quad shouldn't make a big fps drop. Just try to make your texture size near to screen resolution as much as possible.
hardcoras
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Sat Nov 14, 2009 2:31 pm
Location: Lithunia

Postby ameetnsharma » Fri Dec 11, 2009 2:14 am

hardcoras wrote:You can use GL11 Ext function glDrawTexfOES(x, y, z, width, height).

But I think rendering backgroud quad shouldn't make a big fps drop. Just try to make your texture size near to screen resolution as much as possible.


Thanks. I'm still having trouble. But there seem to be some good options for android outside opengl so I'll try that for now.
ameetnsharma
Developer
Developer
 
Posts: 30
Joined: Tue Dec 08, 2009 10:55 pm

Postby Patton » Sat Dec 12, 2009 10:19 pm

Having the same problem. I'm rendering a textured quad as background which is placed at 0.0f, 0.0f, -8.0f. The quad is covering exactly the whole screen and I get 15fps. Same quad with same texture rendered at 0.0f, 0.0f, -16.0f gives 35fps. I don't get it, what's the problem? One quad rendered in front of the camera is slower, than rendering 50 quads in the distance.

Here's the code executed at initialization:

Code: Select all
   float x = 5.5f;
   float y = 3.3f;
   
   float   vertex[] = new float[] {
         -x, y, 0,
          x, y, 0,
         -x,-y, 0,
          x,-y, 0
   };

   float   texel[] = new float[] {
         0, 0,
         1, 0,
         0, 1,
         1, 1
   };

      ByteBuffer vb = ByteBuffer.allocateDirect(vertex.length * 4);
      vb.order(ByteOrder.nativeOrder());
      vertexBuffer = vb.asFloatBuffer();
      vertexBuffer.put(vertex);
      vertexBuffer.position(0);

      ByteBuffer tb = ByteBuffer.allocateDirect(texel.length * 4);
      tb.order(ByteOrder.nativeOrder());
      texelBuffer = tb.asFloatBuffer();
      texelBuffer.put(texel);
      texelBuffer.position(0);


And this one renders it on screen:

Code: Select all
      gl.glMatrixMode(GL10.GL_MODELVIEW);
      gl.glLoadIdentity();
      gl.glTranslatef(0.0f, 0.0f, -18.0f);
      gl.glDisable(GL10.GL_BLEND);
      gl.glEnable(GL10.GL_TEXTURE_2D);
      gl.glBindTexture(GL10.GL_TEXTURE_2D, globals.scnTextures[1]);
      gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
      gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
      gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texelBuffer);
      gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
      gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertex.length / 3);


EDIT: 40fps same background quad without texture.

This is how i generate textures:

Code: Select all
      gl.glBindTexture(GL10.GL_TEXTURE_2D, tx);
      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, bmp, 0);


bmp is a 8bit image.
Patton
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sat Jan 19, 2008 5:52 pm

Postby hardcoras » Sat Dec 12, 2009 11:01 pm

Patton wrote:Having the same problem. I'm rendering a textured quad as background which is placed at 0.0f, 0.0f, -8.0f. The quad is covering exactly the whole screen and I get 15fps. Same quad with same texture rendered at 0.0f, 0.0f, -16.0f gives 35fps. I don't get it, what's the problem? One quad rendered in front of the camera is slower, than rendering 50 quads in the distance.


Interesting news. And what is your projection matrix?
I'm a n00b to opengl, but i don't see how z value (depth) could reflect on a fps in orto2d projection matrix.

Maybe opengl experts could explain it?
hardcoras
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Sat Nov 14, 2009 2:31 pm
Location: Lithunia

Postby Patton » Sat Dec 12, 2009 11:40 pm

I've tried first with GLU.gluPerspective - 15fps... then I've used gluOrtho2D and 2d coordinates for that background - still 15fps :) It's pretty hilarious. So any OpenGL pro's reading this thread/forum? Please help...

If I draw only one quarter of the screen with gluOrtho2D, I get about 25-30fps.
Patton
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sat Jan 19, 2008 5:52 pm

Top

Postby Patton » Sun Dec 13, 2009 12:14 am

Ehm, thanks to my friend, I've tested the same program on a real device HTC Hero.

60fps :D

Damn emulator!
Patton
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sat Jan 19, 2008 5:52 pm

Postby ameetnsharma » Sun Dec 13, 2009 1:19 am

Interesting. I didn't think the emulator would be so off...
ameetnsharma
Developer
Developer
 
Posts: 30
Joined: Tue Dec 08, 2009 10:55 pm

Postby hardcoras » Sun Dec 13, 2009 1:14 pm

Hi,

I was just wondering what kind of hardware you were using. Looks like any :D

@ameetnsharma
An emulator does not have any hardware acceleration. Plus an emulator only runs on a single thread (at least on a linux host). For a complex scene rendering it is almost useless.
From my experience textures rendering is very slow on emulator. Mostly if you are using bi-linear mag filter.
hardcoras
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Sat Nov 14, 2009 2:31 pm
Location: Lithunia

Postby ameetnsharma » Mon Dec 14, 2009 4:19 am

Thanks Patton and hardcoras.

Is there any cheap device I can use to test my programs? Is there a non-phone android device (like ipod touch instead of iphone for apple)
ameetnsharma
Developer
Developer
 
Posts: 30
Joined: Tue Dec 08, 2009 10:55 pm

Postby hardcoras » Mon Dec 14, 2009 3:13 pm

You can grab an used G1 on ebay. They are quite cheap these days.

There is a gaming handheld device called ODroid (http://www.hardkernel.com/). But i think in the near future there will be a lot of different consumer electronic devices, not only phones, tablets or netbooks.

The future is ours :D
hardcoras
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Sat Nov 14, 2009 2:31 pm
Location: Lithunia

Postby Patton » Mon Dec 14, 2009 8:51 pm

Another question now about blending backgrounds.

I have one BMP image which has black background. I use gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE) to make that black background transparent. How to achieve that red portions of the image are transparent, and not the black ones? :)

This looks like a ultra noob question, sorry about that :)
Patton
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sat Jan 19, 2008 5:52 pm

Postby hardcoras » Tue Dec 15, 2009 12:17 am

Why don't you use textures with alpha channel? Many devs use PNGs (including me).
hardcoras
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Sat Nov 14, 2009 2:31 pm
Location: Lithunia

Postby Patton » Tue Dec 15, 2009 12:50 am

How to load png textures into opengl?

For bmp's I used something like this:
Code: Select all
   private static int initBmpTexture(GL10 gl, Bitmap bmp) {
      
      ByteBuffer bb = ByteBuffer.allocateDirect(bmp.getHeight() * bmp.getWidth() * 4);
      bb.order(ByteOrder.nativeOrder());
      IntBuffer ib = bb.asIntBuffer();
      
      for (int y=0; y<bmp.getHeight(); y++)
         for (int x=0; x<bmp.getWidth(); x++) {
            ib.put(bmp.getPixel(x, y));
         }
      ib.position(0);
      bb.position(0);
      ...
      ...
Patton
Junior Developer
Junior Developer
 
Posts: 23
Joined: Sat Jan 19, 2008 5:52 pm

Postby hardcoras » Tue Dec 15, 2009 4:32 am

You can do smt like

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. sBitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
  3.  
  4. final InputStream is = context.getResources().openRawResource(resourceId);
  5.  
  6. Bitmap bitmap = BitmapFactory.decodeStream(is, null, sBitmapOptions);
  7.  
  8. GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Exceptions handling, texture initialization and similar stuff is skipped.

Bitmap.Config.RGB_565 - for smaller textures (16bits) and native color model.
hardcoras
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Sat Nov 14, 2009 2:31 pm
Location: Lithunia

Top
Next

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

Who is online

Users browsing this forum: No registered users and 4 guests