如何在renderscript中制作圆柱体

时间:2012-05-24 07:13:15

标签: android renderscript

我一直试图在renderscript中制作一个圆柱体。这是我尝试过的代码:

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的矩形。任何我想出错的想法?​​

1 个答案:

答案 0 :(得分:2)

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

其次,您不断向网格添加相同的两个三角形,而不是为圆柱体的侧面添加任何三角形,只添加两个端面。在调用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);

}

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