package com.example.opengles3;

import android.opengl.GLES30;
import android.util.Log;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

public class Kocka {
    private FloatBuffer vertexBuffer;
    float a = 0.5f;
    private int lokacijaMatrice;
    private int lokacijaBoje;
    float vrhovi[] = {a,-a,a, a,-a,-a, a,a,-a, a,-a,a, a,a,-a, a,a,a, //prednja strana za TRIANGLES
            -a,-a,a, -a,a,-a, -a,-a,-a, -a,-a,a, -a,a,a, -a,a,-a, //straznja strana za TRIANGLES
            a,-a,-a, a,-a,a, -a,-a,-a, a,-a,a, -a,-a,a, -a,-a,-a, //lijeva strana za TRIANGLES
            a,a,-a, -a,a,-a, a,a,a, a,a,a, -a,a,-a, -a,a,a, //desna strana za TRIANGLES
            -a,-a,a, a,-a,a, a,a,a, -a,-a,a, a,a,a, -a,a,a, // gornja strana za TRIANGLES
            -a,-a,-a, a,a,-a, a,-a,-a, -a,-a,-a, -a,a,-a, a,a,-a}; //donja strana za TRIANGLES
    private float color[] = new float[] {0.0f, 0.6f, 1, 1};
    private float color2[] = new float[] {1.0f, 0.0f, 1, 1};
    private float color3[] = new float[] {0.5f, 0.6f, 0, 1};
    private float color4[] = new float[] {1f, 1f, 0.5f, 1};
    private float color5[] = new float[] {1f, 0.1f, 0.2f, 1};
    private float color6[] = new float[] {1f, 1f, 1f, 1};
    private final String vertexShaderCode =
            "#version 300 es\n" +
                    "in vec4 vPosition;"+
                    "in vec4 vColor;"+
                    "out vec4 fColor;"+
                    "uniform mat4 matrica;"+
                    "void main()"+
                    "{"+
                    "    gl_Position = matrica * vPosition;"+
                    "    fColor = vColor;"+
                    "}";
    private final String fragmentShaderCode =
            "#version 300 es\n" +
                    "precision mediump float;"+
                    "in vec4 fColor;"+
                    "out vec4 finalColor;"+
                    "void main()"+
                    "{"+
                    "    finalColor = fColor;"+
                    "}";
    private int shaderProgram;
    public static int loadShader(int type, String shaderCode){
        int shader = GLES30.glCreateShader(type);
        GLES30.glShaderSource(shader, shaderCode);
        GLES30.glCompileShader(shader);
        return shader;
    }

    public Kocka(){
        ByteBuffer bb = ByteBuffer.allocateDirect(vrhovi.length * 4);
        bb.order(ByteOrder.nativeOrder());
        vertexBuffer = bb.asFloatBuffer();
        vertexBuffer.put(vrhovi);
        vertexBuffer.position(0);
        int vertexShader = loadShader(GLES30.GL_VERTEX_SHADER, vertexShaderCode);
        int fragmentShader = loadShader(GLES30.GL_FRAGMENT_SHADER, fragmentShaderCode);
        shaderProgram = GLES30.glCreateProgram();
        GLES30.glAttachShader(shaderProgram, vertexShader);
        GLES30.glAttachShader(shaderProgram, fragmentShader);

        // Bind vPosition to attribute 0
        GLES30.glBindAttribLocation(shaderProgram, 0, "vPosition");
        GLES30.glLinkProgram(shaderProgram);
    }

    public void draw(float[] matrica) {

        GLES30.glUseProgram(shaderProgram);

        lokacijaMatrice = GLES30.glGetUniformLocation(shaderProgram, "matrica");
        lokacijaBoje = GLES30.glGetAttribLocation(shaderProgram, "vColor");

        GLES30.glUniformMatrix4fv(lokacijaMatrice, 1, false, matrica, 0);

        int VERTEX_POS_INDX = 0;
        vertexBuffer.position(VERTEX_POS_INDX);

        GLES30.glVertexAttribPointer(VERTEX_POS_INDX, 3, GLES30.GL_FLOAT,
                false, 0, vertexBuffer);
        GLES30.glEnableVertexAttribArray(VERTEX_POS_INDX);

        //CRTANJE KOCKE
        int startPos =0;
        int verticesPerface = 6;

        //front face
        GLES30.glVertexAttrib3fv(lokacijaBoje, color, 0);
        GLES30.glDrawArrays(GLES30.GL_TRIANGLES,startPos,verticesPerface);
        startPos += verticesPerface;

        //back face
        GLES30.glVertexAttrib3fv(lokacijaBoje, color2, 0);
        GLES30.glDrawArrays(GLES30.GL_TRIANGLES, startPos, verticesPerface);
        startPos += verticesPerface;

        //left face
        GLES30.glVertexAttrib3fv(lokacijaBoje, color3, 0);
        GLES30.glDrawArrays(GLES30.GL_TRIANGLES,startPos,verticesPerface);
        startPos += verticesPerface;

        //right face
        GLES30.glVertexAttrib3fv(lokacijaBoje, color4, 0);
        GLES30.glDrawArrays(GLES30.GL_TRIANGLES,startPos,verticesPerface);
        startPos += verticesPerface;

        //top face
        GLES30.glVertexAttrib3fv(lokacijaBoje, color5, 0);
        GLES30.glDrawArrays(GLES30.GL_TRIANGLES,startPos,verticesPerface);
        startPos += verticesPerface;

        //bottom face
        GLES30.glVertexAttrib3fv(lokacijaBoje, color6, 0);
        GLES30.glDrawArrays(GLES30.GL_TRIANGLES,startPos,verticesPerface);

    }
}
