I want to use .PVR image for texture purpose.
For this, I used PVRtextool and loaded my pvr image in drawables-mdpi.
Now, when i use this in my project the app just crashes.
Am I missing some step?
Please guide.
Here is the load texture code where I'm getting problem. resource contains the image in .pvr format.
static void loadTexture(GL10 gl, Context context, int[] resource)
{
gl.glGenTextures(n, textureIDs, 0);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inScaled = false;
for (int face = 0; face < n; face++)
{
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[face]);
bitmap[face] = BitmapFactory.decodeResource(
context.getResources(), resource[face],opts);
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, bitmap[face], 0);
bitmap[face].recycle();
}
]
You can't use BitmapFactory.decodeResource() with that format. You have to use the openRawResource() function and pass the InputStream it returns to the ETC1Util.loadTexture() function.
A sample implementation should be at /sdk/platforms/<version>/samples/CompressedTextureActivity.java, or an online version is here.
Related
I am trying to develop an Android live wallpaper using OpenGL wallpaper service , I am able to create live wallpaper as in this example by Mark F Guerra But I want to add some sprite animation to my wallpaper.
I have already created a OpenGL ES sprite animation in another project. I just want to recreate my animation in the live wallpaper project.
But in my live wallpaper project i am not able to get Context and load my images from assets or resources
Any suggestions or sample codes or link about loading resourses or asset files while using glwallpaper service will be very helpfull.
All suggestions and/or sample codes are welcome.
We can use the context as shown below..
in wallpaper service class:
-------------------
renderer = new GlRenderer(this);
in renderer class:
----------------
private Context context;
public GlRenderer(Context context) {
this.context = context;
Instead of this we can use getAssets() or getResources() as parameter to renderer .On using getAssets() you can get the files saved in assets folder and by using getResources() you can get the files placed inside the resources folder in your project.
Pass the context from your engine to your renderer. Then, here's some sample code to load the asset. i.e. resourceID is your R.drawable.xxx bitmap. I have this inside a texture atlas class I made, so a few things might not be completely contained in the method. For example the options I might use to load the bitmap would include inscaled = false, but whatever works for you. I also modified this to remove my error handling for instance.
/**
* Load the resource and push it to the gpu memory, setup default values
* #param gl
* #param context
* #param resourceID
* #return glTextureID
*
*/
public int loadFromContext(GL10 gl, Context context, int resourceID) {
mResourceID = resourceID;
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), resourceID, sBitmapOptions);
sourceWidth = bmp.getWidth();
sourceHeight = bmp.getHeight();
gl.glGenTextures(1, mGLTextures, 0);
mGLTextureID = mGLTextures[0];
// bind and set min and mag scaling to bilinear
gl.glBindTexture(GL10.GL_TEXTURE_2D, mGLTextureID);
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);
// repeat by default
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);
// upload bmp to video memory
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
// check error
int error = gl.glGetError();
if (error != GL10.GL_NO_ERROR) {
// cleanup
bmp.recycle();
bmp = null;
mLoaded = false;
// error handling here
} else {
// unbind.
gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
bmp.recycle();
bmp = null;
mLoaded = true;
mDirty = true;
}
return mGLTextureID;
}
I am currently implementing a 3D viewer which basically renders a subset of all the images the user has on his SD Card. The closest matching product I would think of would be CoolIris:
It simply show a scrolling board of N tiles on screen, each showing different images, with new tiles entering the screen and showing new images.
Now for my problem: I have the program working and rendering nicely the quads. When a quad goes out of the screen, it gets recycled/released. And new quads keep on being added to the tile board before they enter the screen.
Because there can be hundreds of images, the textures need to be created and deleted on the fly (so that we don't run out of memory). The problem I have is that after I delete textures, newly created textures seem to get some IDs of other textures currently in use.
My rendering loop looks like this:
void render(GL10 gl) {
0. Move the camera
// Tile board maintenance
1. Remove tiles out of screen
2. Add new tiles which are about to enter screen
// Texture handling
3. glDeleteTextures on all unused textures followed by glFlush
4. For newly used images
- Create corresponding Bitmap
- Create the OpenGL texture, followed by glFlush
- Release the Bitmap
// Rendering
5. Render the tile (using a textured quad)
}
To give a better idea of how the data is organised, here is an overview of the classes:
TileBoard {
Image[] allImages;
Tile[] board;
}
Tile {
Image image;
}
Image {
String path;
int textureId;
int referenceCount;
}
Texture creation code:
protected void loadSingleTexture(GL10 gl, long objectId, Bitmap bmp) {
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_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
gl.glFlush();
if (bmp != null) bmp.recycle();
if (listener != null) listener.onTextureLoaded(gl, objectId, textures[0]);
}
Texture deletion code:
// pendingTextureUnloads is a Set<Integer>
if (pendingTextureUnloads.size() > 0) {
int[] textureIds = new int[pendingTextureUnloads.size()];
int i = 0;
Iterator<Integer> it = pendingTextureUnloads.iterator();
while (it.hasNext()) {
textureIds[i] = it.next();
}
gl.glDeleteTextures(textureIds.length, textureIds, 0);
gl.glFlush();
}
I have solved the problem: the issue was that you have to keep the texture array passed to glGenTextures and reuse it.
Here is the modified overview for the ones who will have the same problem:
Image {
String path;
int[] textureIds;
int referenceCount;
}
Texture creation code:
// Notice that I don't allocate the int[] at the beginning but use the one of the image
protected void loadSingleTexture(GL10 gl, Image img, Bitmap bmp) {
gl.glGenTextures(1, img.textureIds, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, img.textureIds[0]);
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);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
gl.glFlush();
if (bmp != null) bmp.recycle();
}
Texture deletion code:
foreach Image img {
gl.glDeleteTextures(img.textureIds.length, img.textureIds, 0);
}
gl.glFlush();
I know you said that you solved your issue, but I think I noticed your error in the first bit of code. Look at the while loop in your texture deletion code, you are not increasing the index i for your array, so you will continuously assign the first index until you reach the last entry in pendingTextureUnloads, the rest of the indices will be 0 (null). That might be problematic.
And by the way, I have got texture generation working by not reusing the array, just returning the index that was generated by glGenTextures. My code is to the line equal to yours, except from creating a new int array in the beginning of the method and returning the int at the first index at the end. Your code for texture generation should work, the error was just in the texture deletion.
Ive modified my code to use ETC1 textures to help lower memory usage and while the texture loads, its not automatically generating mipmaps anymore. Is this not supported for ETC1 images? Heres my code:
//Generate three texture pointers...
gl.glGenTextures(3, textures, 0);
//...and bind it to our array
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
//Create Nearest Filtered Texture
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
// gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR_MIPMAP_NEAREST); //problem child
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
//gl.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_GENERATE_MIPMAP, GL11.GL_TRUE); problem child.
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);
//Get the texture from the Android resource directory
Log.e("Sys", "ETC1 texture support: " + ETC1Util.isETC1Supported());
InputStream input = context.getResources().openRawResource(R.drawable.testtile);
try {
ETC1Util.loadTexture(GLES10.GL_TEXTURE_2D, 0, 0,
GLES10.GL_RGB, GLES10.GL_UNSIGNED_SHORT_5_6_5, input);
Log.w("sys", "loaded!");
} catch (IOException e) {
Log.w("sys", "Could not load texture: " + e);
} finally {
try {
input.close();
} catch (IOException e) {
// ignore exception thrown from close.
}
}
An easy way to solve this is to generate the mipmap levels with the tool you use to create your ETC1 texture (all the levels will end up in the file (eg .pvr)) and to upload each level individually.
Use this tool : http://www.malideveloper.com/developer-resources/tools/texture-compression-tool.php
The one from the Android SDK ( etc1tool ) will not generate mipmaps automatically. The mali tool can even batch convert textures to etc1/etc2 .
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.
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);