package com.example.opengles3;

import android.content.Context;
import android.graphics.Bitmap;
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 {
    public Bitmap bitmap;
    Kocka kocka;
    Podloga podloga;
    Stozac stozac;
    Valjak valjak;
    Valjak valjak2;
    Kugla kugla;
    PoluKugla poluKugla;
    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 = 1f;
    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(bitmap);
        podloga = new Podloga();
        stozac = new Stozac(1,3,20);
        valjak = new Valjak(0.6f,1,20);
        valjak2 = new Valjak(0.2f,2,20);
        kugla = new Kugla(1,0.05f);
        poluKugla = new PoluKugla(0.8f,0.05f);
    }

    @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, 1.4f /*+ (float)(Math.sin(mAngle/20)*1.6f)*/, -4f, 0f, 0f, 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,0f,0);
        kocka.draw(mMVPMatrix);


        //mijenjamo kut
        mAngle+=.4;






    }
}
