1

我一直在尝试在渲染脚本中制作一个圆柱体。这是我尝试过的代码:

public Mesh cylinder(){
    float radius=1.25f, halfLength=5;
    int slices=16;

    Mesh.TriangleMeshBuilder mbo= new TriangleMeshBuilder(mRSGL,3, Mesh.TriangleMeshBuilder.TEXTURE_0);
    for(int i=0; i<slices; i++) {
    float theta = (float) (((float)i)*2.0*Math.PI);
    float nextTheta = (float) (((float)i+1)*2.0*Math.PI);

        /*vertex at middle of end*/
    mbo.addVertex(0.0f, halfLength, 0.0f);

        /*vertices at edges of circle*/
    mbo.addVertex((float)(radius*Math.cos(theta)), halfLength, (float)(radius*Math.sin(theta)));
        mbo.addVertex((float)(radius*Math.cos(nextTheta)), halfLength, (float)(radius*Math.sin(nextTheta)));

        /* the same vertices at the bottom of the cylinder*/

    mbo.addVertex((float)(radius*Math.cos(nextTheta)), -halfLength, (float)(radius*Math.sin(nextTheta)));
    mbo.addVertex((float)(radius*Math.cos(theta)), halfLength, (float)(radius*Math.sin(theta)));
    mbo.addVertex(0.0f, -halfLength, 0.0f);

    mbo.addTriangle(0, 1, 2);
    mbo.addTriangle(3, 4, 5);


}
    return mbo.create(true);

}

但是这段代码给了我一个长度为 5 的矩形。我哪里出错了有什么想法吗?

4

1 回答 1

2

您实际上在这里遇到了一些问题。首先,你的角度总是等于 2pi 的倍数。计算角度时,您需要除以扇区数。此外,在这一步中,您有一个不必要的显式类型转换,java 将为您处理整数到双精度的转换。

其次,您不断地向网格中添加相同的两个三角形,而不是为圆柱体的侧面添加任何三角形,只添加两个端面。在调用 addTriangle() 时,您应该在循环中使用索引,例如 addTriangle(n, n+1, n+2)。

最后,当您创建第 4 个顶点时,您错过了一个负号,因此它实际上是 halfLength,而不是 -halfLength。

试试这个:

public Mesh cylinder(){
    float radius=1.25f, halfLength=5;
    int slices=16;

    Mesh.TriangleMeshBuilder mbo= new TriangleMeshBuilder(mRSGL,3, Mesh.TriangleMeshBuilder.TEXTURE_0);

    /*vertex at middle of end*/
    mbo.addVertex(0.0f, halfLength, 0.0f);
    mbo.addVertex(0.0f, -halfLength, 0.0f);

    for(int i=0; i<slices; i++) {
         float theta = (float) (i*2.0*Math.PI / slices);
         float nextTheta = (float) ((i+1)*2.0*Math.PI / slices);

         /*vertices at edges of circle*/
         mbo.addVertex((float)(radius*Math.cos(theta)), halfLength, (float)(radius*Math.sin(theta)));
         mbo.addVertex((float)(radius*Math.cos(nextTheta)), halfLength, (float)(radius*Math.sin(nextTheta)));

         /* the same vertices at the bottom of the cylinder*/
         mbo.addVertex((float)(radius*Math.cos(nextTheta)), -halfLength, (float)(radius*Math.sin(nextTheta)));
         mbo.addVertex((float)(radius*Math.cos(theta)), -halfLength, (float)(radius*Math.sin(theta)));

         /*Add the faces for the ends, ordered for back face culling*/
         mbo.addTriangle(4*i+3, 4*i+2, 0); 
         //The offsets here are to adjust for the first two indices being the center points. The sector number (i) is multiplied by 4 because the way you are building this mesh, there are 4 vertices added with each sector
         mbo.addTriangle(4*i+5, 4*i+4, 1);
         /*Add the faces for the side*/
         mbo.addTriangle(4*i+2, 4*i+4, 4*i+5); 
         mbo.addTriangle(4*i+4, 4*i+2, 4*i+3);
    }
return mbo.create(true);

}

我还添加了一个轻微的优化,其中圆心的顶点只创建一次,从而节省了内存。这里的索引顺序用于背面剔除。如果您想要正面,请将其反转。如果您最终需要更有效的方法,分配构建器允许使用 trifans 和 tristrips,但对于这种复杂的网格,三角形网格的易用性是值得的。我已经在我自己的系统上运行了这段代码来验证它是否有效。

于 2012-05-24T14:50:01.047 回答