带有GL_TRIANGLE_FAN的android/ openGL立方体。

时间:2022-09-10 22:01:59

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:

我是openGL的新成员,我想构建一个简单的立方体,但是似乎有一些问题让indice字节缓冲我想要有4个不同的TRIANGLE_FANs,我将如何做这个/重写我的代码:

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

我认为你必须使用GL10。GL_PRIMITIVE_RESTART,但对于android,我不认为这是存在的……

1 个解决方案

#1


3  

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

您的假设是正确的,没有primitive_restart扩展,您不能在一个调用glDrawElements的调用中呈现多个三角形扇子。你能做的是使用glMultiDrawElements,你可以在一个调用中绘制多个原语,但是它的使用有点麻烦,我不确定它是否真的能让你在一个三角形列表中获得一个性能优势(特别是这个简单的立方体)。

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.

编辑:只要查一下GLES规范,就好像glMultiDraw…在ES中去除函数。因此,对于一个索引三角形列表(或三角形带解决方案),至少如果您想要在一个调用中绘制多维数据集,这是不可能的。

#1


3  

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

您的假设是正确的,没有primitive_restart扩展,您不能在一个调用glDrawElements的调用中呈现多个三角形扇子。你能做的是使用glMultiDrawElements,你可以在一个调用中绘制多个原语,但是它的使用有点麻烦,我不确定它是否真的能让你在一个三角形列表中获得一个性能优势(特别是这个简单的立方体)。

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.

编辑:只要查一下GLES规范,就好像glMultiDraw…在ES中去除函数。因此,对于一个索引三角形列表(或三角形带解决方案),至少如果您想要在一个调用中绘制多维数据集,这是不可能的。