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

しかし、その結果は本当に悪いです。座標を調べて、テクスチャリングから順序を決定するにはどうすればよいですか?

関連するリファレンスは次のとおりです。デカルト座標で側面のある正多角形を描画する方法は?

EDIT以下のMatic Oblakの回答に従って更新しました。これが結果です。

ここに画像の説明を入力

回転は申し分ありません。

これは非常に近いです...しかし、葉巻はまだありません。元のテクスチャは次のとおりです。

ここに画像の説明を入力

4

1 に答える 1

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 サーフェスに画像として配置することさえ想像するのが難しいため、もう少し複雑です。

編集 (コメントから)

これらの三角形を作成することは、ライン ストリップを描くこととまったく同じではありません。三角形のストリップではなく三角形のファンを使用する必要があり、最初のポイントを形状の中心に設定する必要があります。

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 に答える