运行此方法以检查 OpenGL 驱动程序状态错误:
public void checkGlError(String op) {
int error;
while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
Log.e("ShadingZen", op + ": glError " + error);
//throw new RuntimeException(op + ": glError " + error);
}
}
根据您的测试设备,可能无法使用两种纹理的非幂次。此代码向您展示如何将它们转换为 ^2 大小(在 android 中):
int calculateUpperPowerOfTwo(int v)
{
v--;
v |= v >>> 1;
v |= v >>> 2;
v |= v >>> 4;
v |= v >>> 8;
v |= v >>> 16;
v++;
return v;
}
boolean isPowerOfTwo(int i){
return ( i & (i - 1)) == 0;
}
boolean loadAsTexture2D(Context context, String id, int resource_id, BitmapTexture.Parameters params){
_bmps = new Bitmap[1];
Matrix flip = new Matrix();
flip.postScale(1f, -1f);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inScaled = false;
Bitmap textureBmp = BitmapFactory.decodeResource(context.getResources(), resource_id, opts);
if(!isPowerOfTwo(textureBmp.getWidth()) || !isPowerOfTwo(textureBmp.getHeight())){
int target_width = calculateUpperPowerOfTwo(textureBmp.getWidth());
int target_height = calculateUpperPowerOfTwo(textureBmp.getHeight());
Log.i("ShadingZen", "Texture id=" + id + " has no power of two dimesions " + textureBmp.getWidth() + "x" + textureBmp.getHeight() + " adjusting to " + target_width + "x" + target_height);
Bitmap temp = Bitmap.createBitmap(textureBmp, 0, 0, textureBmp.getWidth(), textureBmp.getHeight(), flip, false);
_bmps[0] = Bitmap.createScaledBitmap(temp, target_width, target_height, false);
temp.recycle();
} else{
_bmps[0] = Bitmap.createBitmap(textureBmp, 0, 0, textureBmp.getWidth(), textureBmp.getHeight(), flip, false);
}
textureBmp.recycle();
// At this point _bmp[0] contains a ^2 bitmap
}
检查此类以获取更多信息:https ://github.com/TraxNet/ShadingZen/blob/master/library/src/main/java/org/traxnet/shadingzen/core/BitmapTexture.java