package de.frajul.particlelab.rendering;

import android.opengl.GLES20;

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

/**
 * Created by Julian on 26.11.2015.
 */
public class Quad {

    private FloatBuffer vertexBuffer;
    private FloatBuffer textureBuffer;

    private float vertices[] = {1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f,
            1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f};

    private float textures[] = {1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
            1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};

    public Quad() {
        vertexBuffer = createEmptyFloatBuffer();
        vertexBuffer.put(vertices);
        vertexBuffer.position(0);

        textureBuffer = createEmptyFloatBuffer();
        textureBuffer.put(textures);
        textureBuffer.position(0);
    }

    private FloatBuffer createEmptyFloatBuffer() {
        ByteBuffer bb = ByteBuffer.allocateDirect(vertices.length * 4);
        bb.order(ByteOrder.nativeOrder());
        return bb.asFloatBuffer();
    }

    public void draw() {
        GLES20.glEnableVertexAttribArray(0);
        GLES20.glEnableVertexAttribArray(1);
        GLES20.glVertexAttribPointer(0, 2, GLES20.GL_FLOAT, false, 0, vertexBuffer);
        GLES20.glVertexAttribPointer(1, 2, GLES20.GL_FLOAT, false, 0, textureBuffer);
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertices.length / 2);
        GLES20.glDisableVertexAttribArray(1);
        GLES20.glDisableVertexAttribArray(0);
    }

}
