我有一個應用程序,它在屏幕上用紋理繪製正方形,用戶可以旋轉觸摸屏幕的正方形。它適用於Android 1.5至2.1。但是當我用Android 2.2.2在摩托羅拉Droid上測試應用程序時,由於正方形加載了白色紋理,所以出現了一些故障。在Android 2.2上的白色(錯誤)紋理,但在Android 1.5到2.1上正常紋理
有什麼不對?
一些提示: -the質地是256×256,那麼是不是一個POT問題
這是我的類方形的代碼:
public class Square {
/** The buffer holding the vertices */
private FloatBuffer vertexBuffer;
/** The buffer holding the texture coordinates */
private FloatBuffer textureBuffer;
/** Our texture pointer */
private int[] textures = new int[3];
/** The initial vertex definition */
private float vertices[] = {
-1.0f, -1.0f, 0.0f, //Bottom Left
1.0f, -1.0f, 0.0f, //Bottom Right
-1.0f, 1.0f, 0.0f, //Top Left
1.0f, 1.0f, 0.0f //Top Right
};
/** The initial texture coordinates (u, v) */
private float texture[] = {
//Mapping coordinates for the vertices
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
1.0f, 1.0f
};
/**
* The Square constructor.
*
* Initiate the buffers.
*/
public Square() {
//
ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
vertexBuffer = byteBuf.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
//
byteBuf = ByteBuffer.allocateDirect(texture.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
textureBuffer = byteBuf.asFloatBuffer();
textureBuffer.put(texture);
textureBuffer.position(0);
}
/**
* The object own drawing function.
* Called from the renderer to redraw this instance
* with possible changes in values.
*
* @param gl - The GL Context
*/
public void draw(GL10 gl) {
//Set the face rotation
//gl.glFrontFace(GL10.GL_CW);
gl.glFrontFace(GL10.GL_CCW);
//Bind our only previously generated texture in this case
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
//Point to our vertex buffer
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
//Enable vertex buffer
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
//Set The Color To Blue
//gl.glColor4f(0.5f, 0.5f, 1.0f, 1.0f);
//Draw the vertices as triangle strip
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length/3);
//Disable the client state before leaving
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
///
//Bind the texture according to the set texture filter
//gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[filter]);
}
/**
* Load the textures
*
* @param gl - The GL Context
* @param context - The Activity context
*/
public void loadGLTexture(GL10 gl, Context context) {
//Generate one texture pointer...
gl.glGenTextures(1, 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_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_REPEAT);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
//Get the texture from the Android resource directory
InputStream is = context.getResources().openRawResource(R.drawable.radiocd2);
Bitmap bitmap = null;
try {
//BitmapFactory is an Android graphics utility for images
bitmap = BitmapFactory.decodeStream(is);
} finally {
//Always clear and close
try {
is.close();
is = null;
} catch (IOException e) {
}
}
//Use the Android GLUtils to specify a two-dimensional texture image from our bitmap
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
/*
* This is a change to the original tutorial, as buildMipMap does not exist anymore
* in the Android SDK.
*
* We check if the GL context is version 1.1 and generate MipMaps by flag.
* Otherwise we call our own buildMipMap implementation
*/
if(gl instanceof GL11) {
gl.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_GENERATE_MIPMAP, GL11.GL_TRUE);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
//
} else {
buildMipmap(gl, bitmap);
}
//Clean up
bitmap.recycle();
}
/**
* Our own MipMap generation implementation.
* Scale the original bitmap down, always by factor two,
* and set it as new mipmap level.
*
* Thanks to Mike Miller (with minor changes)!
*
* @param gl - The GL Context
* @param bitmap - The bitmap to mipmap
*/
private void buildMipmap(GL10 gl, Bitmap bitmap) {
//
int level = 0;
//
int height = bitmap.getHeight();
int width = bitmap.getWidth();
//
while(height >= 1 || width >= 1) {
//First of all, generate the texture from our bitmap and set it to the according level
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, level, bitmap, 0);
//
if(height == 1 || width == 1) {
break;
}
//Increase the mipmap level
level++;
//
height /= 2;
width /= 2;
Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap, width, height, true);
//Clean up
bitmap.recycle();
bitmap = bitmap2;
}
}
}
我們可以爲照明和漫射創建給定圖像的法線貼圖嗎? –