package com.example.opengles3;

import android.opengl.GLES30;

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

public class Podloga {
    private FloatBuffer vertexBuffer;
    float a = 0.5f;
    private int lokacijaMatrice;
    private int lokacijaBoje;
    float vrhovi[] = new float[1200];
    private float color[] = new float[] {0.0f, 0.0f, 0f, 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 Podloga(){
        for(int i = 0;i<100;i++){
            vrhovi[i*6]=i-50;
            vrhovi[i*6+1]=0;
            vrhovi[i*6+2]=-50;
            vrhovi[i*6+3]=i-50;
            vrhovi[i*6+4]=0;
            vrhovi[i*6+5]=50;
        }
        for(int i = 0;i<100;i++){
            vrhovi[i*6+600]= -50;
            vrhovi[i*6+1+600]=0;
            vrhovi[i*6+2+600]=i-50;
            vrhovi[i*6+3+600]= 50;
            vrhovi[i*6+4+600]=0;
            vrhovi[i*6+5+600]=i-50;
        }

        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 LINIJA (Podloge)
        GLES30.glVertexAttrib3fv(lokacijaBoje, color, 0);
        GLES30.glDrawArrays(GLES30.GL_LINES, 0, 400);

    }
}
