<commit_before>package com.jcxavier.android.opengl.game.object;

import com.jcxavier.android.opengl.engine.shader.ColorShaderProgram;
import com.jcxavier.android.opengl.engine.shader.ShaderCache;
import com.jcxavier.android.opengl.math.Matrix4;
import com.jcxavier.android.opengl.math.Vector2;
import com.jcxavier.android.opengl.math.Vector3;
import com.jcxavier.android.opengl.math.Vector4;

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

import static android.opengl.GLES20.*;

/**
 * Created on 11/03/2014.
 *
 * @author <NAME> <<EMAIL>>
 */
public class DrawableObject extends GameObject {

    private final Matrix4 mMvpMatrix;
    private final ColorShaderProgram mShader;
    private int mVertexBufferHandle;
    private FloatBuffer mVertexBuffer;

    private final Vector4 mColor;

    public DrawableObject() {
        mColor = new Vector4();
        mMvpMatrix = new Matrix4();

        mShader = (ColorShaderProgram) ShaderCache.getSharedShaderCache().getShaderProgram(ColorShaderProgram.class);

        generateBuffers();
        populateBuffers(true);
    }

    protected void generateBuffers() {
        int[] buffers = { 0 };
        glGenBuffers(1, buffers, 0);
        mVertexBufferHandle = buffers[0];
    }

    protected void populateBuffers(final boolean fromScratch) {
        if (fromScratch) {
            ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * 4 * 4);
            byteBuffer.order(ByteOrder.nativeOrder());
            byteBuffer.position(0);
            mVertexBuffer = byteBuffer.asFloatBuffer();
        }

        updateBufferInformation();

        glBindBuffer(GL_ARRAY_BUFFER, mVertexBufferHandle);

        mVertexBuffer.position(0);

        if (fromScratch) {
            glBufferData(GL_ARRAY_BUFFER, mVertexBuffer.capacity() * 4, mVertexBuffer, GL_DYNAMIC_DRAW);
        } else {
            glBufferSubData(GL_ARRAY_BUFFER, 0, mVertexBuffer.capacity() * 4, mVertexBuffer);
        }
    }

    private void updateBufferInformation() {
        mVertexBuffer.position(0);

        // 0 ---- 3
        //
        // |  \   |
        // |   \  |
        //
        // 1 ---- 2

        final float halfWidth = mSize.x / 2;
        final float halfHeight = mSize.y / 2;

        for (int i = 0; i < 4; i++) {
            if (i == 0 || i == 1) {
                mVertexBuffer.put(-halfWidth);
            } else {
                mVertexBuffer.put(halfWidth);
            }

            if (i == 0 || i == 3) {
                mVertexBuffer.put(-halfHeight);
            } else {
                mVertexBuffer.put(halfHeight);
            }

            mVertexBuffer.put(0.0f);<commit_msg>fixed drawable positioning<commit_after>package com.jcxavier.android.opengl.game.object;

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

import com.jcxavier.android.opengl.engine.shader.ColorShaderProgram;
import com.jcxavier.android.opengl.engine.shader.ShaderCache;
import com.jcxavier.android.opengl.math.Matrix4;
import com.jcxavier.android.opengl.math.Vector2;
import com.jcxavier.android.opengl.math.Vector3;
import com.jcxavier.android.opengl.math.Vector4;

import static android.opengl.GLES20.*;

/**
 * Created on 11/03/2014.
 *
 * @author <NAME> <<EMAIL>>
 */
public class DrawableObject extends GameObject {

    private final Matrix4 mMvpMatrix;
    private final ColorShaderProgram mShader;
    private int mVertexBufferHandle;
    private FloatBuffer mVertexBuffer;

    private final Vector4 mColor;

    public DrawableObject() {
        mColor = new Vector4();
        mMvpMatrix = new Matrix4();

        mShader = (ColorShaderProgram) ShaderCache.getSharedShaderCache().getShaderProgram(ColorShaderProgram.class);

        generateBuffers();
        populateBuffers(true);
    }

    protected void generateBuffers() {
        int[] buffers = { 0 };
        glGenBuffers(1, buffers, 0);
        mVertexBufferHandle = buffers[0];
    }

    protected void populateBuffers(final boolean fromScratch) {
        if (fromScratch) {
            ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * 4 * 4);
            byteBuffer.order(ByteOrder.nativeOrder());
            byteBuffer.position(0);
            mVertexBuffer = byteBuffer.asFloatBuffer();
        }

        updateBufferInformation();

        glBindBuffer(GL_ARRAY_BUFFER, mVertexBufferHandle);

        mVertexBuffer.position(0);

        if (fromScratch) {
            glBufferData(GL_ARRAY_BUFFER, mVertexBuffer.capacity() * 4, mVertexBuffer, GL_DYNAMIC_DRAW);
        } else {
            glBufferSubData(GL_ARRAY_BUFFER, 0, mVertexBuffer.capacity() * 4, mVertexBuffer);
        }
    }

    private void updateBufferInformation() {
        mVertexBuffer.position(0);

        // 0 ---- 3
        //
        // |  \   |
        // |   \  |
        //
        // 1 ---- 2

        for (int i = 0; i < 4; i++) {
            if (i == 0 || i == 1) {
                mVertexBuffer.put(0.0f);
            } else {
                mVertexBuffer.put(mSize.x);
            }

            if (i == 0 || i == 3) {
                mVertexBuffer.put(0.0f);
            } else {
                mVertexBuffer.put(mSize.y);
            }

            mVertexBuffer.put(0.0f);
{'fraction_non_alphanumeric': 0.11218862901692336, 'fraction_numerical': 0.017493820117893136, 'mean_word_length': 2.26302729528536, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2278739', 'n_tokens_mistral': 1662, 'n_tokens_neox': 1559, 'n_words': 293}