package com.example.opengles3;

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

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class MainRenderer implements GLSurfaceView.Renderer {
    Kocka kocka;
    Podloga podloga;
    float ratio;
    private int mWidth;
    private int mHeight;
    private float mAngle =0;
    private float mTransY=0;
    private float mTransX=0;
    private static final float Z_NEAR = -20f;
    private static final float Z_FAR = 100f;

    // "Model View Projection Matrix"
    private final float[] mMVPMatrix = new float[16];
    private final float[] mProjectionMatrix = new float[16];
    private final float[] mViewMatrix = new float[16];
    private final float[] mRotationMatrix = new float[16];
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        kocka = new Kocka();
        podloga = new Podloga();
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        mWidth = width;
        mHeight = height;
        // Set the viewport
        GLES30.glViewport(0, 0, mWidth, mHeight);
        ratio = (float) width / height;
        Matrix.orthoM(mProjectionMatrix, 0, -7f*ratio,7*ratio, -7,7, Z_NEAR, Z_FAR);
        //Matrix.perspectiveM(mProjectionMatrix, 0, 53.13f, ratio, Z_NEAR, Z_FAR);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        GLES30.glClearColor(0.7f, 0.7f, 0.7f, 1);
        GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT | GLES30.GL_DEPTH_BUFFER_BIT);
        GLES30.glEnable(GLES30.GL_DEPTH_TEST);
        GLES30.glEnable(GLES30.GL_CULL_FACE);

        Matrix.setLookAtM(mViewMatrix, 0, 0, 6 + (float)(Math.sin(mAngle/20)*1.6f), -15f, 0f, 2f, 0f, 0f, 1.0f, 0.0f);

        Matrix.setIdentityM(mRotationMatrix, 0);

        //Matrix.rotateM(mRotationMatrix, 0, mAngle, 0.0f, 0.0f, 0.0f);

        Matrix.rotateM(mViewMatrix, 0, mAngle*1.6f, 0.0f, 1.0f, 0.0f);

        // combine the model with the view matrix
        Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mRotationMatrix, 0);

        // combine the model-view with the projection matrix
        Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);

        podloga.draw(mMVPMatrix);
        Matrix.translateM(mMVPMatrix,0,0,0.5f,0);
        kocka.draw(mMVPMatrix);
        Matrix.translateM(mMVPMatrix,0,0,1,0);
        kocka.draw(mMVPMatrix);
        Matrix.translateM(mMVPMatrix,0,0,1,0);
        kocka.draw(mMVPMatrix);
        Matrix.translateM(mMVPMatrix,0,1,0,0);
        kocka.draw(mMVPMatrix);
        Matrix.translateM(mMVPMatrix,0,1,0,0);
        kocka.draw(mMVPMatrix);
        Matrix.translateM(mMVPMatrix,0,-2,1,0);
        kocka.draw(mMVPMatrix);
        Matrix.translateM(mMVPMatrix,0,0,1,0);
        kocka.draw(mMVPMatrix);
        Matrix.translateM(mMVPMatrix,0,1,0,0);
        kocka.draw(mMVPMatrix);
        Matrix.translateM(mMVPMatrix,0,1,0,0);
        kocka.draw(mMVPMatrix);

        //mijenjamo kut
        mAngle+=.4;






    }
}
