tex1 = loadTexture(gl, bmp0);
tex2 = loadTexture(gl, bmp1);
tex3 = loadTexture(gl, bmp2);
tex4 = loadTexture(gl, bmp3);
tex5 = loadTexture(gl, bmp4);
....
protected static int loadTexture(GL10 gl, Bitmap bmp) {
Bitmap bmp_tmp = bmp;
return loadTexture(gl, k,bmp_tmp, false);
}
/**
* Create a texture and send it to the graphics system
* @param gl The GL object
* @param bmp The bitmap of the texture
* @param reverseRGB Should the RGB values be reversed?
(necessary
workaround for loading .pngs...)
* @return The newly created identifier for the texture.
*/
// protected static int[] loadTexture(GL10 gl, Bitmap[] bmp,
boolean
reverseRGB) {
protected static int loadTexture(GL10 gl, Bitmap bmp, boolean
reverseRGB) {
System.out.println("Eenter loadTexture in GLTutorialBase");
int[] tmp_texture = new int[1];
gl.glGenTextures(1, tmp_texture, 0);
int texture = tmp_texture[0] ;//QQ
System.out.println("k=0 loadTexture 11111 in GLTutorialBase");
// ByteBuffer bb = ByteBuffer.allocateDirect(bmp.getHeight()
*bmp.getWidth()*4);
ByteBuffer bb = ByteBuffer.allocateDirect(bmp.getHeight()
*bmp.getWidth
()*4);
bb.order(ByteOrder.BIG_ENDIAN);
IntBuffer ib = bb.asIntBuffer();
System.out.println("k=0 loadTexture 22222 in GLTutorialBase");
for (int y=bmp.getHeight()-1;y>-1;y--)
for (int x=0;x<bmp.getWidth();x++) {
int pix = bmp.getPixel(x,bmp.getHeight()-y-1);
// Convert ARGB -> RGBA
@SuppressWarnings("unused")
byte alpha = (byte)((pix >> 24)&0xFF);
byte red = (byte)((pix >> 16)&0xFF);
byte green = (byte)((pix >> 8)&0xFF);
byte blue = (byte)((pix)&0xFF);
// It seems like alpha is currently broken in
Android...
ib.put(red << 24 | green << 16 | blue << 8 | 0xFF);//
255-alpha);
}
System.out.println("k=0 loadTexture 33333 in GLTutorialBase");
ib.position(0);
bb.position(0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA,
bmp.getWidth(), bmp.getHeight(), 0, GL10.GL_RGBA,
GL10.GL_UNSIGNED_BYTE, bb);
gl.glTexParameterf(GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);
tmp_texture=null;
ib.position(0);
bb.position(0);
ib.clear();
bb.clear();
System.out.println("k =0 Leave loadTexture in
GLTutorialBase");
return texture;
}
.......
//选择并使用纹理 choose and use texture
gl.glBindTexture(GL10.GL_TEXTURE_2D, tex);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glMaterialfv(GL10.GL_FRONT_AND_BACK,
GL10.GL_AMBIENT, matAmbient,
0);
gl.glMaterialfv(GL10.GL_FRONT_AND_BACK,
GL10.GL_DIFFUSE, matDiffuse,
0);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, cubeBuff);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuff);
gl.glNormal3f(0,0,1);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
gl.glBindTexture(GL10.GL_TEXTURE_2D, tex1);
gl.glNormal3f(0,0,-1);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4);
gl.glBindTexture(GL10.GL_TEXTURE_2D, tex2);
gl.glNormal3f(-1,0,0);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4);
gl.glBindTexture(GL10.GL_TEXTURE_2D, tex3);
gl.glNormal3f(1,0,0);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4);
gl.glBindTexture(GL10.GL_TEXTURE_2D, tex4);
gl.glNormal3f(0,1,0);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4);
gl.glBindTexture(GL10.GL_TEXTURE_2D, tex5);
gl.glNormal3f(0,-1,0);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4);
in the above context,when the bmp,bmp1,bmp2,...,bmp5 all be modified
to bmp ,there will be no problem,otherwise when the second to call
loadtexture() there will be "force
close"exception in "ByteBuffer bb =ByteBuffer.allocateDirect
(bmp.getHeight()*bmp.getWidth()*4);"and why?
here is the trace information:
05-05 02:57:25.392: INFO/System.out(1072): Eenter loadTexture in
GLTutorialBase
05-05 02:57:25.392: INFO/System.out(1072): k=0 loadTexture 11111 in
GLTutorialBase
05-05 02:57:25.425: INFO/System.out(1072): k=0 loadTexture 22222 in
GLTutorialBase
05-05 02:57:26.119: INFO/System.out(1072): k=0 loadTexture 33333 in
GLTutorialBase
05-05 02:57:26.129: INFO/System.out(1072): k =0 Leave loadTexture in
GLTutorialBase
05-05 02:57:26.149: INFO/System.out(1072): Before loadTexture bmp1
05-05 02:57:26.149: INFO/System.out(1072): Eenter loadTexture in
GLTutorialBase
05-05 02:57:26.159: INFO/System.out(1072): k=1 loadTexture 11111 in
GLTutorialBase
05-05 02:57:26.231: ERROR/AndroidRuntime(1072): at
com.android.simple_gl.GLTutorialBase.loadTexture(GLTutorialBase.java:
130)
05-05 02:57:26.231: ERROR/AndroidRuntime(1072): at
com.android.simple_gl.GLTutorialBase.loadTexture(GLTutorialBase.java:
72)
best wishes!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---