I have a complex model that has >32767 vertices. now, the indices can only be passed to opengl as type GL_UNSIGNED_BYTE or GL_UNSIGNED_SHORT. java has no concept of unsigned, so the unsigned short option maps to simply (signed) short, which is 16 bits, or +32767. when I specify the vertices, i need to pass opengl a short[], where the values in the array point to a vertex in the vertice array. however, if there are >32767 vertices, the value won't fit in the short[].
Is there another way to specify the indices? code snippet is below,
short[] shorts = ... read the indices ...;
...
ShortBuffer indicesBuffer = null;
ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * Short.SIZE / 8);
ibb.order(ByteOrder.nativeOrder());
indicesBuffer = ibb.asShortBuffer();
indicesBuffer.put(indices);
indicesBuffer.position(0);
...
gl.glDrawElements(GL10.GL_TRIANGLES, numOfIndices, GL10.GL_UNSIGNED_SHORT, indicesBuffer);
...
I haven't used OpenGL from Java so I'm speculating here, but there's a good chance that you can just use the negative numbers whose binary reprentation is the same as the unsigned positive numbers you really want. You're giving GL some byte pairs and telling it to interpret them as unsigned, and as long as they have the right value when interpreted that way, it should work. It doesn't matter if Java thought they meant something different when it stored those bits in memory.
If you're iterating, just ignore the wraparound and keep on incrementing. When you get to -1, you're done.
If you're calculating the index numbers as ints (which don't have this range problem) and then casting to short, subtract 65536 from any number that's greater than 32767.