Drawing translucent textures in Android using OpenGL - android

I've been struggling with this issue for a few weeks now, with no results. I'm sure I'm just missing something silly, so I thought I'd get an outside opinion on it.
I'm drawing a bunch of different textures in an app, and for some reason, some of them aren't showing up. I tried to figure out why only certain textures wouldn't display, and I think I've narrowed it down to textures with translucent pixels (textures that contain pixels with an alpha value that is not 0 or 255). Fully opaque textures display just fine, and textures that have either fully on or fully off pixels do as well. The problem textures display as a white square. Also, I figure I should add that everything displays fine in the emulator, it's only on my testing device (Samsung Captivate) that the textures don't display.
Here are some snippets of my code showing the important parts, as always, let me know if you need more information.
View setup (I've tried both of these, but having these lines there or not seems to make no difference):
gameView.setEGLConfigChooser(8,8,8,8,16,0);
gameView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
Renderer's onSurfaceCreated method (I've also tried enabling depth test to no avail, the lines I used are commented here, and when I tried depth testing, I did clear the depth buffer on every draw):
gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glShadeModel(GL10.GL_FLAT);
gl.glDisable(GL10.GL_DEPTH_TEST);
//gl.glEnable(GL10.GL_DEPTH_TEST);
//gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
Texture loading method (could it have something to do with GLUtils.texImage2D?):
int texId;
gl.glGenTextures(1, mTextureNameWorkspace, 0);
texId = mTextureNameWorkspace[0];
gl.glBindTexture(GL10.GL_TEXTURE_2D, texId);
if (gl instanceof GL11) {
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR_MIPMAP_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL11.GL_GENERATE_MIPMAP, GL11.GL_TRUE);
}
else {
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
}
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_MODULATE);
InputStream is = res.openRawResource(resourceId);
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(is, null, bitmapOptions);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
if (gl instanceof GL11) {
mCropWorkspace[0] = 0;
mCropWorkspace[1] = bitmap.getHeight();
mCropWorkspace[2] = bitmap.getWidth();
mCropWorkspace[3] = -bitmap.getHeight();
((GL11) gl).glTexParameteriv(GL10.GL_TEXTURE_2D, GL11Ext.GL_TEXTURE_CROP_RECT_OES, mCropWorkspace, 0);
}
//Don't poke fun at my archaic error handler
int error;
String errorCodes = "";
while ((error = gl.glGetError()) != GL10.GL_NO_ERROR) {
errorCodes += error + " ";
}
if (errorCodes.length() != 0)
throw new Exception("OpenGL error while loading texture. Error codes: " + errorCodes.substring(0,errorCodes.length()-1));
textureWidth = bitmap.getWidth();
textureHeight = bitmap.getHeight();
textureId = texId;
return texId;
}
finally {
try { is.close(); } catch (IOException e) { }
try { bitmap.recycle(); } catch (Exception e) { }
}

Just to close the question in case anyone stumbles upon it:
This error was occurring because the images were saved in res/drawable. When loaded from there, the Android can do some behind-the-scenes processing, causing the resulting images to not be sized to a power of two. Store the images in res/raw to avoid this processing.

Related

OpenGL ES 2D on Samsung 4 - no textures on the screen

