GLTexture.java 2.99 KB
Newer Older
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
1 2 3 4
package com.projectseptember.RNGL;

import android.graphics.Bitmap;
import android.graphics.Color;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
5
import android.graphics.Matrix;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
6 7 8 9 10 11
import android.opengl.GLUtils;
import android.view.View;

import static android.opengl.GLES20.*;

public class GLTexture {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
12 13
    private int handle;
    private Bitmap bitmapCurrentlyUploaded = null;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
14

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
15 16 17 18 19 20 21
    public GLTexture () {
        makeTexture();
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        int[] handleArr = new int[] { handle };
        glDeleteTextures(1, handleArr, 0);
        bitmapCurrentlyUploaded = null;
    }

    private void makeTexture () {
        int[] handleArr = new int[1];
        glGenTextures(1, handleArr, 0);
        handle = handleArr[0];
        glBindTexture(GL_TEXTURE_2D, handle);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    }

    public int bind (int unit) {
        glActiveTexture(GL_TEXTURE0 + unit);
        glBindTexture(GL_TEXTURE_2D, handle);
        return unit;
    }

    public void bind () {
        glBindTexture(GL_TEXTURE_2D, handle);
    }


    public void setPixels (Bitmap bitmap) {
        if (bitmap != bitmapCurrentlyUploaded) {
            bitmapCurrentlyUploaded = bitmap;
            bind();
            GLUtils.texImage2D(GL_TEXTURE_2D, 0, bitmap, 0);
        }
    }

    public void setPixelsRandom (int width, int height) {
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                bitmap.setPixel(x, y, Color.rgb(
                    (int)(255.0 * Math.random()),
                    (int)(255.0 * Math.random()),
                    (int)(255.0 * Math.random())));
            }
        }
        setPixels(bitmap);
    }

    public void setPixelsEmpty () {
        Bitmap bitmap = Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888);
        setPixels(bitmap);
    }

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
75 76 77 78 79 80 81 82 83
    public void setShape (int width, int height) {
        bind();
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, null);
    }

    public static Bitmap captureView (View view) {
        int w = view.getWidth();
        int h = view.getHeight();
        if (w <= 0 || h <= 0) return Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888);
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
84 85 86 87
        Bitmap bitmap = view.getDrawingCache();
        if (bitmap == null)
            view.setDrawingCacheEnabled(true);
        bitmap = view.getDrawingCache();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
88 89 90 91
        Matrix matrix = new Matrix();
        matrix.postScale(1, -1);
        Bitmap transformedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return transformedBitmap;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
92 93
    }

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
94 95
    public int getHandle() {
        return handle;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
96 97
    }
}