android/openGL 立方体与 GL_TRIANGLE_FAN

发布于 2024-11-28 08:46:20 字数 1562 浏览 2 评论 0原文

我是 openGL ES 的新手,我想构建一个简单的多维数据集,但似乎在让索引字节缓冲区拥有 4 个不同的 TRIANGLE_FAN 时遇到一些问题,我将如何执行此操作/重写我的代码:

public class GLCube{

private float vertices[] = {
    1, 1, -1, //p0 - topFrontRight
    1, -1, -1, //p1 bottomFront Right
    -1, -1, -1, //p2 bottom front left
    -1, 1, -1, //p3 front top left
    1, 1, 1, //p4 - topBackRight
    1, -1, 1, //p5 bottomBack Right
    -1, -1, 1, //p6 bottom back left
    -1, 1, 1, //p7 front back left
};

private FloatBuffer vertBuff;

private short[] pIndex = { 
        0, 4, 1, 3, //i0 fan of top front right
        5, 4, 1, 6, //i1 fan from bottom right back
        2, 1, 3, 6, //i2 fan from bottom left front
        7, 3, 4, 6 //i3 fan from top left back
};

private ShortBuffer pBuff;

public GLCube(){

    ByteBuffer bBuff = ByteBuffer.allocateDirect(vertices.length * 4);
    bBuff.order(ByteOrder.nativeOrder());
    vertBuff = bBuff.asFloatBuffer();
    vertBuff.put(vertices);
    vertBuff.position(0);


    ByteBuffer pbBuff = ByteBuffer.allocateDirect(pIndex.length * 2);
    pbBuff.order(ByteOrder.nativeOrder());
    pBuff = pbBuff.asShortBuffer();
    pBuff.put(pIndex);
    pBuff.position(0);
}

public void draw(GL10 gl){
    gl.glFrontFace(GL10.GL_CW);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff);
    gl.glDrawElements(GL10.GL_TRIANGLE_FAN, pIndex.length, GL10.GL_UNSIGNED_SHORT, pBuff);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

}
}

我假设你必须使用 GL10.GL_PRIMITIVE_RESTART,但是对于 android,我认为这不存在......

I am some what new to openGL ES and I want to build a simple cube, but seem to be having some problems letting the indice byte buffer that I want to have 4 different TRIANGLE_FANs, how would I go about doing this/ rewriting my code:

public class GLCube{

private float vertices[] = {
    1, 1, -1, //p0 - topFrontRight
    1, -1, -1, //p1 bottomFront Right
    -1, -1, -1, //p2 bottom front left
    -1, 1, -1, //p3 front top left
    1, 1, 1, //p4 - topBackRight
    1, -1, 1, //p5 bottomBack Right
    -1, -1, 1, //p6 bottom back left
    -1, 1, 1, //p7 front back left
};

private FloatBuffer vertBuff;

private short[] pIndex = { 
        0, 4, 1, 3, //i0 fan of top front right
        5, 4, 1, 6, //i1 fan from bottom right back
        2, 1, 3, 6, //i2 fan from bottom left front
        7, 3, 4, 6 //i3 fan from top left back
};

private ShortBuffer pBuff;

public GLCube(){

    ByteBuffer bBuff = ByteBuffer.allocateDirect(vertices.length * 4);
    bBuff.order(ByteOrder.nativeOrder());
    vertBuff = bBuff.asFloatBuffer();
    vertBuff.put(vertices);
    vertBuff.position(0);


    ByteBuffer pbBuff = ByteBuffer.allocateDirect(pIndex.length * 2);
    pbBuff.order(ByteOrder.nativeOrder());
    pBuff = pbBuff.asShortBuffer();
    pBuff.put(pIndex);
    pBuff.position(0);
}

public void draw(GL10 gl){
    gl.glFrontFace(GL10.GL_CW);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff);
    gl.glDrawElements(GL10.GL_TRIANGLE_FAN, pIndex.length, GL10.GL_UNSIGNED_SHORT, pBuff);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

}
}

I assume you have to use GL10.GL_PRIMITIVE_RESTART, but with android, I have don't think this exists...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝對不後悔。 2024-12-05 08:46:20

您的假设是正确的,如果没有 primitive_restart 扩展,您无法在一次调用 glDrawElements 中渲染多个三角形扇形。你可以做的是使用glMultiDrawElements,你可以在一次调用中绘制多个图元,但它的使用有点麻烦,我不确定它是否真的比单个三角形给你带来性能优势列表(特别是对于这个简单的立方体)。

我给你的一般建议是,远离复杂的基元,如三角形条和扇形,因为它们仅适用于一些特殊的小几何形状(无论如何,你不会获得任何性能优势)。较大的通用网格需要付出更多努力才能真正从这些原始类型(如果有)中获得优势。只需使用一个简单的索引三角形列表就可以了。

顺便说一句,如果你真的想要一个立方体的“复杂”镶嵌,你可以从单个三角形带构建一个立方体,但解决方案取决于你或谷歌。

编辑:刚刚查找了 GLES 规范,似乎 ES 中删除了 glMultiDraw... 函数。因此,没有办法绕过单个索引三角形列表(或三角形带解决方案),至少如果您想在一次调用中绘制立方体,这是明智的。

You are correct in your assumption, without primitive_restart extension you cannot render multiple triangle fans in one call to glDrawElements. What you can do is use glMultiDrawElements, where you can draw multiple primitives in one call, but it's use is a bit cumbersome and I'm not sure if it really gives you a peformance benefit over a single triangle list (especially with this simple cube).

The general tip I would give you is, stay away from complicated primitives like triangle strips and fans, because they are only of use for some special small geometries (those where you won't get any performance benefit, anyway). Larger general meshes require much more effort to really gain an advantage from those primitive types, if any. Just use a simple indexed triangle list and you're fine.

By the way, if you really want a "sophisticated" tessellation of a cube, you can build a cube from a single triangle strip, but the solution to this is up to you or google.

EDIT: Just looked up the GLES spec and it seems the glMultiDraw... functions were removed in ES. So there's no way around a single indexed triangle list (or the triangle strip solution), at least if you want to draw the cube in one call, which is advisable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文