package xaero.rotatenjump.game.graphics.model;

/**
 * Created by xaero on 18/08/2017.
 */

public class Right3DPolygon extends Model {

    public Right3DPolygon(int sides){
        float[] vertexArray = new float[(sides * 5 + 1) * VERTEX_SIZE];
        short[] indexArray = new short[sides * 9];
        vertexArray[0] = vertexArray[1] = 0;//center point, here just for readability
        vertexArray[2] = 0.5f;//center front
        vertexArray[7] = 1;//center front normal z
        vertexArray[8] = vertexArray[9] = vertexArray[10] = vertexArray[11] = 1;//colour
        int offset = 1;//1 center vertices
        int offset_size = offset * VERTEX_SIZE;//1 center vertices
        float prevX = (float)Math.cos((float) ((2f * Math.PI / sides) * (sides - 1)));
        float prevY = (float)Math.sin((float) ((2f * Math.PI / sides) * (sides - 1)));
        float zSize = (float)Math.cos(Math.PI / 4.0);//size that makes 4-sided right polygons look cubic
        for(int i = 0; i < sides; i++){
            float angle = (float) ((2f * Math.PI / sides) * i);
            float currentX = (float)Math.cos(angle);
            float currentY = (float)Math.sin(angle);
            int current_offset = 5 * VERTEX_SIZE * i + offset_size;
            vertexArray[current_offset] = vertexArray[current_offset + VERTEX_SIZE] = vertexArray[current_offset + 2 * VERTEX_SIZE] = currentX;//duplicate front vertices
            vertexArray[current_offset + 1] = vertexArray[current_offset + VERTEX_SIZE + 1] = vertexArray[current_offset + 2 * VERTEX_SIZE + 1] = currentY;//duplicate front vertices
            vertexArray[current_offset + 2] = vertexArray[current_offset + VERTEX_SIZE + 2] = vertexArray[current_offset + 2 * VERTEX_SIZE + 2] = zSize;//duplicate front vertices
            vertexArray[current_offset + 3 * VERTEX_SIZE] = vertexArray[current_offset + 4 * VERTEX_SIZE] = currentX;//duplicate back vertices
            vertexArray[current_offset + 3 * VERTEX_SIZE + 1] = vertexArray[current_offset + 4 * VERTEX_SIZE + 1] = currentY;//duplicate back vertices
            vertexArray[current_offset + 3 * VERTEX_SIZE + 2] = vertexArray[current_offset + 4 * VERTEX_SIZE + 2] = -zSize;//duplicate back vertices
            //colours
            for(int j = 0; j < 5; j++)
                vertexArray[current_offset + j * VERTEX_SIZE + 8] =
                        vertexArray[current_offset + j * VERTEX_SIZE + 9] =
                                vertexArray[current_offset + j * VERTEX_SIZE + 10] = vertexArray[current_offset + j * VERTEX_SIZE + 11] = 1;//white
            //normals
            int prevPoint = i == 0 ? sides - 1 : i - 1;
            int prev_offset = 5 * VERTEX_SIZE * prevPoint + offset_size;
            vertexArray[current_offset + 7] = 1;//front vertex front normal
            vertexArray[current_offset + VERTEX_SIZE + 5] = vertexArray[current_offset + 3 * VERTEX_SIZE + 5] =
                    vertexArray[prev_offset + 2 * VERTEX_SIZE + 5] = vertexArray[prev_offset + 4 * VERTEX_SIZE + 5] = currentY - prevY;//x = -y
            vertexArray[current_offset + VERTEX_SIZE + 6] = vertexArray[current_offset + 3 * VERTEX_SIZE + 6] =
                            vertexArray[prev_offset + 2 * VERTEX_SIZE + 6] = vertexArray[prev_offset + 4 * VERTEX_SIZE + 6] = prevX - currentX;//y = x
            prevX = currentX;
            prevY = currentY;
            //end
            //vertexArray[VERTEX_SIZE * i + 2] = 0;//0 by default, so not needed

            //front
            indexArray[9 * i] = 0;//center
            indexArray[9 * i + 1] = (short)(prevPoint * 5 + offset);//previous
            indexArray[9 * i + 2] = (short)(i * 5 + offset);//current
            //side triangle 1
            indexArray[9 * i + 3] = (short)(i * 5 + offset + 1);//current front
            indexArray[9 * i + 4] = (short)(prevPoint * 5 + offset + 2);//previous front
            indexArray[9 * i + 5] = (short)(i * 5 + offset + 3);//current back
            //side triangle 2
            indexArray[9 * i + 6] = (short)(i * 5 + offset + 3);//current back
            indexArray[9 * i + 7] = (short)(prevPoint * 5 + offset + 2);//previous front
            indexArray[9 * i + 8] = (short)(prevPoint * 5 + offset + 4);//previous back
        }
        create(vertexArray, indexArray);
    }

}
