Skip to content Skip to sidebar Skip to footer

How To Determine Uv Texture Coordinates For N-sided Polygon

I have generated an n-sided polygon using the code below: public class Vertex { public FloatBuffer floatBuffer; // buffer holding the vertices public ShortBuffer indexBuffe

Solution 1:

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.

EDIT(from comments):

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.

publicPolygon(int lines, float xOffset, float yOffset){       
        float vertices[] = newfloat[(lines+1)*3];  //number of angles + centerfloat texturevertices[] = newfloat[(lines+1)*2];
        short indices[] = newshort[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 = newVertex(vertices,indices);
        texture = newVertex(texturevertices, indices);
    }   

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.

Post a Comment for "How To Determine Uv Texture Coordinates For N-sided Polygon"