2013-03-21 127 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