Use 3D Graphics on Android for draw an .obj file offscreen

Hello all,

I am trying to draw an .obj file offscreen under Android.
The image should then be passed to OpenCV for further use as a matrix.

In 2D the whole thing already works without problems.

public class ObjDrawer extends PApplet {

    PGraphics buffer;
    PShape obj;
    int width;
    int height;

    public ObjDrawer(int width, int height) {
        this.width = width;
        this.height = height;
        setup();
    }

    @Override
    public void setup() {
        buffer = createGraphics(this.width, this.height, P3D);
        //obj = loadShape("Wuerfel_40mm.obj");
        noLoop();
    }

    public Mat testDraw() {
        buffer.beginDraw();
        buffer.background(255);
        buffer.ellipse(200, 200, 100, 100);
        //buffer.shape(obj);
        buffer.endDraw();
        return toMat(buffer.get());
    }

    Mat toMat(PImage image) {
        //source: https://gist.github.com/Spaxe/3543f0005e9f8f3c4dc5
        int w = image.width;
        int h = image.height;

        Mat mat = new Mat(h, w, CvType.CV_8UC4);
        byte[] data8 = new byte[w * h * 4];
        int[] data32 = new int[w * h];
        arrayCopy(image.pixels, data32);
        ByteBuffer bBuf = ByteBuffer.allocate(w * h * 4);
        IntBuffer iBuf = bBuf.asIntBuffer();
        iBuf.put(data32);
        bBuf.get(data8);
        mat.put(0, 0, data8);
        return mat;
    }

}

When using 3D, I now get the following error message:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean processing.core.PGraphics.isGL()' on a null object reference
	at processing.core.PApplet.makeGraphics(PApplet.java:1584)
	at processing.core.PApplet.createGraphics(PApplet.java:1568)
	at Analyzer.ObjDrawer.setup(ObjDrawer.java:52)
	at Analyzer.ObjDrawer.<init>(ObjDrawer.java:45)
	at com.quickbirdstudios.opencvexample.MainActivity$cvCameraViewListener$1.onCameraFrame(MainActivity.kt:75)
	at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:392)
	at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:373)
	at java.lang.Thread.run(Thread.java:920)

In my research I read about a3d, this is missing for me. Since all the stuff I found regarding a3d was about 10 years old: has anything changed in this regard or has it been replaced or do I need to include this separately?

I have included Processing as in the tutorial on https://android.processing.org/tutorials/android_studio/index.html

integrated. I use Android Mode for Processing 3 4.3.0, because the mode manager in Processing 4 is currently broken.

many thanks in advance!

Update your Android_mode to the latest version, you just have to open an Android project from the IDe and it will be downloaded automatically.

Here you have a project for you to open with your editor. It will automatically start downloading in android mode :wink:

Thanks already for the answer.
When I open it in Processing 3, no update starts. In the contribution manager, no update is shown there either.
When I try to open it in Processing 4, it just shows the error message that I should update my Android mode, and it switches back to Java mode.

Which version are we talking about? The one for Processing 4 4.5.0b2?
It is unfortunately grayed out in P3 due to incompatibility, and in P4 the contribution manager for the modes does not work.
Can I get the appropriate core.jar somewhere else?

Solo tienes que tener una version limpia de processing 4 en tu PC, “hablo de windows”, entonces abre el proyecto que te pase y automaticamente te saldrá un dialogo para descargar el Android mode ultima version.

I managed to update, but still get the same error. Does createGraphics() need the size() method before? I tried size() once in the first line of setup() and also tried it in settings(), in both cases I get the error that it is not in setting().