我是Android和OpenGL開發的新手 我嘗試做一個帶有紋理的立方體,但是我需要使這個紋理在ETC1壓縮中被壓縮,我做了一個像我在develop.android網站,但代碼只能在模擬器上運行!壓縮紋理在Android中變成白色
我測試兩個設備
1)的Nexus移動,在該裝置中的立方體沒有出現,我在網上搜索,我發現這個設備的原始的Android爲2.1,它被升級到2.3 ,這是使設備能夠繪製幼仔的原因嗎?
2)摩托羅拉Xoom平板電腦,它是一個Android 3.1設備,立方體出現在這裏,但白色,而不是未壓縮紋理,它支持CTE1,並且沒有OPENGL錯誤!
壓縮紋理PNG 265 * 265 /我把壓縮紋理在原始文件夾
代碼就像下面
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();
/******************************************************/
}
}
datenwolf, 我不知道該怎麼感謝亞人 它的工作,但我真的無法理解以及你的意思,你能幫助我,送我,我可以開始一個很好的教程 謝謝 – Samy
謝謝你的男人 它正在 非常感謝你 – Samy
@Samy:這僅僅是OpenGL的紋理製作的基本模式:使用'glGenTexture'綁定的紋理名稱中使用的目標紋理單元分配質地名'glBindTexture'設置紋理參數(使用mipmaping,過濾器類型)等,通過'glTextureParameter',設置紋理環境(多紋理需要),最後直接使用'glTexImage2D'或一些輔助函數加載實際紋理數據,最後調用它。深入的教程是OpenGL編程指南http://goo.gl/WU9Qz和NeHe紋理教程http://goo.gl/pBjfz – datenwolf