package net.ddns.mahaljsp.opengl_glsurfaceview;

import android.opengl.GLES20;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

/**
 * Created by Thomas Wu(mahaljsp@gmail.com) on 2017/1/10.
 */

public class Triangle {
    private static float triangleCoords[] = {
            0.0f, 0.62008459f, 0.0f,
            -0.5f, -0.311004243f, 0.0f,
            0.5f, -0.311004243f, 0.0f,
    };
    private float color[] = {1.0f, 0.0f, 0.0f, 1.0f};//RGBA
    private FloatBuffer vertexBuffer;
    private static final int COORDS_PER_VERTEX = 3;
    private static final int BYTES_PER_FLOAT = 4;
    private final int vertexStride = COORDS_PER_VERTEX *BYTES_PER_FLOAT;
    private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX;
    int mProgram;
    public Triangle() {
        initVertexBuffer();
        mProgram = MyShaderFactory.getInstanceShader();
        GLES20.glClearColor(1,1,0,0);
    }
    public void onDraw(){
        GLES20.glUseProgram(mProgram);
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT| GLES20.GL_DEPTH_BUFFER_BIT);
        int positionHandle = GLES20.glGetAttribLocation(mProgram, MyShaderFactory.FIELD_POSITION);
        int colorHandle = GLES20.glGetUniformLocation(mProgram, MyShaderFactory.FIELD_COLOR);
        GLES20.glEnableVertexAttribArray(positionHandle);
        GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX,
                GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);
        GLES20.glUniform4fv(colorHandle, 1, color, 0);
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
        GLES20.glDisableVertexAttribArray(positionHandle);
    }
    private void initVertexBuffer(){
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(triangleCoords.length*BYTES_PER_FLOAT);
        byteBuffer.order(ByteOrder.nativeOrder());
        vertexBuffer = byteBuffer.asFloatBuffer();
        vertexBuffer.put(triangleCoords);
        vertexBuffer.position(0);//由第0個頂點開始畫起
    }
}
