gpt4 book ai didi

android - 压缩纹理在 Android 中变为白色

转载 作者:行者123 更新时间:2023-11-29 00:45:52 26 4
gpt4 key购买 nike

我是 android 和 OpenGL 开发的新手我尝试只做一个带有纹理的立方体,但我需要在 ETC1 压缩中压缩这个纹理,我制作了一个代码,就像我在 develop.android 网站上找到的一样,但代码只在模拟器上运行!

我在两个设备上测试

1) Nexus mobile,在这个设备中没有出现立方体,我在网上搜索,我发现这个设备原来的Android是2.1,升级到2.3,这是使设备可以绘图的原因幼崽?

2) Motorola Xoom Tablet, Android 3.1 Device, 这里出现立方体但是颜色是白色, 不是Uncompressed texture, 支持CTE1, 没有OPENGL Error !

压缩后的贴图是PNG 265 * 265/,我把压缩后的贴图放在raw文件夹里

代码如下

public class TextureCube {
private String TAG;
private Context context;
private FloatBuffer vertexBuffer; // Buffer for vertex-array
private FloatBuffer texBuffer; // Buffer for texture-coords-array

private float[] vertices = { // Vertices for a face
-1.0f, -1.0f, 0.0f, // 0. left-bottom-front
1.0f, -1.0f, 0.0f, // 1. right-bottom-front
-1.0f, 1.0f, 0.0f, // 2. left-top-front
1.0f, 1.0f, 0.0f // 3. right-top-front
};
float[] texCoords = { // Texture coords for the above face
0.0f, 1.0f, // A. left-bottom
1.0f, 1.0f, // B. right-bottom
0.0f, 0.0f, // C. left-top
1.0f, 0.0f // D. right-top
};
int[] textureIDs = new int[1]; // Array for 1 texture-ID

// Constructor - Set up the buffers
public TextureCube(Context context) {
this.context = context;
TAG = "Sam Messages: " + this.getClass().getName();
// Setup vertex-array buffer. Vertices in float. An float has 4 bytes
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder()); // Use native byte order
vertexBuffer = vbb.asFloatBuffer(); // Convert from byte to float
vertexBuffer.put(vertices); // Copy data into buffer
vertexBuffer.position(0); // Rewind

// Setup texture-coords-array buffer, in float. An float has 4 bytes
ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4);
tbb.order(ByteOrder.nativeOrder());
texBuffer = tbb.asFloatBuffer();
texBuffer.put(texCoords);
texBuffer.position(0);
}

// Draw the shape
public void draw(GL10 gl) {


gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

gl.glFrontFace(GL10.GL_CCW); // Front face in counter-clockwise orientation
gl.glEnable(GL10.GL_CULL_FACE); // Enable cull face
gl.glCullFace(GL10.GL_BACK); // Cull the back face (don't display)

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); // Enable texture-coords-array
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuffer); // Define texture-coords buffer

// front
gl.glPushMatrix();
gl.glTranslatef(0.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
gl.glPopMatrix();

// left
gl.glPushMatrix();
gl.glRotatef(270.0f, 0.0f, 1.0f, 0.0f);
gl.glTranslatef(0.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
gl.glPopMatrix();

// back
gl.glPushMatrix();
gl.glRotatef(180.0f, 0.0f, 1.0f, 0.0f);
gl.glTranslatef(0.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
gl.glPopMatrix();

// right
gl.glPushMatrix();
gl.glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
gl.glTranslatef(0.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
gl.glPopMatrix();

// top
gl.glPushMatrix();
gl.glRotatef(270.0f, 1.0f, 0.0f, 0.0f);
gl.glTranslatef(0.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
gl.glPopMatrix();

// bottom
gl.glPushMatrix();
gl.glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
gl.glTranslatef(0.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
gl.glPopMatrix();

gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); // Disable
// texture-coords-array
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisable(GL10.GL_CULL_FACE);
}

// Load an image into GL texture
public void loadTexture(GL10 gl) {

Boolean loadCompressed = true;
if (loadCompressed) {
/****************************************************/
/** LOAD A COMPRESSED TEXTURE IMAGE */
/***********************************/
Log.w(TAG, ": ETC1 texture support: " + ETC1Util.isETC1Supported());
try {
ETC1Util.loadTexture(GLES20 .GL_TEXTURE_2D, 0, 0,
GLES20.GL_RGB, GLES20.GL_UNSIGNED_SHORT_5_6_5,
context.getResources().openRawResource(R.raw.pic3));

Log.w(TAG, ": OpenGL Error -After LoadTexture()-:" + gl.glGetError());
Log.w(TAG,"OpenGL Extensions: " + gl.glGetString(GL10.GL_EXTENSIONS));
} catch (IOException e) {
Log.w(TAG, ": Could not load texture: " + e);
} finally {

Log.w(TAG, ": OpenGL Error -In Final()-:" + gl.glGetError());
}
} else {
/*****************************************************/
/** LOAD A TEXTURE IMAGE */
/************************/

gl.glGenTextures(1, textureIDs, 0); // Generate texture-ID array
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[0]); // Bind to texture ID Set up texture filters
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);
InputStream istream = context.getResources().openRawResource(R.drawable.pic5);
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(istream);
} finally {
try {
istream.close();
} catch (IOException e) {
}
}
// Build Texture from loaded bitmap for the currently-bind texture
// ID
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
/******************************************************/
}
}

最佳答案

您未能为压缩纹理加载设置纹理名称,这两个函数调用

gl.glGenTextures(1, textureIDs, 0); // Generate texture-ID array
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[0]); // Bind to texture

这样做,所以将它们放在函数的开头。

“白色”纹理通常表示纹理不完整。如果您启用了 mipmap 但没有提供所有必需的 mipmap 级别,通常会发生这种情况。在上面的代码中,您禁用了未压缩纹理的 mipmaping:

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

但为了压缩,请保持原样——尝试将这两个调用放在名称生成之后,即 if(compressed) 之前,会发生什么情况:

public void loadTexture(GL10 gl) {

gl.glGenTextures(1, textureIDs, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[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);

Boolean loadCompressed = true;
if (loadCompressed) {
/****************************************************/
/** LOAD A COMPRESSED TEXTURE IMAGE */
/***********************************/
Log.w(TAG, ": ETC1 texture support: " + ETC1Util.isETC1Supported());
try {
ETC1Util.loadTexture(GLES20 .GL_TEXTURE_2D, 0, 0,
GLES20.GL_RGB, GLES20.GL_UNSIGNED_SHORT_5_6_5,
context.getResources().openRawResource(R.raw.pic3));

Log.w(TAG, ": OpenGL Error -After LoadTexture()-:" + gl.glGetError());
Log.w(TAG,"OpenGL Extensions: " + gl.glGetString(GL10.GL_EXTENSIONS));
} catch (IOException e) {
Log.w(TAG, ": Could not load texture: " + e);
} finally {

Log.w(TAG, ": OpenGL Error -In Final()-:" + gl.glGetError());
}
} else {
/*****************************************************/
/** LOAD A TEXTURE IMAGE */
/************************/
InputStream istream = context.getResources().openRawResource(R.drawable.pic5);
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(istream);
} finally {
try {
istream.close();
} catch (IOException e) {
}
}
// Build Texture from loaded bitmap for the currently-bind texture
// ID
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
/******************************************************/
}
}

关于android - 压缩纹理在 Android 中变为白色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6403106/

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