我在Android的opengl中实现索引时遇到了问题。我想我差不多了 - 但问题是只绘制了一半的多边形。可能是什么原因以及如何解决它?
1
创建和绑定缓冲区,一个FloatBuffer
用于位置和uvcoords,一个ShortBuffer
用于索引
public int createVBO(ArrayList <SpriteDataHolder> spriteDataList) {
final int BYTES_PER_FLOAT = 4;
final int BYTES_PER_SHORT = 2;
final int N_SPRITES = spriteDataList.size();
final int N_VERTICES_SPRITE = spriteDataList.get(0).getSpriteVertices().length; //godtyckligt indexvärde - alla lika
final int N_UVS_SPRITE = spriteDataList.get(0).getUvsSprites().length;
final int N_INDICES_SPRITE = 6;
final int totalDataLength = N_SPRITES * (N_VERTICES_SPRITE + N_UVS_SPRITE);
final int totalDataLengthShort = N_SPRITES * N_INDICES_SPRITE;
final int POSITION_DATA_SIZE = 3; // antal floats per punktcoordinat
final int TEXTURE_COORDINATE_DATA_SIZE = 2; // antal floats per uvcoordinat
final int INDEX_DATA_SIZE = 1; // antal shorts per indexvärde
final FloatBuffer spriteBuffer = ByteBuffer.allocateDirect(totalDataLength * BYTES_PER_FLOAT)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
final ShortBuffer indexBuffer = ByteBuffer.allocateDirect(totalDataLengthShort * BYTES_PER_SHORT)
.order(ByteOrder.nativeOrder()).asShortBuffer();
int spritePositionOffset = 0;
int spriteTextureOffset = 0;
int spriteIndexOffset = 0;
for (int i = 0; i < N_SPRITES; i++) {
for (int v = 0; v < 6; v++) { // 6 punkter per polygon
spriteBuffer.put(spriteDataList.get(i).getSpriteVertices(), spritePositionOffset, POSITION_DATA_SIZE); // vertices x y z för sprite i
spritePositionOffset += POSITION_DATA_SIZE;
spriteBuffer.put(spriteDataList.get(i).getUvsSprites(), spriteTextureOffset, TEXTURE_COORDINATE_DATA_SIZE);
spriteTextureOffset += TEXTURE_COORDINATE_DATA_SIZE;
indexBuffer.put(spriteDataList.get(i).getIndices(), spriteIndexOffset, INDEX_DATA_SIZE);
spriteIndexOffset += INDEX_DATA_SIZE;
}
spritePositionOffset = 0;
spriteTextureOffset = 0;
spriteIndexOffset = 0;
}
spriteBuffer.position(0);
indexBuffer.position(0);
// skicka flyttalsbufferten till GPU'N
final int buffers[] = new int[1];
GLES20.glGenBuffers(1, buffers, 0);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[0]);
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, spriteBuffer.capacity() * BYTES_PER_FLOAT, spriteBuffer, GLES20.GL_STATIC_DRAW);
final int[] indexbuffers = new int[1];
GLES20.glGenBuffers(1, indexbuffers, 0);
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, indexbuffers[0]);
GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, indexBuffer.capacity() * BYTES_PER_SHORT, indexBuffer, GLES20.GL_STATIC_DRAW);
return buffers[0];
}
2 //以及所有精灵的创建索引
private void createVBOindices(short[][] indices, int nSprites) {
int last = 0;
for (int i = 0; i < nSprites; i++) {
indices[i][0] = (short)(last + 0);
indices[i][1] = (short)(last + 1);
indices[i][2] = (short)(last + 2);
indices[i][3] = (short)(last + 1);
indices[i][4] = (short)(last + 3);
indices[i][5] = (short)(last + 2);
last = last + 4;
}
}
我有这个订单,因为这是跟随顶点的顺序......
TRIANGLE 1
0 2
x x x x
x x
x x
x
1
TRIANGLE 2
2
x
x x
x x
x x x x
1 3
因此,对于第一个三角形,它从0到1再绘制到2,对于第二个三角形,它从1到3然后再到2。
我认为问题可以在这里找到,这是第二个三角形的绘图顺序但是已经尝试了几个组合但是第二个三角形不会被绘制。
继承人在drawmethod
中的拉动 GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_SHORT, 0);
答案 0 :(得分:0)
不确定Columbo的评论是否修复了这个评论,但是三角形的缠绕是不同的,所以如果你启用了GL_CULL_FACE
,那么你的第二个三角形可能被归类为背面。
要么禁用面对测试,要么更改第二个三角形中的索引顺序,以便两者都是面部相同的方式。