Filling the background and the DrawTex extension

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

Filling the background and the DrawTex extension

Postby GambitDash » Fri Jun 04, 2010 6:49 am

Hi guys -

I'm using the DrawTex extension exclusively to render my engine. Currently works out to a be well over 40FPS with a few hundred tiles on the screen, so the performance is fine. One thing I'm trying to figure out how to do, however, is the most efficient way of simply blitting a region of the screen a color. The reason I need to do this is that my font uses a transparent background, and I'd like to prep the section I'm going to render it with some white to make it more visible.

Anyone have a useful snippet of code to do this? My two main GL calls look something like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ((GL11) gl).glTexParameteriv(GL10.GL_TEXTURE_2D,
  2.                 GL11Ext.GL_TEXTURE_CROP_RECT_OES, mCropWorkspace,0);
  4. ((GL11Ext) gl).glDrawTexxOES(left, bottom, depth,
  5.                 (right - left), -(bottom - top));
Parsed in 0.014 seconds, using GeSHi

So I'm not doing any rocket science here. A blit-rectangle code in terms of 'left, right, top, bottom' would be fabulous.

Posts: 2
Joined: Fri Jun 04, 2010 6:42 am


Re: Filling the background and the DrawTex extension

Postby ShadowKntSDS » Fri Jun 04, 2010 6:51 pm

You can use the DrawTex extension to take a 1x1 texture (or a 1x1 crop of a larger texture) and draw over an arbitrary rectangle. I have used a 64x64 color gradient texture in my text boxes, and i draw it in what ever size i need for the text I want to display. Often this is much larger than 64x64.

You could also use glClearColor() if you want the whole screen to be filled one color. Note while this would seem fast (single GL call), its take longer than you'd expect if your device is fill limited. It might not break your game, but i assumed it would be lightning fast, and it is not. It's still probably faster than using drawtex over the whole screen however.
Junior Developer
Junior Developer
Posts: 15
Joined: Wed Apr 28, 2010 10:29 pm

Re: Filling the background and the DrawTex extension

Postby GambitDash » Sat Jun 05, 2010 6:15 am

Ah, interesting idea. I'd thought of something similar, but I'd sorta hoped there was a way to do it without having to take up any texture memory. Also, glClearColor() is fine, but I'm really just looking to provide a opaque background for a piece of text, not the whole screen.

Do you feel like tossing that color gradient file up somewhere? It'd be nice not to have to make it myself :)

EDIT: I bashed together a quick imagemagick script to create a 64x64 color atlas.
Syntax: [ Download ] [ Hide ]
Using bash Syntax Highlighting
  1. DEST=$1
  3. SZX=64      # Width of the resulting image
  4. SZY=64      # Height of the resulting image
  5. DIVS=16     # Number of steps for each color
  6. CMAX=256    # Maximum number to increment to
  8. pix=0       # Start at pixel 0
  10. # Remove the old file
  11. rm $DEST
  13. # Create a new file of an appropriate size
  14. convert -size ${SZX}x${SZY} xc:white $DEST
  16. cr=0
  18. # Iterate over all the reds.
  19. while [ $cr -lt $CMAX ]; do
  20.     cg=0
  22.     # Iterate over all the greens
  23.     while [ $cg -lt $CMAX ]; do
  24.         cb=0
  26.         # Iterate over all the blues
  27.         while [ $cb -lt $CMAX ]; do
  28.             # Calculate the x,y of the pixel
  29.             x=$(($pix % $SZX))
  30.             y=$(($pix / $SZX))
  32.             # Set the pixel to the correct color
  33.             convert $DEST -fill "rgb($cr, $cg, $cb)" -draw "point $x,$y" $DEST
  35.             # Move to the next pixel/color
  36.             pix=$(($pix + 1))
  37.             cb=$(($cb + $CMAX / $DIVS))
  38.         done
  39.         cg=$(($cg + $CMAX / $DIVS))
  40.     done
  41.     cr=$(($cr + $CMAX / $DIVS))
  42. done
Parsed in 0.008 seconds, using GeSHi

Simply run "./ output.png", and wait!

You can, of course, identify the index of the pixel via:
Syntax: [ Download ] [ Hide ]
Using c Syntax Highlighting
  1.     pixel_idx = (red & 0xF0) << 4 + (green & 0xF0) + (blue >> 4);
Parsed in 0.001 seconds, using GeSHi

HTH, enjoy!
Posts: 2
Joined: Fri Jun 04, 2010 6:42 am


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

Who is online

Users browsing this forum: No registered users and 2 guests