简体   繁体   中英

Android OpenGLES Rendering using C++ and Java

I have an android application that uses GLES for rendering. Currently using Java to render stuff, and rendering is fine. Due to limitation in Android Java application memory I plan to integrate native rendering to my Java rendering code.

To do this I followed basic native GLES tutorials. After integrating, Java rendering was not visible, only the things I render in C++ was seen.

The simplest version of the code is at: https://github.com/khedd/JavaCppGLES Java code renders a Triangle, C++ renders a Quad. If both are called only Quad is renderer.

How can I solve this issue? Should I port everything to C++?

Code in a nutshell.

MyGLRenderer(){
    mTriangle = new Triangle();
    mCppRenderer = new MyCppRenderer();
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    gl.glClearColor(1.0f, 0.0f, 1.0f, 1.0f);

    //init java triangle
    mTriangle.init();
    //init c quad
    mCppRenderer.init(); //comment this line to make java triangle appear
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    gl.glViewport(0, 0, width, height);
}

@Override
public void onDrawFrame(GL10 gl) {
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    mTriangle.draw();
    mCppRenderer.draw ();
}

The problem was caused because of not unbinding the buffers.

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

Adding these two lines to init and render fixes the problem.

The easiest way to do this is to directly call your C++ code from your surface renderer.

private class PlayerRenderer implements GLSurfaceView.Renderer {

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        surface_created(); // native c++
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int wid, int hgt) {
        surface_changed(wid, hgt); // native c++
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        surface_draw(); // native c++
    }
}

private native void surface_created();
private native void surface_changed(int w, int h);
private native void surface_draw();

No need for context switching.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM