2013-03-21 128 views
0

我已用下面的代碼生成的n邊多邊形:如何確定UV紋理座標n邊多邊形

public class Vertex 
{ 
    public FloatBuffer floatBuffer; // buffer holding the vertices 
    public ShortBuffer indexBuffer; 
    public int numVertices; 
    public int numIndeces; 

    public Vertex (float[] vertex) 
    {    
     this.setVertices(vertex); 
    } 

    public Vertex (float[] vertex, short[] indices) 
    {    
     this.setVertices(vertex); 
     this.setIndices(indices); 
    } 

    private void setVertices(float vertex[]) 
    { 
     // a float has 4 bytes so we allocate for each coordinate 4 bytes 
     ByteBuffer factory = ByteBuffer.allocateDirect (vertex.length * 4); 
     factory.order (ByteOrder.nativeOrder()); 
     // allocates the memory from the byte buffer 
     floatBuffer = factory.asFloatBuffer(); 
     // fill the vertexBuffer with the vertices 
     floatBuffer.put (vertex); 
     // set the cursor position to the beginning of the buffer 
     floatBuffer.position (0); 
     numVertices = vertex.length; 
    } 

    protected void setIndices(short[] indices) 
    { 
     ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2); 
     ibb.order(ByteOrder.nativeOrder()); 
     indexBuffer = ibb.asShortBuffer(); 
     indexBuffer.put(indices); 
     indexBuffer.position(0); 
     numIndeces = indices.length; 
    } 
} 

然後創建n邊多邊形:

public class Polygon extends Mesh 
{ 

    public Polygon(int lines) 
    { 
     this(lines, 1f, 1f); 
    } 

    public Polygon(int lines, float xOffset, float yOffset) 
    {  
     float vertices[] = new float[lines*3]; 
     float texturevertices[] = new float[lines*2]; 
     short indices[] = new short[lines+1]; 

     for (int i = 0; i < lines;i++) 
     { 
      vertices[i*3]  = (float) (xOffset * Math.cos(2*Math.PI*i/lines)); 
      vertices[(i*3)+1] = (float) (yOffset * Math.sin(2*Math.PI*i/lines)); 
      vertices[(i*3)+2] = 0.0f;//z 

      indices[i] = (short)i; 

      texturevertices[i*2] =(float) (Math.cos(2*Math.PI*i/lines)/2 + 0.5f); 
      texturevertices[(i*2)+1] = (float) (Math.sin(2*Math.PI*i/lines)/2 + 0.5f); 
     } 

     indices[lines] = indices[0];   

     shape = new Vertex(vertices,indices); 
     texture = new Vertex(texturevertices, indices); 
    } 
} 

正如你所看到的那樣,我正在按順序設置indeces,這樣我就可以將它們呈現爲線條。現在我希望紋理多邊形。我該怎麼做呢?

我曾嘗試實現這個:

enter image description here
enter image description here

從這裏:http://en.wikipedia.org/wiki/UV_mapping

但是,結果是真窮。我如何瀏覽座標並確定紋理的排序?

一個相關的參考可以在這裏找到:How to draw a n sided regular polygon in cartesian coordinates?

編輯我更新根據下面馬蒂奇Oblak給出的答案,這就是結果:

enter image description here

旋轉是沒關係。

這是非常接近...但沒有雪茄。原有的質感如下:

enter image description here

回答

2

如果我正確地讀這一點,你要創建從n個多邊形的圓。有很多方法可以使用不同類型的紋理並將它們粘貼到一個形狀上,最直接的方法是繪製一個具有完整形狀的紋理(對於大'n'它將是一個圓形),並且紋理座標將相同如在(.5,.5)爲中心的圓和.5半徑:

//for your case: 
    u = Math.cos(2*Math.PI*i/lines)/2 + .5 
    v = Math.sin(2*Math.PI*i/lines)/2 + .5 
//the center coordinate should be set to (.5, .5) though 

你貼都是爲了一個球,是一個比較複雜一點,因爲這是很難想象的方程式把它作爲一個2D圖像的圖像。

EDIT(從評論)

創建這些三角形是不完全一樣的畫線帶。您應該使用三角形風扇而不是三角形條,並且您需要將第一個點設置爲形狀的中心。

public Polygon(int lines, float xOffset, float yOffset) 
    {  
     float vertices[] = new float[(lines+1)*3]; //number of angles + center 
     float texturevertices[] = new float[(lines+1)*2]; 
     short indices[] = new short[lines+2]; //number of vertices + closing 

     vertices[0*3]  = .0f; //set 1st to center 
     vertices[(0*3)+1] = .0f; 
     vertices[(0*3)+2] = .0f; 
     indices[0] = 0; 
     texturevertices[0] = .5f; 
     texturevertices[1] = .5f; 

     for (int i = 0; i < lines;i++) 
     { 
      vertices[(i+1)*3]  = (float) (xOffset * Math.cos(2*Math.PI*i/lines)); 
      vertices[((i+1)*3)+1] = (float) (yOffset * Math.sin(2*Math.PI*i/lines)); 
      vertices[((i+1)*3)+2] = 0.0f;//z 

      indices[(i+1)] = (short)i; 

      texturevertices[(i+1)*2] =(float) (Math.cos(2*Math.PI*i/lines)/2 + 0.5f); 
      texturevertices[((i+1)*2)+1] = (float) (Math.sin(2*Math.PI*i/lines)/2 + 0.5f); 
     } 

     indices[lines+1] = indices[1]; //closing part is same as for i=0  

     shape = new Vertex(vertices,indices); 
     texture = new Vertex(texturevertices, indices); 
    } 

現在你只需要繪製直到指數與三角形風扇。在這裏稍微注意一下你的「偏移量」,你使用xOffset和yOffset作爲橢圓參數而不是偏移量。如果您將它們用作偏移vertices[(i+1)*3] = (float) (xOffset + Math.cos(2*Math.PI*i/lines));(注意'+'而不是'*'),那麼第1個頂點應該位於偏移量而不是(0,0),而紋理座標保持不變。

+0

你好!非常感謝你 - 這種方式比我想要的更近,我已經嘗試過了,而且我可能只是有點偏離軌道,我可以請你再看一次嗎?我編輯了上面的問題。 – 2013-03-22 12:42:34

+0

它的一切都很好,你只需要最後一個閉合頂點。indices [lines] = indices [0](或index [1]如果indices [0]位於中心)也不會忘記在索引中需要額外的位置所以它應該被定義爲short [lines + 1](或lines + 2) – 2013-03-22 12:59:51

+0

我有最後一個頂點的額外索引,我仍然得到相同的結果。我添加了更多的代碼,以便您可以獲得完整的圖片。對不起,只是複製循環,這是我的愚蠢。 – 2013-03-22 14:12:15