2012-05-24 110 views
1

我一直在試圖在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的矩形。任何想法,我哪裏會出錯?

回答

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

} 

我還添加在其中創建只有一次圓的中心頂點略有優化,從而節省內存。這裏的指數順序是用於背面剔除。如果你想要正面,請將其反轉。如果您的需求最終需要更高效的方法,分配構建器允許使用trifans和tristrips,但對於這種複雜性的網格來說,三角網格的易用性是值得的。我在我自己的系統上運行這個代碼來驗證它的工作原理。

+0

謝謝,那工作:) –

+0

沒問題,高興地幫助 – Jared