【发布时间】:2017-11-10 17:25:04
【问题描述】:
我现在在 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中的drawcall
GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_SHORT, 0);
【问题讨论】:
-
填充 spriteBuffer 的循环 'for (int v = 0; v
-
@Columbo - 好的,我认为应该是 6 - 所以如果你对此正确,答案是肯定的 - 我应该更改 spriteDataList-filling。我想这同样适用于 uv-coords?