'OpenGL - glDrawElements only draws half polygon
I have a problem right now when implementing indices in opengl for Android. I think I am almost there - but the problem is that only half of the polygon is drawn. What could be the reason for this and how to solve it?
1
create and bind buffers, one FloatBuffer for positions and uvcoords, one ShortBuffer for indices
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 // and the created indices for all sprites
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;
}
}
I have this order since this is the order that follows the vertices that is ...
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
so for the first triangle it draws from 0 to 1 and then to 2 and for the second triangle it draws from 1 to 3 and then to 2.
I thougt the problem could be found here that is the drawingorder of the second triangle but a have tried several combinations but the second triangle will not be drawed.
Heres the drawcall in the drawmethod
GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_SHORT, 0);
Solution 1:[1]
Not sure if the comment from Columbo fixed this one, but the winding of your triangles is different so if you have GL_CULL_FACE enabled then it's possible that your second triangle is classified as back-facing.
Either disable the facing test, or change the index order in the second triangle so both are facial the same way.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | solidpixel |