I have Nexsus 4 and several HTCs and my game works fine.
When I launch on Samsung 4 I see white rectangles (empty textures),
no errors,
Further, my game uses sensors but I see on Samsung 4 it doesn't work too,
but Samsung 3 - works,
please help,
[EDIT]
on Samsung 3 works!
I heard that they did a lot of changes with S4
[EDIT 2]
This is how I load textures:
public void loadTextures(GL10 gl, Context context){
//Log.e(LOG_TAG, "DevQuestSprites :: loadTextures");
InputStream is;
Bitmap bitmap;
is = context.getResources().openRawResource(R.drawable.fly_a1_sprite);
bitmap = BitmapFactory.decodeStream(is);
try {
is.close();
is = null;
} catch (IOException e) {
}
gl.glGenTextures(textureCount, textures, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
}
For me most impotent row is:
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
Here actually I put texture.
And this is draw method:
public void draw(GL10 gl,
...
...
)
{
...
//gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glTranslatef(transx, transy, 0.0f);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, floatBufferArray[mFrame]);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
//gl.glDrawElements(GL10.GL_TRIANGLES, 1, GL10.GL_UNSIGNED_SHORT, vertexBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
[EDIT 3]
I added to draw, onDrawFrame and main loop, no errors:
int error = gl.glGetError();
if (error != 0)
Log.e("main loop", "Draw " + error);
After debugging I found only one new error for Samsung 4:
08-07 12:57:53.356: E/ViewRootImpl(29124): sendUserActionEvent() mView == null
[EDIT 4]
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig confid) {
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearColor(0,0,0,0);
gl.glClearDepthf(1.0f);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
gl.glEnable(GL10.GL_BLEND);
gl.glDisable(GL10.GL_DEPTH_TEST);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
}
One issue is that you have enabled alpha blending, but you can't use GLUtils.texImage2D() to load alpha textures on Android. This is a common problem that Google really should document better. The problem is that the Bitmap class converts all images into pre-multiplied format, but that does not work with OpenGL ES unless the image is completely opaque. The best solution is to use native code. This article gives more detail on this:
http://software.intel.com/en-us/articles/porting-opengl-games-to-android-on-intel-atom-processors-part-1
What is the size of your image? if you are using opnel GL It depends on which driver have the mobile but you need the size image to be power of two to work fine in all devices, 2,4,8,16,32...etc

Android opengl 1.1 Render to Texture 1286 error (Invalid framebuffer operation)

I am trying to implement a motion blur effect in my android game.
After a lot of research I found that the best way to do that is to save the previous frame as a texture using the Frame Buffer Object and render it on top of the current frame. So seeing some nice tutorials on how to do something like that I ended up with this code which basically **render my scene on the texture and then draws the texture to the default framebuffer.
It only draw's a white texture** with a 1286 (Invalid framebuffer operation) error in gl.glGetError()
SOLVED:The problem seems to be the non power of two texture dimmensions as Jean said
int[] fb, depthRb, renderTex;
int texW = 480 * 2;
int texH = 800 * 2;
IntBuffer texBuffer;
int[] buf = new int[texW * texH];
GL11ExtensionPack gl11ep ;
void setup(GL10 gl)
{
gl11ep=(GL11ExtensionPack)gl;
fb = new int[1];
depthRb = new int[1];
renderTex = new int[1];
gl11ep.glGenFramebuffersOES(1, fb, 0);
gl11ep.glGenRenderbuffersOES(1, depthRb, 0); // the depth buffer
gl.glGenTextures(1, renderTex, 0);// generate texture
gl.glBindTexture(GL10.GL_TEXTURE_2D, renderTex[0]);
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);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
texBuffer = ByteBuffer.allocateDirect(buf.length*4).order(ByteOrder.nativeOrder()).asIntBuffer();
gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGB, texW, texH, 0, GL10.GL_RGB, GL10.GL_UNSIGNED_SHORT_5_6_5, texBuffer);
gl11ep.glBindRenderbufferOES(GL11ExtensionPack.GL_RENDERBUFFER_OES, depthRb[0]);
gl11ep.glRenderbufferStorageOES(GL11ExtensionPack.GL_RENDERBUFFER_OES, GL11ExtensionPack.GL_DEPTH_COMPONENT16, texW, texH);
}
boolean RenderStart(GL10 gl)
{
// Bind the framebuffer
gl11ep.glBindFramebufferOES(GL11ExtensionPack.GL_FRAMEBUFFER_OES, fb[0]);
// specify texture as color attachment
gl11ep.glFramebufferTexture2DOES(GL11ExtensionPack.GL_FRAMEBUFFER_OES, GL11ExtensionPack.GL_COLOR_ATTACHMENT0_OES, GL10.GL_TEXTURE_2D, renderTex[0], 0);
// attach render buffer as depth buffer
gl11ep.glFramebufferRenderbufferOES(GL11ExtensionPack.GL_FRAMEBUFFER_OES, GL11ExtensionPack.GL_DEPTH_ATTACHMENT_OES, GL11ExtensionPack.GL_RENDERBUFFER_OES, depthRb[0]);
int error = gl.glGetError();
if (error != GL10.GL_NO_ERROR) {
Tools.con("Background Load GLError: " + error) ;//here the 1286 error
}
int status = gl11ep.glCheckFramebufferStatusOES(GL11ExtensionPack.GL_FRAMEBUFFER_OES);
if (status != GL11ExtensionPack.GL_FRAMEBUFFER_COMPLETE_OES)//here always geting true
{
return false;
}
return true;
}
void RenderEnd(GL10 gl)
{
gl11ep.glBindFramebufferOES(GL11ExtensionPack.GL_FRAMEBUFFER_OES, 0);
gl.glClearColor(0f, 0f, 0f, 1.0f);
gl.glClear( gl.GL_DEPTH_BUFFER_BIT | gl.GL_COLOR_BUFFER_BIT);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glBindTexture(GL10.GL_TEXTURE_2D, renderTex[0]);
gl.glColor4f(1,1,1,1);
((GL11Ext) gl).glDrawTexfOES(0, 0, 0,800, 480);
gl.glDisable(GL10.GL_TEXTURE_2D);
}
public void onDrawFrame(GL10 gl)
{
this.RenderStart(gl);
render(gl);//render scene
this.RenderEnd(gl);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
...
setup(gl);
}
Probably you should make your texture have a power of 2 as height and width (i.e. 512x512, or even less since it's only for a blur effect). Almost every device with hardware acceleration needs that. Quick way to test this is to run it into the emulator (being software only it should work if it supports every other feature you need in your code)

Android OpenGL ES texture colors show up correctly on emulator but not on phone

I have a problem where the colors of any textures that I use are bleached, if that's a good word to use to describe it, on two different phones but the textures show up just fine on the emulator.
Here are the images.
The first image is the texture that I'm using.
The second image is how the texture shows up on the emulator and the way it is supposed to show up on the phones.
The third image is how the texture actually shows up on the phone.
Should I include the code where I draw the rectangle? The rectangle is just a vertex buffer and texcoord buffer. Anything that actually is done with the texture is gl.glBindTexture and gl.glBlendFunction(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA). gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f) is also called.
The following code is where I believe my error could exist. I hope someone can help me out here. Thanks!
This is in my GLSurfaceView.Renderer implementation
#Override
public void onSurfaceCreated(final GL10 gl, final EGLConfig config) {
DebugLog.d("onSurfaceCreated");
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glShadeModel(GL10.GL_FLAT);
gl.glDisable(GL10.GL_DEPTH_TEST);
gl.glDisable(GL10.GL_DITHER);
gl.glDisable(GL10.GL_LIGHTING);
gl.glDisable(GL10.GL_MULTISAMPLE);
gl.glEnable(GL10.GL_BLEND);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glFrontFace(GL10.GL_CW);
gl.glCullFace(GL10.GL_BACK);
}
This is in my BitmapTexture class.
#Override
public void loadBitmapToHardware(final GL10 gl) throws IOException {
final Bitmap bitmap = loadBitmap();
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, bitmap, GL10.GL_UNSIGNED_BYTE, 0);
bitmap.recycle();
}
#Override
public Bitmap loadBitmap() {
InputStream is = null;
try {
is = mContext.getAssets().open(mBitmapPath);
return BitmapFactory.decodeStream(is);
} catch (final IOException e) {
DebugLog.e("Failed to load Bitmap in " + this.getClass().getSimpleName() + " from path: " + mBitmapPath, e);
return null;
} finally {
try {
is.close();
} catch (final IOException e) {
e.printStackTrace();
}
}
}
#Override
public void loadTexture() throws IOException {
gl.glGenTextures(1, TEXTURE_CONTAINER, 0);
mTextureId = TEXTURE_CONTAINER[0];
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureId);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_MODULATE);
loadBitmapToHardware();
}
It looks like an overexposure issue to me. It is likely that the screen settings for the emulator and your phone handles this problem differently, especially since dither is disabled in your code.
You should try to:
1: Remove the glColor4f() call, and see if that works (from what I know of GL10, it sets the color to white in your case, which might cause problems with blending).
or
2: Turn of blending and turn on depth-test to see if your blending give this result.

