如何确定UV纹理正边形的坐标 [英] How to determine UV texture coordinates for n-sided polygon
问题描述
我已经生成使用下面的code n边的多边形:
I have generated an n-sided polygon using the code below:
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;
}
}
然后创建一个正边形:
Then to create a n-sided polygon:
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);
}
}
和,你可以看到我settup了的indeces有序,这样我可以使它们作为一个线带。现在,我想纹理多边形。我该怎么做呢?
and as you can see I am settup up the indeces in-order so that I can render them as a line strip. Now I wish to texture the polygon. How do I do this?
我曾尝试实施这个:
I have tried implementing this:
从这里开始: http://en.wikipedia.org/wiki/UV_mapping
但是,结果是真穷。我该如何去通过坐标和确定从纹理订购?
But that result is really poor. How do I go through the coordinates and determine the ordering from texturing?
一个相关的参考资料可以在这里找到:<一href=\"http://stackoverflow.com/questions/7198144/how-to-draw-a-n-sided-regular-polygon-in-cartesian-coordinates\">How画一个n的笛卡尔坐标片面正多边形?
A related reference can be found here: How to draw a n sided regular polygon in cartesian coordinates?
修改我根据下面马蒂奇Oblak给出了答案更新,这就是结果:
EDIT I updated according to the answer given by Matic Oblak below and this is the result:
旋转没有关注。
这是非常接近......但没有雪茄,只是还没有。原始纹理如下:
This is very close... but no cigar just yet. The original texture is as follows:
推荐答案
如果我读这正确,您要创建从n个多边形圆。有使用不同类型的纹理并将其粘贴到形状的方法很多,最直接的将是与绘制的整体形状的纹理(对于大N这将是一个圆圈)和纹理坐标将是相同的如在(.5,.5)中心的圆和.5半径:
If I am reading this correctly you are trying to create a circle from n polygons. There are many ways to use different types of textures and paste them to a shape, the most direct would be to have a texture with a whole shape drawn (for large 'n' it would be a circle) and texture coordinates would be the same as a circle with a center in (.5, .5) and a radius of .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
你贴都是为了一个球,是一个比较复杂一点,因为这是很难想象把它作为一个图像到二维表面的方程。
The equations you posted are meant for a sphere and are a bit more complicated since it is hard to even imagine to put it as an image to a 2d surface.
修改 (从评论)的:
创建这些三角形是不完全一样的画线带。你应该用一个三角形风扇,而不是三角形带,你需要先点设置到形状的中心。
Creating these triangles is not exactly the same as drawing the line strip. You should use a triangle fan and not triangle strip AND you need to set first point to center of the shape.
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为椭圆的参数,而不是作为补偿。如果你要使用他们作为补偿顶点[第(i + 1)* 3 =(浮点)(xOffset + Math.cos(2 * Math.PI * I /线));
(注意'+'而不是'*'),那么第一个顶点应该位于偏移量,而不是(0,0),而纹理坐标保持不变。
Now you just need to draw till index count with triangle FAN. Just a bit of note here to your "offsets", you use xOffset and yOffset as elliptic parameters and not as offsets. If you will be using them as offsets vertices[(i+1)*3] = (float) (xOffset + Math.cos(2*Math.PI*i/lines));
(note '+' instead of '*') then 1st vertex should be at offset instead of (0,0) while texture coordinates remain the same.
这篇关于如何确定UV纹理正边形的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!