How to determine UV texture coordinates for n-sided polygon
我使用下面的代码生成了一个n边多边形:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 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边多边形:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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
但结果真的很糟糕。如何通过坐标确定纹理的顺序?
这里可以找到一个相关的参考:如何在笛卡尔坐标系中绘制一个n边正多边形?
根据下面Matic Oblik给出的答案进行编辑和更新,结果如下:
旋转不重要。
这很接近…但现在还没有雪茄。原始纹理如下:
如果我正在正确地阅读,你试图从N多边形创建一个圆。有许多方法可以使用不同类型的纹理并将其粘贴到形状中,最直接的方法是绘制一个具有完整形状的纹理(对于大的"n",它将是一个圆),纹理坐标将与一个中心在(.5,.5)且半径为.5的圆相同:
1 2 3 4 | //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 |
你发布的方程是针对一个球体的,并且有点复杂,因为很难想象把它作为一个图像放到二维表面上。
编辑(来自注释):
创建这些三角形与绘制线条条不完全相同。你应该使用三角扇而不是三角条,你需要设置第一个点到形状的中心。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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作为椭圆参数,而不是偏移量。如果将它们用作偏移量