OpenGL on Android - Texture Mapping - Only top left pixel visible

I've been playing with OpenGL ES Android and somehow my textures are always filled with the color of the the top left-most pixel. I suppose the flags are wrong, but I've tried a many logical combinations and it does not seem to make any difference.
Why would the texture behave like so? What's the bit (flag bit indeed) that am I missing? Thanks
public void loadGLTexture(GL10 gl, Context context) {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId);
gl.glGenTextures(1, textures, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
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);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
}
public void draw(GL10 gl) {
// bind the previously generated texture
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
((GL11Ext) gl).glDrawTexfOES(position.getX(), position.getY(), 0, width, height);
}
the surface is set to
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
Obviously texture crop rectangle has to be explicitly set:
int cropWorkspace[] = new int[4];
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
cropWorkspace[0] = 0;
cropWorkspace[1] = height;
cropWorkspace[2] = width;
cropWorkspace[3] = -height;
((GL11) gl).glTexParameteriv(GL10.GL_TEXTURE_2D, GL11Ext.GL_TEXTURE_CROP_RECT_OES, cropWorkspace, 0);

Android OpenGL ES Texturing - alphablending

I d like to "kill" the black color of my texture. Or, I want to show png files, without background (transparent bg)
How i can do this?
Any example?
it's not that difficult. This are the main pieces:
public static Bitmap loadBitmapFromId(Context context, int bitmapId) {
InputStream is = context.getResources().openRawResource(bitmapId);
try {
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
return BitmapFactory.decodeStream(is, null, bitmapOptions);
} catch (Exception ex) {
Log.e("bitmap loading exeption", ex.getLocalizedMessage());
return null;
}
}
The Bitmap.Config.RGB_565 is important here. Then add the bitmap and get your texture id as usual.
Now in the onSurfaceCreated(GL10 gl, EGLConfig config) of your renderer add this:
// Transparancy
// important: transparent objects have to be drawn last!
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
And the drawing part (you are probably already doing this):
// first disable color_array for save:
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
// Enabled the vertices buffer for writing and to be used during
// rendering.
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
// Specifies the location and data format of an array of vertex
// coordinates to use when rendering.
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_LINEAR);
gl.glBindTexture(GL10.GL_TEXTURE_2D, myTextureId);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glDrawArrays(drawMode, 0, verticesCount);
gl.glDisable(GL10.GL_TEXTURE_2D);
// Disable the vertices buffer.
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

Categories

Resources