0

我使用下面的代码生成了一个 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);
    }   
}

正如你所看到的,我按顺序设置了索引,以便我可以将它们渲染为线条。现在我想纹理多边形。我该怎么做呢?

我试过实现这个:

在此处输入图像描述
在此处输入图像描述

从这里:http ://en.wikipedia.org/wiki/UV_mapping

但这样的结果真的很糟糕。如何通过坐标并确定纹理的顺序?

可以在此处找到相关参考:如何在笛卡尔坐标中绘制边正多边形?

编辑我根据下面 Matic Oblak 给出的答案进行了更新,结果如下:

在此处输入图像描述

旋转无关紧要。

这非常接近……但还没有雪茄。原始纹理如下:

在此处输入图像描述

4

1 回答 1

2

如果我没看错,你是在尝试从 n 个多边形创建一个圆。有很多方法可以使用不同类型的纹理并将它们粘贴到一个形状中,最直接的方法是绘制一个带有整个形状的纹理(对于大的“n”,它将是一个圆形)并且纹理坐标是相同的以 (.5, .5) 为圆心,半径为 0.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 表面上。

编辑 (来自评论)

创建这些三角形与绘制线带并不完全相同。您应该使用三角扇而不是三角条,并且您需要将第一个点设置为形状的中心。

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

现在您只需要使用三角形 FAN 绘制直到索引计数。请注意这里的“偏移量”,您使用 xOffset 和 yOffset 作为椭圆参数而不是偏移量。如果您将它们用作偏移量vertices[(i+1)*3] = (float) (xOffset + Math.cos(2*Math.PI*i/lines));(注意“+”而不是“*”),那么第一个顶点应该在偏移量而不是 (0,0) 处,而纹理坐标保持不变。

于 2013-03-22T09:48:30.360 回答