gpt4 book ai didi

android - 白色(错误)纹理在 Android 2.2 上,但在 Android 1.5 到 2.1 上工作正常纹理

转载 作者:太空狗 更新时间:2023-10-29 13:39:25 25 4
gpt4 key购买 nike

我有一个应用程序可以在屏幕上绘制一个带有纹理的正方形,用户可以旋转触摸屏幕的正方形。它适用于 android 1.5 到 2.1。但是当我在装有 android 2.2.2 的摩托罗拉 Droid 上测试应用程序时,出现了一些问题,因为正方形加载了白色纹理。

怎么了?

一些提示:-纹理为256x256,则不是POT问题

这是我类(class)广场的代码:

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;
}
}
}

最佳答案

解决了将图像存储在 Assets 上的问题……太棒了

关于android - 白色(错误)纹理在 Android 2.2 上,但在 Android 1.5 到 2.1 上工作正常纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7638475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com