java opengl: glDrawElements() with >32767 vertices

倾然丶 夕夏残阳落幕 提交于 2019-12-10 20:29:15

问题


this must be simple, but i'm missing it.

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, thanks.

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

回答1:


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.



来源:https://stackoverflow.com/questions/4331021/java-opengl-gldrawelements-with-32767-vertices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!