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);
Related
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
I'm trying to understand OpenGL ES 2.0 in android. I got a code from a tutorial, but I do not understand how the texture works. I can't even understand where the rendering took place. Here is the code I got.
#Override
public void surfaceCreated(GL10 gl) {
// set up the surface
gl.glDisable(GL10.GL_DITHER);
gl.glHint(
GL10.GL_PERSPECTIVE_CORRECTION_HINT,
GL10.GL_FASTEST);
gl.glClearColor(0.4f, 0.2f, 0.2f, 0.5f);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_DEPTH_TEST);
// fetch the checker-board
initImage(gl);
}
#Override
public void drawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// apply the checker-board to the shape
gl.glActiveTexture(GL10.GL_TEXTURE0);
gl.glTexEnvx(
GL10.GL_TEXTURE_ENV,
GL10.GL_TEXTURE_ENV_MODE,
GL10.GL_MODULATE);
gl.glTexParameterx(
GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_WRAP_S,
GL10.GL_REPEAT);
gl.glTexParameterx(
GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_WRAP_T,
GL10.GL_REPEAT);
// animation
int t = (int) (SystemClock.uptimeMillis() % (10 * 1000L));
gl.glTranslatef(6.0f - (0.0013f * t), 0, 0);
// draw
gl.glFrontFace(GL10.GL_CCW);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuf);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuf);
gl.glDrawElements(
GL10.GL_TRIANGLE_STRIP,
5,
GL10.GL_UNSIGNED_SHORT, indexBuf);
}
private void initImage(GL10 gl) {
int[] textures = new int[1];
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);
InputStream in = context.getResources().openRawResource(R.drawable.cb);
Bitmap image;
try { image = BitmapFactory.decodeStream(in); }
finally {
try { in.close(); } catch(IOException e) { }
}
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, image, 0);
image.recycle();
}
Can anyone explain to me where the image is loaded from a drawable file, where the conversion to texture, and where the rendering took place? I just can't seem to understand this. I'm familiar with how to do it in OpenGL but it seems OpenGL ES 2.0 uses a different technique?
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, image, 0); is a utility function which converts an android Bitmap into an OpenGL texture.
When this is called, it stores the bitmap in image into the currently bound texture, which in your case is textures[0].
glDrawElements is the command that actually fires off the rendering call. If you want to know what any of the other functions do, I suggest taking a look at their online help pages.
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.
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);
A texture used for a cube is being used for other objects in the view even though they aren't assigned it. This only happens on my HTC Magic running 1.6. I load the texture image from resources using the standard Bitmap libraries. This shouldn't be the problem however since it correctly applies texture to intended models.
I tried deleted the texture after using it however the texture is completely wiped and the cubes never seem to display it.
I recycled a lot of code taken from tutorials around the net.
Code:
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glRotatef(sceneroty, 0, 1, 0);
gl.glTranslatef(xtrans, 0, ztrans);
root.draw(gl);
Root.Draw Function:
gl.glPushMatrix();
gl.glFrontFace(GL10.GL_CCW);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glCullFace(GL10.GL_BACK);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, verticesBuffer);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glColor4f(rgba[0], rgba[1], rgba[2], rgba[3]);
// Smooth color
if (colorBuffer != null) {
// Enable the color array buffer to be used during rendering.
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
}
// New part...
if (mShouldLoadTexture) {
loadGLTexture(gl);mShouldLoadTexture = false;
}
if (mTextureId != -1 && mTextureBuffer != null) {
gl.glEnable(GL10.GL_TEXTURE_2D);
// Enable the texture state
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// Point to our buffers
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer);
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureId);
}
// ... end new part.
gl.glTranslatef(x, y, z);
gl.glRotatef(rx, 1, 0, 0);
gl.glRotatef(ry, 0, 1, 0);
gl.glRotatef(rz, 0, 0, 1);
// Point out the where the color buffer is.
gl.glDrawElements(GL10.GL_TRIANGLES, numOfIndices,
GL10.GL_UNSIGNED_SHORT, indicesBuffer);
// Disable the vertices buffer.
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
// New part...
if (mTextureId != -1 && mTextureBuffer != null) {
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
// ... end new part.
// Disable face culling.
gl.glDisable(GL10.GL_CULL_FACE);
gl.glPopMatrix();
Other used Functions:
protected void setTextureCoordinates(float[] textureCoords) {
// float is 4 bytes, therefore we multiply the number if
// vertices with 4.
ByteBuffer byteBuf = ByteBuffer.allocateDirect(
textureCoords.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
mTextureBuffer = byteBuf.asFloatBuffer();
mTextureBuffer.put(textureCoords);
mTextureBuffer.position(0);
}
public void loadBitmap(Bitmap bitmap) {
this.mBitmap = bitmap;
mShouldLoadTexture = true;
}
private void loadGLTexture(GL10 gl) {
// Generate one texture pointer...
gl.glGenTextures(1, textures, 0);
mTextureId = textures[0];
// ...and bind it to our array
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureId);
// Create Nearest Filtered Texture
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);
// Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
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_REPEAT);
// Use the Android GLUtils to specify a two-dimensional texture image
// from our bitmap
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, mBitmap, 0);
}
It looks like you aren't disabling texturing nor unbinding the TEXTURE_2D target. If your other cubes are untextured they will then be drawn using the same texture left over from the previous draw operation. You should disable texturing if the object doesn't have a texture assigned to it:
if (mTextureId == -1) {
gl.glDisable(GL10.GL_TEXTURE_2D);
gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
}