android纹理坐标,Android-多维数据集纹理问题

即时通讯试图使每个表面具有不同纹理的多维数据集.

我现在正面和背面都在工作.现在,我正在尝试使多维数据集正确的一面.但是出了点问题,因为我完成了正确的操作,但是纹理显示出了错误(就像被拉伸和切碎了一样),我的代码中有一些不好的东西,我也不知道是什么.

这是我的代码

public class Cube {

private FloatBuffer vertexBuffer;//Vertices

private FloatBuffer textureBuffer;//Texture coordinates

private ByteBuffer indexBuffer;//Indices

private int[] textures = new int[6];//Texture pointer

private float vertices[] = { //8 vertices of the cube

-1.0f, -1.0f, 1.0f, // 0

1.0f, -1.0f, 1.0f, // 1

-1.0f, 1.0f, 1.0f, // 2

1.0f, 1.0f, 1.0f, // 3

-1.0f, -1.0f, -1.0f,// 4

1.0f, -1.0f, -1.0f, // 5

-1.0f, 1.0f, -1.0f, // 6

1.0f, 1.0f, -1.0f, // 7

};

private byte indices[] = { //Faces definition

0,1,2, 1,3,2, //front face (*)

6,7,5, 6,5,4, //rear face (**)

1,5,3, 5,7,3, //right face (***) //problems here

};

private float texture[] = {//Mapping coordinates for the vertices

0.0f, 1.0f,

1.0f, 1.0f,

0.0f, 0.0f,

1.0f, 0.0f,

0.0f, 1.0f,

1.0f, 1.0f,

0.0f, 0.0f,

1.0f, 0.0f,

0.0f, 1.0f,

1.0f, 1.0f,

0.0f, 0.0f,

1.0f, 0.0f,

};

public Cube()

{

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

indexBuffer = ByteBuffer.allocateDirect(indices.length);

indexBuffer.put(indices);

indexBuffer.position(0);

}

public void draw(GL10 gl) {

//Point to our buffers

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

//Set the face rotation

gl.glFrontFace(GL10.GL_CCW);

//Enable the vertex and texture state

gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

//para que no pinte los poligonos que no se ven

//gl.glEnable(GL10.GL_CULL_FACE);

for(int i=0; i<3; i++) //<6 por que tenemos 6 texturas que queremos poner en las 6 caras de un cubo.

{

gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[i]);

indexBuffer.position(6*i); //como cada dos triangulos (cuadrado) forman una cara, estos dos triangulos son 6 indices del array de indices, por lo tanto avanzamos 6 posiciones en el indexBuffer para pintar el siguiente cuadrado.

gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_BYTE, indexBuffer); //el segundo parametro es 6 por que solo queremos pintar una cara (cuadrado) por textura.

}

//gl.glDisable(GL10.GL_CULL_FACE); //para que no pinte los poligonos que no se ven

//Disable the client state before leaving

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

}

public void loadGLTexture(GL10 gl, Context context) {

loadTexture(gl,context,R.drawable.s1,0);

loadTexture(gl,context,R.drawable.s2,1);

loadTexture(gl,context,R.drawable.s3,2);

loadTexture(gl,context,R.drawable.s4,3);

loadTexture(gl,context,R.drawable.s5,4);

loadTexture(gl,context,R.drawable.s6,5);

}

public void loadTexture(GL10 gl, Context context, int drawable, int textureNumber)

{

//Get the texture from the Android resource directory

InputStream is = context.getResources().openRawResource(drawable);

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

}

}

//Generate one texture pointer...

gl.glGenTextures(1, textures, textureNumber);

//...and bind it to our array

gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[textureNumber]);

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

//Use the Android GLUtils to specify a two-dimensional texture image from our bitmap

GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

//Clean up

bitmap.recycle();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值