0

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

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:

enter image description here
enter image description here

from here: 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?

A related reference can be found here: How to draw a n sided regular polygon in cartesian coordinates?

EDIT I updated according to the answer given by Matic Oblak below and this is the result:

enter image description here

The rotation is of no concern.

This is very close... but no cigar just yet. The original texture is as follows:

enter image description here

Community
  • 1
  • 1
Quintin Balsdon
  • 5,484
  • 10
  • 54
  • 95

1 Answers1

2

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.

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

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.

Matic Oblak
  • 16,318
  • 3
  • 24
  • 43
  • Hi there! Thank you very much for this - it is WAY closer than where I would have gotten, I have tried it and I am possibly just a little off track, could I ask you to take one more look? I have edited the question above. – Quintin Balsdon Mar 22 '13 at 12:42
  • Its all good, you just need the last closing vertex.. indices[lines] = indices[0] (or indices[1] if indices[0] is in center) also don't forget you need that extra place in indices buffer so it should be defined as short[lines+1] (or lines+2) – Matic Oblak Mar 22 '13 at 12:59
  • I had that extra index for the final vertex, I still get the same result. I added more code so you can get the full picture. Sorry for just copying the loop, that was silly of me. – Quintin Balsdon Mar 22 '13 at 14:12
  • It looks fine when I draw it as a wireframe... so I know the indices buffer is good – Quintin Balsdon Mar 22 '13 at 14:13
  • Thank you - I needed to do a clean and build. – Quintin Balsdon Mar 23 '13 at 08:35