RNGLContext.java 1.82 KB
Newer Older
1 2
package com.projectseptember.RNGL;

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
3 4
import android.opengl.GLSurfaceView;

5 6 7 8 9
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
10 11 12 13 14
import java.util.HashMap;
import java.util.Map;

import javax.microedition.khronos.egl.EGLContext;

15 16
public class RNGLContext extends ReactContextBaseJavaModule {

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
    // Share GL Context ?
    // http://developer.android.com/training/graphics/opengl/environment.html
    // http://stackoverflow.com/questions/8845491/sharing-the-egl2-0-context-between-2-glsurfaceviews-caused-egl-bad-access-on-and
    // http://stackoverflow.com/questions/5675355/sharing-the-gles20-context-and-textures-between-different-glsurfaceviews

    private static String STATIC_VERT =
    "attribute vec2 position;"+
    "varying vec2 uv;"+
    "void main() {"+
        "gl_Position = vec4(position,0.0,1.0);"+
        "uv = vec2(0.5, 0.5) * (position+vec2(1.0, 1.0));"+
    "}";

    private Map<Integer, GLShader> shaders = new HashMap<>();
    private Map<Integer, GLFBO> fbos = new HashMap<>();

33 34 35 36 37 38 39 40 41
    public RNGLContext (ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    public String getName() {
        return "RNGLContext";
    }

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
42 43 44 45 46 47 48 49 50 51 52
    public GLShader getShader (Integer id) {
        return shaders.get(id);
    }

    public GLFBO getFBO (Integer id) {
        if (!fbos.containsKey(id)) {
            fbos.put(id, new GLFBO());
        }
        return fbos.get(id);
    }

53
    @ReactMethod
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
54 55 56 57
    public void addShader (final Integer id, final ReadableMap config) {
        final String frag = config.getString("frag");
        final String name = config.getString("name");
        shaders.put(id, new GLShader(name, STATIC_VERT, frag));
58 59
    }
}